author | antonc27 <antonc27@mail.ru> |
Wed, 04 Nov 2015 06:17:31 +0100 | |
branch | ios-revival |
changeset 11295 | 6b6d96077395 |
parent 11046 | 47a8c19ecb60 |
child 11580 | db7743e2fad1 |
permissions | -rw-r--r-- |
10464
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
1 |
{- |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
10464
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
4 |
* |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
8 |
* |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
12 |
* GNU General Public License for more details. |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
13 |
* |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
17 |
\-} |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
18 |
|
10058 | 19 |
{-# LANGUAGE OverloadedStrings #-} |
10049 | 20 |
module Votes where |
21 |
||
22 |
import Control.Monad.Reader |
|
10216 | 23 |
import Control.Monad.State.Strict |
10049 | 24 |
import ServerState |
10058 | 25 |
import qualified Data.ByteString.Char8 as B |
10081 | 26 |
import qualified Data.List as L |
10195 | 27 |
import qualified Data.Map as Map |
10058 | 28 |
import Data.Maybe |
10880 | 29 |
import Control.Applicative |
10058 | 30 |
------------------- |
31 |
import Utils |
|
32 |
import CoreTypes |
|
33 |
import HandlerUtils |
|
10392 | 34 |
import EngineInteraction |
10049 | 35 |
|
10081 | 36 |
|
10881
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
37 |
voted :: Bool -> Bool -> Reader (ClientIndex, IRnC) [Action] |
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
38 |
voted forced vote = do |
10081 | 39 |
cl <- thisClient |
40 |
rm <- thisRoom |
|
41 |
uid <- liftM clUID thisClient |
|
42 |
||
10392 | 43 |
case voting rm of |
44 |
Nothing -> |
|
45 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "There's no voting going on"]] |
|
46 |
Just voting -> |
|
10881
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
47 |
if (not forced) && (uid `L.notElem` entitledToVote voting) then |
10392 | 48 |
return [] |
10881
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
49 |
else if (not forced) && (uid `L.elem` map fst (votes voting)) then |
10392 | 50 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "You already have voted"]] |
10881
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
51 |
else if forced && (not $ isAdministrator cl) then |
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
52 |
return [] |
10392 | 53 |
else |
10880 | 54 |
((:) (AnswerClients [sendChan cl] ["CHAT", "[server]", loc "Your vote counted"])) |
55 |
<$> (actOnVoting $ voting{votes = (uid, vote):votes voting}) |
|
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
56 |
|
10087 | 57 |
where |
58 |
actOnVoting :: Voting -> Reader (ClientIndex, IRnC) [Action] |
|
59 |
actOnVoting vt = do |
|
10212 | 60 |
let (pro, contra) = L.partition snd $ votes vt |
10392 | 61 |
let totalV = length $ entitledToVote vt |
62 |
let successV = totalV `div` 2 + 1 |
|
10087 | 63 |
|
10881
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
64 |
if (forced && not vote) || (length contra > totalV - successV) then |
10087 | 65 |
closeVoting |
10881
941b5ab9e5a6
"/force" command for server admin to force voting result
unc0rr
parents:
10880
diff
changeset
|
66 |
else if (forced && vote) || (length pro >= successV) then do |
10215 | 67 |
a <- act $ voteType vt |
68 |
c <- closeVoting |
|
69 |
return $ c ++ a |
|
10087 | 70 |
else |
71 |
return [ModifyRoom $ \r -> r{voting = Just vt}] |
|
72 |
||
73 |
closeVoting = do |
|
74 |
chans <- roomClientsChans |
|
75 |
return [ |
|
76 |
AnswerClients chans ["CHAT", "[server]", loc "Voting closed"] |
|
77 |
, ModifyRoom (\r -> r{voting = Nothing}) |
|
78 |
] |
|
79 |
||
80 |
act (VoteKick nickname) = do |
|
81 |
(thisClientId, rnc) <- ask |
|
82 |
maybeClientId <- clientByNick nickname |
|
83 |
rm <- thisRoom |
|
84 |
let kickId = fromJust maybeClientId |
|
85 |
let kickCl = rnc `client` kickId |
|
86 |
let sameRoom = clientRoom rnc thisClientId == clientRoom rnc kickId |
|
87 |
return |
|
88 |
[KickRoomClient kickId | |
|
89 |
isJust maybeClientId |
|
90 |
&& sameRoom |
|
91 |
&& ((isNothing $ gameInfo rm) || teamsInGame kickCl == 0) |
|
92 |
] |
|
10195 | 93 |
act (VoteMap roomSave) = do |
94 |
rm <- thisRoom |
|
95 |
let rs = Map.lookup roomSave (roomSaves rm) |
|
96 |
case rs of |
|
97 |
Nothing -> return [] |
|
10218 | 98 |
Just (mp, p) -> do |
99 |
cl <- thisClient |
|
100 |
chans <- roomClientsChans |
|
101 |
let a = map (replaceChans chans) $ answerFullConfigParams cl mp p |
|
102 |
return $ |
|
103 |
(ModifyRoom $ \r -> r{params = p, mapParams = mp}) |
|
104 |
: SendUpdateOnThisRoom |
|
105 |
: a |
|
106 |
where |
|
107 |
replaceChans chans (AnswerClients _ msg) = AnswerClients chans msg |
|
108 |
replaceChans _ a = a |
|
10392 | 109 |
act (VotePause) = do |
110 |
rm <- thisRoom |
|
111 |
chans <- roomClientsChans |
|
112 |
let modifyGameInfo f room = room{gameInfo = fmap f $ gameInfo room} |
|
113 |
return [ModifyRoom (modifyGameInfo $ \g -> g{isPaused = not $ isPaused g}), |
|
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
114 |
AnswerClients chans ["CHAT", "[server]", loc "Pause toggled"], |
10392 | 115 |
AnswerClients chans ["EM", toEngineMsg "I"]] |
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
116 |
act (VoteNewSeed) = |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
117 |
return [SetRandomSeed] |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
118 |
act (VoteHedgehogsPerTeam h) = do |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
119 |
rm <- thisRoom |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
120 |
chans <- roomClientsChans |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
121 |
let answers = concatMap (\t -> |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
122 |
[ModifyRoom $ modifyTeam t{hhnum = h} |
10787 | 123 |
, AnswerClients chans ["HH_NUM", teamname t, showB h]] |
10879
9bedbd36de49
Don't change hedgehogs total number to a value > 48 as result of voting.
unc0rr
parents:
10787
diff
changeset
|
124 |
) $ if length curteams * h > 48 then [] else curteams |
9bedbd36de49
Don't change hedgehogs total number to a value > 48 as result of voting.
unc0rr
parents:
10787
diff
changeset
|
125 |
; |
9bedbd36de49
Don't change hedgehogs total number to a value > 48 as result of voting.
unc0rr
parents:
10787
diff
changeset
|
126 |
curteams = |
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
127 |
if isJust $ gameInfo rm then |
10879
9bedbd36de49
Don't change hedgehogs total number to a value > 48 as result of voting.
unc0rr
parents:
10787
diff
changeset
|
128 |
teamsAtStart . fromJust . gameInfo $ rm |
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
129 |
else |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
130 |
teams rm |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
131 |
|
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
132 |
return $ ModifyRoom (\r -> r{defaultHedgehogsNumber = h}) : answers |
10081 | 133 |
|
10049 | 134 |
|
135 |
startVote :: VoteType -> Reader (ClientIndex, IRnC) [Action] |
|
10058 | 136 |
startVote vt = do |
137 |
(ci, rnc) <- ask |
|
10090 | 138 |
--cl <- thisClient |
10058 | 139 |
rm <- thisRoom |
140 |
chans <- roomClientsChans |
|
141 |
||
142 |
let uids = map (clUID . client rnc) . roomClients rnc $ clientRoom rnc ci |
|
143 |
||
144 |
if isJust $ voting rm then |
|
145 |
return [] |
|
146 |
else |
|
10212 | 147 |
return [ |
148 |
ModifyRoom (\r -> r{voting = Just (newVoting vt){entitledToVote = uids}}) |
|
149 |
, AnswerClients chans ["CHAT", "[server]", B.concat [loc "New voting started", ": ", voteInfo vt]] |
|
150 |
, ReactCmd ["VOTE", "YES"] |
|
10215 | 151 |
] |
10081 | 152 |
|
10049 | 153 |
|
10215 | 154 |
checkVotes :: StateT ServerState IO [Action] |
155 |
checkVotes = do |
|
156 |
rnc <- gets roomsClients |
|
10216 | 157 |
liftM concat $ io $ do |
10215 | 158 |
ris <- allRoomsM rnc |
10216 | 159 |
mapM (check rnc) ris |
10215 | 160 |
where |
161 |
check rnc ri = do |
|
162 |
e <- room'sM rnc voting ri |
|
163 |
case e of |
|
164 |
Just rv -> do |
|
165 |
modifyRoom rnc (\r -> r{voting = if voteTTL rv == 0 then Nothing else Just rv{voteTTL = voteTTL rv - 1}}) ri |
|
166 |
if voteTTL rv == 0 then do |
|
10216 | 167 |
chans <- liftM (map sendChan) $ roomClientsM rnc ri |
10215 | 168 |
return [AnswerClients chans ["CHAT", "[server]", loc "Voting expired"]] |
169 |
else |
|
170 |
return [] |
|
10216 | 171 |
Nothing -> return [] |
10058 | 172 |
|
10081 | 173 |
|
10058 | 174 |
voteInfo :: VoteType -> B.ByteString |
175 |
voteInfo (VoteKick n) = B.concat [loc "kick", " ", n] |
|
10195 | 176 |
voteInfo (VoteMap n) = B.concat [loc "map", " ", n] |
10392 | 177 |
voteInfo (VotePause) = B.concat [loc "pause"] |
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
178 |
voteInfo (VoteNewSeed) = B.concat [loc "new seed"] |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
179 |
voteInfo (VoteHedgehogsPerTeam i) = B.concat [loc "number of hedgehogs in team", " ", showB i] |