gameServer/RoomsAndClients.hs
author smaxx
Fri, 20 Aug 2010 17:41:59 +0200
changeset 3745 23f2344c0dbe
parent 3741 73246d25dfe1
child 3747 76a197793b62
permissions -rw-r--r--
Misc: * Trying to fix linebreak mess
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     1
module RoomsAndClients(
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     2
    RoomIndex(),
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     3
    ClientIndex(),
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     4
    MRoomsAndClients(),
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     5
    IRoomsAndClients(),
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     6
    newRoomsAndClients,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     7
    addRoom,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     8
    addClient,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
     9
    removeRoom,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    10
    removeClient,
3436
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
    11
    modifyRoom,
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
    12
    modifyClient,
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    13
    lobbyId,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    14
    moveClientToLobby,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    15
    moveClientToRoom,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    16
    clientRoom,
3436
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
    17
    clientRoomM,
3741
73246d25dfe1 Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents: 3656
diff changeset
    18
    clientExists,
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    19
    client,
3501
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
    20
    room,
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
    21
    client'sM,
3645
c0b3f1bb9316 Reimplement REMOVE_TEAM
unc0rr
parents: 3566
diff changeset
    22
    room'sM,
3654
18189fbc7530 Reimplement ping timeout
unc0rr
parents: 3645
diff changeset
    23
    allClientsM,
3458
11cd56019f00 Make some more protocol commands work
unc0rr
parents: 3436
diff changeset
    24
    clientsM,
3502
ad38c653b7d9 Some more progress
unc0rr
parents: 3501
diff changeset
    25
    roomClientsM,
3656
c74a4a407146 Make ROUNDFINISHED work correctly
unc0rr
parents: 3654
diff changeset
    26
    roomClientsIndicesM,
3501
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
    27
    withRoomsAndClients,
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
    28
    allRooms,
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    29
    allClients,
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3502
diff changeset
    30
    clientRoom,
3435
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
    31
    showRooms,
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
    32
    roomClients
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    33
    ) where
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    34
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    35
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    36
import Store
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    37
import Control.Monad
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    38
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    39
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    40
data Room r = Room {
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    41
    roomClients' :: [ClientIndex],
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    42
    room' :: r
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    43
    }
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    44
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    45
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    46
data Client c = Client {
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    47
    clientRoom' :: RoomIndex,
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    48
    client' :: c
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    49
    }
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    50
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    51
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    52
newtype RoomIndex = RoomIndex ElemIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    53
    deriving (Eq)
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    54
newtype ClientIndex = ClientIndex ElemIndex
3566
772a46ef8288 Properly handle client exit
unc0rr
parents: 3555
diff changeset
    55
    deriving (Eq, Show, Read, Ord)
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    56
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    57
instance Show RoomIndex where
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    58
    show (RoomIndex i) = 'r' : show i
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    59
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    60
unRoomIndex :: RoomIndex -> ElemIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    61
unRoomIndex (RoomIndex r) = r
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    62
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    63
unClientIndex :: ClientIndex -> ElemIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    64
unClientIndex (ClientIndex c) = c
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    65
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    66
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    67
newtype MRoomsAndClients r c = MRoomsAndClients (MStore (Room r), MStore (Client c))
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    68
newtype IRoomsAndClients r c = IRoomsAndClients (IStore (Room r), IStore (Client c))
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    69
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    70
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    71
lobbyId :: RoomIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    72
lobbyId = RoomIndex firstIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    73
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    74
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    75
newRoomsAndClients :: r -> IO (MRoomsAndClients r c)
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    76
newRoomsAndClients r = do
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    77
    rooms <- newStore
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    78
    clients <- newStore
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    79
    let rnc = MRoomsAndClients (rooms, clients)
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    80
    ri <- addRoom rnc r
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    81
    when (ri /= lobbyId) $ error "Empty struct inserts not at firstIndex index"
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    82
    return rnc
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    83
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    84
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    85
roomAddClient :: ClientIndex -> Room r -> Room r
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    86
roomAddClient cl room = room{roomClients' = cl : roomClients' room}
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    87
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    88
roomRemoveClient :: ClientIndex -> Room r -> Room r
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    89
roomRemoveClient cl room = room{roomClients' = filter (/= cl) $ roomClients' room}
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    90
3435
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
    91
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    92
addRoom :: MRoomsAndClients r c -> r -> IO RoomIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    93
addRoom (MRoomsAndClients (rooms, _)) room = do
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    94
    i <- addElem rooms (Room  [] room)
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    95
    return $ RoomIndex i
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    96
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    97
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    98
addClient :: MRoomsAndClients r c -> c -> IO ClientIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
    99
