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