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