author | nemo |
Fri, 16 Oct 2009 12:58:21 +0000 | |
changeset 2481 | e0d89f88dea2 |
parent 2408 | 41ebdb5f1e6e |
child 2747 | 7889a3a9724f |
permissions | -rw-r--r-- |
1804 | 1 |
module HWProtoInRoomState where |
2 |
||
1879 | 3 |
import qualified Data.Foldable as Foldable |
1804 | 4 |
import qualified Data.IntMap as IntMap |
5 |
import qualified Data.Map as Map |
|
6 |
import Data.Sequence(Seq, (|>), (><), fromList, empty) |
|
7 |
import Data.List |
|
8 |
import Maybe |
|
2403 | 9 |
import qualified Codec.Binary.UTF8.String as UTF8 |
1804 | 10 |
-------------------------------------- |
11 |
import CoreTypes |
|
12 |
import Actions |
|
13 |
import Utils |
|
14 |
||
15 |
||
16 |
handleCmd_inRoom :: CmdHandler |
|
17 |
||
1815 | 18 |
handleCmd_inRoom clID clients _ ["CHAT", msg] = |
19 |
[AnswerOthersInRoom ["CHAT", clientNick, msg]] |
|
1804 | 20 |
where |
21 |
clientNick = nick $ clients IntMap.! clID |
|
22 |
||
1811 | 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] = |
aeea95909aba
Make server accpet TEAM_CHAT protocol command, and act like on CHAT command for now
unc0rr
parents:
2126
diff
changeset
|
25 |
[AnswerOthersInRoom ["TEAM_CHAT", clientNick, msg]] |
aeea95909aba
Make server accpet TEAM_CHAT protocol command, and act like on CHAT command for now
unc0rr
parents:
2126
diff
changeset
|
26 |
where |
aeea95909aba
Make server accpet TEAM_CHAT protocol command, and act like on CHAT command for now
unc0rr
parents:
2126
diff
changeset
|
27 |
clientNick = nick $ clients IntMap.! clID |
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 | 30 |
handleCmd_inRoom clID clients rooms ["PART"] = |
2352 | 31 |
[RoomRemoveThisClient "part"] |
1804 | 32 |
where |
33 |
client = clients IntMap.! clID |
|
34 |
||
1811 | 35 |
|
2352 | 36 |
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) |
2408 | 37 |
| null paramStrs = [ProtocolError "Empty config entry"] |
2352 | 38 |
| isMaster client = |
39 |
[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}), |
|
40 |
AnswerOthersInRoom ("CFG" : paramName : paramStrs)] |
|
41 |
| otherwise = [ProtocolError "Not room master"] |
|
1804 | 42 |
where |
43 |
client = clients IntMap.! clID |
|
44 |
||
45 |
handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo) |
|
2352 | 46 |
| length hhsInfo /= 16 = [] |
47 |
| length (teams room) == 6 = [Warning "too many teams"] |
|
48 |
| canAddNumber <= 0 = [Warning "too many hedgehogs"] |
|
49 |
| isJust findTeam = [Warning "There's already a team with same name in the list"] |
|
50 |
| gameinprogress room = [Warning "round in progress"] |
|
51 |
| isRestrictedTeams room = [Warning "restricted"] |
|
52 |
| otherwise = |
|
1804 | 53 |
[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}), |
2403 | 54 |
ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1, clientClan = color}), |
1804 | 55 |
AnswerThisClient ["TEAM_ACCEPTED", name], |
56 |
AnswerOthersInRoom $ teamToNet newTeam, |
|
57 |
AnswerOthersInRoom ["TEAM_COLOR", name, color] |
|
58 |
] |
|
59 |
where |
|
60 |
client = clients IntMap.! clID |
|
61 |
room = rooms IntMap.! (roomID client) |
|
62 |
canAddNumber = 48 - (sum . map hhnum $ teams room) |
|
63 |
findTeam = find (\t -> name == teamname t) $ teams room |
|
2403 | 64 |
newTeam = (TeamInfo clID (nick client) name color grave fort voicepack difficulty newTeamHHNum (hhsList hhsInfo)) |
1804 | 65 |
difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int) |
66 |
hhsList [] = [] |
|
67 |
hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs |
|
68 |
newTeamHHNum = min 4 canAddNumber |
|
69 |
||
70 |
||
2352 | 71 |
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] |
72 |
| noSuchTeam = [Warning "REMOVE_TEAM: no such team"] |
|
73 |
| nick client /= teamowner team = [ProtocolError "Not team owner!"] |
|
74 |
| otherwise = |
|
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
75 |
[RemoveTeam teamName, |
2352 | 76 |
ModifyClient (\c -> c{teamsInGame = teamsInGame c - 1}) |
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
77 |
] |
1804 | 78 |
where |
79 |
client = clients IntMap.! clID |
|
80 |
room = rooms IntMap.! (roomID client) |
|
81 |
noSuchTeam = isNothing findTeam |
|
82 |
team = fromJust findTeam |
|
83 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
|
84 |
||
85 |
||
2352 | 86 |
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] |
87 |
| not $ isMaster client = [ProtocolError "Not room master"] |
|
88 |
| hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) = [] |
|
89 |
| otherwise = |
|
90 |
[ModifyRoom $ modifyTeam team{hhnum = hhNumber}, |
|
91 |
AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]] |
|
1804 | 92 |
where |
93 |
client = clients IntMap.! clID |
|
94 |
room = rooms IntMap.! (roomID client) |
|
95 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int) |
|
96 |
noSuchTeam = isNothing findTeam |
|
97 |
team = fromJust findTeam |
|
98 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
|
99 |
canAddNumber = 48 - (sum . map hhnum $ teams room) |
|
100 |
||
101 |
||
2352 | 102 |
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] |
103 |
| not $ isMaster client = [ProtocolError "Not room master"] |
|
104 |
| noSuchTeam = [] |
|
105 |
| otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor}, |
|
2403 | 106 |
AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor], |
107 |
ModifyClient2 (teamownerId team) (\c -> c{clientClan = newColor})] |
|
1804 | 108 |
where |
109 |
noSuchTeam = isNothing findTeam |
|
110 |
team = fromJust findTeam |
|
111 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
|
112 |
client = clients IntMap.! clID |
|
113 |
room = rooms IntMap.! (roomID client) |
|
114 |
||
115 |
||
116 |
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] = |
|
117 |
[ModifyClient (\c -> c{isReady = not $ isReady client}), |
|
118 |
ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}), |
|
2352 | 119 |
AnswerThisRoom [if isReady client then "NOT_READY" else "READY", nick client]] |
1804 | 120 |
where |
121 |
client = clients IntMap.! clID |
|
122 |
||
123 |
||
124 |
handleCmd_inRoom clID clients rooms ["START_GAME"] = |
|
2352 | 125 |
if isMaster client && (playersIn room == readyPlayers room) && (not . gameinprogress) room then |
1804 | 126 |
if enoughClans then |
1811 | 127 |
[ModifyRoom |
128 |
(\r -> r{ |
|
129 |
gameinprogress = True, |
|
130 |
roundMsgs = empty, |
|
131 |
leftTeams = [], |
|
132 |
teamsAtStart = teams r} |
|
133 |
), |
|
1804 | 134 |
AnswerThisRoom ["RUN_GAME"]] |
135 |
else |
|
136 |
[Warning "Less than two clans!"] |
|
137 |
else |
|
138 |
[] |
|
139 |
where |
|
140 |
client = clients IntMap.! clID |
|
141 |
room = rooms IntMap.! (roomID client) |
|
142 |
enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room |
|
143 |
||
144 |
||
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
145 |
handleCmd_inRoom clID clients rooms ["EM", msg] = |
2381
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2352
diff
changeset
|
146 |
if (teamsInGame client > 0) && isLegal then |
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2352
diff
changeset
|
147 |
(AnswerOthersInRoom ["EM", msg]) : [ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}) | not isKeepAlive] |
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
148 |
else |
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
149 |
[] |
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
150 |
where |
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
151 |
client = clients IntMap.! clID |
2381
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2352
diff
changeset
|
152 |
(isLegal, isKeepAlive) = checkNetCmd msg |
1804 | 153 |
|
1811 | 154 |
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] = |
155 |
if isMaster client then |
|
156 |
[ModifyRoom |
|
157 |
(\r -> r{ |
|
158 |
gameinprogress = False, |
|
159 |
readyPlayers = 0, |
|
160 |
roundMsgs = empty, |
|
161 |
leftTeams = [], |
|
162 |
teamsAtStart = []} |
|
163 |
), |
|
164 |
UnreadyRoomClients |
|
165 |
] ++ answerRemovedTeams |
|
166 |
else |
|
167 |
[] |
|
168 |
where |
|
169 |
client = clients IntMap.! clID |
|
170 |
room = rooms IntMap.! (roomID client) |
|
171 |
answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room |
|
172 |
||
173 |
||
2352 | 174 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"] |
175 |
| isMaster client = [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})] |
|
176 |
| otherwise = [ProtocolError "Not room master"] |
|
1831 | 177 |
where |
178 |
client = clients IntMap.! clID |
|
179 |
||
180 |
||
2352 | 181 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"] |
182 |
| isMaster client = [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})] |
|
183 |
| otherwise = [ProtocolError "Not room master"] |
|
1831 | 184 |
where |
185 |
client = clients IntMap.! clID |
|
186 |
||
1879 | 187 |
handleCmd_inRoom clID clients rooms ["KICK", kickNick] = |
2352 | 188 |
[KickRoomClient kickID | isMaster client && not noSuchClient && (kickID /= clID) && (roomID client == roomID kickClient)] |
1879 | 189 |
where |
190 |
client = clients IntMap.! clID |
|
191 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients |
|
192 |
noSuchClient = isNothing maybeClient |
|
193 |
kickClient = fromJust maybeClient |
|
194 |
kickID = clientUID kickClient |
|
195 |
||
1831 | 196 |
|
2403 | 197 |
handleCmd_inRoom clID clients _ ["TEAMCHAT", msg] = |
198 |
if (teamsInGame client > 0) then |
|
199 |
[AnswerSameClan ["EM", engineMsg]] |
|
200 |
else |
|
201 |
[] |
|
202 |
where |
|
203 |
client = clients IntMap.! clID |
|
204 |
engineMsg = toEngineMsg $ 'b' : (nick client ++ "(team): " ++ decodedMsg ++ "\x20\x20") |
|
205 |
decodedMsg = UTF8.decodeString msg |
|
206 |
||
1804 | 207 |
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"] |