gameServer/HWProtoInRoomState.hs
author sheepluva
Sat, 26 Jun 2010 16:00:40 +0200
changeset 3567 28e90e4541ce
parent 3564 7c583c88327b
child 3566 772a46ef8288
permissions -rw-r--r--
adding install_manifest.txt to .hgignore ...
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)
3561
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
    78
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
    79
handleCmd_inRoom ["REMOVE_TEAM", name] = do
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
    80
        (ci, rnc) <- ask
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
    81
        let r = room rnc $ clientRoom rnc ci
3564
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    82
        clNick <- clientNick
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    83
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    84
        let maybeTeam = findTeam r
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    85
        let team = fromJust maybeTeam
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    86
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    87
        return $
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    88
            if isNothing $ findTeam r then
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    89
                [Warning "REMOVE_TEAM: no such team"]
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    90
            else if clNick /= teamowner team then
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    91
                [ProtocolError "Not team owner!"]
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    92
            else
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    93
                [RemoveTeam name,
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    94
                ModifyClient
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    95
                    (\c -> c{
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    96
                        teamsInGame = teamsInGame c - 1,
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    97
                        clientClan = if teamsInGame c == 1 then undefined else anotherTeamClan ci r
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    98
                        })
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
    99
                ]
3561
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
   100
    where
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
   101
        anotherTeamClan ci = teamcolor . fromJust . find (\t -> teamownerId t == ci) . teams
3564
7c583c88327b Reimplement REMOVE_TEAM
unc0rr
parents: 3561
diff changeset
   102
        findTeam = find (\t -> name == teamname t) . teams
3561
7f8e07e4a4e3 Reimplement REMOVE_TEAM
unc0rr
parents: 3555
diff changeset
   103
3544
aad64e15ca03 Start reimplementation of ADD_TEAM
unc0rr
parents: 3543
diff changeset
   104
{-
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   106
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
   107
    | not $ isMaster client = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   108
    | 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
   109
    | otherwise =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   110
        [ModifyRoom $ modifyTeam team{hhnum = hhNumber},
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   111
        AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   112
    where
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)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   115
        hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   116
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   117
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   118
        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
   119
        canAddNumber = 48 - (sum . map hhnum $ teams room)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   120
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   121
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   122
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
   123
    | not $ isMaster client = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   124
    | noSuchTeam = []
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   125
    | otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor},
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   126
            AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   127
            ModifyClient2 (teamownerId team) (\c -> c{clientClan = newColor})]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   128
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   129
        noSuchTeam = isNothing findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   130
        team = fromJust findTeam
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   131
        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
   132
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   133
        room = rooms IntMap.! (roomID client)
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   134
-}
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   135
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   136
handleCmd_inRoom ["TOGGLE_READY"] = do
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   137
    cl <- thisClient
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   138
    chans <- roomClientsChans
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   139
    return [
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   140
        ModifyClient (\c -> c{isReady = not $ isReady cl}),
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   141
        ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady cl then -1 else 1)}),
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   142
        AnswerClients chans [if isReady cl then "NOT_READY" else "READY", nick cl]
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   143
        ]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   144
3543
d84a93b985c1 Reimplement TOGGLE_READY command
unc0rr
parents: 3540
diff changeset
   145
{-
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   146
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
   147
    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
   148
        if enoughClans then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   149
            [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   150
                    (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   151
                        gameinprogress = True,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   152
                        roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   153
                        leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   154
                        teamsAtStart = teams r}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   155
                    ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   156
            AnswerThisRoom ["RUN_GAME"]]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   157
        else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   158
            [Warning "Less than two clans!"]
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
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   164
        enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   165
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   166
2245
c011aecc95e5 unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents: 2207
diff changeset
   167
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
   168
    if (teamsInGame client > 0) && isLegal then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   169
        (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
   170
    else
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
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   173
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   174
        (isLegal, isKeepAlive) = checkNetCmd msg
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   175
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   176
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   177
    if isMaster client then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   178
        [ModifyRoom
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   179
                (\r -> r{
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   180
                    gameinprogress = False,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   181
                    readyPlayers = 0,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   182
                    roundMsgs = empty,
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   183
                    leftTeams = [],
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   184
                    teamsAtStart = []}
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   185
                ),
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   186
        UnreadyRoomClients
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   187
        ] ++ answerRemovedTeams
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   188
    else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   189
        []
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
        room = rooms IntMap.! (roomID client)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   193
        answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   194
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   195
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   196
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
   197
    | 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
   198
    | otherwise = [ProtocolError "Not room master"]
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   199
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   200
        client = clients IntMap.! clID
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   201
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   202
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2337
diff changeset
   203
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
   204
    | 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
   205
    | otherwise = [ProtocolError "Not room master"]
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
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   208
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   209
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
   210
    [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
   211
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   212
        client = clients IntMap.! clID
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   213
        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
   214
        noSuchClient = isNothing maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   215
        kickClient = fromJust maybeClient
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   216
        kickID = clientUID kickClient
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   217
1831
025348f05b9f Implement two more missing protocol commands
unc0rr
parents: 1818
diff changeset
   218
2403
6c5d504af2ba - Proper /team command implementation
unc0rr
parents: 2381
diff changeset
   219
handleCmd_inRoom clID clients _ ["TEAMCHAT", msg] =
2960
b467a681c5e0 - Fix spectators team chat
unc0rr
parents: 2952
diff changeset
   220
    [AnswerSameClan ["EM", engineMsg]]
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   221
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   222
        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
   223
        engineMsg = toEngineMsg $ 'b' : ((nick client) ++ "(team): " ++ msg ++ "\x20\x20")
3531
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
   224
-}
66c403badff6 Reimplement room creating
unC0Rr@gmail.com
parents: 3500
diff changeset
   225
handleCmd_inRoom _ = return [ProtocolError "Incorrect command (state: in room)"]