author | nemo |
Mon, 18 Feb 2013 14:06:16 -0500 | |
changeset 8516 | e422cbf9aa33 |
parent 8452 | 170afc3ac39f |
child 10215 | 26fc5502ba22 |
permissions | -rw-r--r-- |
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
1 |
{-# LANGUAGE BangPatterns, GeneralizedNewtypeDeriving #-} |
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
2 |
|
4905 | 3 |
module RoomsAndClients( |
4 |
RoomIndex(), |
|
5 |
ClientIndex(), |
|
6 |
MRoomsAndClients(), |
|
7 |
IRoomsAndClients(), |
|
8 |
newRoomsAndClients, |
|
9 |
addRoom, |
|
10 |
addClient, |
|
11 |
removeRoom, |
|
12 |
removeClient, |
|
13 |
modifyRoom, |
|
14 |
modifyClient, |
|
15 |
lobbyId, |
|
16 |
moveClientToLobby, |
|
17 |
moveClientToRoom, |
|
18 |
clientRoomM, |
|
19 |
clientExists, |
|
20 |
client, |
|
21 |
room, |
|
22 |
client'sM, |
|
23 |
room'sM, |
|
24 |
allClientsM, |
|
25 |
clientsM, |
|
8452 | 26 |
roomsM, |
4905 | 27 |
roomClientsM, |
28 |
roomClientsIndicesM, |
|
29 |
withRoomsAndClients, |
|
30 |
allRooms, |
|
31 |
allClients, |
|
32 |
clientRoom, |
|
33 |
showRooms, |
|
34 |
roomClients |
|
35 |
) where |
|
36 |
||
37 |
||
38 |
import Store |
|
39 |
import Control.Monad |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
40 |
import Control.DeepSeq |
4905 | 41 |
|
42 |
||
43 |
data Room r = Room { |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
44 |
roomClients' :: ![ClientIndex], |
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
45 |
room' :: !r |
4905 | 46 |
} |
47 |
||
48 |
||
49 |
data Client c = Client { |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
50 |
clientRoom' :: !RoomIndex, |
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
51 |
client' :: !c |
4905 | 52 |
} |
53 |
||
54 |
||
55 |
newtype RoomIndex = RoomIndex ElemIndex |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
56 |
deriving (Eq, NFData) |
4905 | 57 |
newtype ClientIndex = ClientIndex ElemIndex |
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
58 |
deriving (Eq, Show, Read, Ord, NFData) |
4905 | 59 |
|
60 |
instance Show RoomIndex where |
|
61 |
show (RoomIndex i) = 'r' : show i |
|
62 |
||
63 |
unRoomIndex :: RoomIndex -> ElemIndex |
|
64 |
unRoomIndex (RoomIndex r) = r |
|
65 |
||
66 |
unClientIndex :: ClientIndex -> ElemIndex |
|
67 |
unClientIndex (ClientIndex c) = c |
|
68 |
||
69 |
||
70 |
newtype MRoomsAndClients r c = MRoomsAndClients (MStore (Room r), MStore (Client c)) |
|
71 |
newtype IRoomsAndClients r c = IRoomsAndClients (IStore (Room r), IStore (Client c)) |
|
72 |
||
73 |
||
74 |
lobbyId :: RoomIndex |
|
75 |
lobbyId = RoomIndex firstIndex |
|
76 |
||
77 |
||
78 |
newRoomsAndClients :: r -> IO (MRoomsAndClients r c) |
|
79 |
newRoomsAndClients r = do |
|
80 |
rooms <- newStore |
|
81 |
clients <- newStore |
|
82 |
let rnc = MRoomsAndClients (rooms, clients) |
|
83 |
ri <- addRoom rnc r |
|
84 |
when (ri /= lobbyId) $ error "Empty struct inserts not at firstIndex index" |
|
85 |
return rnc |
|
86 |
||
87 |
||
88 |
roomAddClient :: ClientIndex -> Room r -> Room r |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
89 |
roomAddClient cl rm = let cls = cl : roomClients' rm; nr = rm{roomClients' = cls} in cls `deepseq` nr |
4905 | 90 |
|
91 |
roomRemoveClient :: ClientIndex -> Room r -> Room r |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
4932
diff
changeset
|
92 |
roomRemoveClient cl rm = let cls = filter (/= cl) $ roomClients' rm; nr = rm{roomClients' = cls} in cls `deepseq` nr |
4905 | 93 |
|
94 |
||
95 |
addRoom :: MRoomsAndClients r c -> r -> IO RoomIndex |
|
4932 | 96 |
addRoom (MRoomsAndClients (rooms, _)) rm = do |
97 |
i <- addElem rooms (Room [] rm) |
|
4905 | 98 |
return $ RoomIndex i |
99 |
||
100 |
||
101 |
addClient :: MRoomsAndClients r c -> c -> IO ClientIndex |
|
4932 | 102 |
addClient (MRoomsAndClients (rooms, clients)) cl = do |
103 |
i <- addElem clients (Client lobbyId cl) |
|
4905 | 104 |
modifyElem rooms (roomAddClient (ClientIndex i)) (unRoomIndex lobbyId) |
105 |
return $ ClientIndex i |
|
106 |
||
107 |
removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO () |
|
4932 | 108 |
removeRoom rnc@(MRoomsAndClients (rooms, _)) rm@(RoomIndex ri) |
109 |
| rm == lobbyId = error "Cannot delete lobby" |
|
4905 | 110 |
| otherwise = do |
111 |
clIds <- liftM roomClients' $ readElem rooms ri |
|
112 |
forM_ clIds (moveClientToLobby rnc) |
|
113 |
removeElem rooms ri |
|
114 |
||
115 |
||
116 |
removeClient :: MRoomsAndClients r c -> ClientIndex -> IO () |
|
117 |
removeClient (MRoomsAndClients (rooms, clients)) cl@(ClientIndex ci) = do |
|
118 |
RoomIndex ri <- liftM clientRoom' $ readElem clients ci |
|
119 |
modifyElem rooms (roomRemoveClient cl) ri |
|
120 |
removeElem clients ci |
|
121 |
||
122 |
||
123 |
modifyRoom :: MRoomsAndClients r c -> (r -> r) -> RoomIndex -> IO () |
|
124 |
modifyRoom (MRoomsAndClients (rooms, _)) f (RoomIndex ri) = modifyElem rooms (\r -> r{room' = f $ room' r}) ri |
|
125 |
||
126 |
modifyClient :: MRoomsAndClients r c -> (c -> c) -> ClientIndex -> IO () |
|
127 |
modifyClient (MRoomsAndClients (_, clients)) f (ClientIndex ci) = modifyElem clients (\c -> c{client' = f $ client' c}) ci |
|
128 |
||
129 |
moveClientInRooms :: MRoomsAndClients r c -> RoomIndex -> RoomIndex -> ClientIndex -> IO () |
|
130 |
moveClientInRooms (MRoomsAndClients (rooms, clients)) (RoomIndex riFrom) rt@(RoomIndex riTo) cl@(ClientIndex ci) = do |
|
131 |
modifyElem rooms (roomRemoveClient cl) riFrom |
|
132 |
modifyElem rooms (roomAddClient cl) riTo |
|
133 |
modifyElem clients (\c -> c{clientRoom' = rt}) ci |
|
134 |
||
135 |
||
136 |
moveClientToLobby :: MRoomsAndClients r c -> ClientIndex -> IO () |
|
137 |
moveClientToLobby rnc ci = do |
|
4932 | 138 |
rm <- clientRoomM rnc ci |
139 |
moveClientInRooms rnc rm lobbyId ci |
|
4905 | 140 |
|
141 |
||
142 |
moveClientToRoom :: MRoomsAndClients r c -> RoomIndex -> ClientIndex -> IO () |
|
4932 | 143 |
moveClientToRoom rnc = moveClientInRooms rnc lobbyId |
4905 | 144 |
|
145 |
||
146 |
clientExists :: MRoomsAndClients r c -> ClientIndex -> IO Bool |
|
147 |
clientExists (MRoomsAndClients (_, clients)) (ClientIndex ci) = elemExists clients ci |
|
148 |
||
149 |
clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex |
|
150 |
clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci) |
|
151 |
||
152 |
client'sM :: MRoomsAndClients r c -> (c -> a) -> ClientIndex -> IO a |
|
153 |
client'sM (MRoomsAndClients (_, clients)) f (ClientIndex ci) = liftM (f . client') (clients `readElem` ci) |
|
154 |
||
155 |
room'sM :: MRoomsAndClients r c -> (r -> a) -> RoomIndex -> IO a |
|
156 |
room'sM (MRoomsAndClients (rooms, _)) f (RoomIndex ri) = liftM (f . room') (rooms `readElem` ri) |
|
157 |
||
158 |
allClientsM :: MRoomsAndClients r c -> IO [ClientIndex] |
|
159 |
allClientsM (MRoomsAndClients (_, clients)) = liftM (map ClientIndex) $ indicesM clients |
|
160 |
||
161 |
clientsM :: MRoomsAndClients r c -> IO [c] |
|
4932 | 162 |
clientsM (MRoomsAndClients (_, clients)) = indicesM clients >>= mapM (liftM client' . readElem clients) |
4905 | 163 |
|
8452 | 164 |
roomsM :: MRoomsAndClients r c -> IO [r] |
165 |
roomsM (MRoomsAndClients (rooms, _)) = indicesM rooms >>= mapM (liftM room' . readElem rooms) |
|
166 |
||
4905 | 167 |
roomClientsIndicesM :: MRoomsAndClients r c -> RoomIndex -> IO [ClientIndex] |
4932 | 168 |
roomClientsIndicesM (MRoomsAndClients (rooms, _)) (RoomIndex ri) = liftM roomClients' (rooms `readElem` ri) |
4905 | 169 |
|
170 |
roomClientsM :: MRoomsAndClients r c -> RoomIndex -> IO [c] |
|
171 |
roomClientsM (MRoomsAndClients (rooms, clients)) (RoomIndex ri) = liftM roomClients' (rooms `readElem` ri) >>= mapM (\(ClientIndex ci) -> liftM client' $ readElem clients ci) |
|
172 |
||
173 |
withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a |
|
174 |
withRoomsAndClients (MRoomsAndClients (rooms, clients)) f = |
|
175 |
withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c)) |
|
176 |
||
177 |
---------------------------------------- |
|
178 |
----------- IRoomsAndClients ----------- |
|
179 |
||
180 |
showRooms :: (Show r, Show c) => IRoomsAndClients r c -> String |
|
181 |
showRooms rnc@(IRoomsAndClients (rooms, clients)) = concatMap showRoom (allRooms rnc) |
|
182 |
where |
|
4932 | 183 |
showRoom r = unlines $ (show r ++ ": " ++ (show . room' $ rooms ! unRoomIndex r)) : map showClient (roomClients' $ rooms ! unRoomIndex r) |
184 |
showClient c = " " ++ show c ++ ": " ++ (show . client' $ clients ! unClientIndex c) |
|
4905 | 185 |
|
186 |
||
187 |
allRooms :: IRoomsAndClients r c -> [RoomIndex] |
|
188 |
allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms |
|
189 |
||
190 |
allClients :: IRoomsAndClients r c -> [ClientIndex] |
|
191 |
allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients |
|
192 |
||
193 |
clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex |
|
194 |
clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci) |
|
195 |
||
196 |
client :: IRoomsAndClients r c -> ClientIndex -> c |
|
197 |
client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci) |
|
198 |
||
199 |
room :: IRoomsAndClients r c -> RoomIndex -> r |
|
200 |
room (IRoomsAndClients (rooms, _)) (RoomIndex ri) = room' (rooms ! ri) |
|
201 |
||
202 |
roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex] |
|
4932 | 203 |
roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' (rooms ! ri) |