addClient (MRoomsAndClients (rooms, clients)) client = do
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   100
    i <- addElem clients (Client lobbyId client)
3501
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   101
    modifyElem rooms (roomAddClient (ClientIndex i)) (unRoomIndex lobbyId)
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   102
    return $ ClientIndex i
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   103
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   104
removeRoom :: MRoomsAndClients r c -> RoomIndex -> IO ()
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   105
removeRoom rnc@(MRoomsAndClients (rooms, _)) room@(RoomIndex ri) 
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   106
    | room == lobbyId = error "Cannot delete lobby"
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   107
    | otherwise = do
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   108
        clIds <- liftM roomClients' $ readElem rooms ri
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   109
        forM_ clIds (moveClientToLobby rnc)
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   110
        removeElem rooms ri
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   111
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   112
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   113
removeClient :: MRoomsAndClients r c -> ClientIndex -> IO ()
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   114
removeClient (MRoomsAndClients (rooms, clients)) cl@(ClientIndex ci) = do
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   115
    RoomIndex ri <- liftM clientRoom' $ readElem clients ci
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   116
    modifyElem rooms (roomRemoveClient cl) ri
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   117
    removeElem clients ci
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   118
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   119
3436
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
   120
modifyRoom :: MRoomsAndClients r c -> (r -> r) -> RoomIndex -> IO ()
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
   121
modifyRoom (MRoomsAndClients (rooms, _)) f (RoomIndex ri) = modifyElem rooms (\r -> r{room' = f $ room' r}) ri
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
   122
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
   123
modifyClient :: MRoomsAndClients r c -> (c -> c) -> ClientIndex -> IO ()
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
   124
