author | sheepluva |
Wed, 18 Mar 2015 21:03:53 +0100 | |
changeset 10860 | 891bf0aca23e |
parent 10787 | 50a4cdeedb44 |
child 10879 | 9bedbd36de49 |
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 |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10392
diff
changeset
|
3 |
* Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com> |
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 |
29 |
------------------- |
|
30 |
import Utils |
|
31 |
import CoreTypes |
|
32 |
import HandlerUtils |
|
10392 | 33 |
import EngineInteraction |
10049 | 34 |
|
10081 | 35 |
|
36 |
voted :: Bool -> Reader (ClientIndex, IRnC) [Action] |
|
37 |
voted vote = do |
|
38 |
cl <- thisClient |
|
39 |
rm <- thisRoom |
|
40 |
uid <- liftM clUID thisClient |
|
41 |
||
10392 | 42 |
case voting rm of |
43 |
Nothing -> |
|
44 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "There's no voting going on"]] |
|
45 |
Just voting -> |
|
46 |
if uid `L.notElem` entitledToVote voting then |
|
47 |
return [] |
|
48 |
else if uid `L.elem` map fst (votes voting) then |
|
49 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "You already have voted"]] |
|
50 |
else |
|
51 |
actOnVoting $ voting{votes = (uid, vote):votes voting} |
|
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
52 |
|
10087 | 53 |
where |
54 |
actOnVoting :: Voting -> Reader (ClientIndex, IRnC) [Action] |
|
55 |
actOnVoting vt = do |
|
10212 | 56 |
let (pro, contra) = L.partition snd $ votes vt |
10392 | 57 |
let totalV = length $ entitledToVote vt |
58 |
let successV = totalV `div` 2 + 1 |
|
10087 | 59 |
|
10392 | 60 |
if length contra > totalV - successV then |
10087 | 61 |
closeVoting |
10392 | 62 |
else if length pro >= successV then do |
10215 | 63 |
a <- act $ voteType vt |
64 |
c <- closeVoting |
|
65 |
return $ c ++ a |
|
10087 | 66 |
else |
67 |
return [ModifyRoom $ \r -> r{voting = Just vt}] |
|
68 |
||
69 |
closeVoting = do |
|
70 |
chans <- roomClientsChans |
|
71 |
return [ |
|
72 |
AnswerClients chans ["CHAT", "[server]", loc "Voting closed"] |
|
73 |
, ModifyRoom (\r -> r{voting = Nothing}) |
|
74 |
] |
|
75 |
||
76 |
act (VoteKick nickname) = do |
|
77 |
(thisClientId, rnc) <- ask |
|
78 |
maybeClientId <- clientByNick nickname |
|
79 |
rm <- thisRoom |
|
80 |
let kickId = fromJust maybeClientId |
|
81 |
let kickCl = rnc `client` kickId |
|
82 |
let sameRoom = clientRoom rnc thisClientId == clientRoom rnc kickId |
|
83 |
return |
|
84 |
[KickRoomClient kickId | |
|
85 |
isJust maybeClientId |
|
86 |
&& sameRoom |
|
87 |
&& ((isNothing $ gameInfo rm) || teamsInGame kickCl == 0) |
|
88 |
] |
|
10195 | 89 |
act (VoteMap roomSave) = do |
90 |
rm <- thisRoom |
|
91 |
let rs = Map.lookup roomSave (roomSaves rm) |
|
92 |
case rs of |
|
93 |
Nothing -> return [] |
|
10218 | 94 |
Just (mp, p) -> do |
95 |
cl <- thisClient |
|
96 |
chans <- roomClientsChans |
|
97 |
let a = map (replaceChans chans) $ answerFullConfigParams cl mp p |
|
98 |
return $ |
|
99 |
(ModifyRoom $ \r -> r{params = p, mapParams = mp}) |
|
100 |
: SendUpdateOnThisRoom |
|
101 |
: a |
|
102 |
where |
|
103 |
replaceChans chans (AnswerClients _ msg) = AnswerClients chans msg |
|
104 |
replaceChans _ a = a |
|
10392 | 105 |
act (VotePause) = do |
106 |
rm <- thisRoom |
|
107 |
chans <- roomClientsChans |
|
108 |
let modifyGameInfo f room = room{gameInfo = fmap f $ gameInfo room} |
|
109 |
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
|
110 |
AnswerClients chans ["CHAT", "[server]", loc "Pause toggled"], |
10392 | 111 |
AnswerClients chans ["EM", toEngineMsg "I"]] |
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
112 |
act (VoteNewSeed) = |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
113 |
return [SetRandomSeed] |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
114 |
act (VoteHedgehogsPerTeam h) = do |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
115 |
rm <- thisRoom |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
116 |
chans <- roomClientsChans |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
117 |
let answers = concatMap (\t -> |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
118 |
[ModifyRoom $ modifyTeam t{hhnum = h} |
10787 | 119 |
, AnswerClients chans ["HH_NUM", teamname t, showB h]] |
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
120 |
) |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
121 |
$ |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
122 |
if isJust $ gameInfo rm then |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
123 |
teamsAtStart . fromJust . gameInfo $ rm |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
124 |
else |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
125 |
teams rm |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
126 |
|
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
127 |
return $ ModifyRoom (\r -> r{defaultHedgehogsNumber = h}) : answers |
10081 | 128 |
|
10049 | 129 |
|
130 |
startVote :: VoteType -> Reader (ClientIndex, IRnC) [Action] |
|
10058 | 131 |
startVote vt = do |
132 |
(ci, rnc) <- ask |
|
10090 | 133 |
--cl <- thisClient |
10058 | 134 |
rm <- thisRoom |
135 |
chans <- roomClientsChans |
|
136 |
||
137 |
let uids = map (clUID . client rnc) . roomClients rnc $ clientRoom rnc ci |
|
138 |
||
139 |
if isJust $ voting rm then |
|
140 |
return [] |
|
141 |
else |
|
10212 | 142 |
return [ |
143 |
ModifyRoom (\r -> r{voting = Just (newVoting vt){entitledToVote = uids}}) |
|
144 |
, AnswerClients chans ["CHAT", "[server]", B.concat [loc "New voting started", ": ", voteInfo vt]] |
|
145 |
, ReactCmd ["VOTE", "YES"] |
|
10215 | 146 |
] |
10081 | 147 |
|
10049 | 148 |
|
10215 | 149 |
checkVotes :: StateT ServerState IO [Action] |
150 |
checkVotes = do |
|
151 |
rnc <- gets roomsClients |
|
10216 | 152 |
liftM concat $ io $ do |
10215 | 153 |
ris <- allRoomsM rnc |
10216 | 154 |
mapM (check rnc) ris |
10215 | 155 |
where |
156 |
check rnc ri = do |
|
157 |
e <- room'sM rnc voting ri |
|
158 |
case e of |
|
159 |
Just rv -> do |
|
160 |
modifyRoom rnc (\r -> r{voting = if voteTTL rv == 0 then Nothing else Just rv{voteTTL = voteTTL rv - 1}}) ri |
|
161 |
if voteTTL rv == 0 then do |
|
10216 | 162 |
chans <- liftM (map sendChan) $ roomClientsM rnc ri |
10215 | 163 |
return [AnswerClients chans ["CHAT", "[server]", loc "Voting expired"]] |
164 |
else |
|
165 |
return [] |
|
10216 | 166 |
Nothing -> return [] |
10058 | 167 |
|
10081 | 168 |
|
10058 | 169 |
voteInfo :: VoteType -> B.ByteString |
170 |
voteInfo (VoteKick n) = B.concat [loc "kick", " ", n] |
|
10195 | 171 |
voteInfo (VoteMap n) = B.concat [loc "map", " ", n] |
10392 | 172 |
voteInfo (VotePause) = B.concat [loc "pause"] |
10786
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
173 |
voteInfo (VoteNewSeed) = B.concat [loc "new seed"] |
712283ed86e0
Implement /newseed and /hedgehogs commands. Only tested for building.
unc0rr
parents:
10464
diff
changeset
|
174 |
voteInfo (VoteHedgehogsPerTeam i) = B.concat [loc "number of hedgehogs in team", " ", showB i] |