author | nemo |
Fri, 24 Jul 2009 23:50:53 +0000 | |
changeset 2273 | 6b34f5fa6434 |
parent 2155 | d897222d3339 |
child 2352 | 7eaf82cf0890 |
permissions | -rw-r--r-- |
1804 | 1 |
module HWProtoLobbyState where |
2 |
||
3 |
import qualified Data.Map as Map |
|
4 |
import qualified Data.IntMap as IntMap |
|
5 |
import qualified Data.IntSet as IntSet |
|
1813 | 6 |
import qualified Data.Foldable as Foldable |
1804 | 7 |
import Maybe |
8 |
import Data.List |
|
9 |
-------------------------------------- |
|
10 |
import CoreTypes |
|
11 |
import Actions |
|
12 |
import Utils |
|
13 |
||
14 |
answerAllTeams teams = concatMap toAnswer teams |
|
15 |
where |
|
16 |
toAnswer team = |
|
17 |
[AnswerThisClient $ teamToNet team, |
|
18 |
AnswerThisClient ["TEAM_COLOR", teamname team, teamcolor team], |
|
19 |
AnswerThisClient ["HH_NUM", teamname team, show $ hhnum team]] |
|
20 |
||
21 |
handleCmd_lobby :: CmdHandler |
|
22 |
||
23 |
handleCmd_lobby clID clients rooms ["LIST"] = |
|
24 |
[AnswerThisClient ("ROOMS" : roomsInfoList)] |
|
25 |
where |
|
26 |
roomsInfoList = concatMap roomInfo $ sameProtoRooms |
|
27 |
sameProtoRooms = filter (\r -> (roomProto r == protocol) && (not $ isRestrictedJoins r)) roomsList |
|
28 |
roomsList = IntMap.elems rooms |
|
29 |
protocol = clientProto client |
|
30 |
client = clients IntMap.! clID |
|
31 |
roomInfo room = [ |
|
32 |
name room, |
|
33 |
(show $ playersIn room) ++ "(" ++ (show $ length $ teams room) ++ ")", |
|
34 |
show $ gameinprogress room |
|
35 |
] |
|
36 |
||
1862 | 37 |
|
1815 | 38 |
handleCmd_lobby clID clients _ ["CHAT", msg] = |
39 |
[AnswerOthersInRoom ["CHAT", clientNick, msg]] |
|
1804 | 40 |
where |
41 |
clientNick = nick $ clients IntMap.! clID |
|
42 |
||
1862 | 43 |
|
1905 | 44 |
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, roomPassword] = |
1804 | 45 |
if haveSameRoom then |
46 |
[Warning "Room exists"] |
|
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2126
diff
changeset
|
47 |
else if illegalName newRoom then |
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2126
diff
changeset
|
48 |
[Warning "Illegal room name"] |
1804 | 49 |
else |
2126 | 50 |
[RoomRemoveThisClient "", -- leave lobby |
1804 | 51 |
AddRoom newRoom roomPassword, |
52 |
AnswerThisClient ["NOT_READY", clientNick] |
|
53 |
] |
|
54 |
where |
|
55 |
clientNick = nick $ clients IntMap.! clID |
|
56 |
haveSameRoom = isJust $ find (\room -> newRoom == name room) $ IntMap.elems rooms |
|
57 |
||
1862 | 58 |
|
1905 | 59 |
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom] = |
60 |
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, ""] |
|
1804 | 61 |
|
1862 | 62 |
|
1905 | 63 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, roomPassword] = |
1804 | 64 |
if noSuchRoom then |
65 |
[Warning "No such room"] |
|
66 |
else if isRestrictedJoins jRoom then |
|
67 |
[Warning "Joining restricted"] |
|
68 |
else if roomPassword /= password jRoom then |
|
69 |
[Warning "Wrong password"] |
|
70 |
else |
|
2126 | 71 |
[RoomRemoveThisClient "", -- leave lobby |
1804 | 72 |
RoomAddThisClient rID] -- join room |
73 |
++ answerNicks |
|
74 |
++ answerReady |
|
75 |
++ [AnswerThisRoom ["NOT_READY", nick client]] |
|
1871 | 76 |
++ answerFullConfig |
1804 | 77 |
++ answerTeams |
1813 | 78 |
++ watchRound |
1804 | 79 |
where |
80 |
noSuchRoom = isNothing mbRoom |
|
81 |
mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms |
|
82 |
jRoom = fromJust mbRoom |
|
83 |
rID = roomUID jRoom |
|
84 |
client = clients IntMap.! clID |
|
85 |
roomClientsIDs = IntSet.elems $ playersIDs jRoom |
|
86 |
answerNicks = if playersIn jRoom /= 0 then |
|
87 |
[AnswerThisClient $ ["JOINED"] ++ (map (\clID -> nick $ clients IntMap.! clID) $ roomClientsIDs)] |
|
88 |
else |
|
89 |
[] |
|
90 |
answerReady = |
|
91 |
map (\c -> AnswerThisClient [if isReady c then "READY" else "NOT_READY", nick c]) $ |
|
92 |
map (\clID -> clients IntMap.! clID) roomClientsIDs |
|
93 |
||
94 |
toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs |
|
1871 | 95 |
|
96 |
answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart) |
|
97 |
(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom) |
|
1813 | 98 |
|
99 |
watchRound = if not $ gameinprogress jRoom then |
|
1804 | 100 |
[] |
101 |
else |
|
1813 | 102 |
[AnswerThisClient ["RUN_GAME"], |
1866 | 103 |
AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : (Foldable.toList $ roundMsgs jRoom)] |
1813 | 104 |
|
1804 | 105 |
answerTeams = if gameinprogress jRoom then |
106 |
answerAllTeams (teamsAtStart jRoom) |
|
107 |
else |
|
108 |
answerAllTeams (teams jRoom) |
|
109 |
||
110 |
||
1905 | 111 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] = |
112 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, ""] |
|
1862 | 113 |
|
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
114 |
--------------------------- |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
115 |
-- Administrator's stuff -- |
1862 | 116 |
|
117 |
handleCmd_lobby clID clients rooms ["KICK", kickNick] = |
|
118 |
if not $ isAdministrator client then |
|
119 |
[] |
|
120 |
else |
|
121 |
if noSuchClient then |
|
122 |
[] |
|
123 |
else |
|
124 |
if kickID == clID then |
|
125 |
[] |
|
126 |
else |
|
127 |
[KickClient kickID] |
|
128 |
where |
|
129 |
client = clients IntMap.! clID |
|
130 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients |
|
131 |
noSuchClient = isNothing maybeClient |
|
132 |
kickID = clientUID $ fromJust maybeClient |
|
1866 | 133 |
|
134 |
||
135 |
handleCmd_lobby clID clients rooms ["BAN", banNick] = |
|
136 |
if not $ isAdministrator client then |
|
137 |
[] |
|
138 |
else |
|
139 |
BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick] |
|
140 |
where |
|
141 |
client = clients IntMap.! clID |
|
1862 | 142 |
|
1804 | 143 |
|
1925 | 144 |
handleCmd_lobby clID clients rooms ["SET_SERVER_MESSAGE", newMessage] = |
145 |
if not $ isAdministrator client then |
|
146 |
[] |
|
147 |
else |
|
148 |
[ModifyServerInfo (\si -> si{serverMessage = newMessage})] |
|
149 |
where |
|
150 |
client = clients IntMap.! clID |
|
151 |
||
152 |
||
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
153 |
handleCmd_lobby clID clients rooms ["CLEAR_ACCOUNTS_CACHE"] = |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
154 |
if not $ isAdministrator client then |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
155 |
[] |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
156 |
else |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
157 |
[ClearAccountsCache] |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
158 |
where |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
159 |
client = clients IntMap.! clID |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
160 |
|
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
161 |
|
1804 | 162 |
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"] |