author | koda |
Thu, 09 Jul 2009 00:56:26 +0000 | |
changeset 2244 | 853a1db1cff6 |
parent 2207 | aeea95909aba |
child 2245 | c011aecc95e5 |
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"] = |
1804 | 30 |
if isMaster client then |
31 |
[RemoveRoom] |
|
32 |
else |
|
2126 | 33 |
[RoomRemoveThisClient "part"] |
1804 | 34 |
where |
35 |
client = clients IntMap.! clID |
|
36 |
||
1811 | 37 |
|
1804 | 38 |
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) = |
39 |
if isMaster client then |
|
40 |
[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}) |
|
41 |
, AnswerOthersInRoom ("CFG" : paramName : paramStrs)] |
|
42 |
else |
|
43 |
[ProtocolError "Not room master"] |
|
44 |
where |
|
45 |
client = clients IntMap.! clID |
|
46 |
||
47 |
handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo) |
|
48 |
| length hhsInfo == 16 = |
|
49 |
if length (teams room) == 6 then |
|
50 |
[Warning "too many teams"] |
|
51 |
else if canAddNumber <= 0 then |
|
52 |
[Warning "too many hedgehogs"] |
|
53 |
else if isJust findTeam then |
|
54 |
[Warning "already have a team with same name"] |
|
55 |
else if gameinprogress room then |
|
56 |
[Warning "round in progress"] |
|
57 |
else if isRestrictedTeams room then |
|
58 |
[Warning "restricted"] |
|
59 |
else |
|
60 |
[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}), |
|
61 |
AnswerThisClient ["TEAM_ACCEPTED", name], |
|
62 |
AnswerOthersInRoom $ teamToNet newTeam, |
|
63 |
AnswerOthersInRoom ["TEAM_COLOR", name, color] |
|
64 |
] |
|
65 |
where |
|
66 |
client = clients IntMap.! clID |
|
67 |
room = rooms IntMap.! (roomID client) |
|
68 |
canAddNumber = 48 - (sum . map hhnum $ teams room) |
|
69 |
findTeam = find (\t -> name == teamname t) $ teams room |
|
70 |
newTeam = (TeamInfo (nick client) name color grave fort voicepack difficulty newTeamHHNum (hhsList hhsInfo)) |
|
71 |
difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int) |
|
72 |
hhsList [] = [] |
|
73 |
hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs |
|
74 |
newTeamHHNum = min 4 canAddNumber |
|
75 |
||
76 |
||
77 |
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] = |
|
78 |
if noSuchTeam then |
|
79 |
[Warning "REMOVE_TEAM: no such team"] |
|
80 |
else |
|
81 |
if not $ nick client == teamowner team then |
|
82 |
[ProtocolError "Not team owner!"] |
|
83 |
else |
|
1813 | 84 |
[RemoveTeam teamName] |
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 |
||
1866 | 158 |
handleCmd_inRoom _ _ rooms ["EM", msg] = |
1804 | 159 |
[ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}), |
1866 | 160 |
AnswerOthersInRoom ["EM", msg]] |
1804 | 161 |
|
162 |
||
1811 | 163 |
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] = |
164 |
if isMaster client then |
|
165 |
[ModifyRoom |
|
166 |
(\r -> r{ |
|
167 |
gameinprogress = False, |
|
168 |
readyPlayers = 0, |
|
169 |
roundMsgs = empty, |
|
170 |
leftTeams = [], |
|
171 |
teamsAtStart = []} |
|
172 |
), |
|
173 |
UnreadyRoomClients |
|
174 |
] ++ answerRemovedTeams |
|
175 |
else |
|
176 |
[] |
|
177 |
where |
|
178 |
client = clients IntMap.! clID |
|
179 |
room = rooms IntMap.! (roomID client) |
|
180 |
answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room |
|
181 |
||
182 |
||
1831 | 183 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"] = |
184 |
if isMaster client then |
|
185 |
[ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})] |
|
186 |
else |
|
187 |
[ProtocolError "Not room master"] |
|
188 |
where |
|
189 |
client = clients IntMap.! clID |
|
190 |
||
191 |
||
192 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"] = |
|
193 |
if isMaster client then |
|
194 |
[ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})] |
|
195 |
else |
|
196 |
[ProtocolError "Not room master"] |
|
197 |
where |
|
198 |
client = clients IntMap.! clID |
|
199 |
||
1879 | 200 |
handleCmd_inRoom clID clients rooms ["KICK", kickNick] = |
201 |
if not $ isMaster client then |
|
202 |
[] |
|
203 |
else |
|
204 |
if noSuchClient then |
|
205 |
[] |
|
206 |
else |
|
207 |
if (kickID == clID) || (roomID client /= roomID kickClient) then |
|
208 |
[] |
|
209 |
else |
|
1929 | 210 |
[KickRoomClient kickID] |
1879 | 211 |
where |
212 |
client = clients IntMap.! clID |
|
213 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients |
|
214 |
noSuchClient = isNothing maybeClient |
|
215 |
kickClient = fromJust maybeClient |
|
216 |
kickID = clientUID kickClient |
|
217 |
||
1831 | 218 |
|
1804 | 219 |
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"] |