Remove FindSDL2 find-module, use sdl2-config.cmake instead
This requires SDL >= 2.0.4.
Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in
SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was
not itself built using CMake: it installs a sdl2-config.cmake file to
${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and
library, analogous to a pkg-config .pc file.
As a result, we no longer need to copy/paste a "find-module package"
to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2).
Find-module packages are now discouraged by the CMake developers, in
favour of having upstream projects behave as config-file packages.
This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR
and SDL2_LIBRARY, but the standard behaviour for config-file packages is
to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword
to make sure we search in config-file package mode, and will not find a
FindSDL2.cmake in some other directory that implements the old interface.
In addition to deleting redundant code, this avoids some assumptions in
FindSDL2 about the layout of a SDL installation. The current libsdl2-dev
package in Debian breaks those assumptions; this is considered a bug
and will hopefully be fixed soon, but it illustrates how fragile these
assumptions can be. We can be more robust against different installation
layouts by relying on SDL's own CMake integration.
When linking to a copy of CMake in a non-standard location, users can
now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point
to it; previously, these users would have used the SDL2DIR environment
variable. This continues to be unnecessary if using matching system-wide
installations of CMake and SDL2, for example both from Debian.
{-
* Hedgewars, a free turn based strategy game
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\-}
{-# LANGUAGE OverloadedStrings #-}
module Votes where
import Control.Monad.Reader
import Control.Monad.State.Strict
import ServerState
import qualified Data.ByteString.Char8 as B
import qualified Data.List as L
import qualified Data.Map as Map
import Data.Maybe
import Control.Applicative
-------------------
import Consts
import Utils
import CoreTypes
import HandlerUtils
import EngineInteraction
voted :: Bool -> Bool -> Reader (ClientIndex, IRnC) [Action]
voted forced vote = do
cl <- thisClient
rm <- thisRoom
uid <- liftM clUID thisClient
case voting rm of
Nothing ->
return [Warning $ loc "There's no voting going on."]
Just voting ->
if (not forced) && (uid `L.notElem` entitledToVote voting) then
return []
else if (not forced) && (uid `L.elem` map fst (votes voting)) then
return [Warning $ loc "You already have voted."]
else if forced && (not $ isAdministrator cl) then
return []
else
((:) (AnswerClients [sendChan cl] ["CHAT", nickServer, loc "Your vote has been counted."]))
<$> (actOnVoting $ voting{votes = (uid, vote):votes voting})
where
actOnVoting :: Voting -> Reader (ClientIndex, IRnC) [Action]
actOnVoting vt = do
let (pro, contra) = L.partition snd $ votes vt
let totalV = length $ entitledToVote vt
let successV = totalV `div` 2 + 1
if (forced && not vote) || (length contra > totalV - successV) then
closeVoting
else if (forced && vote) || (length pro >= successV) then do
a <- act $ voteType vt
c <- closeVoting
return $ c ++ a
else
return [ModifyRoom $ \r -> r{voting = Just vt}]
closeVoting = do
chans <- roomClientsChans
return [
AnswerClients chans ["CHAT", nickServer, loc "Voting closed."]
, ModifyRoom (\r -> r{voting = Nothing})
]
act (VoteKick nickname) = do
(thisClientId, rnc) <- ask
maybeClientId <- clientByNick nickname
rm <- thisRoom
let kickId = fromJust maybeClientId
let kickCl = rnc `client` kickId
let sameRoom = clientRoom rnc thisClientId == clientRoom rnc kickId
return
[KickRoomClient kickId |
isJust maybeClientId
&& sameRoom
&& ((isNothing $ gameInfo rm) || teamsInGame kickCl == 0)
]
act (VoteMap roomSave) = do
rm <- thisRoom
let rs = Map.lookup roomSave (roomSaves rm)
case rs of
Nothing -> return []
Just (location, mp, p) -> do
cl <- thisClient
chans <- roomClientsChans
return $
[ModifyRoom $ \r -> r{params = p, mapParams = mp}
, AnswerClients chans ["CHAT", nickServer, location]
, SendUpdateOnThisRoom
, LoadGhost location]
act (VotePause) = do
rm <- thisRoom
chans <- roomClientsChans
let modifyGameInfo f room = room{gameInfo = fmap f $ gameInfo room}
return [ModifyRoom (modifyGameInfo $ \g -> g{isPaused = not $ isPaused g}),
AnswerClients chans ["CHAT", nickServer, loc "Pause toggled."],
AnswerClients chans ["EM", toEngineMsg "I"]]
act (VoteNewSeed) =
return [SetRandomSeed]
act (VoteHedgehogsPerTeam h) = do
rm <- thisRoom
chans <- roomClientsChans
let answers = concatMap (\t ->
[ModifyRoom $ modifyTeam t{hhnum = h}
, AnswerClients chans ["HH_NUM", teamname t, showB h]]
) $ if length curteams * h > cMaxHHs then [] else curteams
;
curteams =
if isJust $ gameInfo rm then
teamsAtStart . fromJust . gameInfo $ rm
else
teams rm
return $ ModifyRoom (\r -> r{defaultHedgehogsNumber = h}) : answers
startVote :: VoteType -> Reader (ClientIndex, IRnC) [Action]
startVote vt = do
(ci, rnc) <- ask
--cl <- thisClient
rm <- thisRoom
chans <- roomClientsChans
let uids = map (clUID . client rnc) . roomClients rnc $ clientRoom rnc ci
if isJust $ voting rm then
return []
else
return [
ModifyRoom (\r -> r{voting = Just (newVoting vt){entitledToVote = uids}})
, AnswerClients chans ["CHAT", nickServer, B.concat [loc "New voting started", ": ", voteInfo vt]]
, ReactCmd ["VOTE", "YES"]
]
checkVotes :: StateT ServerState IO [Action]
checkVotes = do
rnc <- gets roomsClients
liftM concat $ io $ do
ris <- allRoomsM rnc
mapM (check rnc) ris
where
check rnc ri = do
e <- room'sM rnc voting ri
case e of
Just rv -> do
modifyRoom rnc (\r -> r{voting = if voteTTL rv == 0 then Nothing else Just rv{voteTTL = voteTTL rv - 1}}) ri
if voteTTL rv == 0 then do
chans <- liftM (map sendChan) $ roomClientsM rnc ri
return [AnswerClients chans ["CHAT", nickServer, loc "Voting expired."]]
else
return []
Nothing -> return []
voteInfo :: VoteType -> B.ByteString
voteInfo (VoteKick n) = B.concat [loc "kick", " ", n]
voteInfo (VoteMap n) = B.concat [loc "map", " ", n]
voteInfo (VotePause) = B.concat [loc "pause"]
voteInfo (VoteNewSeed) = B.concat [loc "new seed"]
voteInfo (VoteHedgehogsPerTeam i) = B.concat [loc "hedgehogs per team: ", " ", showB i]