gameServer/Actions.hs
author koda
Sat, 30 Apr 2011 20:06:05 +0200
changeset 5194 851c6fbb44e6
parent 5184 bf7bba60ed93
child 5209 f7a610e2ef5f
permissions -rw-r--r--
small fix for starting the ios game
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5184
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     1
{-# LANGUAGE OverloadedStrings #-}
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     2
module Actions where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     3
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     4
import Control.Concurrent
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     5
import qualified Data.Set as Set
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     6
import qualified Data.Sequence as Seq
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     7
import qualified Data.List as L
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     8
import qualified Control.Exception as Exception
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
     9
import System.Log.Logger
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    10
import Control.Monad
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    11
import Data.Time
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    12
import Data.Maybe
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    13
import Control.Monad.Reader
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    14
import Control.Monad.State.Strict
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    15
import qualified Data.ByteString.Char8 as B
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    16
import Control.DeepSeq
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    17
import Data.Unique
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    18
import Control.Arrow
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    19
import Control.Exception
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    20
import OfficialServer.GameReplayStore
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    21
-----------------------------
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    22
import CoreTypes
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    23
import Utils
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    24
import ClientIO
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    25
import ServerState
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    26
import Consts
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    27
import ConfigFile
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    28
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    29
data Action =
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    30
    AnswerClients ![ClientChan] ![B.ByteString]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    31
    | SendServerMessage
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    32
    | SendServerVars
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    33
    | MoveToRoom RoomIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    34
    | MoveToLobby B.ByteString
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    35
    | RemoveTeam B.ByteString
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    36
    | RemoveRoom
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    37
    | UnreadyRoomClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    38
    | JoinLobby
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    39
    | ProtocolError B.ByteString
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    40
    | Warning B.ByteString
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    41
    | NoticeMessage Notice
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    42
    | ByeClient B.ByteString
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    43
    | KickClient ClientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    44
    | KickRoomClient ClientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    45
    | BanClient NominalDiffTime B.ByteString ClientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    46
    | ChangeMaster
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    47
    | RemoveClientTeams ClientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    48
    | ModifyClient (ClientInfo -> ClientInfo)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    49
    | ModifyClient2 ClientIndex (ClientInfo -> ClientInfo)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    50
    | ModifyRoom (RoomInfo -> RoomInfo)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    51
    | ModifyServerInfo (ServerInfo -> ServerInfo)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    52
    | AddRoom B.ByteString B.ByteString
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    53
    | CheckRegistered
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    54
    | ClearAccountsCache
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    55
    | ProcessAccountInfo AccountInfo
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    56
    | AddClient ClientInfo
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    57
    | DeleteClient ClientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    58
    | PingAll
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    59
    | StatsAction
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    60
    | RestartServer Bool
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    61
    | AddNick2Bans B.ByteString B.ByteString UTCTime
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    62
    | AddIP2Bans B.ByteString B.ByteString UTCTime
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    63
    | CheckBanned
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    64
    | SaveReplay
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    65
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    66
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    67
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    68
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    69
instance NFData Action where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    70
    rnf (AnswerClients chans msg) = chans `deepseq` msg `deepseq` ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    71
    rnf a = a `seq` ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    72
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    73
instance NFData B.ByteString
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    74
instance NFData (Chan a)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    75
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    76
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    77
othersChans :: StateT ServerState IO [ClientChan]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    78
othersChans = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    79
    cl <- client's id
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    80
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    81
    liftM (map sendChan . filter (/= cl)) $ roomClientsS ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    82
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    83
processAction :: Action -> StateT ServerState IO ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    84
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    85
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    86
processAction (AnswerClients chans msg) =
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    87
    io $ mapM_ (`writeChan` (msg `deepseq` msg)) (chans `deepseq` chans)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    88
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    89
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    90
processAction SendServerMessage = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    91
    chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    92
    protonum <- client's clientProto
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    93
    si <- liftM serverInfo get
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    94
    let message = if protonum < latestReleaseVersion si then
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    95
            serverMessageForOldVersions si
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    96
            else
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    97
            serverMessage si
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    98
    processAction $ AnswerClients [chan] ["SERVER_MESSAGE", message]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
    99
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   100
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   101
processAction SendServerVars = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   102
    chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   103
    si <- gets serverInfo
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   104
    io $ writeChan chan ("SERVER_VARS" : vars si)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   105
    where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   106
        vars si = [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   107
            "MOTD_NEW", serverMessage si,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   108
            "MOTD_OLD", serverMessageForOldVersions si,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   109
            "LATEST_PROTO", showB $ latestReleaseVersion si
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   110
            ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   111
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   112
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   113
processAction (ProtocolError msg) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   114
    chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   115
    processAction $ AnswerClients [chan] ["ERROR", msg]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   116
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   117
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   118
processAction (Warning msg) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   119
    chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   120
    processAction $ AnswerClients [chan] ["WARNING", msg]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   121
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   122
processAction (NoticeMessage n) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   123
    chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   124
    processAction $ AnswerClients [chan] ["NOTICE", showB . fromEnum $ n]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   125
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   126
processAction (ByeClient msg) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   127
    (Just ci) <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   128
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   129
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   130
    chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   131
    clNick <- client's nick
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   132
    loggedIn <- client's logonPassed
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   133
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   134
    when (ri /= lobbyId) $ do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   135
        processAction $ MoveToLobby ("quit: " `B.append` msg)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   136
        return ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   137
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   138
    clientsChans <- liftM (Prelude.map sendChan . Prelude.filter logonPassed) $! allClientsS
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   139
    io $
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   140
        infoM "Clients" (show ci ++ " quits: " ++ B.unpack msg)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   141
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   142
    processAction $ AnswerClients [chan] ["BYE", msg]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   143
    when loggedIn $ processAction $ AnswerClients clientsChans ["LOBBY:LEFT", clNick, msg]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   144
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   145
    s <- get
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   146
    put $! s{removedClients = ci `Set.insert` removedClients s}
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   147
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   148
processAction (DeleteClient ci) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   149
    io $ debugM "Clients"  $ "DeleteClient: " ++ show ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   150
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   151
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   152
    io $ removeClient rnc ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   153
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   154
    s <- get
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   155
    put $! s{removedClients = ci `Set.delete` removedClients s}
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   156
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   157
processAction (ModifyClient f) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   158
    (Just ci) <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   159
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   160
    io $ modifyClient rnc f ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   161
    return ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   162
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   163
processAction (ModifyClient2 ci f) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   164
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   165
    io $ modifyClient rnc f ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   166
    return ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   167
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   168
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   169
processAction (ModifyRoom f) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   170
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   171
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   172
    io $ modifyRoom rnc f ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   173
    return ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   174
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   175
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   176
processAction (ModifyServerInfo f) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   177
    modify (\s -> s{serverInfo = f $ serverInfo s})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   178
    si <- gets serverInfo
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   179
    io $ writeServerConfig si
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   180
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   181
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   182
processAction (MoveToRoom ri) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   183
    (Just ci) <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   184
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   185
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   186
    io $ do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   187
        modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False}) ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   188
        modifyRoom rnc (\r -> r{playersIn = playersIn r + 1}) ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   189
        moveClientToRoom rnc ri ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   190
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   191
    chans <- liftM (map sendChan) $ roomClientsS ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   192
    clNick <- client's nick
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   193
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   194
    processAction $ AnswerClients chans ["JOINED", clNick]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   195
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   196
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   197
processAction (MoveToLobby msg) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   198
    (Just ci) <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   199
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   200
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   201
    (gameProgress, playersNum) <- io $ room'sM rnc (gameinprogress &&& playersIn) ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   202
    ready <- client's isReady
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   203
    master <- client's isMaster
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   204
--    client <- client's id
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   205
    clNick <- client's nick
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   206
    chans <- othersChans
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   207
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   208
    if master then
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   209
        if gameProgress && playersNum > 1 then
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   210
            mapM_ processAction [ChangeMaster, AnswerClients chans ["LEFT", clNick, msg], NoticeMessage AdminLeft, RemoveClientTeams ci]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   211
            else
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   212
            processAction RemoveRoom
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   213
        else
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   214
        mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   215
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   216
    -- when not removing room
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   217
    when (not master || (gameProgress && playersNum > 1)) . io $ do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   218
        modifyRoom rnc (\r -> r{
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   219
                playersIn = playersIn r - 1,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   220
                readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   221
                }) ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   222
        moveClientToLobby rnc ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   223
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   224
processAction ChangeMaster = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   225
    (Just ci) <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   226
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   227
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   228
    newMasterId <- liftM (head . filter (/= ci)) . io $ roomClientsIndicesM rnc ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   229
    newMaster <- io $ client'sM rnc id newMasterId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   230
    let newRoomName = nick newMaster
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   231
    mapM_ processAction [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   232
        ModifyRoom (\r -> r{masterID = newMasterId, name = newRoomName}),
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   233
        ModifyClient2 newMasterId (\c -> c{isMaster = True}),
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   234
        AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   235
        ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   236
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   237
processAction (AddRoom roomName roomPassword) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   238
    Just clId <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   239
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   240
    proto <- io $ client'sM rnc clientProto clId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   241
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   242
    let rm = newRoom{
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   243
            masterID = clId,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   244
            name = roomName,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   245
            password = roomPassword,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   246
            roomProto = proto
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   247
            }
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   248
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   249
    rId <- io $ addRoom rnc rm
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   250
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   251
    processAction $ MoveToRoom rId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   252
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   253
    chans <- liftM (map sendChan) $! roomClientsS lobbyId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   254
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   255
    mapM_ processAction [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   256
        AnswerClients chans ["ROOM", "ADD", roomName]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   257
        , ModifyClient (\cl -> cl{isMaster = True})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   258
        ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   259
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   260
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   261
processAction RemoveRoom = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   262
    Just clId <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   263
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   264
    ri <- io $ clientRoomM rnc clId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   265
    roomName <- io $ room'sM rnc name ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   266
    others <- othersChans
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   267
    lobbyChans <- liftM (map sendChan) $! roomClientsS lobbyId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   268
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   269
    mapM_ processAction [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   270
            AnswerClients lobbyChans ["ROOM", "DEL", roomName],
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   271
            AnswerClients others ["ROOMABANDONED", roomName]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   272
        ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   273
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   274
    io $ removeRoom rnc ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   275
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   276
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   277
processAction (UnreadyRoomClients) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   278
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   279
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   280
    roomPlayers <- roomClientsS ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   281
    roomClIDs <- io $ roomClientsIndicesM rnc ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   282
    pr <- client's clientProto
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   283
    processAction $ AnswerClients (map sendChan roomPlayers) $ notReadyMessage pr (map nick roomPlayers)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   284
    io $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   285
    processAction $ ModifyRoom (\r -> r{readyPlayers = 0})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   286
    where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   287
        notReadyMessage p nicks = if p < 38 then "NOT_READY" : nicks else "CLIENT_FLAGS" : "-r" : nicks
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   288
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   289
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   290
processAction (RemoveTeam teamName) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   291
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   292
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   293
    inGame <- io $ room'sM rnc gameinprogress ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   294
    chans <- othersChans
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   295
    if not $ inGame then
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   296
            mapM_ processAction [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   297
                AnswerClients chans ["REMOVE_TEAM", teamName],
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   298
                ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   299
                ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   300
        else
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   301
            mapM_ processAction [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   302
                AnswerClients chans ["EM", rmTeamMsg],
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   303
                ModifyRoom (\r -> r{
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   304
                    teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   305
                    leftTeams = teamName : leftTeams r,
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   306
                    roundMsgs = roundMsgs r Seq.|> rmTeamMsg
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   307
                    })
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   308
                ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   309
    where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   310
        rmTeamMsg = toEngineMsg $ 'F' `B.cons` teamName
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   311
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   312
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   313
processAction (RemoveClientTeams clId) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   314
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   315
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   316
    removeTeamActions <- io $ do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   317
        clNick <- client'sM rnc nick clId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   318
        rId <- clientRoomM rnc clId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   319
        roomTeams <- room'sM rnc teams rId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   320
        return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   321
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   322
    mapM_ processAction removeTeamActions
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   323
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   324
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   325
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   326
processAction CheckRegistered = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   327
    (Just ci) <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   328
    n <- client's nick
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   329
    h <- client's host
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   330
    p <- client's clientProto
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   331
    uid <- client's clUID
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   332
    haveSameNick <- liftM (not . null . tail . filter (\c -> nick c == n)) allClientsS
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   333
    if haveSameNick then
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   334
        if p < 38 then
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   335
            mapM_ processAction [ByeClient "Nickname is already in use", removeNick]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   336
            else
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   337
            mapM_ processAction [NoticeMessage NickAlreadyInUse, removeNick]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   338
        else
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   339
        do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   340
        db <- gets (dbQueries . serverInfo)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   341
        io $ writeChan db $ CheckAccount ci (hashUnique uid) n h
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   342
        return ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   343
   where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   344
       removeNick = ModifyClient (\c -> c{nick = ""})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   345
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   346
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   347
processAction ClearAccountsCache = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   348
    dbq <- gets (dbQueries . serverInfo)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   349
    io $ writeChan dbq ClearCache
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   350
    return ()
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   351
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   352
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   353
processAction (ProcessAccountInfo info) =
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   354
    case info of
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   355
        HasAccount passwd isAdmin -> do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   356
            chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   357
            mapM_ processAction [AnswerClients [chan] ["ASKPASSWORD"], ModifyClient (\c -> c{webPassword = passwd, isAdministrator = isAdmin})]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   358
        Guest ->
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   359
            processAction JoinLobby
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   360
        Admin -> do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   361
            mapM_ processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   362
            chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   363
            processAction $ AnswerClients [chan] ["ADMIN_ACCESS"]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   364
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   365
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   366
processAction JoinLobby = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   367
    chan <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   368
    clientNick <- client's nick
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   369
    (lobbyNicks, clientsChans) <- liftM (unzip . Prelude.map (nick &&& sendChan) . Prelude.filter logonPassed) $! allClientsS
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   370
    mapM_ processAction $
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   371
        AnswerClients clientsChans ["LOBBY:JOINED", clientNick]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   372
        : AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   373
        : [ModifyClient (\cl -> cl{logonPassed = True}), SendServerMessage]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   374
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   375
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   376
processAction (KickClient kickId) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   377
    modify (\s -> s{clientIndex = Just kickId})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   378
    processAction $ ByeClient "Kicked"
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   379
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   380
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   381
processAction (BanClient seconds reason banId) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   382
    modify (\s -> s{clientIndex = Just banId})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   383
    clHost <- client's host
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   384
    currentTime <- io getCurrentTime
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   385
    let msg = B.concat ["Ban for ", B.pack . show $ seconds, "seconds (", reason, ")"]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   386
    mapM_ processAction [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   387
        AddIP2Bans clHost msg (addUTCTime seconds currentTime)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   388
        , KickClient banId
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   389
        ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   390
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   391
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   392
processAction (KickRoomClient kickId) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   393
    modify (\s -> s{clientIndex = Just kickId})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   394
    ch <- client's sendChan
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   395
    mapM_ processAction [AnswerClients [ch] ["KICKED"], MoveToLobby "kicked"]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   396
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   397
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   398
processAction (AddClient cl) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   399
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   400
    si <- gets serverInfo
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   401
    newClId <- io $ do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   402
        ci <- addClient rnc cl
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   403
        _ <- Exception.mask (forkIO . clientRecvLoop (clientSocket cl) (coreChan si) (sendChan cl) ci)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   404
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   405
        infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime cl))
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   406
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   407
        return ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   408
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   409
    modify (\s -> s{clientIndex = Just newClId})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   410
    mapM_ processAction
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   411
        [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   412
            AnswerClients [sendChan cl] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/", serverVersion]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   413
            , CheckBanned
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   414
            , AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   415
        ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   416
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   417
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   418
processAction (AddNick2Bans n reason expiring) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   419
    processAction $ ModifyServerInfo (\s -> s{bans = BanByNick n reason expiring : bans s})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   420
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   421
processAction (AddIP2Bans ip reason expiring) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   422
    (Just ci) <- gets clientIndex
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   423
    rc <- gets removedClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   424
    when (not $ ci `Set.member` rc)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   425
        $ processAction $ ModifyServerInfo (\s -> s{bans = BanByIP ip reason expiring : bans s})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   426
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   427
processAction CheckBanned = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   428
    clTime <- client's connectTime
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   429
    clNick <- client's nick
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   430
    clHost <- client's host
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   431
    si <- gets serverInfo
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   432
    let validBans = filter (checkNotExpired clTime) $ bans si
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   433
    let ban = L.find (checkBan clHost clNick) $ validBans
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   434
    when (isJust ban) $
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   435
        mapM_ processAction [
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   436
        ModifyServerInfo (\s -> s{bans = validBans})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   437
        , ByeClient (getBanReason $ fromJust ban)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   438
        ]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   439
    where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   440
        checkNotExpired testTime (BanByIP _ _ time) = testTime `diffUTCTime` time <= 0
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   441
        checkNotExpired testTime (BanByNick _ _ time) = testTime `diffUTCTime` time <= 0
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   442
        checkBan ip _ (BanByIP bip _ _) = bip == ip
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   443
        checkBan _ n (BanByNick bn _ _) = bn == n
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   444
        getBanReason (BanByIP _ msg _) = msg
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   445
        getBanReason (BanByNick _ msg _) = msg
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   446
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   447
processAction PingAll = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   448
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   449
    io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   450
    cis <- io $ allClientsM rnc
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   451
    chans <- io $ mapM (client'sM rnc sendChan) cis
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   452
    io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   453
    processAction $ AnswerClients chans ["PING"]
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   454
    where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   455
        kickTimeouted rnc ci = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   456
            pq <- io $ client'sM rnc pingsQueue ci
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   457
            when (pq > 0) $
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   458
                withStateT (\as -> as{clientIndex = Just ci}) $
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   459
                    processAction (ByeClient "Ping timeout")
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   460
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   461
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   462
processAction StatsAction = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   463
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   464
    si <- gets serverInfo
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   465
    (roomsNum, clientsNum) <- io $ withRoomsAndClients rnc st
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   466
    io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   467
    where
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   468
          st irnc = (length $ allRooms irnc, length $ allClients irnc)
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   469
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   470
processAction (RestartServer force) = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   471
    if force then do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   472
        throw RestartException
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   473
        else
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   474
        processAction $ ModifyServerInfo (\s -> s{restartPending=True})
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   475
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   476
processAction SaveReplay = do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   477
    ri <- clientRoomA
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   478
    rnc <- gets roomsClients
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   479
    io $ do
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   480
        r <- room'sM rnc id ri
bf7bba60ed93 update iphone too
nemo
parents: 5143
diff changeset
   481
        saveReplay r