10058
|
1 |
{-# LANGUAGE OverloadedStrings #-}
|
10049
|
2 |
module Votes where
|
|
3 |
|
|
4 |
import Control.Monad.Reader
|
|
5 |
import Control.Monad.State
|
|
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
|
|
34 |
let (contra, pro) = L.partition snd $ votes vt
|
|
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
|
|
40 |
act $ voteType vt
|
|
41 |
closeVoting
|
|
42 |
else
|
|
43 |
return [ModifyRoom $ \r -> r{voting = Just vt}]
|
|
44 |
|
|
45 |
closeVoting = do
|
|
46 |
chans <- roomClientsChans
|
|
47 |
return [
|
|
48 |
AnswerClients chans ["CHAT", "[server]", loc "Voting closed"]
|
|
49 |
, ModifyRoom (\r -> r{voting = Nothing})
|
|
50 |
]
|
|
51 |
|
|
52 |
act (VoteKick nickname) = do
|
|
53 |
(thisClientId, rnc) <- ask
|
|
54 |
maybeClientId <- clientByNick nickname
|
|
55 |
rm <- thisRoom
|
|
56 |
let kickId = fromJust maybeClientId
|
|
57 |
let kickCl = rnc `client` kickId
|
|
58 |
let sameRoom = clientRoom rnc thisClientId == clientRoom rnc kickId
|
|
59 |
return
|
|
60 |
[KickRoomClient kickId |
|
|
61 |
isJust maybeClientId
|
|
62 |
&& sameRoom
|
|
63 |
&& ((isNothing $ gameInfo rm) || teamsInGame kickCl == 0)
|
|
64 |
]
|
10195
|
65 |
act (VoteMap roomSave) = do
|
|
66 |
rm <- thisRoom
|
|
67 |
let rs = Map.lookup roomSave (roomSaves rm)
|
|
68 |
case rs of
|
|
69 |
Nothing -> return []
|
|
70 |
Just (mp, p) -> return [ModifyRoom $ \r -> r{params = p, mapParams = mp}]
|
10081
|
71 |
|
10049
|
72 |
|
|
73 |
startVote :: VoteType -> Reader (ClientIndex, IRnC) [Action]
|
10058
|
74 |
startVote vt = do
|
|
75 |
(ci, rnc) <- ask
|
10090
|
76 |
--cl <- thisClient
|
10058
|
77 |
rm <- thisRoom
|
|
78 |
chans <- roomClientsChans
|
|
79 |
|
|
80 |
let uids = map (clUID . client rnc) . roomClients rnc $ clientRoom rnc ci
|
|
81 |
|
|
82 |
if isJust $ voting rm then
|
|
83 |
return []
|
|
84 |
else
|
|
85 |
liftM ([ModifyRoom (\r -> r{voting = Just (newVoting vt){entitledToVote = uids}})
|
|
86 |
, AnswerClients chans ["CHAT", "[server]", B.concat [loc "New voting started", ": ", voteInfo vt]]
|
10081
|
87 |
] ++ ) $ voted True
|
|
88 |
|
10049
|
89 |
|
|
90 |
checkVotes :: StateT ServerState IO ()
|
|
91 |
checkVotes = undefined
|
10058
|
92 |
|
10081
|
93 |
|
10058
|
94 |
voteInfo :: VoteType -> B.ByteString
|
|
95 |
voteInfo (VoteKick n) = B.concat [loc "kick", " ", n]
|
10195
|
96 |
voteInfo (VoteMap n) = B.concat [loc "map", " ", n]
|