3425
|
1 |
module RoomsAndClients(
|
|
2 |
RoomIndex(),
|
|
3 |
ClientIndex(),
|
|
4 |
MRoomsAndClients(),
|
|
5 |
IRoomsAndClients(),
|
|
6 |
newRoomsAndClients,
|
|
7 |
addRoom,
|
|
8 |
addClient,
|
|
9 |
removeRoom,
|
|
10 |
removeClient,
|
|
11 |
lobbyId,
|
|
12 |
moveClientToLobby,
|
|
13 |
moveClientToRoom,
|
|
14 |
clientRoom,
|
|
15 |
client,
|
|
16 |
allClients,
|
|
17 |
withRoomsAndClients,
|
3435
|
18 |
showRooms,
|
|
19 |
roomClients
|
3425
|
20 |
) where
|
|
21 |
|
|
22 |
|
|
23 |
import Store
|
|
24 |
import Control.Monad
|
|
25 |
|
|
26 |
|
|
27 |
data Room r = Room {
|
|
28 |
roomClients' :: [ClientIndex],
|
|
29 |
room' :: r
|
|
30 |
}
|
|
31 |
|
|
32 |
|
|
33 |
data Client c = Client {
|
|
34 |
clientRoom' :: RoomIndex,
|
|
35 |
client' :: c
|
|
36 |
}
|
|
37 |
|
|
38 |
|
|
39 |
newtype RoomIndex = RoomIndex ElemIndex
|
|
40 |
deriving (Eq)
|
|
41 |
newtype ClientIndex = ClientIndex ElemIndex
|
3435
|
42 |
deriving (Eq, Show, Read)
|
3425
|
43 |
|
|
44 |
instance Show RoomIndex where
|
|
45 |
show (RoomIndex i) = 'r' : show i
|
|
46 |
|
|
47 |
unRoomIndex :: RoomIndex -> ElemIndex
|
|
48 |
unRoomIndex (RoomIndex r) = r
|
|
49 |
|
|
50 |
unClientIndex :: ClientIndex -> ElemIndex
|
|
51 |
unClientIndex (ClientIndex c) = c
|
|
52 |
|
|
53 |
|
|
54 |
newtype MRoomsAndClients r c = MRoomsAndClients (MStore (Room r), MStore (Client c))
|
|
55 |
newtype IRoomsAndClients r c = IRoomsAndClients (IStore (Room r), IStore (Client c))
|
|
56 |
|
|
57 |
|
|
58 |
lobbyId :: RoomIndex
|
|
59 |
lobbyId = RoomIndex firstIndex
|
|
60 |
|
|
61 |
|
|
62 |
newRoomsAndClients :: r -> IO (MRoomsAndClients r c)
|
|
63 |
newRoomsAndClients r = do
|
|
64 |
rooms <- newStore
|
|
65 |
clients <- newStore
|
|
66 |
let rnc = MRoomsAndClients (rooms, clients)
|
|
67 |
ri <- addRoom rnc r
|
|
68 |
when (ri /= lobbyId) $ error "Empty struct inserts not at firstIndex index"
|
|
69 |
return rnc
|
|
70 |
|
|
71 |
|
|
72 |
roomAddClient :: ClientIndex -> Room r -> Room r
|
|
73 |
roomAddClient cl room = room{roomClients' = cl : roomClients' room}
|
|
74 |
|
|
75 |
roomRemoveClient :: ClientIndex -> Room r -> Room r
|
|
76 |
roomRemoveClient cl room = room{roomClients' = filter (/= cl) $ roomClients' room}
|
|
77 |
|
3435
|
78 |
|
3425
|
79 |
addRoom :: MRoomsAndClients r c -> r -> IO RoomIndex
|
|
80 |
addRoom (MRoomsAndClients (rooms, _)) room = do
|
|
81 |
i <- addElem rooms (Room [] room)
|
|
82 |
return $ RoomIndex i
|
|
83 |
|
|
84 |
|
|
85 |
addClient :: MRoomsAndClients r c -> c -> IO ClientIndex
|
|
86 |
addClient (MRoomsAndClients (rooms, clients)) client = do
|
|
87 |
i <- addElem clients (Client lobbyId client)
|
|
88 |
modifyElem rooms (roomAddClient (ClientIndex i)) rid
|
|
89 |
return $ ClientIndex i
|
|
90 |
where
|
|
91 |
rid = (\(RoomIndex i) -> i) lobbyId
|
|
92 |
|
|
93 |
removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO ()
|
|
94 |
removeRoom rnc@(MRoomsAndClients (rooms, _)) room@(RoomIndex ri)
|
|
95 |
| room == lobbyId = error "Cannot delete lobby"
|
|
96 |
| otherwise = do
|
|
97 |
clIds <- liftM roomClients' $ readElem rooms ri
|
|
98 |
forM_ clIds (moveClientToLobby rnc)
|
|
99 |
removeElem rooms ri
|
|
100 |
|
|
101 |
|
|
102 |
removeClient :: MRoomsAndClients r c -> ClientIndex -> IO ()
|
|
103 |
removeClient (MRoomsAndClients (rooms, clients)) cl@(ClientIndex ci) = do
|
|
104 |
RoomIndex ri <- liftM clientRoom' $ readElem clients ci
|
|
105 |
modifyElem rooms (roomRemoveClient cl) ri
|
|
106 |
removeElem clients ci
|
|
107 |
|
|
108 |
|
|
109 |
moveClientInRooms :: MRoomsAndClients r c -> RoomIndex -> RoomIndex -> ClientIndex -> IO ()
|
|
110 |
moveClientInRooms (MRoomsAndClients (rooms, clients)) (RoomIndex riFrom) rt@(RoomIndex riTo) cl@(ClientIndex ci) = do
|
|
111 |
modifyElem rooms (roomRemoveClient cl) riFrom
|
|
112 |
modifyElem rooms (roomAddClient cl) riTo
|
|
113 |
modifyElem clients (\c -> c{clientRoom' = rt}) ci
|
|
114 |
|
|
115 |
|
|
116 |
moveClientToLobby :: MRoomsAndClients r c -> ClientIndex -> IO ()
|
|
117 |
moveClientToLobby rnc ci = do
|
|
118 |
room <- clientRoomM rnc ci
|
|
119 |
moveClientInRooms rnc room lobbyId ci
|
|
120 |
|
|
121 |
|
|
122 |
moveClientToRoom :: MRoomsAndClients r c -> RoomIndex -> ClientIndex -> IO ()
|
|
123 |
moveClientToRoom rnc ri ci = moveClientInRooms rnc lobbyId ri ci
|
|
124 |
|
|
125 |
|
|
126 |
clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex
|
|
127 |
clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci)
|
|
128 |
|
|
129 |
|
|
130 |
withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a
|
|
131 |
withRoomsAndClients (MRoomsAndClients (rooms, clients)) f =
|
|
132 |
withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c))
|
|
133 |
|
|
134 |
----------------------------------------
|
|
135 |
----------- IRoomsAndClients -----------
|
|
136 |
|
|
137 |
showRooms :: (Show r, Show c) => IRoomsAndClients r c -> String
|
|
138 |
showRooms rnc@(IRoomsAndClients (rooms, clients)) = concatMap showRoom (allRooms rnc)
|
|
139 |
where
|
|
140 |
showRoom r = unlines $ ((show r) ++ ": " ++ (show $ room' $ rooms ! (unRoomIndex r))) : (map showClient (roomClients' $ rooms ! (unRoomIndex r)))
|
|
141 |
showClient c = " " ++ (show c) ++ ": " ++ (show $ client' $ clients ! (unClientIndex c))
|
|
142 |
|
|
143 |
|
|
144 |
allRooms :: IRoomsAndClients r c -> [RoomIndex]
|
|
145 |
allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms
|
|
146 |
|
|
147 |
allClients :: IRoomsAndClients r c -> [ClientIndex]
|
|
148 |
allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients
|
|
149 |
|
|
150 |
|
3435
|
151 |
clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex
|
|
152 |
clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci)
|
3425
|
153 |
|
|
154 |
client :: IRoomsAndClients r c -> ClientIndex -> c
|
|
155 |
client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci)
|
3435
|
156 |
|
|
157 |
roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex]
|
|
158 |
roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' $ (rooms ! ri)
|