author | unc0rr |
Sun, 30 Jan 2011 20:43:18 +0300 | |
branch | server_refactor |
changeset 4620 | 6122a43d3424 |
parent 4614 | 26661bf28dd5 |
child 4932 | f11d80bac7ed |
permissions | -rw-r--r-- |
3435 | 1 |
module HandlerUtils where |
2 |
||
3 |
import Control.Monad.Reader |
|
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
4 |
import qualified Data.ByteString.Char8 as B |
4614 | 5 |
import Data.List |
3435 | 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 |
||
3568 | 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 |
||
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
22 |
clientNick :: Reader (ClientIndex, IRnC) B.ByteString |
3435 | 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 |
|
3542 | 29 |
return $ map (sendChan . client rnc) $ filter (/= ci) (roomClients rnc ri) |
3435 | 30 |
|
4614 | 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 |
||
3543 | 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 |
||
3435 | 49 |
thisClientChans :: Reader (ClientIndex, IRnC) [ClientChan] |
50 |
thisClientChans = do |
|
51 |
(ci, rnc) <- ask |
|
52 |
return $ [sendChan (rnc `client` ci)] |
|
53 |
||
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
54 |
answerClient :: [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
3435 | 55 |
answerClient msg = thisClientChans >>= return . (: []) . flip AnswerClients msg |
3501 | 56 |
|
57 |
allRoomInfos :: Reader (a, IRnC) [RoomInfo] |
|
58 |
allRoomInfos = liftM ((\irnc -> map (room irnc) $ allRooms irnc) . snd) ask |
|
4614 | 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 |