gameServer/Actions.hs
changeset 9753 9579596cf471
parent 9702 27006953d901
child 9786 e33ee5ef5d9d
equal deleted inserted replaced
9752:656c511ab0f3 9753:9579596cf471
    19 import Control.Arrow
    19 import Control.Arrow
    20 import Control.Exception as E
    20 import Control.Exception as E
    21 import System.Process
    21 import System.Process
    22 import Network.Socket
    22 import Network.Socket
    23 import System.Random
    23 import System.Random
       
    24 import qualified Data.Traversable as DT
    24 -----------------------------
    25 -----------------------------
    25 #if defined(OFFICIAL_SERVER)
    26 #if defined(OFFICIAL_SERVER)
    26 import OfficialServer.GameReplayStore
    27 import OfficialServer.GameReplayStore
    27 #endif
    28 #endif
    28 import CoreTypes
    29 import CoreTypes
   185 processAction (MoveToLobby msg) = do
   186 processAction (MoveToLobby msg) = do
   186     (Just ci) <- gets clientIndex
   187     (Just ci) <- gets clientIndex
   187     ri <- clientRoomA
   188     ri <- clientRoomA
   188     rnc <- gets roomsClients
   189     rnc <- gets roomsClients
   189     playersNum <- io $ room'sM rnc playersIn ri
   190     playersNum <- io $ room'sM rnc playersIn ri
       
   191     specialRoom <- io $ room'sM rnc isSpecial ri
   190     master <- client's isMaster
   192     master <- client's isMaster
   191 --    client <- client's id
   193 --    client <- client's id
   192     clNick <- client's nick
   194     clNick <- client's nick
   193     chans <- othersChans
   195     chans <- othersChans
   194 
   196 
   195     if master then
   197     if master then
   196         if playersNum > 1 then
   198         if (playersNum > 1) || specialRoom then
   197             mapM_ processAction [ChangeMaster Nothing, NoticeMessage AdminLeft, RemoveClientTeams, AnswerClients chans ["LEFT", clNick, msg]]
   199             mapM_ processAction [ChangeMaster Nothing, NoticeMessage AdminLeft, RemoveClientTeams, AnswerClients chans ["LEFT", clNick, msg]]
   198             else
   200             else
   199             processAction RemoveRoom
   201             processAction RemoveRoom
   200         else
   202         else
   201         mapM_ processAction [RemoveClientTeams, AnswerClients chans ["LEFT", clNick, msg]]
   203         mapM_ processAction [RemoveClientTeams, AnswerClients chans ["LEFT", clNick, msg]]
   203     allClientsChans <- liftM (Prelude.map sendChan . Prelude.filter isVisible) $! allClientsS
   205     allClientsChans <- liftM (Prelude.map sendChan . Prelude.filter isVisible) $! allClientsS
   204     processAction $ AnswerClients allClientsChans ["CLIENT_FLAGS", "-i", clNick]
   206     processAction $ AnswerClients allClientsChans ["CLIENT_FLAGS", "-i", clNick]
   205 
   207 
   206     -- when not removing room
   208     -- when not removing room
   207     ready <- client's isReady
   209     ready <- client's isReady
   208     when (not master || playersNum > 1) . io $ do
   210     when (not master || playersNum > 1 || specialRoom) . io $ do
   209         modifyRoom rnc (\r -> r{
   211         modifyRoom rnc (\r -> r{
   210                 playersIn = playersIn r - 1,
   212                 playersIn = playersIn r - 1,
   211                 readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
   213                 readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
   212                 }) ri
   214                 }) ri
   213         moveClientToLobby rnc ci
   215         moveClientToLobby rnc ci
   216 processAction (ChangeMaster delegateId)= do
   218 processAction (ChangeMaster delegateId)= do
   217     (Just ci) <- gets clientIndex
   219     (Just ci) <- gets clientIndex
   218     proto <- client's clientProto
   220     proto <- client's clientProto
   219     ri <- clientRoomA
   221     ri <- clientRoomA
   220     rnc <- gets roomsClients
   222     rnc <- gets roomsClients
   221     newMasterId <- liftM (\ids -> fromMaybe (last . filter (/= ci) $ ids) delegateId) . io $ roomClientsIndicesM rnc ri
   223     specialRoom <- io $ room'sM rnc isSpecial ri
   222     newMaster <- io $ client'sM rnc id newMasterId
   224     newMasterId <- liftM (\ids -> fromMaybe (listToMaybe . reverse . filter (/= ci) $ ids) $ liftM Just delegateId) . io $ roomClientsIndicesM rnc ri
       
   225     newMaster <- io $ client'sM rnc id `DT.mapM` newMasterId
   223     oldMasterId <- io $ room'sM rnc masterID ri
   226     oldMasterId <- io $ room'sM rnc masterID ri
   224     oldMaster <- io $ client'sM rnc id oldMasterId
       
   225     oldRoomName <- io $ room'sM rnc name ri
   227     oldRoomName <- io $ room'sM rnc name ri
   226     kicked <- client's isKickedFromServer
   228     kicked <- client's isKickedFromServer
   227     thisRoomChans <- liftM (map sendChan) $ roomClientsS ri
   229     thisRoomChans <- liftM (map sendChan) $ roomClientsS ri
   228     let newRoomName = if (proto < 42) || kicked then nick newMaster else oldRoomName
   230     let newRoomName = if ((proto < 42) || kicked) && (not specialRoom) then maybeNick newMaster else oldRoomName
   229     mapM_ processAction [
   231 
       
   232     when (isJust oldMasterId) $ do
       
   233         oldMasterNick <- io $ client'sM rnc nick (fromJust oldMasterId)
       
   234         mapM_ processAction [
       
   235             ModifyClient2 (fromJust oldMasterId) (\c -> c{isMaster = False})
       
   236             , AnswerClients thisRoomChans ["CLIENT_FLAGS", "-h", oldMasterNick]
       
   237             ]
       
   238 
       
   239     when (isJust newMasterId) $
       
   240         mapM_ processAction [
       
   241           ModifyClient2 (fromJust newMasterId) (\c -> c{isMaster = True})
       
   242         , AnswerClients [sendChan $ fromJust newMaster] ["ROOM_CONTROL_ACCESS", "1"]
       
   243         , AnswerClients thisRoomChans ["CLIENT_FLAGS", "+h", nick $ fromJust newMaster]
       
   244         ]
       
   245 
       
   246     processAction $
   230         ModifyRoom (\r -> r{masterID = newMasterId
   247         ModifyRoom (\r -> r{masterID = newMasterId
   231                 , name = newRoomName
   248                 , name = newRoomName
   232                 , isRestrictedJoins = False
   249                 , isRestrictedJoins = False
   233                 , isRestrictedTeams = False
   250                 , isRestrictedTeams = False
   234                 , isRegisteredOnly = False}
   251                 , isRegisteredOnly = isSpecial r}
   235                 )
   252                 )
   236         , ModifyClient2 newMasterId (\c -> c{isMaster = True})
       
   237         , ModifyClient2 oldMasterId (\c -> c{isMaster = False})
       
   238         , AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"]
       
   239         , AnswerClients thisRoomChans ["CLIENT_FLAGS", "-h", nick oldMaster]
       
   240         , AnswerClients thisRoomChans ["CLIENT_FLAGS", "+h", nick newMaster]
       
   241         ]
       
   242 
   253 
   243     newRoom' <- io $ room'sM rnc id ri
   254     newRoom' <- io $ room'sM rnc id ri
   244     chans <- liftM (map sendChan) $! sameProtoClientsS proto
   255     chans <- liftM (map sendChan) $! sameProtoClientsS proto
   245     processAction $ AnswerClients chans ("ROOM" : "UPD" : oldRoomName : roomInfo proto(nick newMaster) newRoom')
   256     processAction $ AnswerClients chans ("ROOM" : "UPD" : oldRoomName : roomInfo proto (maybeNick newMaster) newRoom')
   246 
   257 
   247 
   258 
   248 processAction (AddRoom roomName roomPassword) = do
   259 processAction (AddRoom roomName roomPassword) = do
   249     Just clId <- gets clientIndex
   260     Just clId <- gets clientIndex
   250     rnc <- gets roomsClients
   261     rnc <- gets roomsClients
   251     proto <- client's clientProto
   262     proto <- client's clientProto
   252     n <- client's nick
   263     n <- client's nick
   253 
   264 
   254     let rm = newRoom{
   265     let rm = newRoom{
   255             masterID = clId,
   266             masterID = Just clId,
   256             name = roomName,
   267             name = roomName,
   257             password = roomPassword,
   268             password = roomPassword,
   258             roomProto = proto
   269             roomProto = proto
   259             }
   270             }
   260 
   271 
   290     Just clId <- gets clientIndex
   301     Just clId <- gets clientIndex
   291     proto <- client's clientProto
   302     proto <- client's clientProto
   292     rnc <- gets roomsClients
   303     rnc <- gets roomsClients
   293     ri <- io $ clientRoomM rnc clId
   304     ri <- io $ clientRoomM rnc clId
   294     rm <- io $ room'sM rnc id ri
   305     rm <- io $ room'sM rnc id ri
   295     n <- io $ client'sM rnc nick (masterID rm)
   306     masterCl <- io $ client'sM rnc id `DT.mapM` (masterID rm)
   296     chans <- liftM (map sendChan) $! sameProtoClientsS proto
   307     chans <- liftM (map sendChan) $! sameProtoClientsS proto
   297     processAction $ AnswerClients chans ("ROOM" : "UPD" : name rm : roomInfo proto n rm)
   308     processAction $ AnswerClients chans ("ROOM" : "UPD" : name rm : roomInfo proto (maybeNick masterCl) rm)
   298 
   309 
   299 
   310 
   300 processAction UnreadyRoomClients = do
   311 processAction UnreadyRoomClients = do
   301     ri <- clientRoomA
   312     ri <- clientRoomA
   302     roomPlayers <- roomClientsS ri
   313     roomPlayers <- roomClientsS ri