modifyClient (MRoomsAndClients (_, clients)) f (ClientIndex ci) = modifyElem clients (\c -> c{client' = f $ client' c}) ci
288fcbdb77b6 Make server build again (it's still useless though)
unc0rr
parents: 3435
diff changeset
   125
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   126
moveClientInRooms :: MRoomsAndClients r c -> RoomIndex -> RoomIndex -> ClientIndex -> IO ()
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   127
moveClientInRooms (MRoomsAndClients (rooms, clients)) (RoomIndex riFrom) rt@(RoomIndex riTo) cl@(ClientIndex ci) = do
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   128
    modifyElem rooms (roomRemoveClient cl) riFrom
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   129
    modifyElem rooms (roomAddClient cl) riTo
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   130
    modifyElem clients (\c -> c{clientRoom' = rt}) ci
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   131
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   132
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   133
moveClientToLobby :: MRoomsAndClients r c -> ClientIndex -> IO ()
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   134
moveClientToLobby rnc ci = do
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   135
    room <- clientRoomM rnc ci
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   136
    moveClientInRooms rnc room lobbyId ci
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   137
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   138
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   139
moveClientToRoom :: MRoomsAndClients r c -> RoomIndex -> ClientIndex -> IO ()
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   140
moveClientToRoom rnc ri ci = moveClientInRooms rnc lobbyId ri ci
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   141
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   142
3741
73246d25dfe1 Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents: 3656
diff changeset
   143
clientExists :: MRoomsAndClients r c -> ClientIndex -> IO Bool
73246d25dfe1 Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents: 3656
diff changeset
   144
clientExists (MRoomsAndClients (_, clients)) (ClientIndex ci) = elemExists clients ci
73246d25dfe1 Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents: 3656
diff changeset
   145
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   146
clientRoomM :: MRoomsAndClients r c -> ClientIndex -> IO RoomIndex
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   147
clientRoomM (MRoomsAndClients (_, clients)) (ClientIndex ci) = liftM clientRoom' (clients `readElem` ci)
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   148
3501
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   149
client'sM :: MRoomsAndClients r c -> (c -> a) -> ClientIndex -> IO a
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   150
client'sM (MRoomsAndClients (_, clients)) f (ClientIndex ci) = liftM (f . client') (clients `readElem` ci)
3458
11cd56019f00 Make some more protocol commands work
unc0rr
parents: 3436
diff changeset
   151
3645
c0b3f1bb9316 Reimplement REMOVE_TEAM
unc0rr
parents: 3566
diff changeset
   152
room'sM :: MRoomsAndClients r c -> (r -> a) -> RoomIndex -> IO a
c0b3f1bb9316 Reimplement REMOVE_TEAM
unc0rr
parents: 3566
diff changeset
   153
room'sM (MRoomsAndClients (rooms, _)) f (RoomIndex ri) = liftM (f . room') (rooms `readElem` ri)
c0b3f1bb9316 Reimplement REMOVE_TEAM
unc0rr
parents: 3566
diff changeset
   154
3654
18189fbc7530 Reimplement ping timeout
unc0rr
parents: 3645
diff changeset
   155
allClientsM :: MRoomsAndClients r c -> IO [ClientIndex]
18189fbc7530 Reimplement ping timeout
unc0rr
parents: 3645
diff changeset
   156
allClientsM (MRoomsAndClients (_, clients)) = liftM (map ClientIndex) $ indicesM clients
18189fbc7530 Reimplement ping timeout
unc0rr
parents: 3645
diff changeset
   157
3501
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   158
clientsM :: MRoomsAndClients r c -> IO [c]
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   159
clientsM (MRoomsAndClients (_, clients)) = indicesM clients >>= mapM (\ci -> liftM client' $ readElem clients ci)
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   160
3656
c74a4a407146 Make ROUNDFINISHED work correctly
unc0rr
parents: 3654
diff changeset
   161
roomClientsIndicesM :: MRoomsAndClients r c -> RoomIndex -> IO [ClientIndex]
c74a4a407146 Make ROUNDFINISHED work correctly
unc0rr
parents: 3654
diff changeset
   162
roomClientsIndicesM (MRoomsAndClients (rooms, clients)) (RoomIndex ri) = liftM roomClients' (rooms `readElem` ri)
c74a4a407146 Make ROUNDFINISHED work correctly
unc0rr
parents: 3654
diff changeset
   163
3502
ad38c653b7d9 Some more progress
unc0rr
parents: 3501
diff changeset
   164
roomClientsM :: MRoomsAndClients r c -> RoomIndex -> IO [c]
ad38c653b7d9 Some more progress
unc0rr
parents: 3501
diff changeset
   165
roomClientsM (MRoomsAndClients (rooms, clients)) (RoomIndex ri) = liftM roomClients' (rooms `readElem` ri) >>= mapM (\(ClientIndex ci) -> liftM client' $ readElem clients ci)
ad38c653b7d9 Some more progress
unc0rr
parents: 3501
diff changeset
   166
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   167
withRoomsAndClients :: MRoomsAndClients r c -> (IRoomsAndClients r c -> a) -> IO a
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   168
withRoomsAndClients (MRoomsAndClients (rooms, clients)) f =
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   169
    withIStore2 rooms clients (\r c -> f $ IRoomsAndClients (r, c))
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   170
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   171
----------------------------------------
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   172
----------- IRoomsAndClients -----------
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   173
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   174
showRooms :: (Show r, Show c) => IRoomsAndClients r c -> String
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   175
showRooms rnc@(IRoomsAndClients (rooms, clients)) = concatMap showRoom (allRooms rnc)
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   176
    where
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   177
    showRoom r = unlines $ ((show r) ++ ": " ++ (show $ room' $ rooms ! (unRoomIndex r))) : (map showClient (roomClients' $ rooms ! (unRoomIndex r)))
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   178
    showClient c = "    " ++ (show c) ++ ": " ++ (show $ client' $ clients ! (unClientIndex c))
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   179
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   180
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   181
allRooms :: IRoomsAndClients r c -> [RoomIndex]
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   182
allRooms (IRoomsAndClients (rooms, _)) = map RoomIndex $ indices rooms
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   183
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   184
allClients :: IRoomsAndClients r c -> [ClientIndex]
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   185
allClients (IRoomsAndClients (_, clients)) = map ClientIndex $ indices clients
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   186
3435
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
   187
clientRoom :: IRoomsAndClients r c -> ClientIndex -> RoomIndex
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
   188
clientRoom (IRoomsAndClients (_, clients)) (ClientIndex ci) = clientRoom' (clients ! ci)
3425
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   189
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   190
client :: IRoomsAndClients r c -> ClientIndex -> c
ead2ed20dfd4 Start the server refactoring
unc0rr
parents:
diff changeset
   191
client (IRoomsAndClients (_, clients)) (ClientIndex ci) = client' (clients ! ci)
3435
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
   192
3501
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   193
room :: IRoomsAndClients r c -> RoomIndex -> r
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   194
room (IRoomsAndClients (rooms, _)) (RoomIndex ri) = room' (rooms ! ri)
a3159a410e5c Reimplement more core actions
unc0rr
parents: 3458
diff changeset
   195
3435
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
   196
roomClients :: IRoomsAndClients r c -> RoomIndex -> [ClientIndex]
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 3425
diff changeset
   197
roomClients (IRoomsAndClients (rooms, _)) (RoomIndex ri) = roomClients' $ (rooms ! ri)