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