|
1 module HandlerUtils where |
|
2 |
|
3 import Control.Monad.Reader |
|
4 import qualified Data.ByteString.Char8 as B |
|
5 import Data.List |
|
6 |
|
7 import RoomsAndClients |
|
8 import CoreTypes |
|
9 import Actions |
|
10 |
|
11 thisClient :: Reader (ClientIndex, IRnC) ClientInfo |
|
12 thisClient = do |
|
13 (ci, rnc) <- ask |
|
14 return $ rnc `client` ci |
|
15 |
|
16 thisRoom :: Reader (ClientIndex, IRnC) RoomInfo |
|
17 thisRoom = do |
|
18 (ci, rnc) <- ask |
|
19 let ri = clientRoom rnc ci |
|
20 return $ rnc `room` ri |
|
21 |
|
22 clientNick :: Reader (ClientIndex, IRnC) B.ByteString |
|
23 clientNick = liftM nick thisClient |
|
24 |
|
25 roomOthersChans :: Reader (ClientIndex, IRnC) [ClientChan] |
|
26 roomOthersChans = do |
|
27 (ci, rnc) <- ask |
|
28 let ri = clientRoom rnc ci |
|
29 return $ map (sendChan . client rnc) $ filter (/= ci) (roomClients rnc ri) |
|
30 |
|
31 roomSameClanChans :: Reader (ClientIndex, IRnC) [ClientChan] |
|
32 roomSameClanChans = do |
|
33 (ci, rnc) <- ask |
|
34 let ri = clientRoom rnc ci |
|
35 let otherRoomClients = map (client rnc) . filter (/= ci) $ roomClients rnc ri |
|
36 let cl = rnc `client` ci |
|
37 let thisClan = clientClan cl |
|
38 let sameClanClients = Prelude.filter (\c -> teamsInGame cl > 0 && clientClan c == thisClan) otherRoomClients |
|
39 let spectators = Prelude.filter (\c -> teamsInGame c == 0) otherRoomClients |
|
40 let sameClanOrSpec = if teamsInGame cl > 0 then sameClanClients else spectators |
|
41 return $ map sendChan sameClanOrSpec |
|
42 |
|
43 roomClientsChans :: Reader (ClientIndex, IRnC) [ClientChan] |
|
44 roomClientsChans = do |
|
45 (ci, rnc) <- ask |
|
46 let ri = clientRoom rnc ci |
|
47 return $ map (sendChan . client rnc) (roomClients rnc ri) |
|
48 |
|
49 thisClientChans :: Reader (ClientIndex, IRnC) [ClientChan] |
|
50 thisClientChans = do |
|
51 (ci, rnc) <- ask |
|
52 return $ [sendChan (rnc `client` ci)] |
|
53 |
|
54 answerClient :: [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
|
55 answerClient msg = thisClientChans >>= return . (: []) . flip AnswerClients msg |
|
56 |
|
57 allRoomInfos :: Reader (a, IRnC) [RoomInfo] |
|
58 allRoomInfos = liftM ((\irnc -> map (room irnc) $ allRooms irnc) . snd) ask |
|
59 |
|
60 clientByNick :: B.ByteString -> Reader (ClientIndex, IRnC) (Maybe ClientIndex) |
|
61 clientByNick n = do |
|
62 (_, rnc) <- ask |
|
63 let allClientIDs = allClients rnc |
|
64 return $ find (\clId -> n == nick (client rnc clId)) allClientIDs |
|
65 |