author | nemo |
Wed, 19 May 2010 02:10:28 +0000 | |
changeset 3476 | 1ec68b8d3bd1 |
parent 3458 | 11cd56019f00 |
child 3501 | a3159a410e5c |
permissions | -rw-r--r-- |
3425 | 1 |
module RoomsAndClients( |
2 |
RoomIndex(), |
|
3 |
ClientIndex(), |
|
4 |
MRoomsAndClients(), |
|
5 |
IRoomsAndClients(), |
|
6 |
newRoomsAndClients, |
|
7 |
addRoom, |
|
8 |
addClient, |
|
9 |
removeRoom, |
|
10 |
removeClient, |
|
3436
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
11 |
modifyRoom, |
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
12 |
modifyClient, |
3425 | 13 |
lobbyId, |
14 |
moveClientToLobby, |
|
15 |
moveClientToRoom, |
|
16 |
clientRoom, |
|
3436
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
17 |
clientRoomM, |
3425 | 18 |
client, |
3458 | 19 |
clientsM, |
3425 | 20 |
allClients, |
21 |
withRoomsAndClients, |
|
3435 | 22 |
showRooms, |
23 |
roomClients |
|
3425 | 24 |
) where |
25 |
||
26 |
||
27 |
import Store |
|
28 |
import Control.Monad |
|
29 |
||
30 |
||
31 |
data Room r = Room { |
|
32 |
roomClients' :: [ClientIndex], |
|
33 |
room' :: r |
|
34 |
} |
|
35 |
||
36 |
||
37 |
data Client c = Client { |
|
38 |
clientRoom' :: RoomIndex, |
|
39 |
client' :: c |
|
40 |
} |
|
41 |
||
42 |
||
43 |
newtype RoomIndex = RoomIndex ElemIndex |
|
44 |
deriving (Eq) |
|
45 |
newtype ClientIndex = ClientIndex ElemIndex |
|
3435 | 46 |
deriving (Eq, Show, Read) |
3425 | 47 |
|
48 |
instance Show RoomIndex where |
|
49 |
show (RoomIndex i) = 'r' : show i |
|
50 |
||
51 |
unRoomIndex :: RoomIndex -> ElemIndex |
|
52 |
unRoomIndex (RoomIndex r) = r |
|
53 |
||
54 |
unClientIndex :: ClientIndex -> ElemIndex |
|
55 |
unClientIndex (ClientIndex c) = c |
|
56 |
||
57 |
||
58 |
newtype MRoomsAndClients r c = MRoomsAndClients (MStore (Room r), MStore (Client c)) |
|
59 |
newtype IRoomsAndClients r c = IRoomsAndClients (IStore (Room r), IStore (Client c)) |
|
60 |
||
61 |
||
62 |
lobbyId :: RoomIndex |
|
63 |
lobbyId = RoomIndex firstIndex |
|
64 |
||
65 |
||
66 |
newRoomsAndClients :: r -> IO (MRoomsAndClients r c) |
|
67 |
newRoomsAndClients r = do |
|
68 |
rooms <- newStore |
|
69 |
clients <- newStore |
|
70 |
let rnc = MRoomsAndClients (rooms, clients) |
|
71 |
ri <- addRoom rnc r |
|
72 |
when (ri /= lobbyId) $ error "Empty struct inserts not at firstIndex index" |
|
73 |
return rnc |
|
74 |
||
75 |
||
76 |
roomAddClient :: ClientIndex -> Room r -> Room r |
|
77 |
roomAddClient cl room = room{roomClients' = cl : roomClients' room} |
|
78 |
||
79 |
roomRemoveClient :: ClientIndex -> Room r -> Room r |
|
80 |
roomRemoveClient cl room = room{roomClients' = filter (/= cl) $ roomClients' room} |
|
81 |
||
3435 | 82 |
|
3425 | 83 |
addRoom :: MRoomsAndClients r c -> r -> IO RoomIndex |
84 |
addRoom (MRoomsAndClients (rooms, _)) room = do |
|
85 |
i <- addElem rooms (Room [] room) |
|
86 |
return $ RoomIndex i |
|
87 |
||
88 |
||
89 |
addClient :: MRoomsAndClients r c -> c -> IO ClientIndex |
|
90 |
addClient (MRoomsAndClients (rooms, clients)) client = do |
|
91 |
i <- addElem clients (Client lobbyId client) |
|
92 |
modifyElem rooms (roomAddClient (ClientIndex i)) rid |
|
93 |
return $ ClientIndex i |
|
94 |
where |
|
95 |
rid = (\(RoomIndex i) -> i) lobbyId |
|
96 |
||
97 |
removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO () |
|
98 |
removeRoom rnc@(MRoomsAndClients (rooms, _)) room@(RoomIndex ri) |
|
99 |
| room == lobbyId = error "Cannot delete lobby" |
|
100 |
| otherwise = do |
|
101 |
clIds <- liftM roomClients' $ readElem rooms ri |
|
102 |
forM_ clIds (moveClientToLobby rnc) |
|
103 |
removeElem rooms ri |
|
104 |
||
105 |
||
106 |
removeClient :: MRoomsAndClients r c -> ClientIndex -> IO () |
|
107 |
removeClient (MRoomsAndClients (rooms, clients)) cl@(ClientIndex ci) = do |
|
108 |
RoomIndex ri <- liftM clientRoom' $ readElem clients ci |
|
109 |
modifyElem rooms (roomRemoveClient cl) ri |
|
110 |
removeElem clients ci |
|
111 |
||
112 |
||
3436
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
113 |
modifyRoom :: MRoomsAndClients r c -> (r -> r) -> RoomIndex -> IO () |
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
114 |
modifyRoom (MRoomsAndClients (rooms, _)) f (RoomIndex ri) = modifyElem rooms (\r -> r{room' = f $ room' r}) ri |
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
115 |
|
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
116 |
modifyClient :: MRoomsAndClients r c -> (c -> c) -> ClientIndex -> IO () |
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
117 |
modifyClient (MRoomsAndClients (_, clients)) f (ClientIndex ci) = modifyElem clients (\c -> c{client' = f $ client' c}) ci |
288fcbdb77b6
Make server build again (it's still useless though)
unc0rr
parents:
3435
diff
changeset
|
118 |
|
3425 | 119 |
moveClientInRooms :: MRoomsAndClients r c -> RoomIndex -> RoomIndex -> ClientIndex -> IO () |
120 |
moveClientInRooms (MRoomsAndClients (rooms, clients)) (RoomIndex riFrom) rt@(RoomIndex riTo) cl@(ClientIndex ci) = do |
|
121 |
modifyElem rooms (roomRemoveClient cl) riFrom |
|
122 |
modifyElem rooms (roomAddClient cl) riTo |
|
123 |
modifyElem clients (\c -> c{clientRoom' = rt}) ci |
|
124 |
||
125 |
||
126 |
moveClientToLobby :: MRoomsAndClients r c -> ClientIndex -> IO () |
|
127 |
moveClientToLobby rnc ci = do |
|
128 |
room <- clientRoomM rnc ci |
|
129 |
moveClientInRooms rnc room lobbyId ci |
|
130 |
||
131 |
||
132 |
moveClientToRoom :: MRoomsAndClients r c -> RoomIndex -> ClientIndex -> IO () |
|
133 |
moveClientToRoom rnc ri ci = moveClientInRooms rnc lobbyId ri ci |
|
134 |
||
135 |
||
136 |
clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex |
|
137 |
clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci) |
|
138 |
||
3458 | 139 |
clientsM :: MRoomsAndClients r c -> (c -> a) -> ClientIndex -> IO a |
140 |
clientsM (MRoomsAndClients (_, clients)) f (ClientIndex ci) = liftM (f . client') (clients `readElem` ci) |
|
141 |
||
3425 | 142 |
|
143 |
withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a |
|
144 |
withRoomsAndClients (MRoomsAndClients (rooms, clients)) f = |
|
145 |
withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c)) |
|
146 |
||
147 |
---------------------------------------- |
|
148 |
----------- IRoomsAndClients ----------- |
|
149 |
||
150 |
showRooms :: (Show r, Show c) => IRoomsAndClients r c -> String |
|
151 |
showRooms rnc@(IRoomsAndClients (rooms, clients)) = concatMap showRoom (allRooms rnc) |
|
152 |
where |
|
153 |
showRoom r = unlines $ ((show r) ++ ": " ++ (show $ room' $ rooms ! (unRoomIndex r))) : (map showClient (roomClients' $ rooms ! (unRoomIndex r))) |
|
154 |
showClient c = " " ++ (show c) ++ ": " ++ (show $ client' $ clients ! (unClientIndex c)) |
|
155 |
||
156 |
||
157 |
allRooms :: IRoomsAndClients r c -> [RoomIndex] |
|
158 |
allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms |
|
159 |
||
160 |
allClients :: IRoomsAndClients r c -> [ClientIndex] |
|
161 |
allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients |
|
162 |
||
163 |
||
3435 | 164 |
clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex |
165 |
clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci) |
|
3425 | 166 |
|
167 |
client :: IRoomsAndClients r c -> ClientIndex -> c |
|
168 |
client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci) |
|
3435 | 169 |
|
170 |
roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex] |
|
171 |
roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' $ (rooms ! ri) |