author | nemo |
Thu, 15 Sep 2011 22:33:55 -0400 | |
changeset 5925 | 2a9353b800cd |
parent 4989 | 4771fed9272e |
child 6541 | 08ed346ed341 |
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 |
|
4986
33fe91b2bcbf
Use Maybe for storing client's clan, allows less error-prone spectator checks
unc0rr
parents:
4975
diff
changeset
|
37 |
let sameClanClients = Prelude.filter (\c -> clientClan c == clientClan cl) otherRoomClients |
33fe91b2bcbf
Use Maybe for storing client's clan, allows less error-prone spectator checks
unc0rr
parents:
4975
diff
changeset
|
38 |
return $ map sendChan sameClanClients |
4614 | 39 |
|
3543 | 40 |
roomClientsChans :: Reader (ClientIndex, IRnC) [ClientChan] |
41 |
roomClientsChans = do |
|
42 |
(ci, rnc) <- ask |
|
43 |
let ri = clientRoom rnc ci |
|
44 |
return $ map (sendChan . client rnc) (roomClients rnc ri) |
|
45 |
||
3435 | 46 |
thisClientChans :: Reader (ClientIndex, IRnC) [ClientChan] |
47 |
thisClientChans = do |
|
48 |
(ci, rnc) <- ask |
|
4932 | 49 |
return [sendChan (rnc `client` ci)] |
3435 | 50 |
|
4989 | 51 |
answerClient :: [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
4932 | 52 |
answerClient msg = liftM ((: []) . flip AnswerClients msg) thisClientChans |
3501 | 53 |
|
54 |
allRoomInfos :: Reader (a, IRnC) [RoomInfo] |
|
55 |
allRoomInfos = liftM ((\irnc -> map (room irnc) $ allRooms irnc) . snd) ask |
|
4614 | 56 |
|
57 |
clientByNick :: B.ByteString -> Reader (ClientIndex, IRnC) (Maybe ClientIndex) |
|
58 |
clientByNick n = do |
|
59 |
(_, rnc) <- ask |
|
60 |
let allClientIDs = allClients rnc |
|
61 |
return $ find (\clId -> n == nick (client rnc clId)) allClientIDs |
|
62 |