87 |
90 |
88 |
91 |
89 addClient :: MRoomsAndClients r c -> c -> IO ClientIndex |
92 addClient :: MRoomsAndClients r c -> c -> IO ClientIndex |
90 addClient (MRoomsAndClients (rooms, clients)) client = do |
93 addClient (MRoomsAndClients (rooms, clients)) client = do |
91 i <- addElem clients (Client lobbyId client) |
94 i <- addElem clients (Client lobbyId client) |
92 modifyElem rooms (roomAddClient (ClientIndex i)) rid |
95 modifyElem rooms (roomAddClient (ClientIndex i)) (unRoomIndex lobbyId) |
93 return $ ClientIndex i |
96 return $ ClientIndex i |
94 where |
|
95 rid = (\(RoomIndex i) -> i) lobbyId |
|
96 |
97 |
97 removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO () |
98 removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO () |
98 removeRoom rnc@(MRoomsAndClients (rooms, _)) room@(RoomIndex ri) |
99 removeRoom rnc@(MRoomsAndClients (rooms, _)) room@(RoomIndex ri) |
99 | room == lobbyId = error "Cannot delete lobby" |
100 | room == lobbyId = error "Cannot delete lobby" |
100 | otherwise = do |
101 | otherwise = do |
134 |
135 |
135 |
136 |
136 clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex |
137 clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex |
137 clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci) |
138 clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci) |
138 |
139 |
139 clientsM :: MRoomsAndClients r c -> (c -> a) -> ClientIndex -> IO a |
140 client'sM :: MRoomsAndClients r c -> (c -> a) -> ClientIndex -> IO a |
140 clientsM (MRoomsAndClients (_, clients)) f (ClientIndex ci) = liftM (f . client') (clients `readElem` ci) |
141 client'sM (MRoomsAndClients (_, clients)) f (ClientIndex ci) = liftM (f . client') (clients `readElem` ci) |
141 |
142 |
|
143 clientsM :: MRoomsAndClients r c -> IO [c] |
|
144 clientsM (MRoomsAndClients (_, clients)) = indicesM clients >>= mapM (\ci -> liftM client' $ readElem clients ci) |
142 |
145 |
143 withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a |
146 withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a |
144 withRoomsAndClients (MRoomsAndClients (rooms, clients)) f = |
147 withRoomsAndClients (MRoomsAndClients (rooms, clients)) f = |
145 withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c)) |
148 withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c)) |
146 |
149 |
158 allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms |
161 allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms |
159 |
162 |
160 allClients :: IRoomsAndClients r c -> [ClientIndex] |
163 allClients :: IRoomsAndClients r c -> [ClientIndex] |
161 allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients |
164 allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients |
162 |
165 |
163 |
|
164 clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex |
166 clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex |
165 clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci) |
167 clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci) |
166 |
168 |
167 client :: IRoomsAndClients r c -> ClientIndex -> c |
169 client :: IRoomsAndClients r c -> ClientIndex -> c |
168 client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci) |
170 client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci) |
169 |
171 |
|
172 room :: IRoomsAndClients r c -> RoomIndex -> r |
|
173 room (IRoomsAndClients (rooms, _)) (RoomIndex ri) = room' (rooms ! ri) |
|
174 |
170 roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex] |
175 roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex] |
171 roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' $ (rooms ! ri) |
176 roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' $ (rooms ! ri) |