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