4 import Data.List |
4 import Data.List |
5 import Data.Word |
5 import Data.Word |
6 import Miscutils |
6 import Miscutils |
7 import Maybe |
7 import Maybe |
8 import qualified Data.Map as Map |
8 import qualified Data.Map as Map |
|
9 |
|
10 teamToNet team = ["ADD_TEAM", teamname team, teamgrave team, teamfort team, show $ difficulty team] ++ hhsInfo |
|
11 where |
|
12 hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
9 |
13 |
10 answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])] |
14 answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])] |
11 answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])] |
15 answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])] |
12 answerBadParam = [(clientOnly, ["ERROR", "Bad parameter"])] |
16 answerBadParam = [(clientOnly, ["ERROR", "Bad parameter"])] |
13 answerQuit = [(clientOnly, ["off"])] |
17 answerQuit = [(clientOnly, ["off"])] |
30 where |
34 where |
31 toAnswer (paramName, paramStrs) = |
35 toAnswer (paramName, paramStrs) = |
32 (clientOnly, "CONFIG_PARAM" : paramName : paramStrs) |
36 (clientOnly, "CONFIG_PARAM" : paramName : paramStrs) |
33 answerCantAdd = [(clientOnly, ["WARNING", "Too many teams or hedgehogs, or same name team"])] |
37 answerCantAdd = [(clientOnly, ["WARNING", "Too many teams or hedgehogs, or same name team"])] |
34 answerTeamAccepted team = [(clientOnly, ["TEAM_ACCEPTED", teamname team])] |
38 answerTeamAccepted team = [(clientOnly, ["TEAM_ACCEPTED", teamname team])] |
35 answerAddTeam team = [(othersInRoom, ["ADD_TEAM", teamname team, teamgrave team, teamfort team, show $ difficulty team] ++ hhsInfo)] |
39 answerAddTeam team = [(othersInRoom, teamToNet team)] |
36 where |
|
37 hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
|
38 answerHHNum teamName hhNumber = [(othersInRoom, ["HH_NUM", teamName, show hhNumber])] |
40 answerHHNum teamName hhNumber = [(othersInRoom, ["HH_NUM", teamName, show hhNumber])] |
39 answerRemoveTeam teamName = [(othersInRoom, ["REMOVE_TEAM", teamName])] |
41 answerRemoveTeam teamName = [(othersInRoom, ["REMOVE_TEAM", teamName])] |
40 answerNotOwner = [(clientOnly, ["ERROR", "You do not own this team"])] |
42 answerNotOwner = [(clientOnly, ["ERROR", "You do not own this team"])] |
41 answerTeamColor teamName newColor = [(othersInRoom, ["TEAM_COLOR", teamName, newColor])] |
43 answerTeamColor teamName newColor = [(othersInRoom, ["TEAM_COLOR", teamName, newColor])] |
|
44 answerAllTeams room = concatMap toAnswer (teams room) |
|
45 where |
|
46 toAnswer team = |
|
47 [(clientOnly, teamToNet team), |
|
48 (clientOnly, ["TEAM_COLOR", teamname team, teamcolor team]), |
|
49 (clientOnly, ["HH_NUM", teamname team, show $ hhnum team])] |
42 |
50 |
43 -- Main state-independent cmd handler |
51 -- Main state-independent cmd handler |
44 handleCmd :: CmdHandler |
52 handleCmd :: CmdHandler |
45 handleCmd client _ rooms ("QUIT":xs) = |
53 handleCmd client _ rooms ("QUIT":xs) = |
46 if null (room client) then |
54 if null (room client) then |
106 if noSuchRoom then |
114 if noSuchRoom then |
107 (noChangeClients, noChangeRooms, answerNoRoom) |
115 (noChangeClients, noChangeRooms, answerNoRoom) |
108 else if roomPassword /= password clRoom then |
116 else if roomPassword /= password clRoom then |
109 (noChangeClients, noChangeRooms, answerWrongPassword) |
117 (noChangeClients, noChangeRooms, answerWrongPassword) |
110 else |
118 else |
111 (modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks ++ answerFullConfig clRoom) |
119 (modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks ++ answerFullConfig clRoom ++ answerAllTeams clRoom) |
112 where |
120 where |
113 noSuchRoom = isNothing $ find (\room -> roomName == name room) rooms |
121 noSuchRoom = isNothing $ find (\room -> roomName == name room) rooms |
114 answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ filter (\ci -> room ci == roomName) clients))] |
122 answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ filter (\ci -> room ci == roomName) clients))] |
115 clRoom = roomByName roomName rooms |
123 clRoom = roomByName roomName rooms |
116 |
124 |