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