gameServer/Actions.hs
branchserver_refactor
changeset 4601 08ae94dd4c0d
parent 4599 a9e4093a7e78
child 4604 831a4b91e9bc
equal deleted inserted replaced
4599:a9e4093a7e78 4601:08ae94dd4c0d
    66 
    66 
    67 processAction :: Action -> StateT ServerState IO ()
    67 processAction :: Action -> StateT ServerState IO ()
    68 
    68 
    69 
    69 
    70 processAction (AnswerClients chans msg) = do
    70 processAction (AnswerClients chans msg) = do
    71     liftIO $ mapM_ (flip writeChan msg) chans
    71     io $ mapM_ (flip writeChan msg) chans
    72 
    72 
    73 
    73 
    74 processAction SendServerMessage = do
    74 processAction SendServerMessage = do
    75     chan <- client's sendChan
    75     chan <- client's sendChan
    76     protonum <- client's clientProto
    76     protonum <- client's clientProto
   114 
   114 
   115     when (ri /= lobbyId) $ do
   115     when (ri /= lobbyId) $ do
   116         processAction $ MoveToLobby ("quit: " `B.append` msg)
   116         processAction $ MoveToLobby ("quit: " `B.append` msg)
   117         return ()
   117         return ()
   118 
   118 
   119     liftIO $ do
   119     io $ do
   120         infoM "Clients" (show ci ++ " quits: " ++ (B.unpack msg))
   120         infoM "Clients" (show ci ++ " quits: " ++ (B.unpack msg))
   121 
   121 
   122         --mapM_ (processAction (ci, serverInfo, rnc)) $ answerOthersQuit ++ answerInformRoom
   122         --mapM_ (processAction (ci, serverInfo, rnc)) $ answerOthersQuit ++ answerInformRoom
   123 
   123 
   124     processAction $ AnswerClients [chan] ["BYE", msg]
   124     processAction $ AnswerClients [chan] ["BYE", msg]
   126     s <- get
   126     s <- get
   127     put $! s{removedClients = ci `Set.insert` removedClients s}
   127     put $! s{removedClients = ci `Set.insert` removedClients s}
   128 
   128 
   129 processAction (DeleteClient ci) = do
   129 processAction (DeleteClient ci) = do
   130     rnc <- gets roomsClients
   130     rnc <- gets roomsClients
   131     liftIO $ removeClient rnc ci
   131     io $ removeClient rnc ci
   132 
   132 
   133     s <- get
   133     s <- get
   134     put $! s{removedClients = ci `Set.delete` removedClients s}
   134     put $! s{removedClients = ci `Set.delete` removedClients s}
   135 
   135 
   136 {-
   136 {-
   156 -}
   156 -}
   157 
   157 
   158 processAction (ModifyClient f) = do
   158 processAction (ModifyClient f) = do
   159     (Just ci) <- gets clientIndex
   159     (Just ci) <- gets clientIndex
   160     rnc <- gets roomsClients
   160     rnc <- gets roomsClients
   161     liftIO $ modifyClient rnc f ci
   161     io $ modifyClient rnc f ci
   162     return ()
   162     return ()
   163 
   163 
   164 processAction (ModifyClient2 ci f) = do
   164 processAction (ModifyClient2 ci f) = do
   165     rnc <- gets roomsClients
   165     rnc <- gets roomsClients
   166     liftIO $ modifyClient rnc f ci
   166     io $ modifyClient rnc f ci
   167     return ()
   167     return ()
   168 
   168 
   169 
   169 
   170 processAction (ModifyRoom f) = do
   170 processAction (ModifyRoom f) = do
   171     rnc <- gets roomsClients
   171     rnc <- gets roomsClients
   172     ri <- clientRoomA
   172     ri <- clientRoomA
   173     liftIO $ modifyRoom rnc f ri
   173     io $ modifyRoom rnc f ri
   174     return ()
   174     return ()
   175 
   175 
   176 {-
   176 {-
   177 
   177 
   178 processAction (clID, serverInfo, rnc) (ModifyServerInfo func) =
   178 processAction (clID, serverInfo, rnc) (ModifyServerInfo func) =
   182 
   182 
   183 processAction (MoveToRoom ri) = do
   183 processAction (MoveToRoom ri) = do
   184     (Just ci) <- gets clientIndex
   184     (Just ci) <- gets clientIndex
   185     rnc <- gets roomsClients
   185     rnc <- gets roomsClients
   186 
   186 
   187     liftIO $ do
   187     io $ do
   188         modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False}) ci
   188         modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False}) ci
   189         modifyRoom rnc (\r -> r{playersIn = (playersIn r) + 1}) ri
   189         modifyRoom rnc (\r -> r{playersIn = (playersIn r) + 1}) ri
   190         moveClientToRoom rnc ri ci
   190         moveClientToRoom rnc ri ci
   191 
   191 
   192     chans <- liftM (map sendChan) $ roomClientsS ri
   192     chans <- liftM (map sendChan) $ roomClientsS ri
   211         clNick <- client's nick
   211         clNick <- client's nick
   212         clChan <- client's sendChan
   212         clChan <- client's sendChan
   213         chans <- othersChans
   213         chans <- othersChans
   214         mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci]
   214         mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci]
   215 
   215 
   216     liftIO $ do
   216     io $ do
   217             modifyRoom rnc (\r -> r{
   217             modifyRoom rnc (\r -> r{
   218                     playersIn = (playersIn r) - 1,
   218                     playersIn = (playersIn r) - 1,
   219                     readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
   219                     readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
   220                     }) ri
   220                     }) ri
   221             moveClientToLobby rnc ci
   221             moveClientToLobby rnc ci
   270 -}
   270 -}
   271 
   271 
   272 processAction (AddRoom roomName roomPassword) = do
   272 processAction (AddRoom roomName roomPassword) = do
   273     Just clId <- gets clientIndex
   273     Just clId <- gets clientIndex
   274     rnc <- gets roomsClients
   274     rnc <- gets roomsClients
   275     proto <- liftIO $ client'sM rnc clientProto clId
   275     proto <- io $ client'sM rnc clientProto clId
   276 
   276 
   277     let room = newRoom{
   277     let room = newRoom{
   278             masterID = clId,
   278             masterID = clId,
   279             name = roomName,
   279             name = roomName,
   280             password = roomPassword,
   280             password = roomPassword,
   281             roomProto = proto
   281             roomProto = proto
   282             }
   282             }
   283 
   283 
   284     rId <- liftIO $ addRoom rnc room
   284     rId <- io $ addRoom rnc room
   285 
   285 
   286     processAction $ MoveToRoom rId
   286     processAction $ MoveToRoom rId
   287 
   287 
   288     chans <- liftM (map sendChan) $! roomClientsS lobbyId
   288     chans <- liftM (map sendChan) $! roomClientsS lobbyId
   289 
   289 
   294 
   294 
   295 
   295 
   296 processAction RemoveRoom = do
   296 processAction RemoveRoom = do
   297     Just clId <- gets clientIndex
   297     Just clId <- gets clientIndex
   298     rnc <- gets roomsClients
   298     rnc <- gets roomsClients
   299     ri <- liftIO $ clientRoomM rnc clId
   299     ri <- io $ clientRoomM rnc clId
   300     roomName <- liftIO $ room'sM rnc name ri
   300     roomName <- io $ room'sM rnc name ri
   301     others <- othersChans
   301     others <- othersChans
   302     lobbyChans <- liftM (map sendChan) $! roomClientsS lobbyId
   302     lobbyChans <- liftM (map sendChan) $! roomClientsS lobbyId
   303 
   303 
   304     mapM_ processAction [
   304     mapM_ processAction [
   305             AnswerClients lobbyChans ["ROOM", "DEL", roomName],
   305             AnswerClients lobbyChans ["ROOM", "DEL", roomName],
   306             AnswerClients others ["ROOMABANDONED", roomName]
   306             AnswerClients others ["ROOMABANDONED", roomName]
   307         ]
   307         ]
   308 
   308 
   309     liftIO $ removeRoom rnc ri
   309     io $ removeRoom rnc ri
   310 
   310 
   311 
   311 
   312 processAction (UnreadyRoomClients) = do
   312 processAction (UnreadyRoomClients) = do
   313     rnc <- gets roomsClients
   313     rnc <- gets roomsClients
   314     ri <- clientRoomA
   314     ri <- clientRoomA
   315     roomPlayers <- roomClientsS ri
   315     roomPlayers <- roomClientsS ri
   316     roomClIDs <- liftIO $ roomClientsIndicesM rnc ri
   316     roomClIDs <- io $ roomClientsIndicesM rnc ri
   317     processAction $ AnswerClients (map sendChan roomPlayers) ("NOT_READY" : map nick roomPlayers)
   317     processAction $ AnswerClients (map sendChan roomPlayers) ("NOT_READY" : map nick roomPlayers)
   318     liftIO $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs
   318     io $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs
   319     processAction $ ModifyRoom (\r -> r{readyPlayers = 0})
   319     processAction $ ModifyRoom (\r -> r{readyPlayers = 0})
   320 
   320 
   321 
   321 
   322 processAction (RemoveTeam teamName) = do
   322 processAction (RemoveTeam teamName) = do
   323     rnc <- gets roomsClients
   323     rnc <- gets roomsClients
   324     cl <- client's id
   324     cl <- client's id
   325     ri <- clientRoomA
   325     ri <- clientRoomA
   326     inGame <- liftIO $ room'sM rnc gameinprogress ri
   326     inGame <- io $ room'sM rnc gameinprogress ri
   327     chans <- othersChans
   327     chans <- othersChans
   328     if inGame then
   328     if inGame then
   329             mapM_ processAction [
   329             mapM_ processAction [
   330                 AnswerClients chans ["REMOVE_TEAM", teamName],
   330                 AnswerClients chans ["REMOVE_TEAM", teamName],
   331                 ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r})
   331                 ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r})
   344 
   344 
   345 
   345 
   346 processAction (RemoveClientTeams clId) = do
   346 processAction (RemoveClientTeams clId) = do
   347     rnc <- gets roomsClients
   347     rnc <- gets roomsClients
   348 
   348 
   349     removeTeamActions <- liftIO $ do
   349     removeTeamActions <- io $ do
   350         clNick <- client'sM rnc nick clId
   350         clNick <- client'sM rnc nick clId
   351         rId <- clientRoomM rnc clId
   351         rId <- clientRoomM rnc clId
   352         roomTeams <- room'sM rnc teams rId
   352         roomTeams <- room'sM rnc teams rId
   353         return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams
   353         return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams
   354 
   354 
   359 processAction CheckRegistered = do
   359 processAction CheckRegistered = do
   360     (Just ci) <- gets clientIndex
   360     (Just ci) <- gets clientIndex
   361     n <- client's nick
   361     n <- client's nick
   362     h <- client's host
   362     h <- client's host
   363     db <- gets (dbQueries . serverInfo)
   363     db <- gets (dbQueries . serverInfo)
   364     liftIO $ writeChan db $ CheckAccount ci n h
   364     io $ writeChan db $ CheckAccount ci n h
   365     return ()
   365     return ()
   366 
   366 
   367 
   367 
   368 processAction ClearAccountsCache = do
   368 processAction ClearAccountsCache = do
   369     dbq <- gets (dbQueries . serverInfo)
   369     dbq <- gets (dbQueries . serverInfo)
   370     liftIO $ writeChan dbq ClearCache
   370     io $ writeChan dbq ClearCache
   371     return ()
   371     return ()
   372 
   372 
   373 
   373 
   374 processAction (ProcessAccountInfo info) =
   374 processAction (ProcessAccountInfo info) =
   375     case info of
   375     case info of
   424 -}
   424 -}
   425 
   425 
   426 processAction (AddClient client) = do
   426 processAction (AddClient client) = do
   427     rnc <- gets roomsClients
   427     rnc <- gets roomsClients
   428     si <- gets serverInfo
   428     si <- gets serverInfo
   429     liftIO $ do
   429     io $ do
   430         ci <- addClient rnc client
   430         ci <- addClient rnc client
   431         t <- forkIO $ clientRecvLoop (clientSocket client) (coreChan si) ci
   431         t <- forkIO $ clientRecvLoop (clientSocket client) (coreChan si) ci
   432         forkIO $ clientSendLoop (clientSocket client) t (coreChan si) (sendChan client) ci
   432         forkIO $ clientSendLoop (clientSocket client) t (coreChan si) (sendChan client) ci
   433 
   433 
   434         infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime client))
   434         infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime client))
   444 
   444 
   445 
   445 
   446 
   446 
   447 processAction PingAll = do
   447 processAction PingAll = do
   448     rnc <- gets roomsClients
   448     rnc <- gets roomsClients
   449     liftIO (allClientsM rnc) >>= mapM_ (kickTimeouted rnc)
   449     io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc)
   450     cis <- liftIO $ allClientsM rnc
   450     cis <- io $ allClientsM rnc
   451     chans <- liftIO $ mapM (client'sM rnc sendChan) cis
   451     chans <- io $ mapM (client'sM rnc sendChan) cis
   452     liftIO $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis
   452     io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis
   453     processAction $ AnswerClients chans ["PING"]
   453     processAction $ AnswerClients chans ["PING"]
   454     where
   454     where
   455         kickTimeouted rnc ci = do
   455         kickTimeouted rnc ci = do
   456             pq <- liftIO $ client'sM rnc pingsQueue ci
   456             pq <- io $ client'sM rnc pingsQueue ci
   457             when (pq > 0) $
   457             when (pq > 0) $
   458                 withStateT (\as -> as{clientIndex = Just ci}) $
   458                 withStateT (\as -> as{clientIndex = Just ci}) $
   459                     processAction (ByeClient "Ping timeout")
   459                     processAction (ByeClient "Ping timeout")
   460 
   460 
   461 
   461 
   462 processAction (StatsAction) = do
   462 processAction (StatsAction) = do
   463     rnc <- gets roomsClients
   463     rnc <- gets roomsClients
   464     si <- gets serverInfo
   464     si <- gets serverInfo
   465     (roomsNum, clientsNum) <- liftIO $ withRoomsAndClients rnc stats
   465     (roomsNum, clientsNum) <- io $ withRoomsAndClients rnc stats
   466     liftIO $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1)
   466     io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1)
   467     where
   467     where
   468           stats irnc = (length $ allRooms irnc, length $ allClients irnc)
   468           stats irnc = (length $ allRooms irnc, length $ allClients irnc)
   469 
   469