gameServer/HWProtoInRoomState.hs
author unc0rr
Fri, 25 Jun 2010 11:54:07 +0400
changeset 3559 c41c2e08a3f8
parent 3555 4c5ca656d1bb
child 3561 7f8e07e4a4e3
permissions -rw-r--r--
Ignore this file too
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
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    46
        let r = room rnc $ clientRoom rnc ci
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 $
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    51
            if null . drop 5 $ teams r then
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 [] = []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    76
        hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
3555
4c5ca656d1bb Reimplement ADD_TEAM
unc0rr
parents: 3544
diff changeset
    77
        newTeamHHNum r = min 4 (canAddNumber r)
3544
aad64e15ca03 Start reimplementation of ADD_TEAM
unc0rr
parents: 3543
diff changeset
    78
{-
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
    79
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
    80
    | noSuchTeam = [Warning "REMOVE_TEAM: no such team"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    81
    | 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
    82
    | otherwise =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    83
            [RemoveTeam teamName,
2928
0e95e0e24fd8 When removing team, set player's clan property to a colour of his another team
unc0rr
parents: 2917
diff changeset
    84
            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
    85
            ]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    86
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    87
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    88
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    89
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    90
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    91
        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
    92
        anotherTeamClan = teamcolor $ fromJust $ find (\t -> teamownerId t == clID) $ teams room
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    93
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    94
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
    95
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
    96
    | not $ isMaster client = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    97
    | 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
    98
    | otherwise =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    99
        [ModifyRoom $ modifyTeam team{hhnum = hhNumber},
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   100
        AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   101
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   102
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   103
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   104
        hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   105
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   106
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   107
        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
   108
        canAddNumber = 48 - (sum . map hhnum $ teams room)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   109
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   110
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   111
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
   112
    | not $ isMaster client = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   113
    | noSuchTeam = []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   114
    | otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor},
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   115
            AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   116
            ModifyClient2 (teamownerId team) (\c -> c{clientClan = newColor})]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   117
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   118
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   119
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   120
        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
   121
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   122
        room = rooms IntMap.! (roomID client)
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   123
-}
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   124
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   125
handleCmd_inRoom ["TOGGLE_READY"] = do
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   126
    cl <- thisClient
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   127
    chans <- roomClientsChans
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   128
    return [
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   129
        ModifyClient (\c -> c{isReady = not $ isReady cl}),
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   130
        ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady cl then -1 else 1)}),
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   131
        AnswerClients chans [if isReady cl then "NOT_READY" else "READY", nick cl]
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   132
        ]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   133
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   134
{-
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   135
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
   136
    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
   137
        if enoughClans then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   138
            [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   139
                    (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   140
                        gameinprogress = True,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   141
                        roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   142
                        leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   143
                        teamsAtStart = teams r}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   144
                    ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   145
            AnswerThisRoom ["RUN_GAME"]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   146
        else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   147
            [Warning "Less than two clans!"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   148
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   149
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   150
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   151
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   152
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   153
        enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   154
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   155
2245
c011aecc95e5 unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents: 2207
diff changeset
   156
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
   157
    if (teamsInGame client > 0) && isLegal then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   158
        (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
   159
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   160
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   161
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   162
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   163
        (isLegal, isKeepAlive) = checkNetCmd msg
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   164
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   165
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   166
    if isMaster client then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   167
        [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   168
                (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   169
                    gameinprogress = False,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   170
                    readyPlayers = 0,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   171
                    roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   172
                    leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   173
                    teamsAtStart = []}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   174
                ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   175
        UnreadyRoomClients
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   176
        ] ++ answerRemovedTeams
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   177
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   178
        []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   179
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   180
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   181
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   182
        answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   183
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   184
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   185
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
   186
    | 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
   187
    | otherwise = [ProtocolError "Not room master"]
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
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   190
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   191
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   192
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
   193
    | 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
   194
    | otherwise = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   195
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   196
        client = clients IntMap.! clID
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   197
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   198
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
   199
    [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
   200
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   201
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   202
        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
   203
        noSuchClient = isNothing maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   204
        kickClient = fromJust maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   205
        kickID = clientUID kickClient
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   206
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   207
2403
6c5d504af2ba - Proper /team command implementation
unc0rr
parents: 2381
diff changeset
   208
handleCmd_inRoom clID clients _ ["TEAMCHAT", msg] =
2960
b467a681c5e0 - Fix spectators team chat
unc0rr
parents: 2952
diff changeset
   209
    [AnswerSameClan ["EM", engineMsg]]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   210
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   211
        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
   212
        engineMsg = toEngineMsg $ 'b' : ((nick client) ++ "(team): " ++ msg ++ "\x20\x20")
3531
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
   213
-}
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
   214
handleCmd_inRoom _ = return [ProtocolError "Incorrect command (state: in room)"]