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