author | nemo |
Fri, 21 Jan 2011 17:07:42 -0500 | |
changeset 4865 | 3a48fbc4a281 |
parent 4681 | f2c30204a3fd |
child 4904 | 0eab727d4717 |
permissions | -rw-r--r-- |
1804 | 1 |
module HWProtoInRoomState where |
2 |
||
1879 | 3 |
import qualified Data.Foldable as Foldable |
4568 | 4 |
import qualified Data.IntMap as IntMap |
1804 | 5 |
import qualified Data.Map as Map |
6 |
import Data.Sequence(Seq, (|>), (><), fromList, empty) |
|
7 |
import Data.List |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
8 |
import Data.Maybe |
1804 | 9 |
-------------------------------------- |
10 |
import CoreTypes |
|
11 |
import Actions |
|
12 |
import Utils |
|
4568 | 13 |
|
1804 | 14 |
|
15 |
handleCmd_inRoom :: CmdHandler |
|
16 |
||
4568 | 17 |
handleCmd_inRoom clID clients _ ["CHAT", msg] = |
18 |
[AnswerOthersInRoom ["CHAT", clientNick, msg]] |
|
19 |
where |
|
20 |
clientNick = nick $ clients IntMap.! clID |
|
1804 | 21 |
|
4568 | 22 |
handleCmd_inRoom clID clients rooms ["PART"] = |
23 |
[RoomRemoveThisClient "part"] |
|
24 |
where |
|
25 |
client = clients IntMap.! clID |
|
3531 | 26 |
|
1811 | 27 |
|
4568 | 28 |
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) |
29 |
| null paramStrs = [ProtocolError "Empty config entry"] |
|
30 |
| isMaster client = |
|
31 |
[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}), |
|
32 |
AnswerOthersInRoom ("CFG" : paramName : paramStrs)] |
|
33 |
| otherwise = [ProtocolError "Not room master"] |
|
34 |
where |
|
35 |
client = clients IntMap.! clID |
|
1804 | 36 |
|
4568 | 37 |
handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : flag : difStr : hhsInfo) |
38 |
| length hhsInfo == 15 && clientProto client < 30 = handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : " " : flag : difStr : hhsInfo) |
|
39 |
| length hhsInfo /= 16 = [ProtocolError "Corrupted hedgehogs info"] |
|
4681 | 40 |
| length (teams room) == 8 = [Warning "too many teams"] |
4568 | 41 |
| canAddNumber <= 0 = [Warning "too many hedgehogs"] |
42 |
| isJust findTeam = [Warning "There's already a team with same name in the list"] |
|
43 |
| gameinprogress room = [Warning "round in progress"] |
|
44 |
| isRestrictedTeams room = [Warning "restricted"] |
|
45 |
| otherwise = |
|
46 |
[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}), |
|
47 |
ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1, clientClan = color}), |
|
48 |
AnswerThisClient ["TEAM_ACCEPTED", name], |
|
49 |
AnswerOthersInRoom $ teamToNet (clientProto client) newTeam, |
|
50 |
AnswerOthersInRoom ["TEAM_COLOR", name, color] |
|
51 |
] |
|
52 |
where |
|
53 |
client = clients IntMap.! clID |
|
54 |
room = rooms IntMap.! (roomID client) |
|
55 |
canAddNumber = 48 - (sum . map hhnum $ teams room) |
|
56 |
findTeam = find (\t -> name == teamname t) $ teams room |
|
57 |
newTeam = (TeamInfo clID (nick client) name color grave fort voicepack flag difficulty newTeamHHNum (hhsList hhsInfo)) |
|
58 |
difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int) |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
59 |
hhsList [] = [] |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
60 |
hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs |
4568 | 61 |
newTeamHHNum = min 4 canAddNumber |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
62 |
|
4568 | 63 |
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] |
64 |
| noSuchTeam = [Warning "REMOVE_TEAM: no such team"] |
|
65 |
| nick client /= teamowner team = [ProtocolError "Not team owner!"] |
|
66 |
| otherwise = |
|
67 |
[RemoveTeam teamName, |
|
68 |
ModifyClient (\c -> c{teamsInGame = teamsInGame c - 1, clientClan = if teamsInGame client == 1 then undefined else anotherTeamClan}) |
|
69 |
] |
|
4242 | 70 |
where |
4568 | 71 |
client = clients IntMap.! clID |
72 |
room = rooms IntMap.! (roomID client) |
|
73 |
noSuchTeam = isNothing findTeam |
|
74 |
team = fromJust findTeam |
|
75 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
|
76 |
anotherTeamClan = teamcolor $ fromJust $ find (\t -> teamownerId t == clID) $ teams room |
|
3561 | 77 |
|
3568 | 78 |
|
4568 | 79 |
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] |
80 |
| not $ isMaster client = [ProtocolError "Not room master"] |
|
81 |
| hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) = [] |
|
82 |
| otherwise = |
|
83 |
[ModifyRoom $ modifyTeam team{hhnum = hhNumber}, |
|
84 |
AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]] |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
85 |
where |
4568 | 86 |
client = clients IntMap.! clID |
87 |
room = rooms IntMap.! (roomID client) |
|
88 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int) |
|
89 |
noSuchTeam = isNothing findTeam |
|
90 |
team = fromJust findTeam |
|
91 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
|
92 |
canAddNumber = 48 - (sum . map hhnum $ teams room) |
|
1804 | 93 |
|
3568 | 94 |
|
4568 | 95 |
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] |
96 |
| not $ isMaster client = [ProtocolError "Not room master"] |
|
97 |
| noSuchTeam = [] |
|
98 |
| otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor}, |
|
99 |
AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor], |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
100 |
ModifyClient2 (teamownerId team) (\c -> c{clientClan = newColor})] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
101 |
where |
4568 | 102 |
noSuchTeam = isNothing findTeam |
103 |
team = fromJust findTeam |
|
104 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
|
105 |
client = clients IntMap.! clID |
|
106 |
room = rooms IntMap.! (roomID client) |
|
3568 | 107 |
|
1804 | 108 |
|
4568 | 109 |
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] = |
110 |
[ModifyClient (\c -> c{isReady = not $ isReady client}), |
|
111 |
ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}), |
|
112 |
AnswerThisRoom [if isReady client then "NOT_READY" else "READY", nick client]] |
|
113 |
where |
|
114 |
client = clients IntMap.! clID |
|
1804 | 115 |
|
3577 | 116 |
|
4568 | 117 |
handleCmd_inRoom clID clients rooms ["START_GAME"] = |
118 |
if isMaster client && (playersIn room == readyPlayers room) && (not . gameinprogress) room then |
|
119 |
if enoughClans then |
|
120 |
[ModifyRoom |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
121 |
(\r -> r{ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
122 |
gameinprogress = True, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
123 |
roundMsgs = empty, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
124 |
leftTeams = [], |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
125 |
teamsAtStart = teams r} |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
126 |
), |
4568 | 127 |
AnswerThisRoom ["RUN_GAME"]] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
128 |
else |
4568 | 129 |
[Warning "Less than two clans!"] |
130 |
else |
|
131 |
[] |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
132 |
where |
4568 | 133 |
client = clients IntMap.! clID |
134 |
room = rooms IntMap.! (roomID client) |
|
135 |
enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room |
|
1804 | 136 |
|
137 |
||
4568 | 138 |
handleCmd_inRoom clID clients rooms ["EM", msg] = |
139 |
if (teamsInGame client > 0) && isLegal then |
|
140 |
(AnswerOthersInRoom ["EM", msg]) : [ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}) | not isKeepAlive] |
|
141 |
else |
|
142 |
[] |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
143 |
where |
4568 | 144 |
client = clients IntMap.! clID |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
145 |
(isLegal, isKeepAlive) = checkNetCmd msg |
1804 | 146 |
|
4568 | 147 |
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] = |
148 |
if isMaster client then |
|
149 |
[ModifyRoom |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
150 |
(\r -> r{ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
151 |
gameinprogress = False, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
152 |
readyPlayers = 0, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
153 |
roundMsgs = empty, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
154 |
leftTeams = [], |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
155 |
teamsAtStart = []} |
4568 | 156 |
), |
157 |
UnreadyRoomClients |
|
158 |
] ++ answerRemovedTeams |
|
159 |
else |
|
160 |
[] |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
161 |
where |
4568 | 162 |
client = clients IntMap.! clID |
163 |
room = rooms IntMap.! (roomID client) |
|
164 |
answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room |
|
1811 | 165 |
|
166 |
||
4568 | 167 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"] |
168 |
| isMaster client = [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})] |
|
169 |
| otherwise = [ProtocolError "Not room master"] |
|
170 |
where |
|
171 |
client = clients IntMap.! clID |
|
172 |
||
1831 | 173 |
|
4568 | 174 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"] |
175 |
| isMaster client = [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})] |
|
176 |
| otherwise = [ProtocolError "Not room master"] |
|
177 |
where |
|
178 |
client = clients IntMap.! clID |
|
179 |
||
1879 | 180 |
handleCmd_inRoom clID clients rooms ["KICK", kickNick] = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
181 |
[KickRoomClient kickID | isMaster client && not noSuchClient && (kickID /= clID) && (roomID client == roomID kickClient)] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
182 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
183 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
184 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
185 |
noSuchClient = isNothing maybeClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
186 |
kickClient = fromJust maybeClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
187 |
kickID = clientUID kickClient |
1879 | 188 |
|
1831 | 189 |
|
2403 | 190 |
handleCmd_inRoom clID clients _ ["TEAMCHAT", msg] = |
2960 | 191 |
[AnswerSameClan ["EM", engineMsg]] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
192 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
193 |
client = clients IntMap.! clID |
2952
18fada739b55
- Convert strings from utf-8 on recieve, and back to utf-8 when send them
unc0rr
parents:
2928
diff
changeset
|
194 |
engineMsg = toEngineMsg $ 'b' : ((nick client) ++ "(team): " ++ msg ++ "\x20\x20") |
4568 | 195 |
|
196 |
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"] |