1804
|
1 |
module HWProtoInRoomState where
|
|
2 |
|
|
3 |
import qualified Data.IntMap as IntMap
|
|
4 |
import qualified Data.Map as Map
|
|
5 |
import Data.Sequence(Seq, (|>), (><), fromList, empty)
|
|
6 |
import Data.List
|
|
7 |
import Maybe
|
|
8 |
--------------------------------------
|
|
9 |
import CoreTypes
|
|
10 |
import Actions
|
|
11 |
import Utils
|
|
12 |
|
|
13 |
|
|
14 |
handleCmd_inRoom :: CmdHandler
|
|
15 |
|
|
16 |
handleCmd_inRoom clID clients _ ["CHAT_STRING", msg] =
|
|
17 |
[AnswerOthersInRoom ["CHAT_STRING", clientNick, msg]]
|
|
18 |
where
|
|
19 |
clientNick = nick $ clients IntMap.! clID
|
|
20 |
|
1811
|
21 |
|
1804
|
22 |
handleCmd_inRoom clID clients _ ["PART"] =
|
|
23 |
if isMaster client then
|
|
24 |
[RemoveRoom]
|
|
25 |
else
|
|
26 |
[RoomRemoveThisClient]
|
|
27 |
where
|
|
28 |
client = clients IntMap.! clID
|
|
29 |
|
1811
|
30 |
|
1804
|
31 |
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) =
|
|
32 |
if isMaster client then
|
|
33 |
[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)})
|
|
34 |
, AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
|
|
35 |
else
|
|
36 |
[ProtocolError "Not room master"]
|
|
37 |
where
|
|
38 |
client = clients IntMap.! clID
|
|
39 |
|
|
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
|
|
78 |
if not $ gameinprogress room then
|
|
79 |
[ModifyRoom (\r -> r{teams = filter (\t -> teamName /= teamname t) $ teams r}),
|
|
80 |
AnswerOthersInRoom ["REMOVE_TEAM", teamName]]
|
|
81 |
else
|
|
82 |
[]
|
|
83 |
{- else
|
|
84 |
(noChangeClients,
|
|
85 |
modifyRoom clRoom{
|
|
86 |
teams = filter (\t -> teamName /= teamname t) $ teams clRoom,
|
|
87 |
leftTeams = teamName : leftTeams clRoom,
|
|
88 |
roundMsgs = roundMsgs clRoom |> rmTeamMsg
|
|
89 |
},
|
|
90 |
answerOthersRoom ["GAMEMSG", rmTeamMsg]) -}
|
|
91 |
where
|
|
92 |
client = clients IntMap.! clID
|
|
93 |
room = rooms IntMap.! (roomID client)
|
|
94 |
noSuchTeam = isNothing findTeam
|
|
95 |
team = fromJust findTeam
|
|
96 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
97 |
rmTeamMsg = toEngineMsg $ 'F' : teamName
|
|
98 |
|
|
99 |
|
|
100 |
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] =
|
|
101 |
if not $ isMaster client then
|
|
102 |
[ProtocolError "Not room master"]
|
|
103 |
else
|
|
104 |
if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then
|
|
105 |
[]
|
|
106 |
else
|
|
107 |
[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
|
|
108 |
AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
|
|
109 |
where
|
|
110 |
client = clients IntMap.! clID
|
|
111 |
room = rooms IntMap.! (roomID client)
|
|
112 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
|
|
113 |
noSuchTeam = isNothing findTeam
|
|
114 |
team = fromJust findTeam
|
|
115 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
116 |
canAddNumber = 48 - (sum . map hhnum $ teams room)
|
|
117 |
|
|
118 |
|
|
119 |
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] =
|
|
120 |
if not $ isMaster client then
|
|
121 |
[ProtocolError "Not room master"]
|
|
122 |
else
|
|
123 |
if noSuchTeam then
|
|
124 |
[]
|
|
125 |
else
|
|
126 |
[ModifyRoom $ modifyTeam team{teamcolor = newColor},
|
|
127 |
AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
|
|
128 |
where
|
|
129 |
noSuchTeam = isNothing findTeam
|
|
130 |
team = fromJust findTeam
|
|
131 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
132 |
client = clients IntMap.! clID
|
|
133 |
room = rooms IntMap.! (roomID client)
|
|
134 |
|
|
135 |
|
|
136 |
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
|
|
137 |
[ModifyClient (\c -> c{isReady = not $ isReady client}),
|
|
138 |
ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
|
|
139 |
AnswerThisRoom $ [if isReady client then "NOT_READY" else "READY", nick client]]
|
|
140 |
where
|
|
141 |
client = clients IntMap.! clID
|
|
142 |
|
|
143 |
|
|
144 |
handleCmd_inRoom clID clients rooms ["START_GAME"] =
|
|
145 |
if isMaster client && (playersIn room == readyPlayers room) && (not $ gameinprogress room) then
|
|
146 |
if enoughClans then
|
1811
|
147 |
[ModifyRoom
|
|
148 |
(\r -> r{
|
|
149 |
gameinprogress = True,
|
|
150 |
roundMsgs = empty,
|
|
151 |
leftTeams = [],
|
|
152 |
teamsAtStart = teams r}
|
|
153 |
),
|
1804
|
154 |
AnswerThisRoom ["RUN_GAME"]]
|
|
155 |
else
|
|
156 |
[Warning "Less than two clans!"]
|
|
157 |
else
|
|
158 |
[]
|
|
159 |
where
|
|
160 |
client = clients IntMap.! clID
|
|
161 |
room = rooms IntMap.! (roomID client)
|
|
162 |
enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
|
|
163 |
|
|
164 |
|
1811
|
165 |
handleCmd_inRoom _ _ rooms ["GAMEMSG", msg] =
|
1804
|
166 |
[ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}),
|
|
167 |
AnswerOthersInRoom ["GAMEMSG", msg]]
|
|
168 |
|
|
169 |
|
1811
|
170 |
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
|
|
171 |
if isMaster client then
|
|
172 |
[ModifyRoom
|
|
173 |
(\r -> r{
|
|
174 |
gameinprogress = False,
|
|
175 |
readyPlayers = 0,
|
|
176 |
roundMsgs = empty,
|
|
177 |
leftTeams = [],
|
|
178 |
teamsAtStart = []}
|
|
179 |
),
|
|
180 |
UnreadyRoomClients
|
|
181 |
] ++ answerRemovedTeams
|
|
182 |
else
|
|
183 |
[]
|
|
184 |
where
|
|
185 |
client = clients IntMap.! clID
|
|
186 |
room = rooms IntMap.! (roomID client)
|
|
187 |
answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
|
|
188 |
|
|
189 |
|
1804
|
190 |
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"]
|