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