gameServer/HWProtoInRoomState.hs
author unc0rr
Sun, 27 Jun 2010 21:28:28 +0400
changeset 3576 d85bdd5dc835
parent 3568 ae89cf0735dc
child 3577 0ef6f5182a75
permissions -rw-r--r--
merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3500
af8390d807d6 Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents: 3435
diff changeset
     1
{-# LANGUAGE OverloadedStrings #-}
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
module HWProtoInRoomState where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
     4
import qualified Data.Foldable as Foldable
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import qualified Data.Map as Map
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import Data.Sequence(Seq, (|>), (><), fromList, empty)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Data.List
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
     8
import Data.Maybe
3531
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
     9
import qualified Data.ByteString.Char8 as B
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    10
import Control.Monad
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    11
import Control.Monad.Reader
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
--------------------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
import Actions
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
import Utils
3435
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 2960
diff changeset
    16
import HandlerUtils
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    17
import RoomsAndClients
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
handleCmd_inRoom :: CmdHandler
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
3435
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 2960
diff changeset
    21
handleCmd_inRoom ["CHAT", msg] = do
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 2960
diff changeset
    22
    n <- clientNick
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 2960
diff changeset
    23
    s <- roomOthersChans
4e4f88a7bdf2 Some more steps in refactoring
unc0rr
parents: 2960
diff changeset
    24
    return [AnswerClients s ["CHAT", n, msg]]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
3531
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
    26
handleCmd_inRoom ["PART"] = return [MoveToLobby "part"]
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
    27
handleCmd_inRoom ["PART", msg] = return [MoveToLobby $ "part: " `B.append` msg]
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
    28
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
    29
3540
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    30
handleCmd_inRoom ("CFG" : paramName : paramStrs)
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    31
    | null paramStrs = return [ProtocolError "Empty config entry"]
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    32
    | otherwise = do
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    33
        chans <- roomOthersChans
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    34
        cl <- thisClient
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    35
        if isMaster cl then
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    36
           return [
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    37
                ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}),
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    38
                AnswerClients chans ("CFG" : paramName : paramStrs)]
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    39
            else
b602a57ba0fb Reimplement CFG protocol command
unc0rr
parents: 3531
diff changeset
    40
            return [ProtocolError "Not room master"]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
3544
aad64e15ca03 Start reimplementation of ADD_TEAM
unc0rr
parents: 3543
diff changeset
    42
handleCmd_inRoom ("ADD_TEAM" : name : color : grave : fort : voicepack : flag : difStr : hhsInfo)
aad64e15ca03 Start reimplementation of ADD_TEAM
unc0rr
parents: 3543
diff changeset
    43
    | length hhsInfo /= 16 = return [ProtocolError "Corrupted hedgehogs info"]
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    44
    | otherwise = do
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    45
        (ci, rnc) <- ask
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
    46
        r <- thisRoom
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    47
        clNick <- clientNick
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    48
        clChan <- thisClientChans
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    49
        othersChans <- roomOthersChans
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    50
        return $
3566
772a46ef8288 Properly handle client exit
unc0rr
parents: 3564
diff changeset
    51
            if not . null . drop 5 $ teams r then
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    52
                [Warning "too many teams"]
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    53
            else if canAddNumber r <= 0 then
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    54
                [Warning "too many hedgehogs"]
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    55
            else if isJust $ findTeam r then
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    56
                [Warning "There's already a team with same name in the list"]
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    57
            else if gameinprogress r then
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    58
                [Warning "round in progress"]
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    59
            else if isRestrictedTeams r then
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    60
                [Warning "restricted"]
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    61
            else
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    62
                [ModifyRoom (\r -> r{teams = teams r ++ [newTeam ci clNick r]}),
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    63
                ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1, clientClan = color}),
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    64
                AnswerClients clChan ["TEAM_ACCEPTED", name],
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    65
                AnswerClients othersChans $ teamToNet $ newTeam ci clNick r,
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    66
                AnswerClients othersChans ["TEAM_COLOR", name, color]
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    67
                ]
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    68
        where
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    69
        canAddNumber r = 48 - (sum . map hhnum $ teams r)
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    70
        findTeam = find (\t -> name == teamname t) . teams
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    71
        newTeam ci clNick r = (TeamInfo ci clNick name color grave fort voicepack flag difficulty (newTeamHHNum r) (hhsList hhsInfo))
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    72
        difficulty = case B.readInt difStr of
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    73
                           Just (i, t) | B.null t -> fromIntegral i
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    74
                           otherwise -> 0
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    75
        hhsList [] = []
