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