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 |
|
1814
|
23 |
handleCmd_inRoom clID clients rooms ["PART"] =
|
1804
|
24 |
if isMaster client then
|
|
25 |
[RemoveRoom]
|
|
26 |
else
|
1818
|
27 |
[RoomRemoveThisClient]
|
1804
|
28 |
where
|
|
29 |
client = clients IntMap.! clID
|
|
30 |
|
1811
|
31 |
|
1804
|
32 |
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) =
|
|
33 |
if isMaster client then
|
|
34 |
[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)})
|
|
35 |
, AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
|
|
36 |
else
|
|
37 |
[ProtocolError "Not room master"]
|
|
38 |
where
|
|
39 |
client = clients IntMap.! clID
|
|
40 |
|
|
41 |
handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo)
|
|
42 |
| length hhsInfo == 16 =
|
|
43 |
if length (teams room) == 6 then
|
|
44 |
[Warning "too many teams"]
|
|
45 |
else if canAddNumber <= 0 then
|
|
46 |
[Warning "too many hedgehogs"]
|
|
47 |
else if isJust findTeam then
|
|
48 |
[Warning "already have a team with same name"]
|
|
49 |
else if gameinprogress room then
|
|
50 |
[Warning "round in progress"]
|
|
51 |
else if isRestrictedTeams room then
|
|
52 |
[Warning "restricted"]
|
|
53 |
else
|
|
54 |
[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}),
|
|
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
|
|
64 |
newTeam = (TeamInfo (nick client) name color grave fort voicepack difficulty newTeamHHNum (hhsList hhsInfo))
|
|
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 |
|
|
71 |
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] =
|
|
72 |
if noSuchTeam then
|
|
73 |
[Warning "REMOVE_TEAM: no such team"]
|
|
74 |
else
|
|
75 |
if not $ nick client == teamowner team then
|
|
76 |
[ProtocolError "Not team owner!"]
|
|
77 |
else
|
1813
|
78 |
[RemoveTeam teamName]
|
1804
|
79 |
where
|
|
80 |
client = clients IntMap.! clID
|
|
81 |
room = rooms IntMap.! (roomID client)
|
|
82 |
noSuchTeam = isNothing findTeam
|
|
83 |
team = fromJust findTeam
|
|
84 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
85 |
|
|
86 |
|
|
87 |
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] =
|
|
88 |
if not $ isMaster client then
|
|
89 |
[ProtocolError "Not room master"]
|
|
90 |
else
|
|
91 |
if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then
|
|
92 |
[]
|
|
93 |
else
|
|
94 |
[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
|
|
95 |
AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
|
|
96 |
where
|
|
97 |
client = clients IntMap.! clID
|
|
98 |
room = rooms IntMap.! (roomID client)
|
|
99 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
|
|
100 |
noSuchTeam = isNothing findTeam
|
|
101 |
team = fromJust findTeam
|
|
102 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
103 |
canAddNumber = 48 - (sum . map hhnum $ teams room)
|
|
104 |
|
|
105 |
|
|
106 |
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] =
|
|
107 |
if not $ isMaster client then
|
|
108 |
[ProtocolError "Not room master"]
|
|
109 |
else
|
|
110 |
if noSuchTeam then
|
|
111 |
[]
|
|
112 |
else
|
|
113 |
[ModifyRoom $ modifyTeam team{teamcolor = newColor},
|
|
114 |
AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
|
|
115 |
where
|
|
116 |
noSuchTeam = isNothing findTeam
|
|
117 |
team = fromJust findTeam
|
|
118 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
119 |
client = clients IntMap.! clID
|
|
120 |
room = rooms IntMap.! (roomID client)
|
|
121 |
|
|
122 |
|
|
123 |
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
|
|
124 |
[ModifyClient (\c -> c{isReady = not $ isReady client}),
|
|
125 |
ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
|
|
126 |
AnswerThisRoom $ [if isReady client then "NOT_READY" else "READY", nick client]]
|
|
127 |
where
|
|
128 |
client = clients IntMap.! clID
|
|
129 |
|
|
130 |
|
|
131 |
handleCmd_inRoom clID clients rooms ["START_GAME"] =
|
|
132 |
if isMaster client && (playersIn room == readyPlayers room) && (not $ gameinprogress room) then
|
|
133 |
if enoughClans then
|
1811
|
134 |
[ModifyRoom
|
|
135 |
(\r -> r{
|
|
136 |
gameinprogress = True,
|
|
137 |
roundMsgs = empty,
|
|
138 |
leftTeams = [],
|
|
139 |
teamsAtStart = teams r}
|
|
140 |
),
|
1804
|
141 |
AnswerThisRoom ["RUN_GAME"]]
|
|
142 |
else
|
|
143 |
[Warning "Less than two clans!"]
|
|
144 |
else
|
|
145 |
[]
|
|
146 |
where
|
|
147 |
client = clients IntMap.! clID
|
|
148 |
room = rooms IntMap.! (roomID client)
|
|
149 |
enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
|
|
150 |
|
|
151 |
|
1866
|
152 |
handleCmd_inRoom _ _ rooms ["EM", msg] =
|
1804
|
153 |
[ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}),
|
1866
|
154 |
AnswerOthersInRoom ["EM", msg]]
|
1804
|
155 |
|
|
156 |
|
1811
|
157 |
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
|
|
158 |
if isMaster client then
|
|
159 |
[ModifyRoom
|
|
160 |
(\r -> r{
|
|
161 |
gameinprogress = False,
|
|
162 |
readyPlayers = 0,
|
|
163 |
roundMsgs = empty,
|
|
164 |
leftTeams = [],
|
|
165 |
teamsAtStart = []}
|
|
166 |
),
|
|
167 |
UnreadyRoomClients
|
|
168 |
] ++ answerRemovedTeams
|
|
169 |
else
|
|
170 |
[]
|
|
171 |
where
|
|
172 |
client = clients IntMap.! clID
|
|
173 |
room = rooms IntMap.! (roomID client)
|
|
174 |
answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
|
|
175 |
|
|
176 |
|
1831
|
177 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"] =
|
|
178 |
if isMaster client then
|
|
179 |
[ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
|
|
180 |
else
|
|
181 |
[ProtocolError "Not room master"]
|
|
182 |
where
|
|
183 |
client = clients IntMap.! clID
|
|
184 |
|
|
185 |
|
|
186 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"] =
|
|
187 |
if isMaster client then
|
|
188 |
[ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
|
|
189 |
else
|
|
190 |
[ProtocolError "Not room master"]
|
|
191 |
where
|
|
192 |
client = clients IntMap.! clID
|
|
193 |
|
1879
|
194 |
handleCmd_inRoom clID clients rooms ["KICK", kickNick] =
|
|
195 |
if not $ isMaster client then
|
|
196 |
[]
|
|
197 |
else
|
|
198 |
if noSuchClient then
|
|
199 |
[]
|
|
200 |
else
|
|
201 |
if (kickID == clID) || (roomID client /= roomID kickClient) then
|
|
202 |
[]
|
|
203 |
else
|
1929
|
204 |
[KickRoomClient kickID]
|
1879
|
205 |
where
|
|
206 |
client = clients IntMap.! clID
|
|
207 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
|
|
208 |
noSuchClient = isNothing maybeClient
|
|
209 |
kickClient = fromJust maybeClient
|
|
210 |
kickID = clientUID kickClient
|
|
211 |
|
1831
|
212 |
|
1804
|
213 |
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"]
|