gameServer/HWProtoInRoomState.hs
author nemo
Fri, 05 Mar 2010 02:28:03 +0000
changeset 2937 da06c25edf1d
parent 2928 0e95e0e24fd8
child 2952 18fada739b55
permissions -rw-r--r--
More tweaking, make blowtorch interact badly w/ explosives
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module HWProtoInRoomState where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
     3
import qualified Data.Foldable as Foldable
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
import qualified Data.IntMap as IntMap
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
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import Maybe
2403
6c5d504af2ba - Proper /team command implementation
unc0rr
parents: 2381
diff changeset
     9
import qualified Codec.Binary.UTF8.String as UTF8
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
--------------------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
import Actions
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
import Utils
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
handleCmd_inRoom :: CmdHandler
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
1815
3d62cf9c350e Save some more bytes in protocol
unc0rr
parents: 1814
diff changeset
    18
handleCmd_inRoom clID clients _ ["CHAT", msg] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    19
    [AnswerOthersInRoom ["CHAT", clientNick, msg]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    20
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    21
        clientNick = nick $ clients IntMap.! clID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
    23
2207
aeea95909aba Make server accpet TEAM_CHAT protocol command, and act like on CHAT command for now
unc0rr
parents: 2126
diff changeset
    24
handleCmd_inRoom clID clients _ ["TEAM_CHAT", msg] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    25
    [AnswerOthersInRoom ["TEAM_CHAT", clientNick, msg]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    26
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    27
        clientNick = nick $ clients IntMap.! clID
2207
aeea95909aba Make server accpet TEAM_CHAT protocol command, and act like on CHAT command for now
unc0rr
parents: 2126
diff changeset
    28
aeea95909aba Make server accpet TEAM_CHAT protocol command, and act like on CHAT command for now
unc0rr
parents: 2126
diff changeset
    29
1814
e5391d901cff - Remove client teams on exit
unc0rr
parents: 1813
diff changeset
    30
handleCmd_inRoom clID clients rooms ["PART"] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    31
    [RoomRemoveThisClient "part"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    32
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    33
        client = clients IntMap.! clID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
    35
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
    36
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs)
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    37
    | null paramStrs = [ProtocolError "Empty config entry"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    38
    | isMaster client =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    39
        [ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    40
        AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    41
    | otherwise = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    42
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    43
        client = clients IntMap.! clID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
2747
7889a3a9724f Server:
smxx
parents: 2408
diff changeset
    45
handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : flag : difStr : hhsInfo)
2917
2f4320a1b31f Fix server for 0.9.12 (tested)
unc0rr
parents: 2902
diff changeset
    46
    | length hhsInfo == 15 && clientProto client < 30 = handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : " " : flag : difStr : hhsInfo)
2f4320a1b31f Fix server for 0.9.12 (tested)
unc0rr
parents: 2902
diff changeset
    47
    | length hhsInfo /= 16 = [ProtocolError "Corrupted hedgehogs info"]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    48
    | length (teams room) == 6 = [Warning "too many teams"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    49
    | canAddNumber <= 0 = [Warning "too many hedgehogs"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    50
    | isJust findTeam = [Warning "There's already a team with same name in the list"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    51
    | gameinprogress room = [Warning "round in progress"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    52
    | isRestrictedTeams room = [Warning "restricted"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    53
    | otherwise =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    54
        [ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    55
        ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1, clientClan = color}),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    56
        AnswerThisClient ["TEAM_ACCEPTED", name],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    57
        AnswerOthersInRoom $ teamToNet (clientProto client) newTeam,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    58
        AnswerOthersInRoom ["TEAM_COLOR", name, color]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    59
        ]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    60
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    61
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    62
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    63
        canAddNumber = 48 - (sum . map hhnum $ teams room)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    64
        findTeam = find (\t -> name == teamname t) $ teams room
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    65
        newTeam = (TeamInfo clID (nick client) name color grave fort voicepack flag difficulty newTeamHHNum (hhsList hhsInfo))
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    66
        difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    67
        hhsList [] = []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    68
        hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    69
        newTeamHHNum = min 4 canAddNumber
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    70
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
    71
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    72
    | noSuchTeam = [Warning "REMOVE_TEAM: no such team"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    73
    | nick client /= teamowner team = [ProtocolError "Not team owner!"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    74
    | otherwise =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    75
            [RemoveTeam teamName,
2928
0e95e0e24fd8 When removing team, set player's clan property to a colour of his another team
unc0rr
parents: 2917
diff changeset
    76
            ModifyClient (\c -> c{teamsInGame = teamsInGame c - 1, clientClan = if teamsInGame client == 1 then undefined else anotherTeamClan})
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    77
            ]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    78
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    79
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    80
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    81
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    82
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    83
        findTeam = find (\t -> teamName == teamname t) $ teams room
2928
0e95e0e24fd8 When removing team, set player's clan property to a colour of his another team
unc0rr
parents: 2917
diff changeset
    84
        anotherTeamClan = teamcolor $ fromJust $ find (\t -> teamownerId t == clID) $ teams room
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    86
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
    87
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    88
    | not $ isMaster client = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    89
    | hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) = []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    90
    | otherwise =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    91
        [ModifyRoom $ modifyTeam team{hhnum = hhNumber},
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    92
        AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    93
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    94
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    95
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    96
        hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    97
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    98
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    99
        findTeam = find (\t -> teamName == teamname t) $ teams room
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   100
        canAddNumber = 48 - (sum . map hhnum $ teams room)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   103
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   104
    | not $ isMaster client = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   105
    | noSuchTeam = []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   106
    | otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor},
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   107
            AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   108
            ModifyClient2 (teamownerId team) (\c -> c{clientClan = newColor})]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   109
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   110
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   111
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   112
        findTeam = find (\t -> teamName == teamname t) $ teams room
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   113
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   114
        room = rooms IntMap.! (roomID client)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   115
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   116
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   117
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   118
    [ModifyClient (\c -> c{isReady = not $ isReady client}),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   119
    ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   120
    AnswerThisRoom [if isReady client then "NOT_READY" else "READY", nick client]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   121
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   122
        client = clients IntMap.! clID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   123
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   124
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   125
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
   126
    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
   127
        if enoughClans then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   128
            [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   129
                    (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   130
                        gameinprogress = True,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   131
                        roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   132
                        leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   133
                        teamsAtStart = teams r}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   134
                    ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   135
            AnswerThisRoom ["RUN_GAME"]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   136
        else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   137
            [Warning "Less than two clans!"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   138
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   139
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   140
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   141
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   142
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   143
        enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   144
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   145
2245
c011aecc95e5 unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents: 2207
diff changeset
   146
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
   147
    if (teamsInGame client > 0) && isLegal then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   148
        (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
   149
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   150
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   151
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   152
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   153
        (isLegal, isKeepAlive) = checkNetCmd msg
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   154
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   155
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   156
    if isMaster client then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   157
        [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   158
                (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   159
                    gameinprogress = False,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   160
                    readyPlayers = 0,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   161
                    roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   162
                    leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   163
                    teamsAtStart = []}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   164
                ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   165
        UnreadyRoomClients
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   166
        ] ++ answerRemovedTeams
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   167
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   168
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   169
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   170
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   171
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   172
        answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   173
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   174
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   175
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   176
    | isMaster client = [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   177
    | otherwise = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   178
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   179
        client = clients IntMap.! clID
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   180
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   181
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   182
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   183
    | isMaster client = [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   184
    | otherwise = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   185
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   186
        client = clients IntMap.! clID
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   187
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   188
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
   189
    [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
   190
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   191
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   192
        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
   193
        noSuchClient = isNothing maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   194
        kickClient = fromJust maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   195
        kickID = clientUID kickClient
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   196
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   197
2403
6c5d504af2ba - Proper /team command implementation
unc0rr
parents: 2381
diff changeset
   198
handleCmd_inRoom clID clients _ ["TEAMCHAT", msg] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   199
    if (teamsInGame client > 0) then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   200
        [AnswerSameClan ["EM", engineMsg]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   201
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   202
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   203
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   204
        client = clients IntMap.! clID
2902
1566f05ca371 Fix problems with utf8-encoded nicknames in team chat (not tested)
unc0rr
parents: 2867
diff changeset
   205
        -- FIXME: why are those decoded* function used? 
1566f05ca371 Fix problems with utf8-encoded nicknames in team chat (not tested)
unc0rr
parents: 2867
diff changeset
   206
        -- it would be better to use ByteString instead of String
1566f05ca371 Fix problems with utf8-encoded nicknames in team chat (not tested)
unc0rr
parents: 2867
diff changeset
   207
        engineMsg = toEngineMsg $ 'b' : (decodedNick ++ "(team): " ++ decodedMsg ++ "\x20\x20")
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   208
        decodedMsg = UTF8.decodeString msg
2902
1566f05ca371 Fix problems with utf8-encoded nicknames in team chat (not tested)
unc0rr
parents: 2867
diff changeset
   209
        decodedNick = UTF8.decodeString $ nick client
2403
6c5d504af2ba - Proper /team command implementation
unc0rr
parents: 2381
diff changeset
   210
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   211
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"]