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
|
|
29 |
return [ModifyRoom $ \r -> r{voting = liftM (\v -> v{votes = (uid, vote):votes v}) $ voting rm}]
|
|
30 |
|
10049
|
31 |
|
|
32 |
startVote :: VoteType -> Reader (ClientIndex, IRnC) [Action]
|
10058
|
33 |
startVote vt = do
|
|
34 |
(ci, rnc) <- ask
|
|
35 |
cl <- thisClient
|
|
36 |
rm <- thisRoom
|
|
37 |
chans <- roomClientsChans
|
|
38 |
|
|
39 |
let uids = map (clUID . client rnc) . roomClients rnc $ clientRoom rnc ci
|
|
40 |
|
|
41 |
if isJust $ voting rm then
|
|
42 |
return []
|
|
43 |
else
|
|
44 |
liftM ([ModifyRoom (\r -> r{voting = Just (newVoting vt){entitledToVote = uids}})
|
|
45 |
, AnswerClients chans ["CHAT", "[server]", B.concat [loc "New voting started", ": ", voteInfo vt]]
|
10081
|
46 |
] ++ ) $ voted True
|
|
47 |
|
10049
|
48 |
|
|
49 |
checkVotes :: StateT ServerState IO ()
|
|
50 |
checkVotes = undefined
|
10058
|
51 |
|
10081
|
52 |
|
10058
|
53 |
voteInfo :: VoteType -> B.ByteString
|
|
54 |
voteInfo (VoteKick n) = B.concat [loc "kick", " ", n]
|
|
55 |
|