author | unc0rr |
Thu, 09 Oct 2008 13:45:40 +0000 | |
changeset 1328 | c41344e3c236 |
parent 1327 | 9d43a6e6b9ca |
child 1329 | 69ddc231a911 |
permissions | -rw-r--r-- |
890 | 1 |
module HWProto where |
2 |
||
3 |
import IO |
|
896 | 4 |
import Data.List |
894 | 5 |
import Data.Word |
890 | 6 |
import Miscutils |
1320 | 7 |
import Maybe |
1317 | 8 |
import qualified Data.Map as Map |
890 | 9 |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
10 |
answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])] |
1317 | 11 |
answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])] |
1327 | 12 |
answerBadParam = [(clientOnly, ["ERROR", "Bad parameter"])] |
1309 | 13 |
answerQuit = [(clientOnly, ["off"])] |
1327 | 14 |
answerAbandoned = [(othersInRoom, ["BYE"])] |
1309 | 15 |
answerQuitInform nick = [(othersInRoom, ["LEFT", nick])] |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
16 |
answerNickChosen = [(clientOnly, ["ERROR", "The nick already chosen"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
17 |
answerNickChooseAnother = [(clientOnly, ["WARNING", "Choose another nick"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
18 |
answerNick nick = [(clientOnly, ["NICK", nick])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
19 |
answerProtocolKnown = [(clientOnly, ["ERROR", "Protocol number already known"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
20 |
answerBadInput = [(clientOnly, ["ERROR", "Bad input"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
21 |
answerProto protoNum = [(clientOnly, ["PROTO", show protoNum])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
22 |
answerRoomsList list = [(clientOnly, ["ROOMS"] ++ list)] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
23 |
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
|
24 |
answerJoined nick = [(sameRoom, ["JOINED", nick])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
25 |
answerNoRoom = [(clientOnly, ["WARNING", "There's no room with that name"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
26 |
answerWrongPassword = [(clientOnly, ["WARNING", "Wrong password"])] |
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
27 |
answerChatString nick msg = [(othersInRoom, ["CHAT_STRING", nick, msg])] |
1317 | 28 |
answerConfigParam paramName paramStrs = [(othersInRoom, "CONFIG_PARAM" : paramName : paramStrs)] |
29 |
answerFullConfig room = map toAnswer (Map.toList $ params room) |
|
30 |
where |
|
1321 | 31 |
toAnswer (paramName, paramStrs) = |
1317 | 32 |
(clientOnly, "CONFIG_PARAM" : paramName : paramStrs) |
1328 | 33 |
answerCantAdd = [(clientOnly, ["WARNING", "Too many teams or hedgehogs, or same name team"])] |
1325 | 34 |
answerTeamAccepted team = [(clientOnly, ["TEAM_ACCEPTED", teamname team])] |
35 |
answerAddTeam team = [(othersInRoom, ["ADD_TEAM", teamname team, teamgrave team, teamfort team, show $ difficulty team] ++ hhsInfo)] |
|
36 |
where |
|
37 |
hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
|
1327 | 38 |
answerHHNum teamName hhNumber = [(othersInRoom, ["HH_NUM", teamName, show hhNumber])] |
1328 | 39 |
answerRemoveTeam teamName = [(othersInRoom, ["REMOVE_TEAM", teamName])] |
1307 | 40 |
|
1082 | 41 |
-- Main state-independent cmd handler |
42 |
handleCmd :: CmdHandler |
|
43 |
handleCmd client _ rooms ("QUIT":xs) = |
|
44 |
if null (room client) then |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
45 |
(noChangeClients, noChangeRooms, answerQuit) |
1082 | 46 |
else if isMaster client then |
1327 | 47 |
(noChangeClients, removeRoom (room client), answerQuit ++ answerAbandoned) -- core disconnects clients on ROOMABANDONED answer |
1082 | 48 |
else |
1309 | 49 |
(noChangeClients, noChangeRooms, answerQuit ++ (answerQuitInform $ nick client)) |
895 | 50 |
|
1307 | 51 |
|
1082 | 52 |
-- check state and call state-dependent commmand handlers |
53 |
handleCmd client clients rooms cmd = |
|
54 |
if null (nick client) || protocol client == 0 then |
|
55 |
handleCmd_noInfo client clients rooms cmd |
|
56 |
else if null (room client) then |
|
57 |
handleCmd_noRoom client clients rooms cmd |
|
58 |
else |
|
59 |
handleCmd_inRoom client clients rooms cmd |
|
60 |
||
1307 | 61 |
|
1082 | 62 |
-- 'no info' state - need to get protocol number and nickname |
63 |
handleCmd_noInfo :: CmdHandler |
|
64 |
handleCmd_noInfo client clients _ ["NICK", newNick] = |
|
894 | 65 |
if not . null $ nick client then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
66 |
(noChangeClients, noChangeRooms, answerNickChosen) |
894 | 67 |
else if haveSameNick then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
68 |
(noChangeClients, noChangeRooms, answerNickChooseAnother) |
894 | 69 |
else |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
70 |
(modifyClient client{nick = newNick}, noChangeRooms, answerNick newNick) |
894 | 71 |
where |
1320 | 72 |
haveSameNick = isJust $ find (\cl -> newNick == nick cl) clients |
894 | 73 |
|
1082 | 74 |
handleCmd_noInfo client _ _ ["PROTO", protoNum] = |
894 | 75 |
if protocol client > 0 then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
76 |
(noChangeClients, noChangeRooms, answerProtocolKnown) |
894 | 77 |
else if parsedProto == 0 then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
78 |
(noChangeClients, noChangeRooms, answerBadInput) |
894 | 79 |
else |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
80 |
(modifyClient client{protocol = parsedProto}, noChangeRooms, answerProto parsedProto) |
894 | 81 |
where |
82 |
parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16) |
|
83 |
||
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
84 |
handleCmd_noInfo _ _ _ _ = (noChangeClients, noChangeRooms, answerBadCmd) |
894 | 85 |
|
1307 | 86 |
|
894 | 87 |
-- 'noRoom' clients state command handlers |
1082 | 88 |
handleCmd_noRoom :: CmdHandler |
89 |
handleCmd_noRoom client _ rooms ["LIST"] = |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
90 |
(noChangeClients, noChangeRooms, answerRoomsList $ map name rooms) |
903 | 91 |
|
1082 | 92 |
handleCmd_noRoom client _ rooms ["CREATE", newRoom, roomPassword] = |
895 | 93 |
if haveSameRoom then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
94 |
(noChangeClients, noChangeRooms, answerRoomExists) |
895 | 95 |
else |
1317 | 96 |
(modifyClient client{room = newRoom, isMaster = True}, addRoom (RoomInfo newRoom roomPassword (protocol client) [] Map.empty), answerJoined $ nick client) |
895 | 97 |
where |
1320 | 98 |
haveSameRoom = isJust $ find (\room -> newRoom == name room) rooms |
895 | 99 |
|
1082 | 100 |
handleCmd_noRoom client clients rooms ["CREATE", newRoom] = |
101 |
handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""] |
|
102 |
||
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
103 |
handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] = |
902 | 104 |
if noSuchRoom then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
105 |
(noChangeClients, noChangeRooms, answerNoRoom) |
1321 | 106 |
else if roomPassword /= password clRoom then |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
107 |
(noChangeClients, noChangeRooms, answerWrongPassword) |
895 | 108 |
else |
1321 | 109 |
(modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks ++ answerFullConfig clRoom) |
895 | 110 |
where |
1320 | 111 |
noSuchRoom = isNothing $ find (\room -> roomName == name room) rooms |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
112 |
answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ filter (\ci -> room ci == roomName) clients))] |
1321 | 113 |
clRoom = roomByName roomName rooms |
895 | 114 |
|
1082 | 115 |
handleCmd_noRoom client clients rooms ["JOIN", roomName] = |
116 |
handleCmd_noRoom client clients rooms ["JOIN", roomName, ""] |
|
894 | 117 |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
118 |
handleCmd_noRoom _ _ _ _ = (noChangeClients, noChangeRooms, answerBadCmd) |
895 | 119 |
|
1307 | 120 |
|
897 | 121 |
-- 'inRoom' clients state command handlers |
1082 | 122 |
handleCmd_inRoom :: CmdHandler |
1322
c624b04699fb
Fix protocol implementation to conform documentation
unc0rr
parents:
1321
diff
changeset
|
123 |
handleCmd_inRoom client _ _ ["CHAT_STRING", msg] = |
1317 | 124 |
(noChangeClients, noChangeRooms, answerChatString (nick client) msg) |
897 | 125 |
|
1327 | 126 |
handleCmd_inRoom client _ rooms ("CONFIG_PARAM" : paramName : paramStrs) = |
1317 | 127 |
if isMaster client then |
1322
c624b04699fb
Fix protocol implementation to conform documentation
unc0rr
parents:
1321
diff
changeset
|
128 |
(noChangeClients, modifyRoom clRoom{params = Map.insert paramName paramStrs (params clRoom)}, answerConfigParam paramName paramStrs) |
1317 | 129 |
else |
130 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
1321 | 131 |
where |
132 |
clRoom = roomByName (room client) rooms |
|
133 |
||
1327 | 134 |
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
|
135 |
| length hhsInfo == 16 = |
1328 | 136 |
if length (teams clRoom) == 6 || canAddNumber <= 0 || isJust findTeam then |
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
|
137 |
(noChangeClients, noChangeRooms, answerCantAdd) |
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
|
138 |
else |
1325 | 139 |
(noChangeClients, modifyRoom clRoom{teams = newTeam : teams clRoom}, answerTeamAccepted newTeam ++ answerAddTeam newTeam) |
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
|
140 |
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
|
141 |
clRoom = roomByName (room client) rooms |
1327 | 142 |
newTeam = (TeamInfo name color grave fort difficulty newTeamHHNum (hhsList hhsInfo)) |
1328 | 143 |
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
|
144 |
difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int) |
1325 | 145 |
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
|
146 |
hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs |
1327 | 147 |
canAddNumber = 18 - (sum . map hhnum $ teams clRoom) |
148 |
newTeamHHNum = min 4 canAddNumber |
|
149 |
||
150 |
handleCmd_inRoom client _ rooms ["HH_NUM", teamName, numberStr] = |
|
151 |
if not $ isMaster client then |
|
152 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
153 |
else |
|
154 |
if hhNumber < 1 || hhNumber > 8 || hhNumber > canAddNumber|| noSuchTeam then |
|
155 |
(noChangeClients, noChangeRooms, answerBadParam) |
|
156 |
else |
|
157 |
(noChangeClients, modifyRoom $ modifyTeam clRoom team{hhnum = hhNumber}, answerHHNum teamName hhNumber) |
|
158 |
where |
|
159 |
hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int) |
|
160 |
noSuchTeam = isNothing findTeam |
|
161 |
team = fromJust findTeam |
|
162 |
findTeam = find (\t -> teamName == teamname t) $ teams clRoom |
|
163 |
clRoom = roomByName (room client) rooms |
|
164 |
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
|
165 |
|
1328 | 166 |
handleCmd_inRoom client _ rooms ["REMOVE_TEAM", teamName] = |
167 |
if not $ isMaster client then |
|
168 |
(noChangeClients, noChangeRooms, answerNotMaster) |
|
169 |
else |
|
170 |
if noSuchTeam then |
|
171 |
(noChangeClients, noChangeRooms, answerBadParam) |
|
172 |
else |
|
173 |
(noChangeClients, modifyRoom clRoom{teams = filter (\t -> teamName /= teamname t) $ teams clRoom}, answerRemoveTeam teamName) |
|
174 |
where |
|
175 |
noSuchTeam = isNothing findTeam |
|
176 |
team = fromJust findTeam |
|
177 |
findTeam = find (\t -> teamName == teamname t) $ teams clRoom |
|
178 |
clRoom = roomByName (room client) rooms |
|
1083 | 179 |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
180 |
handleCmd_inRoom _ _ _ _ = (noChangeClients, noChangeRooms, answerBadCmd) |