3566
772a46ef8288 Properly handle client exit
unc0rr
parents: 3564
diff changeset
    76
        hhsList [_] = error "Hedgehogs list with odd elements number"
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    77
        hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    78
        newTeamHHNum r = min 4 (canAddNumber r)
3561
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
    79
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
    80
handleCmd_inRoom ["REMOVE_TEAM", name] = do
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
    81
        (ci, rnc) <- ask
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
    82
        r <- thisRoom
3564
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    83
        clNick <- clientNick
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    84
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    85
        let maybeTeam = findTeam r
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    86
        let team = fromJust maybeTeam
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    87
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    88
        return $
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    89
            if isNothing $ findTeam r then
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    90
                [Warning "REMOVE_TEAM: no such team"]
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    91
            else if clNick /= teamowner team then
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    92
                [ProtocolError "Not team owner!"]
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    93
            else
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    94
                [RemoveTeam name,
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    95
                ModifyClient
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    96
                    (\c -> c{
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    97
                        teamsInGame = teamsInGame c - 1,
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    98
                        clientClan = if teamsInGame c == 1 then undefined else anotherTeamClan ci r
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    99
                        })
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
   100
                ]
3561
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
   101
    where
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
   102
        anotherTeamClan ci = teamcolor . fromJust . find (\t -> teamownerId t == ci) . teams
3564
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
   103
        findTeam = find (\t -> name == teamname t) . teams
