gameServer/Votes.hs
changeset 10216 6928a323097f
parent 10215 26fc5502ba22
child 10218 1d7112ccb3e9
equal deleted inserted replaced
10215:26fc5502ba22 10216:6928a323097f
     1 {-# LANGUAGE OverloadedStrings #-}
     1 {-# LANGUAGE OverloadedStrings #-}
     2 module Votes where
     2 module Votes where
     3 
     3 
     4 import Control.Monad.Reader
     4 import Control.Monad.Reader
     5 import Control.Monad.State
     5 import Control.Monad.State.Strict
     6 import ServerState
     6 import ServerState
     7 import qualified Data.ByteString.Char8 as B
     7 import qualified Data.ByteString.Char8 as B
     8 import qualified Data.List as L
     8 import qualified Data.List as L
     9 import qualified Data.Map as Map
     9 import qualified Data.Map as Map
    10 import Data.Maybe
    10 import Data.Maybe
    91 
    91 
    92 
    92 
    93 checkVotes :: StateT ServerState IO [Action]
    93 checkVotes :: StateT ServerState IO [Action]
    94 checkVotes = do
    94 checkVotes = do
    95     rnc <- gets roomsClients
    95     rnc <- gets roomsClients
    96     io $ do
    96     liftM concat $ io $ do
    97         ris <- allRoomsM rnc
    97         ris <- allRoomsM rnc
    98         actions <- mapM (check rnc) ris
    98         mapM (check rnc) ris
    99         mapM_ processAction actions
       
   100     where
    99     where
   101         check rnc ri = do
   100         check rnc ri = do
   102             e <- room'sM rnc voting ri
   101             e <- room'sM rnc voting ri
   103             case e of
   102             case e of
   104                  Just rv -> do
   103                  Just rv -> do
   105                      modifyRoom rnc (\r -> r{voting = if voteTTL rv == 0 then Nothing else Just rv{voteTTL = voteTTL rv - 1}}) ri
   104                      modifyRoom rnc (\r -> r{voting = if voteTTL rv == 0 then Nothing else Just rv{voteTTL = voteTTL rv - 1}}) ri
   106                      if voteTTL rv == 0 then do
   105                      if voteTTL rv == 0 then do
   107                         chans <- liftM sendChan $ roomClientsM rnc ri
   106                         chans <- liftM (map sendChan) $ roomClientsM rnc ri
   108                         return [AnswerClients chans ["CHAT", "[server]", loc "Voting expired"]]
   107                         return [AnswerClients chans ["CHAT", "[server]", loc "Voting expired"]]
   109                         else
   108                         else
   110                         return []
   109                         return []
   111                 Nothing -> return []
   110                  Nothing -> return []
   112 
   111 
   113 
   112 
   114 voteInfo :: VoteType -> B.ByteString
   113 voteInfo :: VoteType -> B.ByteString
   115 voteInfo (VoteKick n) = B.concat [loc "kick", " ", n]
   114 voteInfo (VoteKick n) = B.concat [loc "kick", " ", n]
   116 voteInfo (VoteMap n) = B.concat [loc "map", " ", n]
   115 voteInfo (VoteMap n) = B.concat [loc "map", " ", n]