author | unc0rr |
Tue, 04 Nov 2008 21:53:30 +0000 | |
changeset 1473 | 60e1fad78d58 |
parent 1470 | ebaca3b66d92 |
child 1478 | 8bfb417d165e |
permissions | -rw-r--r-- |
1473 | 1 |
module HWProto |
2 |
( |
|
3 |
handleCmd |
|
4 |
) where |
|
890 | 5 |
|
6 |
import IO |
|
896 | 7 |
import Data.List |
894 | 8 |
import Data.Word |
890 | 9 |
import Miscutils |
1320 | 10 |
import Maybe |
1317 | 11 |
import qualified Data.Map as Map |
1384 | 12 |
import Opts |
890 | 13 |
|
1331
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
14 |
teamToNet team = ["ADD_TEAM", teamname team, teamgrave team, teamfort team, show $ difficulty team] ++ hhsInfo |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
15 |
where |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
16 |
hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
17 |
|
1452 | 18 |
answerServerMessage clients = [(clientOnly, "SERVER_MESSAGE" : [mainbody ++ clientsIn])] |
1384 | 19 |
where |
1452 | 20 |
mainbody = serverMessage globalOptions ++ if isDedicated globalOptions then "<p align=center>Dedicated server</p>" else "<p align=center>Private server</p>" |
21 |
clientsIn = "<p align=left>" ++ (show $ length nicks) ++ " clients in: " ++ clientslist ++ "</p>" |
|
22 |
clientslist = if not $ null nicks then foldr1 (\a b -> a ++ ", " ++ b) nicks else "" |
|
23 |
nicks = filter (not . null) $ map nick clients |
|
24 |
||
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
25 |
answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])] |
1317 | 26 |
answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])] |
1327 | 27 |
answerBadParam = [(clientOnly, ["ERROR", "Bad parameter"])] |
1381 | 28 |
answerQuit = [(clientOnly, ["BYE"])] |
1327 | 29 |
answerAbandoned = [(othersInRoom, ["BYE"])] |
1309 | 30 |
answerQuitInform nick = [(othersInRoom, ["LEFT", nick])] |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
31 |
answerNickChosen = [(clientOnly, ["ERROR", "The nick already chosen"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
32 |
answerNickChooseAnother = [(clientOnly, ["WARNING", "Choose another nick"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
33 |
answerNick nick = [(clientOnly, ["NICK", nick])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
34 |
answerProtocolKnown = [(clientOnly, ["ERROR", "Protocol number already known"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
35 |
answerBadInput = [(clientOnly, ["ERROR", "Bad input"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
36 |
answerProto protoNum = [(clientOnly, ["PROTO", show protoNum])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
37 |
answerRoomsList list = [(clientOnly, ["ROOMS"] ++ list)] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
38 |
answerRoomExists = [(clientOnly, ["WARNING", "There's already a room with that name"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
39 |
answerJoined nick = [(sameRoom, ["JOINED", nick])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
40 |
answerNoRoom = [(clientOnly, ["WARNING", "There's no room with that name"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
41 |
answerWrongPassword = [(clientOnly, ["WARNING", "Wrong password"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
42 |
answerChatString nick msg = [(othersInRoom, ["CHAT_STRING", nick, msg])] |
1317 | 43 |
answerConfigParam paramName paramStrs = [(othersInRoom, "CONFIG_PARAM" : paramName : paramStrs)] |
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
44 |
answerFullConfig room = map toAnswer (Map.toList $ params room) ++ [(clientOnly, ["MAP", gamemap room])] |
1317 | 45 |
where |
1321 | 46 |
toAnswer (paramName, paramStrs) = |
1317 | 47 |
(clientOnly, "CONFIG_PARAM" : paramName : paramStrs) |
1470
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
48 |
answerCantAdd reason = [(clientOnly, ["WARNING", "Cannot add team: " ++ reason])] |
1325 | 49 |
answerTeamAccepted team = [(clientOnly, ["TEAM_ACCEPTED", teamname team])] |
1331
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
50 |
answerAddTeam team = [(othersInRoom, teamToNet team)] |
1327 | 51 |
answerHHNum teamName hhNumber = [(othersInRoom, ["HH_NUM", teamName, show hhNumber])] |
1328 | 52 |
answerRemoveTeam teamName = [(othersInRoom, ["REMOVE_TEAM", teamName])] |
1329 | 53 |
answerNotOwner = [(clientOnly, ["ERROR", "You do not own this team"])] |
1330 | 54 |
answerTeamColor teamName newColor = [(othersInRoom, ["TEAM_COLOR", teamName, newColor])] |
1331
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
55 |
answerAllTeams room = concatMap toAnswer (teams room) |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
56 |
where |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
57 |
toAnswer team = |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
58 |
[(clientOnly, teamToNet team), |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
59 |
(clientOnly, ["TEAM_COLOR", teamname team, teamcolor team]), |
ae291cfd617a
Send teams info to newly connected client (has a bug with team sequence, need to discover)
unc0rr
parents:
1330
diff
changeset
|
60 |
(clientOnly, ["HH_NUM", teamname team, show $ hhnum team])] |
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
61 |
answerMap mapName = [(othersInRoom, ["MAP", mapName])] |
1338 | 62 |
answerRunGame = [(sameRoom, ["RUN_GAME"])] |
1384 | 63 |
answerCannotCreateRoom = [(clientOnly, ["WARNING", "Cannot create more rooms"])] |
1406 | 64 |
answerIsReady nick = [(sameRoom, ["READY", nick])] |
1403 | 65 |
answerNotReady nick = [(sameRoom, ["NOT_READY", nick])] |
1411 | 66 |
answerTooFewClans = [(clientOnly, ["ERROR", "Too few clans in game"])] |
67 |
answerRestricted = [(clientOnly, ["WARNING", "Room joining restricted"])] |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1452
diff
changeset
|
68 |
answerPing = [(allClients, ["PING"])] |
1469 | 69 |
answerConnected = [(clientOnly, ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"])] |
1403 | 70 |
|
1082 | 71 |
-- Main state-independent cmd handler |
72 |
handleCmd :: CmdHandler |
|
73 |
handleCmd client _ rooms ("QUIT":xs) = |
|
74 |
if null (room client) then |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
75 |
(noChangeClients, noChangeRooms, answerQuit) |
1082 | 76 |
else if isMaster client then |
1327 | 77 |
(noChangeClients, removeRoom (room client), answerQuit ++ answerAbandoned) -- core disconnects clients on ROOMABANDONED answer |
1082 | 78 |
else |
1403 | 79 |
(noChangeClients, modifyRoom clRoom{teams = othersTeams, playersIn = (playersIn clRoom) - 1, readyPlayers = newReadyPlayers}, answerQuit ++ (answerQuitInform $ nick client) ++ answerRemoveClientTeams) |
1334
b58afaadf7ae
Send team removal message to others in room when client disconnects
unc0rr
parents:
1333
diff
changeset
|
80 |
where |
b58afaadf7ae
Send team removal message to others in room when client disconnects
unc0rr
parents:
1333
diff
changeset
|
81 |
clRoom = roomByName (room client) rooms |
1335
c795cbc752c1
Small optimization (use partition instead of two filters with opposite predicates)
unc0rr
parents:
1334
diff
changeset
|
82 |
answerRemoveClientTeams = map (\tn -> (othersInRoom, ["REMOVE_TEAM", teamname tn])) clientTeams |
c795cbc752c1
Small optimization (use partition instead of two filters with opposite predicates)
unc0rr
parents:
1334
diff
changeset
|
83 |
(clientTeams, othersTeams) = partition (\t -> teamowner t == nick client) $ teams clRoom |
1403 | 84 |
newReadyPlayers = if isReady client then (readyPlayers clRoom) - 1 else readyPlayers clRoom |
895 | 85 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1452
diff
changeset
|
86 |
handleCmd _ _ _ ["PING"] = -- core requsted |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1452
diff
changeset
|
87 |
(noChangeClients, noChangeRooms, answerPing) |
1307 | 88 |
|
1469 | 89 |
handleCmd _ _ _ ["ASKME"] = -- core requsted |
90 |
(noChangeClients, noChangeRooms, answerConnected) |
|
91 |
||
1462 | 92 |
handleCmd _ _ _ ["PONG"] = |
93 |
(noChangeClients, noChangeRooms, []) |
|
94 |
||
1082 | 95 |
-- check state and call state-dependent commmand handlers |
96 |
handleCmd client clients rooms cmd = |
|
97 |
if null (nick client) || protocol client == 0 then |
|
98 |
handleCmd_noInfo client clients rooms cmd |
|
99 |
else if null (room client) then |
|
100 |
handleCmd_noRoom client clients rooms cmd |
|
101 |
else |
|
102 |
handleCmd_inRoom client clients rooms cmd |
|
103 |
||
1307 | 104 |
|
1082 | 105 |
-- 'no info' state - need to get protocol number and nickname |
106 |
handleCmd_noInfo :: CmdHandler |
|
107 |
handleCmd_noInfo client clients _ ["NICK", newNick] = |
|
894 | 108 |
if not . null $ nick client then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
109 |
(noChangeClients, noChangeRooms, answerNickChosen) |
894 | 110 |
else if haveSameNick then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
111 |
(noChangeClients, noChangeRooms, answerNickChooseAnother) |
894 | 112 |
else |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
113 |
(modifyClient client{nick = newNick}, noChangeRooms, answerNick newNick) |
894 | 114 |
where |
1320 | 115 |
haveSameNick = isJust $ find (\cl -> newNick == nick cl) clients |
894 | 116 |
|
1082 | 117 |
handleCmd_noInfo client _ _ ["PROTO", protoNum] = |
894 | 118 |
if protocol client > 0 then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
119 |
(noChangeClients, noChangeRooms, answerProtocolKnown) |
894 | 120 |
else if parsedProto == 0 then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
121 |
(noChangeClients, noChangeRooms, answerBadInput) |
894 | 122 |
else |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
123 |
(modifyClient client{protocol = parsedProto}, noChangeRooms, answerProto parsedProto) |
894 | 124 |
where |
125 |
parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16) |
|
126 |
||
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
127 |
handleCmd_noInfo _ _ _ _ = (noChangeClients, noChangeRooms, answerBadCmd) |
894 | 128 |
|
1307 | 129 |
|
894 | 130 |
-- 'noRoom' clients state command handlers |
1082 | 131 |
handleCmd_noRoom :: CmdHandler |
1452 | 132 |
handleCmd_noRoom client clients rooms ["LIST"] = |
133 |
(noChangeClients, noChangeRooms, answerServerMessage clients ++ (answerRoomsList $ concatMap roomInfo $ sameProtoRooms)) |
|
1396 | 134 |
where |
1402 | 135 |
roomInfo room = [ |
136 |
name room, |
|
137 |
(show $ playersIn room) ++ "(" ++ (show $ length $ teams room) ++ ")", |
|
138 |
show $ gameinprogress room |
|
139 |
] |
|
1412 | 140 |
sameProtoRooms = filter (\r -> (roomProto r == protocol client) && (not $ isRestrictedJoins r)) rooms |
903 | 141 |
|
1082 | 142 |
handleCmd_noRoom client _ rooms ["CREATE", newRoom, roomPassword] = |
1384 | 143 |
if (not $ isDedicated globalOptions) && (not $ null rooms) then |
144 |
(noChangeClients, noChangeRooms, answerCannotCreateRoom) |
|
895 | 145 |
else |
1384 | 146 |
if haveSameRoom then |
147 |
(noChangeClients, noChangeRooms, answerRoomExists) |
|
148 |
else |
|
1407 | 149 |
(modifyClient client{room = newRoom, isMaster = True}, addRoom createRoom{name = newRoom, password = roomPassword, roomProto = (protocol client)}, (answerJoined $ nick client) ++ (answerNotReady $ nick client)) |
895 | 150 |
where |
1320 | 151 |
haveSameRoom = isJust $ find (\room -> newRoom == name room) rooms |
895 | 152 |
|
1082 | 153 |
handleCmd_noRoom client clients rooms ["CREATE", newRoom] = |
154 |
handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""] |
|
155 |
||
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
156 |
handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] = |
902 | 157 |
if noSuchRoom then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
158 |
(noChangeClients, noChangeRooms, answerNoRoom) |
1321 | 159 |
else if roomPassword /= password clRoom then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
160 |
(noChangeClients, noChangeRooms, answerWrongPassword) |
1411 | 161 |
else if isRestrictedJoins clRoom then |
162 |
(noChangeClients, noChangeRooms, answerRestricted) |
|
895 | 163 |
else |
1406 | 164 |
(modifyClient client{room = roomName}, modifyRoom clRoom{playersIn = 1 + playersIn clRoom}, answerNicks ++ answerReady ++ (answerJoined $ nick client) ++ (answerNotReady $ nick client) ++ answerFullConfig clRoom ++ answerAllTeams clRoom) |
895 | 165 |
where |
1401 | 166 |
noSuchRoom = isNothing $ find (\room -> roomName == name room && roomProto room == protocol client) rooms |
1406 | 167 |
answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ sameRoomClients))] |
168 |
answerReady = map (\c -> (clientOnly, [if isReady c then "READY" else "NOT_READY", nick c])) sameRoomClients |
|
169 |
sameRoomClients = filter (\ci -> room ci == roomName) clients |
|
1321 | 170 |
clRoom = roomByName roomName rooms |
895 | 171 |
|
1082 | 172 |
handleCmd_noRoom client clients rooms ["JOIN", roomName] = |
173 |
handleCmd_noRoom client clients rooms ["JOIN", roomName, ""] |
|
894 | 174 |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
175 |
handleCmd_noRoom _ _ _ _ = (noChangeClients, noChangeRooms, answerBadCmd) |
895 | 176 |
|
1307 | 177 |
|
897 | 178 |
-- 'inRoom' clients state command handlers |
1082 | 179 |
handleCmd_inRoom :: CmdHandler |
1322
c624b04699fb
Fix protocol implementation to conform documentation
unc0rr
parents:
1321
diff
changeset
|
180 |
handleCmd_inRoom client _ _ ["CHAT_STRING", msg] = |
1317 | 181 |
(noChangeClients, noChangeRooms, answerChatString (nick client) msg) |
897 | 182 |
|
1327 | 183 |
handleCmd_inRoom client _ rooms ("CONFIG_PARAM" : paramName : paramStrs) = |
1317 | 184 |
if isMaster client then |
1322
c624b04699fb
Fix protocol implementation to conform documentation
unc0rr
parents:
1321
diff
changeset
|
185 |
(noChangeClients, modifyRoom clRoom{params = Map.insert paramName paramStrs (params clRoom)}, answerConfigParam paramName paramStrs) |
1317 | 186 |
else |
187 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
1321 | 188 |
where |
189 |
clRoom = roomByName (room client) rooms |
|
190 |
||
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
191 |
handleCmd_inRoom client _ rooms ["MAP", mapName] = |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
192 |
if isMaster client then |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
193 |
(noChangeClients, modifyRoom clRoom{gamemap = mapName}, answerMap mapName) |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
194 |
else |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
195 |
(noChangeClients, noChangeRooms, answerNotMaster) |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
196 |
where |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
197 |
clRoom = roomByName (room client) rooms |
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1332
diff
changeset
|
198 |
|
1327 | 199 |
handleCmd_inRoom client _ rooms ("ADD_TEAM" : name : color : grave : fort : difStr : hhsInfo) |
1323
d166f9069c2b
Build neccessary structures in memory on ADDTEAM message, but don't send answer yet (need to review team id concept)
unc0rr
parents:
1322
diff
changeset
|
200 |
| length hhsInfo == 16 = |
1470
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
201 |
if length (teams clRoom) == 6 then |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
202 |
(noChangeClients, noChangeRooms, answerCantAdd "too many teams") |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
203 |
else if canAddNumber <= 0 then |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
204 |
(noChangeClients, noChangeRooms, answerCantAdd "too many hedgehogs") |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
205 |
else if isJust findTeam then |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
206 |
(noChangeClients, noChangeRooms, answerCantAdd "already has a team with same name") |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
207 |
else if gameinprogress clRoom then |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
208 |
(noChangeClients, noChangeRooms, answerCantAdd "round in progress") |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
209 |
else if isRestrictedTeams clRoom then |
ebaca3b66d92
Give more specific explanation when deny to add team
unc0rr
parents:
1469
diff
changeset
|
210 |
(noChangeClients, noChangeRooms, answerCantAdd "restricted") |
1323
d166f9069c2b
Build neccessary structures in memory on ADDTEAM message, but don't send answer yet (need to review team id concept)
unc0rr
parents:
1322
diff
changeset
|
211 |
else |
1336
4e88eccbe7f6
Fix team colors mismatch on some conditions (just synchronize them when team is added)
unc0rr
parents:
1335
diff
changeset
|
212 |
(noChangeClients, modifyRoom clRoom{teams = teams clRoom ++ [newTeam]}, answerTeamAccepted newTeam ++ answerAddTeam newTeam ++ answerTeamColor name color) |
1323
d166f9069c2b
Build neccessary structures in memory on ADDTEAM message, but don't send answer yet (need to review team id concept)
unc0rr
parents:
1322
diff
changeset
|
213 |
where |
d166f9069c2b
Build neccessary structures in memory on ADDTEAM message, but don't send answer yet (need to review team id concept)
unc0rr
parents:
1322
diff
changeset
|
214 |
clRoom = roomByName (room client) rooms |
1329 | 215 |
newTeam = (TeamInfo (nick client) name color grave fort difficulty newTeamHHNum (hhsList hhsInfo)) |
1328 | 216 |
findTeam = find (\t -> name == teamname t) $ teams clRoom |
1323
d166f9069c2b
Build neccessary structures in memory on ADDTEAM message, but don't send answer yet (need to review team id concept)
unc0rr
parents:
1322
diff
changeset
|
217 |
difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int) |
1325 | 218 |
hhsList [] = [] |
1323
d166f9069c2b
Build neccessary structures in memory on ADDTEAM message, but don't send answer yet (need to review team id concept)
unc0rr
parents:
1322
diff
changeset
|
219 |
hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs |
1327 | 220 |
canAddNumber = 18 - (sum . map hhnum $ teams clRoom) |
221 |
newTeamHHNum = min 4 canAddNumber |
|
222 |
||
223 |
handleCmd_inRoom client _ rooms ["HH_NUM", teamName, numberStr] = |
|
224 |
if not $ isMaster client then |
|
225 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
226 |
else |
|
1329 | 227 |
if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then |
1327 | 228 |
(noChangeClients, noChangeRooms, answerBadParam) |
229 |
else |
|
230 |
(noChangeClients, modifyRoom $ modifyTeam clRoom team{hhnum = hhNumber}, answerHHNum teamName hhNumber) |
|
231 |
where |
|
232 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int) |
|
233 |
noSuchTeam = isNothing findTeam |
|
234 |
team = fromJust findTeam |
|
235 |
findTeam = find (\t -> teamName == teamname t) $ teams clRoom |
|
236 |
clRoom = roomByName (room client) rooms |
|
237 |
canAddNumber = 18 - (sum . map hhnum $ teams clRoom) |
|
1323
d166f9069c2b
Build neccessary structures in memory on ADDTEAM message, but don't send answer yet (need to review team id concept)
unc0rr
parents:
1322
diff
changeset
|
238 |
|
1330 | 239 |
handleCmd_inRoom client _ rooms ["TEAM_COLOR", teamName, newColor] = |
240 |
if not $ isMaster client then |
|
241 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
242 |
else |
|
1442 | 243 |
if noSuchTeam then |
244 |
(noChangeClients, noChangeRooms, answerBadParam) |
|
245 |
else |
|
246 |
(noChangeClients, modifyRoom $ modifyTeam clRoom team{teamcolor = newColor}, answerTeamColor teamName newColor) |
|
1330 | 247 |
where |
248 |
noSuchTeam = isNothing findTeam |
|
249 |
team = fromJust findTeam |
|
250 |
findTeam = find (\t -> teamName == teamname t) $ teams clRoom |
|
251 |
clRoom = roomByName (room client) rooms |
|
252 |
||
1328 | 253 |
handleCmd_inRoom client _ rooms ["REMOVE_TEAM", teamName] = |
1329 | 254 |
if noSuchTeam then |
255 |
(noChangeClients, noChangeRooms, answerBadParam) |
|
1328 | 256 |
else |
1329 | 257 |
if not $ nick client == teamowner team then |
258 |
(noChangeClients, noChangeRooms, answerNotOwner) |
|
1328 | 259 |
else |
260 |
(noChangeClients, modifyRoom clRoom{teams = filter (\t -> teamName /= teamname t) $ teams clRoom}, answerRemoveTeam teamName) |
|
261 |
where |
|
262 |
noSuchTeam = isNothing findTeam |
|
263 |
team = fromJust findTeam |
|
264 |
findTeam = find (\t -> teamName == teamname t) $ teams clRoom |
|
265 |
clRoom = roomByName (room client) rooms |
|
1083 | 266 |
|
1403 | 267 |
handleCmd_inRoom client _ rooms ["TOGGLE_READY"] = |
268 |
if isReady client then |
|
1411 | 269 |
(modifyClient client{isReady = False}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, answerNotReady $ nick client) |
1338 | 270 |
else |
1411 | 271 |
(modifyClient client{isReady = True}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, answerIsReady $ nick client) |
1350 | 272 |
where |
273 |
clRoom = roomByName (room client) rooms |
|
1404 | 274 |
newReadyPlayers = (readyPlayers clRoom) + if isReady client then -1 else 1 |
1338 | 275 |
|
1411 | 276 |
handleCmd_inRoom client _ rooms ["START_GAME"] = |
277 |
if isMaster client && (playersIn clRoom == readyPlayers clRoom) && (not $ gameinprogress clRoom) then |
|
278 |
if enoughClans then |
|
279 |
(noChangeClients, modifyRoom clRoom{gameinprogress = True}, answerRunGame) |
|
280 |
else |
|
281 |
(noChangeClients, noChangeRooms, answerTooFewClans) |
|
282 |
else |
|
283 |
(noChangeClients, noChangeRooms, []) |
|
284 |
where |
|
285 |
clRoom = roomByName (room client) rooms |
|
286 |
enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams clRoom |
|
287 |
||
288 |
handleCmd_inRoom client _ rooms ["TOGGLE_RESTRICT_JOINS"] = |
|
289 |
if isMaster client then |
|
290 |
(noChangeClients, modifyRoom clRoom{isRestrictedJoins = newStatus}, []) |
|
291 |
else |
|
292 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
293 |
where |
|
294 |
clRoom = roomByName (room client) rooms |
|
295 |
newStatus = not $ isRestrictedJoins clRoom |
|
296 |
||
297 |
handleCmd_inRoom client _ rooms ["TOGGLE_RESTRICT_TEAMS"] = |
|
298 |
if isMaster client then |
|
299 |
(noChangeClients, modifyRoom clRoom{isRestrictedTeams = newStatus}, []) |
|
300 |
else |
|
301 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
302 |
where |
|
303 |
clRoom = roomByName (room client) rooms |
|
304 |
newStatus = not $ isRestrictedTeams clRoom |
|
305 |
||
1408 | 306 |
handleCmd_inRoom client clients rooms ["ROUNDFINISHED"] = |
1345
73119de7d3be
Server erases teams list after round finish, so everything should be okay now
unc0rr
parents:
1344
diff
changeset
|
307 |
if isMaster client then |
1408 | 308 |
(modifyRoomClients clRoom (\cl -> cl{isReady = False}), modifyRoom clRoom{gameinprogress = False, readyPlayers = 0}, answerAllNotReady) |
1345
73119de7d3be
Server erases teams list after round finish, so everything should be okay now
unc0rr
parents:
1344
diff
changeset
|
309 |
else |
1344
4004e597f1bf
Clients send roundfinished message to server when the round is over
unc0rr
parents:
1338
diff
changeset
|
310 |
(noChangeClients, noChangeRooms, []) |
1345
73119de7d3be
Server erases teams list after round finish, so everything should be okay now
unc0rr
parents:
1344
diff
changeset
|
311 |
where |
73119de7d3be
Server erases teams list after round finish, so everything should be okay now
unc0rr
parents:
1344
diff
changeset
|
312 |
clRoom = roomByName (room client) rooms |
1408 | 313 |
sameRoomClients = filter (\ci -> room ci == name clRoom) clients |
314 |
answerAllNotReady = map (\cl -> (sameRoom, ["NOT_READY", nick cl])) sameRoomClients |
|
1344
4004e597f1bf
Clients send roundfinished message to server when the round is over
unc0rr
parents:
1338
diff
changeset
|
315 |
|
1338 | 316 |
handleCmd_inRoom client _ _ ["GAMEMSG", msg] = |
317 |
(noChangeClients, noChangeRooms, [(othersInRoom, ["GAMEMSG", msg])]) |
|
318 |
||
1391 | 319 |
handleCmd_inRoom client clients rooms ["KICK", kickNick] = |
320 |
if isMaster client then |
|
321 |
if noSuchClient || (kickClient == client) then |
|
322 |
(noChangeClients, noChangeRooms, []) |
|
323 |
else |
|
324 |
(modifyClient kickClient{forceQuit = True}, noChangeRooms, []) |
|
325 |
else |
|
326 |
(noChangeClients, noChangeRooms, []) |
|
327 |
where |
|
328 |
clRoom = roomByName (room client) rooms |
|
329 |
noSuchClient = isNothing findClient |
|
330 |
kickClient = fromJust findClient |
|
331 |
findClient = find (\t -> ((room t) == (room client)) && ((nick t) == kickNick)) $ clients |
|
332 |
||
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
333 |
handleCmd_inRoom _ _ _ _ = (noChangeClients, noChangeRooms, answerBadCmd) |