author | unc0rr |
Sun, 27 Jun 2010 21:06:41 +0400 | |
changeset 3566 | 772a46ef8288 |
parent 3564 | 7c583c88327b |
child 3568 | ae89cf0735dc |
permissions | -rw-r--r-- |
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
1 |
{-# LANGUAGE OverloadedStrings #-} |
1804 | 2 |
module HWProtoInRoomState where |
3 |
||
1879 | 4 |
import qualified Data.Foldable as Foldable |
1804 | 5 |
import qualified Data.Map as Map |
6 |
import Data.Sequence(Seq, (|>), (><), fromList, empty) |
|
7 |
import Data.List |
|
3555 | 8 |
import Data.Maybe |
3531 | 9 |
import qualified Data.ByteString.Char8 as B |
3555 | 10 |
import Control.Monad |
11 |
import Control.Monad.Reader |
|
1804 | 12 |
-------------------------------------- |
13 |
import CoreTypes |
|
14 |
import Actions |
|
15 |
import Utils |
|
3435 | 16 |
import HandlerUtils |
3555 | 17 |
import RoomsAndClients |
1804 | 18 |
|
19 |
handleCmd_inRoom :: CmdHandler |
|
20 |
||
3435 | 21 |
handleCmd_inRoom ["CHAT", msg] = do |
22 |
n <- clientNick |
|
23 |
s <- roomOthersChans |
|
24 |
return [AnswerClients s ["CHAT", n, msg]] |
|
1804 | 25 |
|
3531 | 26 |
handleCmd_inRoom ["PART"] = return [MoveToLobby "part"] |
27 |
handleCmd_inRoom ["PART", msg] = return [MoveToLobby $ "part: " `B.append` msg] |
|
28 |
||
1811 | 29 |
|
3540 | 30 |
handleCmd_inRoom ("CFG" : paramName : paramStrs) |
31 |
| null paramStrs = return [ProtocolError "Empty config entry"] |
|
32 |
| otherwise = do |
|
33 |
chans <- roomOthersChans |
|
34 |
cl <- thisClient |
|
35 |
if isMaster cl then |
|
36 |
return [ |
|
37 |
ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}), |
|
38 |
AnswerClients chans ("CFG" : paramName : paramStrs)] |
|
39 |
else |
|
40 |
return [ProtocolError "Not room master"] |
|
1804 | 41 |
|
3544 | 42 |
handleCmd_inRoom ("ADD_TEAM" : name : color : grave : fort : voicepack : flag : difStr : hhsInfo) |
43 |
| length hhsInfo /= 16 = return [ProtocolError "Corrupted hedgehogs info"] |
|
3555 | 44 |
| otherwise = do |
45 |
(ci, rnc) <- ask |
|
46 |
let r = room rnc $ clientRoom rnc ci |
|
47 |
clNick <- clientNick |
|
48 |
clChan <- thisClientChans |
|
49 |
othersChans <- roomOthersChans |
|
50 |
return $ |
|
3566 | 51 |
if not . null . drop 5 $ teams r then |
3555 | 52 |
[Warning "too many teams"] |
53 |
else if canAddNumber r <= 0 then |
|
54 |
[Warning "too many hedgehogs"] |
|
55 |
else if isJust $ findTeam r then |
|
56 |
[Warning "There's already a team with same name in the list"] |
|
57 |
else if gameinprogress r then |
|
58 |
[Warning "round in progress"] |
|
59 |
else if isRestrictedTeams r then |
|
60 |
[Warning "restricted"] |
|
61 |
else |
|
62 |
[ModifyRoom (\r -> r{teams = teams r ++ [newTeam ci clNick r]}), |
|
63 |
ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1, clientClan = color}), |
|
64 |
AnswerClients clChan ["TEAM_ACCEPTED", name], |
|
65 |
AnswerClients othersChans $ teamToNet $ newTeam ci clNick r, |
|
66 |
AnswerClients othersChans ["TEAM_COLOR", name, color] |
|
67 |
] |
|
68 |
where |
|
69 |
canAddNumber r = 48 - (sum . map hhnum $ teams r) |
|
70 |
findTeam = find (\t -> name == teamname t) . teams |
|
71 |
newTeam ci clNick r = (TeamInfo ci clNick name color grave fort voicepack flag difficulty (newTeamHHNum r) (hhsList hhsInfo)) |
|
72 |
difficulty = case B.readInt difStr of |
|
73 |
Just (i, t) | B.null t -> fromIntegral i |
|
74 |
otherwise -> 0 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
75 |
hhsList [] = [] |
3566 | 76 |
hhsList [_] = error "Hedgehogs list with odd elements number" |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
77 |
hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs |
3555 | 78 |
newTeamHHNum r = min 4 (canAddNumber r) |
3561 | 79 |
|
80 |
handleCmd_inRoom ["REMOVE_TEAM", name] = do |
|
81 |
(ci, rnc) <- ask |
|
82 |
let r = room rnc $ clientRoom rnc ci |
|
3564 | 83 |
clNick <- clientNick |
84 |
||
85 |
let maybeTeam = findTeam r |
|
86 |
let team = fromJust maybeTeam |
|
87 |
||
88 |
return $ |
|
89 |
if isNothing $ findTeam r then |
|
90 |
[Warning "REMOVE_TEAM: no such team"] |
|
91 |
else if clNick /= teamowner team then |
|
92 |
[ProtocolError "Not team owner!"] |
|
93 |
else |
|
94 |
[RemoveTeam name, |
|
95 |
ModifyClient |
|
96 |
(\c -> c{ |
|
97 |
teamsInGame = teamsInGame c - 1, |
|
98 |
clientClan = if teamsInGame c == 1 then undefined else anotherTeamClan ci r |
|
99 |
}) |
|
100 |
] |
|
3561 | 101 |
where |
102 |
anotherTeamClan ci = teamcolor . fromJust . find (\t -> teamownerId t == ci) . teams |
|
3564 | 103 |
findTeam = find (\t -> name == teamname t) . teams |
3561 | 104 |
|
3544 | 105 |
{- |
1804 | 106 |
|
2352 | 107 |
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
108 |
| not $ isMaster client = [ProtocolError "Not room master"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
109 |
| hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) = [] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
110 |
| otherwise = |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
111 |
[ModifyRoom $ modifyTeam team{hhnum = hhNumber}, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
112 |
AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
113 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
114 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
115 |
room = rooms IntMap.! (roomID client) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
116 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
117 |
noSuchTeam = isNothing findTeam |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
118 |
team = fromJust findTeam |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
119 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
120 |
canAddNumber = 48 - (sum . map hhnum $ teams room) |
1804 | 121 |
|
122 |
||
2352 | 123 |
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
124 |
| not $ isMaster client = [ProtocolError "Not room master"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
125 |
| noSuchTeam = [] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
126 |
| otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor}, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
127 |
AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor], |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
128 |
ModifyClient2 (teamownerId team) (\c -> c{clientClan = newColor})] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
129 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
130 |
noSuchTeam = isNothing findTeam |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
131 |
team = fromJust findTeam |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
132 |
findTeam = find (\t -> teamName == teamname t) $ teams room |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
133 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
134 |
room = rooms IntMap.! (roomID client) |
3543 | 135 |
-} |
1804 | 136 |
|
3543 | 137 |
handleCmd_inRoom ["TOGGLE_READY"] = do |
138 |
cl <- thisClient |
|
139 |
chans <- roomClientsChans |
|
140 |
return [ |
|
141 |
ModifyClient (\c -> c{isReady = not $ isReady cl}), |
|
142 |
ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady cl then -1 else 1)}), |
|
143 |
AnswerClients chans [if isReady cl then "NOT_READY" else "READY", nick cl] |
|
144 |
] |
|
1804 | 145 |
|
3543 | 146 |
{- |
1804 | 147 |
handleCmd_inRoom clID clients rooms ["START_GAME"] = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
148 |
if isMaster client && (playersIn room == readyPlayers room) && (not . gameinprogress) room then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
149 |
if enoughClans then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
150 |
[ModifyRoom |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
151 |
(\r -> r{ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
152 |
gameinprogress = True, |
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 = teams r} |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
156 |
), |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
157 |
AnswerThisRoom ["RUN_GAME"]] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
158 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
159 |
[Warning "Less than two clans!"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
160 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
161 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
162 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
163 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
164 |
room = rooms IntMap.! (roomID client) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
165 |
enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room |
1804 | 166 |
|
167 |
||
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2207
diff
changeset
|
168 |
handleCmd_inRoom clID clients rooms ["EM", msg] = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
169 |
if (teamsInGame client > 0) && isLegal then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
170 |
(AnswerOthersInRoom ["EM", msg]) : [ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}) | not isKeepAlive] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
171 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
172 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
173 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
174 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
175 |
(isLegal, isKeepAlive) = checkNetCmd msg |
1804 | 176 |
|
1811 | 177 |
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
178 |
if isMaster client then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
179 |
[ModifyRoom |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
180 |
(\r -> r{ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
181 |
gameinprogress = False, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
182 |
readyPlayers = 0, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
183 |
roundMsgs = empty, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
184 |
leftTeams = [], |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
185 |
teamsAtStart = []} |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
186 |
), |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
187 |
UnreadyRoomClients |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
188 |
] ++ answerRemovedTeams |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
189 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
190 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
191 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
192 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
193 |
room = rooms IntMap.! (roomID client) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
194 |
answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room |
1811 | 195 |
|
196 |
||
2352 | 197 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
198 |
| isMaster client = [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
199 |
| otherwise = [ProtocolError "Not room master"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
200 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
201 |
client = clients IntMap.! clID |
1831 | 202 |
|
203 |
||
2352 | 204 |
handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
205 |
| isMaster client = [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
206 |
| otherwise = [ProtocolError "Not room master"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
207 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
208 |
client = clients IntMap.! clID |
1831 | 209 |
|
1879 | 210 |
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
|
211 |
[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
|
212 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
213 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
214 |
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
|
215 |
noSuchClient = isNothing maybeClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
216 |
kickClient = fromJust maybeClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
217 |
kickID = clientUID kickClient |
1879 | 218 |
|
1831 | 219 |
|
2403 | 220 |
handleCmd_inRoom clID clients _ ["TEAMCHAT", msg] = |
2960 | 221 |
[AnswerSameClan ["EM", engineMsg]] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
222 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
223 |
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
|
224 |
engineMsg = toEngineMsg $ 'b' : ((nick client) ++ "(team): " ++ msg ++ "\x20\x20") |
3531 | 225 |
-} |
226 |
handleCmd_inRoom _ = return [ProtocolError "Incorrect command (state: in room)"] |