gameServer/RoomsAndClients.hs
changeset 3501 a3159a410e5c
parent 3458 11cd56019f00
child 3502 ad38c653b7d9
equal deleted inserted replaced
3500:af8390d807d6 3501:a3159a410e5c
    14     moveClientToLobby,
    14     moveClientToLobby,
    15     moveClientToRoom,
    15     moveClientToRoom,
    16     clientRoom,
    16     clientRoom,
    17     clientRoomM,
    17     clientRoomM,
    18     client,
    18     client,
       
    19     room,
       
    20     client'sM,
    19     clientsM,
    21     clientsM,
       
    22     withRoomsAndClients,
       
    23     allRooms,
    20     allClients,
    24     allClients,
    21     withRoomsAndClients,
       
    22     showRooms,
    25     showRooms,
    23     roomClients
    26     roomClients
    24     ) where
    27     ) where
    25 
    28 
    26 
    29 
    87 
    90 
    88 
    91 
    89 addClient :: MRoomsAndClients r c -> c -> IO ClientIndex
    92 addClient :: MRoomsAndClients r c -> c -> IO ClientIndex
    90 addClient (MRoomsAndClients (rooms, clients)) client = do
    93 addClient (MRoomsAndClients (rooms, clients)) client = do
    91     i <- addElem clients (Client lobbyId client)
    94     i <- addElem clients (Client lobbyId client)
    92     modifyElem rooms (roomAddClient (ClientIndex i)) rid
    95     modifyElem rooms (roomAddClient (ClientIndex i)) (unRoomIndex lobbyId)
    93     return $ ClientIndex i
    96     return $ ClientIndex i
    94     where
       
    95         rid = (\(RoomIndex i) -> i) lobbyId
       
    96 
    97 
    97 removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO ()
    98 removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO ()
    98 removeRoom rnc@(MRoomsAndClients (rooms, _)) room@(RoomIndex ri) 
    99 removeRoom rnc@(MRoomsAndClients (rooms, _)) room@(RoomIndex ri) 
    99     | room == lobbyId = error "Cannot delete lobby"
   100     | room == lobbyId = error "Cannot delete lobby"
   100     | otherwise = do
   101     | otherwise = do
   134 
   135 
   135 
   136 
   136 clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex
   137 clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex
   137 clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci)
   138 clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci)
   138 
   139 
   139 clientsM :: MRoomsAndClients r c -> (c -> a) -> ClientIndex -> IO a
   140 client'sM :: MRoomsAndClients r c -> (c -> a) -> ClientIndex -> IO a
   140 clientsM (MRoomsAndClients (_, clients)) f (ClientIndex ci) = liftM (f . client') (clients `readElem` ci)
   141 client'sM (MRoomsAndClients (_, clients)) f (ClientIndex ci) = liftM (f . client') (clients `readElem` ci)
   141 
   142 
       
   143 clientsM :: MRoomsAndClients r c -> IO [c]
       
   144 clientsM (MRoomsAndClients (_, clients)) = indicesM clients >>= mapM (\ci -> liftM client' $ readElem clients ci)
   142 
   145 
   143 withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a
   146 withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a
   144 withRoomsAndClients (MRoomsAndClients (rooms, clients)) f =
   147 withRoomsAndClients (MRoomsAndClients (rooms, clients)) f =
   145     withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c))
   148     withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c))
   146 
   149 
   158 allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms
   161 allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms
   159 
   162 
   160 allClients :: IRoomsAndClients r c -> [ClientIndex]
   163 allClients :: IRoomsAndClients r c -> [ClientIndex]
   161 allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients
   164 allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients
   162 
   165 
   163 
       
   164 clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex
   166 clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex
   165 clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci)
   167 clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci)
   166 
   168 
   167 client :: IRoomsAndClients r c -> ClientIndex -> c
   169 client :: IRoomsAndClients r c -> ClientIndex -> c
   168 client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci)
   170 client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci)
   169 
   171 
       
   172 room :: IRoomsAndClients r c -> RoomIndex -> r
       
   173 room (IRoomsAndClients (rooms, _)) (RoomIndex ri) = room' (rooms ! ri)
       
   174 
   170 roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex]
   175 roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex]
   171 roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' $ (rooms ! ri)
   176 roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' $ (rooms ! ri)