3561
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
   104
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   105
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   106
handleCmd_inRoom ["HH_NUM", teamName, numberStr] = do
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   107
    cl <- thisClient
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   108
    others <- roomOthersChans
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   109
    r <- thisRoom
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   110
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   111
    let maybeTeam = findTeam r
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   112
    let team = fromJust maybeTeam
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   113
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   114
    return $
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   115
        if not $ isMaster cl then
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   116
            [ProtocolError "Not room master"]
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   117
        else if hhNumber < 1 || hhNumber > 8 || isNothing maybeTeam || hhNumber > (canAddNumber r) + (hhnum team) then
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   118
            []
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   119
        else
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   120
            [ModifyRoom $ modifyTeam team{hhnum = hhNumber},
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   121
            AnswerClients others ["HH_NUM", teamName, B.pack $ show hhNumber]]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   122
    where
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   123
        hhNumber = case B.readInt numberStr of
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   124
                           Just (i, t) | B.null t -> fromIntegral i
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   125
                           otherwise -> 0
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   126
        findTeam = find (\t -> teamName == teamname t) . teams
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   127
        canAddNumber = (-) 48 . sum . map hhnum . teams
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   128
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   129
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   130
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   131
handleCmd_inRoom ["TEAM_COLOR", teamName, newColor] = do
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   132
    cl <- thisClient
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   133
    others <- roomOthersChans
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   134
    r <- thisRoom
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   135
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   136
    let maybeTeam = findTeam r
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   137
    let team = fromJust maybeTeam
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   138
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   139
    return $
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   140
        if not $ isMaster cl then
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   141
            [ProtocolError "Not room master"]
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   142
        else if isNothing maybeTeam then
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   143
            []
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   144
        else
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   145
            [ModifyRoom $ modifyTeam team{teamcolor = newColor},
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   146
            AnswerClients others ["TEAM_COLOR", teamName, newColor],
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   147
            ModifyClient2 (teamownerId team) (\c -> c{clientClan = newColor})]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   148
    where
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   149
        findTeam = find (\t -> teamName == teamname t) . teams
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   150
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   151
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   152
handleCmd_inRoom ["TOGGLE_READY"] = do
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   153
    cl <- thisClient
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   154
    chans <- roomClientsChans
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   155
    return [
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   156
        ModifyClient (\c -> c{isReady = not $ isReady cl}),
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   157
        ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady cl then -1 else 1)}),
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   158
        AnswerClients chans [if isReady cl then "NOT_READY" else "READY", nick cl]
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   159
        ]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   160
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   161
{-
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   162
handleCmd_inRoom clID clients rooms ["START_GAME"] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   163
    if isMaster client && (playersIn room == readyPlayers room) && (not . gameinprogress) room then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   164
        if enoughClans then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   165
            [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   166
                    (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   167
                        gameinprogress = True,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   168
                        roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   169
                        leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   170
                        teamsAtStart = teams r}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   171
                    ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   172
            AnswerThisRoom ["RUN_GAME"]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   173
        else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   174
            [Warning "Less than two clans!"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   175
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   176
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   177
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   178
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   179
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   180
        enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   181
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   182
2245
c011aecc95e5 unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents: 2207
diff changeset
   183
handleCmd_inRoom clID clients rooms ["EM", msg] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   184
    if (teamsInGame client > 0) && isLegal then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   185
        (AnswerOthersInRoom ["EM", msg]) : [ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}) | not isKeepAlive]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   186
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   187
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   188
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   189
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   190
        (isLegal, isKeepAlive) = checkNetCmd msg
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   191
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   192
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   193
    if isMaster client then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   194
        [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   195
                (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   196
                    gameinprogress = False,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   197
                    readyPlayers = 0,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   198
                    roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   199
                    leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   200
                    teamsAtStart = []}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   201
                ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   202
        UnreadyRoomClients
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   203
        ] ++ answerRemovedTeams
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   204
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   205
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   206
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   207
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   208
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   209
        answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   210
-}
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   211
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   212
handleCmd_inRoom ["TOGGLE_RESTRICT_JOINS"] = do
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   213
    cl <- thisClient
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   214
    return $
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   215
        if not $ isMaster cl then
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   216
            [ProtocolError "Not room master"]
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   217
        else
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   218
            [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   219
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   220
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   221
handleCmd_inRoom ["TOGGLE_RESTRICT_TEAMS"] = do
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   222
    cl <- thisClient
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   223
    return $
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   224
        if not $ isMaster cl then
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   225
            [ProtocolError "Not room master"]
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   226
        else
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   227
            [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   228
3568
ae89cf0735dc A bunch of reimplemented commands
unc0rr
parents: 3566
diff changeset
   229
{-
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   230
handleCmd_inRoom clID clients rooms ["KICK", kickNick] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   231
    [KickRoomClient kickID | isMaster client && not noSuchClient && (kickID /= clID) && (roomID client == roomID kickClient)]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   232
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   233
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   234
        maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   235
        noSuchClient = isNothing maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   236
        kickClient = fromJust maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   237
        kickID = clientUID kickClient
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   238
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   239
2403
6c5d504af2ba - Proper /team command implementation
unc0rr
parents: 2381
diff changeset
   240
handleCmd_inRoom clID clients _ ["TEAMCHAT", msg] =
2960
b467a681c5e0 - Fix spectators team chat
unc0rr
parents: 2952
diff changeset
   241
    [AnswerSameClan ["EM", engineMsg]]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   242
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   243
        client = clients IntMap.! clID
2952
18fada739b55 - Convert strings from utf-8 on recieve, and back to utf-8 when send them
unc0rr
parents: 2928
diff changeset
   244
        engineMsg = toEngineMsg $ 'b' : ((nick client) ++ "(team): " ++ msg ++ "\x20\x20")
3531
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
   245
-}
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
   246
handleCmd_inRoom _ = return [ProtocolError "Incorrect command (state: in room)"]