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