gameServer/Actions.hs
author unc0rr
Wed, 02 Feb 2011 21:23:12 +0300
changeset 4905 7842d085acf4
parent 4904 0eab727d4717
child 4907 8bf14795a528
permissions -rw-r--r--
Fix merge :D
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4905
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     1
{-# LANGUAGE OverloadedStrings #-}
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     2
module Actions where
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     3
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     4
import Control.Concurrent
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     5
import Control.Concurrent.Chan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     6
import qualified Data.IntSet as IntSet
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     7
import qualified Data.Set as Set
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     8
import qualified Data.Sequence as Seq
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
     9
import System.Log.Logger
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    10
import Control.Monad
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    11
import Data.Time
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    12
import Data.Maybe
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    13
import Control.Monad.Reader
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    14
import Control.Monad.State.Strict
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    15
import qualified Data.ByteString.Char8 as B
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    16
import Control.DeepSeq
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    17
-----------------------------
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    18
import CoreTypes
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    19
import Utils
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    20
import ClientIO
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    21
import ServerState
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    22
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    23
data Action =
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    24
    AnswerClients ![ClientChan] ![B.ByteString]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    25
    | SendServerMessage
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    26
    | SendServerVars
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    27
    | MoveToRoom RoomIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    28
    | MoveToLobby B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    29
    | RemoveTeam B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    30
    | RemoveRoom
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    31
    | UnreadyRoomClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    32
    | JoinLobby
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    33
    | ProtocolError B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    34
    | Warning B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    35
    | NoticeMessage Notice
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    36
    | ByeClient B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    37
    | KickClient ClientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    38
    | KickRoomClient ClientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    39
    | BanClient B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    40
    | ChangeMaster
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    41
    | RemoveClientTeams ClientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    42
    | ModifyClient (ClientInfo -> ClientInfo)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    43
    | ModifyClient2 ClientIndex (ClientInfo -> ClientInfo)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    44
    | ModifyRoom (RoomInfo -> RoomInfo)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    45
    | ModifyServerInfo (ServerInfo -> ServerInfo)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    46
    | AddRoom B.ByteString B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    47
    | CheckRegistered
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    48
    | ClearAccountsCache
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    49
    | ProcessAccountInfo AccountInfo
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    50
    | AddClient ClientInfo
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    51
    | DeleteClient ClientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    52
    | PingAll
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    53
    | StatsAction
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    54
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    55
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    56
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    57
instance NFData Action where
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    58
    rnf (AnswerClients chans msg) = chans `deepseq` msg `deepseq` ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    59
    rnf a = a `seq` ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    60
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    61
instance NFData B.ByteString
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    62
instance NFData (Chan a)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    63
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    64
othersChans = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    65
    cl <- client's id
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    66
    ri <- clientRoomA
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    67
    liftM (map sendChan . filter (/= cl)) $ roomClientsS ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    68
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    69
processAction :: Action -> StateT ServerState IO ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    70
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    71
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    72
processAction (AnswerClients chans msg) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    73
    io $ mapM_ (flip writeChan (msg `deepseq` msg)) (chans `deepseq` chans)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    74
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    75
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    76
processAction SendServerMessage = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    77
    chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    78
    protonum <- client's clientProto
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    79
    si <- liftM serverInfo get
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    80
    let message = if protonum < latestReleaseVersion si then
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    81
            serverMessageForOldVersions si
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    82
            else
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    83
            serverMessage si
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    84
    processAction $ AnswerClients [chan] ["SERVER_MESSAGE", message]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    85
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    86
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    87
processAction SendServerVars = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    88
    chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    89
    si <- gets serverInfo
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    90
    io $ writeChan chan ("SERVER_VARS" : vars si)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    91
    where
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    92
        vars si = [
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    93
            "MOTD_NEW", serverMessage si,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    94
            "MOTD_OLD", serverMessageForOldVersions si,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    95
            "LATEST_PROTO", B.pack . show $ latestReleaseVersion si
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    96
            ]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    97
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    98
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    99
processAction (ProtocolError msg) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   100
    chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   101
    processAction $ AnswerClients [chan] ["ERROR", msg]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   102
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   103
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   104
processAction (Warning msg) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   105
    chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   106
    processAction $ AnswerClients [chan] ["WARNING", msg]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   107
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   108
processAction (NoticeMessage n) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   109
    chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   110
    processAction $ AnswerClients [chan] ["NOTICE", B.pack . show . fromEnum $ n]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   111
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   112
processAction (ByeClient msg) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   113
    (Just ci) <- gets clientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   114
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   115
    ri <- clientRoomA
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   116
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   117
    chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   118
    clNick <- client's nick
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   119
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   120
    when (ri /= lobbyId) $ do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   121
        processAction $ MoveToLobby ("quit: " `B.append` msg)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   122
        return ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   123
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   124
    clientsChans <- liftM (Prelude.map sendChan . Prelude.filter logonPassed) $! allClientsS
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   125
    io $ do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   126
        infoM "Clients" (show ci ++ " quits: " ++ (B.unpack msg))
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   127
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   128
    processAction $ AnswerClients [chan] ["BYE", msg]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   129
    processAction $ AnswerClients clientsChans ["LOBBY:LEFT", clNick, msg]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   130
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   131
    s <- get
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   132
    put $! s{removedClients = ci `Set.insert` removedClients s}
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   133
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   134
processAction (DeleteClient ci) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   135
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   136
    io $ removeClient rnc ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   137
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   138
    s <- get
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   139
    put $! s{removedClients = ci `Set.delete` removedClients s}
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   140
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   141
processAction (ModifyClient f) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   142
    (Just ci) <- gets clientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   143
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   144
    io $ modifyClient rnc f ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   145
    return ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   146
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   147
processAction (ModifyClient2 ci f) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   148
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   149
    io $ modifyClient rnc f ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   150
    return ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   151
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   152
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   153
processAction (ModifyRoom f) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   154
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   155
    ri <- clientRoomA
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   156
    io $ modifyRoom rnc f ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   157
    return ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   158
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   159
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   160
processAction (ModifyServerInfo f) =
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   161
    modify (\s -> s{serverInfo = f $ serverInfo s})
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   162
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   163
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   164
processAction (MoveToRoom ri) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   165
    (Just ci) <- gets clientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   166
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   167
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   168
    io $ do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   169
        modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False}) ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   170
        modifyRoom rnc (\r -> r{playersIn = (playersIn r) + 1}) ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   171
        moveClientToRoom rnc ri ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   172
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   173
    chans <- liftM (map sendChan) $ roomClientsS ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   174
    clNick <- client's nick
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   175
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   176
    processAction $ AnswerClients chans ["JOINED", clNick]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   177
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   178
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   179
processAction (MoveToLobby msg) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   180
    (Just ci) <- gets clientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   181
    ri <- clientRoomA
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   182
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   183
    (gameProgress, playersNum) <- io $ room'sM rnc (\r -> (gameinprogress r, playersIn r)) ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   184
    ready <- client's isReady
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   185
    master <- client's isMaster
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   186
--    client <- client's id
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   187
    clNick <- client's nick
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   188
    chans <- othersChans
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   189
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   190
    if master then
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   191
        if gameProgress && playersNum > 1 then
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   192
            mapM_ processAction [ChangeMaster, AnswerClients chans ["LEFT", clNick, msg], NoticeMessage AdminLeft, RemoveClientTeams ci]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   193
            else
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   194
            processAction RemoveRoom
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   195
        else
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   196
        mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   197
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   198
    io $ do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   199
            modifyRoom rnc (\r -> r{
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   200
                    playersIn = (playersIn r) - 1,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   201
                    readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   202
                    }) ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   203
            moveClientToLobby rnc ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   204
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   205
processAction ChangeMaster = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   206
    ri <- clientRoomA
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   207
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   208
    newMasterId <- liftM head . io $ roomClientsIndicesM rnc ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   209
    newMaster <- io $ client'sM rnc id newMasterId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   210
    let newRoomName = nick newMaster
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   211
    mapM_ processAction [
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   212
        ModifyRoom (\r -> r{masterID = newMasterId, name = newRoomName}),
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   213
        ModifyClient2 newMasterId (\c -> c{isMaster = True}),
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   214
        AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   215
        ]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   216
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   217
processAction (AddRoom roomName roomPassword) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   218
    Just clId <- gets clientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   219
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   220
    proto <- io $ client'sM rnc clientProto clId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   221
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   222
    let room = newRoom{
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   223
            masterID = clId,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   224
            name = roomName,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   225
            password = roomPassword,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   226
            roomProto = proto
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   227
            }
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   228
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   229
    rId <- io $ addRoom rnc room
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   230
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   231
    processAction $ MoveToRoom rId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   232
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   233
    chans <- liftM (map sendChan) $! roomClientsS lobbyId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   234
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   235
    mapM_ processAction [
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   236
        AnswerClients chans ["ROOM", "ADD", roomName]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   237
        , ModifyClient (\cl -> cl{isMaster = True})
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   238
        ]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   239
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   240
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   241
processAction RemoveRoom = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   242
    Just clId <- gets clientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   243
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   244
    ri <- io $ clientRoomM rnc clId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   245
    roomName <- io $ room'sM rnc name ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   246
    others <- othersChans
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   247
    lobbyChans <- liftM (map sendChan) $! roomClientsS lobbyId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   248
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   249
    mapM_ processAction [
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   250
            AnswerClients lobbyChans ["ROOM", "DEL", roomName],
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   251
            AnswerClients others ["ROOMABANDONED", roomName]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   252
        ]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   253
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   254
    io $ removeRoom rnc ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   255
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   256
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   257
processAction (UnreadyRoomClients) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   258
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   259
    ri <- clientRoomA
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   260
    roomPlayers <- roomClientsS ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   261
    roomClIDs <- io $ roomClientsIndicesM rnc ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   262
    processAction $ AnswerClients (map sendChan roomPlayers) ("NOT_READY" : map nick roomPlayers)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   263
    io $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   264
    processAction $ ModifyRoom (\r -> r{readyPlayers = 0})
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   265
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   266
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   267
processAction (RemoveTeam teamName) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   268
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   269
    cl <- client's id
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   270
    ri <- clientRoomA
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   271
    inGame <- io $ room'sM rnc gameinprogress ri
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   272
    chans <- othersChans
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   273
    if inGame then
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   274
            mapM_ processAction [
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   275
                AnswerClients chans ["REMOVE_TEAM", teamName],
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   276
                ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r})
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   277
                ]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   278
        else
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   279
            mapM_ processAction [
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   280
                AnswerClients chans ["EM", rmTeamMsg],
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   281
                ModifyRoom (\r -> r{
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   282
                    teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   283
                    leftTeams = teamName : leftTeams r,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   284
                    roundMsgs = roundMsgs r Seq.|> rmTeamMsg
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   285
                    })
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   286
                ]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   287
    where
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   288
        rmTeamMsg = toEngineMsg $ (B.singleton 'F') `B.append` teamName
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   289
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   290
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   291
processAction (RemoveClientTeams clId) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   292
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   293
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   294
    removeTeamActions <- io $ do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   295
        clNick <- client'sM rnc nick clId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   296
        rId <- clientRoomM rnc clId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   297
        roomTeams <- room'sM rnc teams rId
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   298
        return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   299
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   300
    mapM_ processAction removeTeamActions
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   301
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   302
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   303
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   304
processAction CheckRegistered = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   305
    (Just ci) <- gets clientIndex
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   306
    n <- client's nick
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   307
    h <- client's host
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   308
    db <- gets (dbQueries . serverInfo)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   309
    io $ writeChan db $ CheckAccount ci n h
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   310
    return ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   311
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   312
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   313
processAction ClearAccountsCache = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   314
    dbq <- gets (dbQueries . serverInfo)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   315
    io $ writeChan dbq ClearCache
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   316
    return ()
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   317
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   318
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   319
processAction (ProcessAccountInfo info) =
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   320
    case info of
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   321
        HasAccount passwd isAdmin -> do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   322
            chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   323
            processAction $ AnswerClients [chan] ["ASKPASSWORD"]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   324
        Guest -> do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   325
            processAction JoinLobby
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   326
        Admin -> do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   327
            mapM processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   328
            chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   329
            processAction $ AnswerClients [chan] ["ADMIN_ACCESS"]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   330
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   331
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   332
processAction JoinLobby = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   333
    chan <- client's sendChan
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   334
    clientNick <- client's nick
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   335
    (lobbyNicks, clientsChans) <- liftM (unzip . Prelude.map (\c -> (nick c, sendChan c)) . Prelude.filter logonPassed) $! allClientsS
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   336
    mapM_ processAction $
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   337
        (AnswerClients clientsChans ["LOBBY:JOINED", clientNick])
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   338
        : [AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks)]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   339
        ++ [ModifyClient (\cl -> cl{logonPassed = True}), SendServerMessage]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   340
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   341
{-
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   342
processAction (clID, serverInfo, rnc) (RoomAddThisClient rID) =
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   343
    processAction (
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   344
        clID,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   345
        serverInfo,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   346
        adjust (\cl -> cl{roomID = rID, teamsInGame = if rID == 0 then teamsInGame cl else 0}) clID clients,
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   347
        adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   348
            adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   349
        ) joinMsg
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   350
    where
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   351
        client = clients ! clID
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   352
        joinMsg = if rID == 0 then
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   353
                AnswerAllOthers ["LOBBY:JOINED", nick client]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   354
            else
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   355
                AnswerThisRoom ["JOINED", nick client]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   356
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   357
processAction (clID, serverInfo, rnc) (KickClient kickID) =
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   358
    liftM2 replaceID (return clID) (processAction (kickID, serverInfo, rnc) $ ByeClient "Kicked")
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   359
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   360
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   361
processAction (clID, serverInfo, rnc) (BanClient banNick) =
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   362
    return (clID, serverInfo, rnc)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   363
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   364
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   365
processAction (clID, serverInfo, rnc) (KickRoomClient kickID) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   366
    writeChan (sendChan $ clients ! kickID) ["KICKED"]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   367
    liftM2 replaceID (return clID) (processAction (kickID, serverInfo, rnc) $ RoomRemoveThisClient "kicked")
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   368
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   369
-}
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   370
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   371
processAction (AddClient client) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   372
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   373
    si <- gets serverInfo
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   374
    io $ do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   375
        ci <- addClient rnc client
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   376
        t <- forkIO $ clientRecvLoop (clientSocket client) (coreChan si) ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   377
        forkIO $ clientSendLoop (clientSocket client) t (coreChan si) (sendChan client) ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   378
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   379
        infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime client))
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   380
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   381
    processAction $ AnswerClients [sendChan client] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   382
{-        let newLogins = takeWhile (\(_ , time) -> (connectTime client) `diffUTCTime` time <= 11) $ lastLogins serverInfo
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   383
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   384
        if False && (isJust $ host client `Prelude.lookup` newLogins) then
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   385
            processAction (ci, serverInfo{lastLogins = newLogins}, rnc) $ ByeClient "Reconnected too fast"
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   386
            else
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   387
            return (ci, serverInfo)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   388
-}
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   389
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   390
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   391
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   392
processAction PingAll = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   393
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   394
    io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   395
    cis <- io $ allClientsM rnc
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   396
    chans <- io $ mapM (client'sM rnc sendChan) cis
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   397
    io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   398
    processAction $ AnswerClients chans ["PING"]
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   399
    where
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   400
        kickTimeouted rnc ci = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   401
            pq <- io $ client'sM rnc pingsQueue ci
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   402
            when (pq > 0) $
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   403
                withStateT (\as -> as{clientIndex = Just ci}) $
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   404
                    processAction (ByeClient "Ping timeout")
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   405
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   406
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   407
processAction (StatsAction) = do
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   408
    rnc <- gets roomsClients
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   409
    si <- gets serverInfo
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   410
    (roomsNum, clientsNum) <- io $ withRoomsAndClients rnc stats
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   411
    io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   412
    where
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   413
          stats irnc = (length $ allRooms irnc, length $ allClients irnc)
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
   414