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