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