10058
|
1 |
{-# LANGUAGE OverloadedStrings #-}
|
10049
|
2 |
module Votes where
|
|
3 |
|
|
4 |
import Control.Monad.Reader
|
10216
|
5 |
import Control.Monad.State.Strict
|
10049
|
6 |
import ServerState
|
10058
|
7 |
import qualified Data.ByteString.Char8 as B
|
10081
|
8 |
import qualified Data.List as L
|
10195
|
9 |
import qualified Data.Map as Map
|
10058
|
10 |
import Data.Maybe
|
|
11 |
-------------------
|
|
12 |
import Utils
|
|
13 |
import CoreTypes
|
|
14 |
import HandlerUtils
|
10049
|
15 |
|
10081
|
16 |
|
|
17 |
voted :: Bool -> Reader (ClientIndex, IRnC) [Action]
|
|
18 |
voted vote = do
|
|
19 |
cl <- thisClient
|
|
20 |
rm <- thisRoom
|
|
21 |
uid <- liftM clUID thisClient
|
|
22 |
|
|
23 |
if isNothing $ voting rm then
|
|
24 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "There's no voting going on"]]
|
|
25 |
else if uid `L.notElem` entitledToVote (fromJust $ voting rm) then
|
|
26 |
return []
|
|
27 |
else if uid `L.elem` map fst (votes . fromJust $ voting rm) then
|
|
28 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "You already have voted"]]
|
|
29 |
else
|
10087
|
30 |
actOnVoting . fromJust . liftM (\v -> v{votes = (uid, vote):votes v}) $ voting rm
|
|
31 |
where
|
|
32 |
actOnVoting :: Voting -> Reader (ClientIndex, IRnC) [Action]
|
|
33 |
actOnVoting vt = do
|
10212
|
34 |
let (pro, contra) = L.partition snd $ votes vt
|
10087
|
35 |
let v = (length $ entitledToVote vt) `div` 2 + 1
|
|
36 |
|
|
37 |
if length contra >= v then
|
|
38 |
closeVoting
|
|
39 |
else if length pro >= v then do
|
10215
|
40 |
a <- act $ voteType vt
|
|
41 |
c <- closeVoting
|
|
42 |
return $ c ++ a
|
10087
|
43 |
else
|
|
44 |
return [ModifyRoom $ \r -> r{voting = Just vt}]
|
|
45 |
|
|
46 |
closeVoting = do
|
|
47 |
chans <- roomClientsChans
|
|
48 |
return [
|
|
49 |
AnswerClients chans ["CHAT", "[server]", loc "Voting closed"]
|
|
50 |
, ModifyRoom (\r -> r{voting = Nothing})
|
|
51 |
]
|
|
52 |
|
|
53 |
act (VoteKick nickname) = do
|
|
54 |
(thisClientId, rnc) <- ask
|
|
55 |
maybeClientId <- clientByNick nickname
|
|
56 |
rm <- thisRoom
|
|
57 |
let kickId = fromJust maybeClientId
|
|
58 |
let kickCl = rnc `client` kickId
|
|
59 |
let sameRoom = clientRoom rnc thisClientId == clientRoom rnc kickId
|
|
60 |
return
|
|
61 |
[KickRoomClient kickId |
|
|
62 |
isJust maybeClientId
|
|
63 |
&& sameRoom
|
|
64 |
&& ((isNothing $ gameInfo rm) || teamsInGame kickCl == 0)
|
|
65 |
]
|
10195
|
66 |
act (VoteMap roomSave) = do
|
|
67 |
rm <- thisRoom
|
|
68 |
let rs = Map.lookup roomSave (roomSaves rm)
|
|
69 |
case rs of
|
|
70 |
Nothing -> return []
|
10218
|
71 |
Just (mp, p) -> do
|
|
72 |
cl <- thisClient
|
|
73 |
chans <- roomClientsChans
|
|
74 |
let a = map (replaceChans chans) $ answerFullConfigParams cl mp p
|
|
75 |
return $
|
|
76 |
(ModifyRoom $ \r -> r{params = p, mapParams = mp})
|
|
77 |
: SendUpdateOnThisRoom
|
|
78 |
: a
|
|
79 |
where
|
|
80 |
replaceChans chans (AnswerClients _ msg) = AnswerClients chans msg
|
|
81 |
replaceChans _ a = a
|
10081
|
82 |
|
10049
|
83 |
|
|
84 |
startVote :: VoteType -> Reader (ClientIndex, IRnC) [Action]
|
10058
|
85 |
startVote vt = do
|
|
86 |
(ci, rnc) <- ask
|
10090
|
87 |
--cl <- thisClient
|
10058
|
88 |
rm <- thisRoom
|
|
89 |
chans <- roomClientsChans
|
|
90 |
|
|
91 |
let uids = map (clUID . client rnc) . roomClients rnc $ clientRoom rnc ci
|
|
92 |
|
|
93 |
if isJust $ voting rm then
|
|
94 |
return []
|
|
95 |
else
|
10212
|
96 |
return [
|
|
97 |
ModifyRoom (\r -> r{voting = Just (newVoting vt){entitledToVote = uids}})
|
|
98 |
, AnswerClients chans ["CHAT", "[server]", B.concat [loc "New voting started", ": ", voteInfo vt]]
|
|
99 |
, ReactCmd ["VOTE", "YES"]
|
10215
|
100 |
]
|
10081
|
101 |
|
10049
|
102 |
|
10215
|
103 |
checkVotes :: StateT ServerState IO [Action]
|
|
104 |
checkVotes = do
|
|
105 |
rnc <- gets roomsClients
|
10216
|
106 |
liftM concat $ io $ do
|
10215
|
107 |
ris <- allRoomsM rnc
|
10216
|
108 |
mapM (check rnc) ris
|
10215
|
109 |
where
|
|
110 |
check rnc ri = do
|
|
111 |
e <- room'sM rnc voting ri
|
|
112 |
case e of
|
|
113 |
Just rv -> do
|
|
114 |
modifyRoom rnc (\r -> r{voting = if voteTTL rv == 0 then Nothing else Just rv{voteTTL = voteTTL rv - 1}}) ri
|
|
115 |
if voteTTL rv == 0 then do
|
10216
|
116 |
chans <- liftM (map sendChan) $ roomClientsM rnc ri
|
10215
|
117 |
return [AnswerClients chans ["CHAT", "[server]", loc "Voting expired"]]
|
|
118 |
else
|
|
119 |
return []
|
10216
|
120 |
Nothing -> return []
|
10058
|
121 |
|
10081
|
122 |
|
10058
|
123 |
voteInfo :: VoteType -> B.ByteString
|
|
124 |
voteInfo (VoteKick n) = B.concat [loc "kick", " ", n]
|
10195
|
125 |
voteInfo (VoteMap n) = B.concat [loc "map", " ", n]
|