gameServer/HandlerUtils.hs
changeset 4588 5ef5415c4ee1
parent 4529 467ab0685890
parent 4586 4ba4f021070f
child 4647 20b982afbe6e
equal deleted inserted replaced
4529:467ab0685890 4588:5ef5415c4ee1
     1 module HandlerUtils where
       
     2 
       
     3 import Control.Monad.Reader
       
     4 import qualified Data.ByteString.Char8 as B
       
     5 
       
     6 import RoomsAndClients
       
     7 import CoreTypes
       
     8 import Actions
       
     9 
       
    10 thisClient :: Reader (ClientIndex, IRnC) ClientInfo
       
    11 thisClient = do
       
    12     (ci, rnc) <- ask
       
    13     return $ rnc `client` ci
       
    14 
       
    15 thisRoom :: Reader (ClientIndex, IRnC) RoomInfo
       
    16 thisRoom = do
       
    17     (ci, rnc) <- ask
       
    18     let ri = clientRoom rnc ci
       
    19     return $ rnc `room` ri
       
    20 
       
    21 clientNick :: Reader (ClientIndex, IRnC) B.ByteString
       
    22 clientNick = liftM nick thisClient
       
    23 
       
    24 roomOthersChans :: Reader (ClientIndex, IRnC) [ClientChan]
       
    25 roomOthersChans = do
       
    26     (ci, rnc) <- ask
       
    27     let ri = clientRoom rnc ci
       
    28     return $ map (sendChan . client rnc) $ filter (/= ci) (roomClients rnc ri)
       
    29 
       
    30 roomClientsChans :: Reader (ClientIndex, IRnC) [ClientChan]
       
    31 roomClientsChans = do
       
    32     (ci, rnc) <- ask
       
    33     let ri = clientRoom rnc ci
       
    34     return $ map (sendChan . client rnc) (roomClients rnc ri)
       
    35 
       
    36 thisClientChans :: Reader (ClientIndex, IRnC) [ClientChan]
       
    37 thisClientChans = do
       
    38     (ci, rnc) <- ask
       
    39     return $ [sendChan (rnc `client` ci)]
       
    40 
       
    41 answerClient :: [B.ByteString] -> Reader (ClientIndex, IRnC) [Action]
       
    42 answerClient msg = thisClientChans >>= return . (: []) . flip AnswerClients msg
       
    43 
       
    44 allRoomInfos :: Reader (a, IRnC) [RoomInfo]
       
    45 allRoomInfos = liftM ((\irnc -> map (room irnc) $ allRooms irnc) . snd) ask