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