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
|
10058
|
9 |
import Data.Maybe
|
|
10 |
-------------------
|
|
11 |
import Utils
|
|
12 |
import CoreTypes
|
|
13 |
import HandlerUtils
|
10049
|
14 |
|
10081
|
15 |
|
|
16 |
voted :: Bool -> Reader (ClientIndex, IRnC) [Action]
|
|
17 |
voted vote = do
|
|
18 |
cl <- thisClient
|
|
19 |
rm <- thisRoom
|
|
20 |
uid <- liftM clUID thisClient
|
|
21 |
|
|
22 |
if isNothing $ voting rm then
|
|
23 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "There's no voting going on"]]
|
|
24 |
else if uid `L.notElem` entitledToVote (fromJust $ voting rm) then
|
|
25 |
return []
|
|
26 |
else if uid `L.elem` map fst (votes . fromJust $ voting rm) then
|
|
27 |
return [AnswerClients [sendChan cl] ["CHAT", "[server]", loc "You already have voted"]]
|
|
28 |
else
|
10087
|
29 |
actOnVoting . fromJust . liftM (\v -> v{votes = (uid, vote):votes v}) $ voting rm
|
|
30 |
where
|
|
31 |
actOnVoting :: Voting -> Reader (ClientIndex, IRnC) [Action]
|
|
32 |
actOnVoting vt = do
|
|
33 |
let (contra, pro) = L.partition snd $ votes vt
|
|
34 |
let v = (length $ entitledToVote vt) `div` 2 + 1
|
|
35 |
|
|
36 |
if length contra >= v then
|
|
37 |
closeVoting
|
|
38 |
else if length pro >= v then do
|
|
39 |
act $ voteType vt
|
|
40 |
closeVoting
|
|
41 |
else
|
|
42 |
return [ModifyRoom $ \r -> r{voting = Just vt}]
|
|
43 |
|
|
44 |
closeVoting = do
|
|
45 |
chans <- roomClientsChans
|
|
46 |
return [
|
|
47 |
AnswerClients chans ["CHAT", "[server]", loc "Voting closed"]
|
|
48 |
, ModifyRoom (\r -> r{voting = Nothing})
|
|
49 |
]
|
|
50 |
|
|
51 |
act (VoteKick nickname) = do
|
|
52 |
(thisClientId, rnc) <- ask
|
|
53 |
maybeClientId <- clientByNick nickname
|
|
54 |
rm <- thisRoom
|
|
55 |
let kickId = fromJust maybeClientId
|
|
56 |
let kickCl = rnc `client` kickId
|
|
57 |
let sameRoom = clientRoom rnc thisClientId == clientRoom rnc kickId
|
|
58 |
return
|
|
59 |
[KickRoomClient kickId |
|
|
60 |
isJust maybeClientId
|
|
61 |
&& sameRoom
|
|
62 |
&& ((isNothing $ gameInfo rm) || teamsInGame kickCl == 0)
|
|
63 |
]
|
10081
|
64 |
|
10049
|
65 |
|
|
66 |
startVote :: VoteType -> Reader (ClientIndex, IRnC) [Action]
|
10058
|
67 |
startVote vt = do
|
|
68 |
(ci, rnc) <- ask
|
10090
|
69 |
--cl <- thisClient
|
10058
|
70 |
rm <- thisRoom
|
|
71 |
chans <- roomClientsChans
|
|
72 |
|
|
73 |
let uids = map (clUID . client rnc) . roomClients rnc $ clientRoom rnc ci
|
|
74 |
|
|
75 |
if isJust $ voting rm then
|
|
76 |
return []
|
|
77 |
else
|
|
78 |
liftM ([ModifyRoom (\r -> r{voting = Just (newVoting vt){entitledToVote = uids}})
|
|
79 |
, AnswerClients chans ["CHAT", "[server]", B.concat [loc "New voting started", ": ", voteInfo vt]]
|
10081
|
80 |
] ++ ) $ voted True
|
|
81 |
|
10049
|
82 |
|
|
83 |
checkVotes :: StateT ServerState IO ()
|
|
84 |
checkVotes = undefined
|
10058
|
85 |
|
10081
|
86 |
|
10058
|
87 |
voteInfo :: VoteType -> B.ByteString
|
|
88 |
voteInfo (VoteKick n) = B.concat [loc "kick", " ", n]
|
|
89 |
|