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 |
|
1814
|
22 |
handleCmd_inRoom clID clients rooms ["PART"] =
|
1804
|
23 |
if isMaster client then
|
|
24 |
[RemoveRoom]
|
|
25 |
else
|
1814
|
26 |
RoomRemoveThisClient
|
|
27 |
: removeClientTeams
|
1804
|
28 |
where
|
|
29 |
client = clients IntMap.! clID
|
1814
|
30 |
room = rooms IntMap.! (roomID client)
|
|
31 |
clientTeams = filter (\t -> teamowner t == nick client) $ teams room
|
|
32 |
removeClientTeams = map (RemoveTeam . teamname) clientTeams
|
1804
|
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]}),
|
|
58 |
AnswerThisClient ["TEAM_ACCEPTED", name],
|
|
59 |
AnswerOthersInRoom $ teamToNet newTeam,
|
|
60 |
AnswerOthersInRoom ["TEAM_COLOR", name, color]
|
|
61 |
]
|
|
62 |
where
|
|
63 |
client = clients IntMap.! clID
|
|
64 |
room = rooms IntMap.! (roomID client)
|
|
65 |
canAddNumber = 48 - (sum . map hhnum $ teams room)
|
|
66 |
findTeam = find (\t -> name == teamname t) $ teams room
|
|
67 |
newTeam = (TeamInfo (nick client) name color grave fort voicepack difficulty newTeamHHNum (hhsList hhsInfo))
|
|
68 |
difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int)
|
|
69 |
hhsList [] = []
|
|
70 |
hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
|
|
71 |
newTeamHHNum = min 4 canAddNumber
|
|
72 |
|
|
73 |
|
|
74 |
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] =
|
|
75 |
if noSuchTeam then
|
|
76 |
[Warning "REMOVE_TEAM: no such team"]
|
|
77 |
else
|
|
78 |
if not $ nick client == teamowner team then
|
|
79 |
[ProtocolError "Not team owner!"]
|
|
80 |
else
|
1813
|
81 |
[RemoveTeam teamName]
|
1804
|
82 |
where
|
|
83 |
client = clients IntMap.! clID
|
|
84 |
room = rooms IntMap.! (roomID client)
|
|
85 |
noSuchTeam = isNothing findTeam
|
|
86 |
team = fromJust findTeam
|
|
87 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
88 |
|
|
89 |
|
|
90 |
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] =
|
|
91 |
if not $ isMaster client then
|
|
92 |
[ProtocolError "Not room master"]
|
|
93 |
else
|
|
94 |
if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then
|
|
95 |
[]
|
|
96 |
else
|
|
97 |
[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
|
|
98 |
AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
|
|
99 |
where
|
|
100 |
client = clients IntMap.! clID
|
|
101 |
room = rooms IntMap.! (roomID client)
|
|
102 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
|
|
103 |
noSuchTeam = isNothing findTeam
|
|
104 |
team = fromJust findTeam
|
|
105 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
106 |
canAddNumber = 48 - (sum . map hhnum $ teams room)
|
|
107 |
|
|
108 |
|
|
109 |
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] =
|
|
110 |
if not $ isMaster client then
|
|
111 |
[ProtocolError "Not room master"]
|
|
112 |
else
|
|
113 |
if noSuchTeam then
|
|
114 |
[]
|
|
115 |
else
|
|
116 |
[ModifyRoom $ modifyTeam team{teamcolor = newColor},
|
|
117 |
AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
|
|
118 |
where
|
|
119 |
noSuchTeam = isNothing findTeam
|
|
120 |
team = fromJust findTeam
|
|
121 |
findTeam = find (\t -> teamName == teamname t) $ teams room
|
|
122 |
client = clients IntMap.! clID
|
|
123 |
room = rooms IntMap.! (roomID client)
|
|
124 |
|
|
125 |
|
|
126 |
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
|
|
127 |
[ModifyClient (\c -> c{isReady = not $ isReady client}),
|
|
128 |
ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
|
|
129 |
AnswerThisRoom $ [if isReady client then "NOT_READY" else "READY", nick client]]
|
|
130 |
where
|
|
131 |
client = clients IntMap.! clID
|
|
132 |
|
|
133 |
|
|
134 |
handleCmd_inRoom clID clients rooms ["START_GAME"] =
|
|
135 |
if isMaster client && (playersIn room == readyPlayers room) && (not $ gameinprogress room) then
|
|
136 |
if enoughClans then
|
1811
|
137 |
[ModifyRoom
|
|
138 |
(\r -> r{
|
|
139 |
gameinprogress = True,
|
|
140 |
roundMsgs = empty,
|
|
141 |
leftTeams = [],
|
|
142 |
teamsAtStart = teams r}
|
|
143 |
),
|
1804
|
144 |
AnswerThisRoom ["RUN_GAME"]]
|
|
145 |
else
|
|
146 |
[Warning "Less than two clans!"]
|
|
147 |
else
|
|
148 |
[]
|
|
149 |
where
|
|
150 |
client = clients IntMap.! clID
|
|
151 |
room = rooms IntMap.! (roomID client)
|
|
152 |
enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
|
|
153 |
|
|
154 |
|
1811
|
155 |
handleCmd_inRoom _ _ rooms ["GAMEMSG", msg] =
|
1804
|
156 |
[ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}),
|
|
157 |
AnswerOthersInRoom ["GAMEMSG", msg]]
|
|
158 |
|
|
159 |
|
1811
|
160 |
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
|
|
161 |
if isMaster client then
|
|
162 |
[ModifyRoom
|
|
163 |
(\r -> r{
|
|
164 |
gameinprogress = False,
|
|
165 |
readyPlayers = 0,
|
|
166 |
roundMsgs = empty,
|
|
167 |
leftTeams = [],
|
|
168 |
teamsAtStart = []}
|
|
169 |
),
|
|
170 |
UnreadyRoomClients
|
|
171 |
] ++ answerRemovedTeams
|
|
172 |
else
|
|
173 |
[]
|
|
174 |
where
|
|
175 |
client = clients IntMap.! clID
|
|
176 |
room = rooms IntMap.! (roomID client)
|
|
177 |
answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
|
|
178 |
|
|
179 |
|
1804
|
180 |
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"]
|