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