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