author | smxx |
Mon, 22 Feb 2010 00:47:50 +0000 | |
changeset 2839 | 95977f876122 |
parent 2408 | 41ebdb5f1e6e |
child 2867 | 9be6693c78cb |
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 |
|
2352 | 26 |
roomsInfoList = concatMap roomInfo sameProtoRooms |
27 |
sameProtoRooms = filter (\r -> (roomProto r == protocol) && not (isRestrictedJoins r)) roomsList |
|
1804 | 28 |
roomsList = IntMap.elems rooms |
29 |
protocol = clientProto client |
|
30 |
client = clients IntMap.! clID |
|
2408 | 31 |
roomInfo room |
32 |
| clientProto client < 28 = [ |
|
1804 | 33 |
name room, |
2352 | 34 |
show (playersIn room) ++ "(" ++ show (length $ teams room) ++ ")", |
1804 | 35 |
show $ gameinprogress room |
36 |
] |
|
2408 | 37 |
| otherwise = [ |
38 |
show $ gameinprogress room, |
|
39 |
name room, |
|
40 |
show $ playersIn room, |
|
41 |
show $ length $ teams room, |
|
42 |
nick $ clients IntMap.! (masterID room), |
|
43 |
head (Map.findWithDefault ["+gen+"] "MAP" (params room)), |
|
44 |
head (Map.findWithDefault ["Default"] "SCHEME" (params room)), |
|
45 |
head (Map.findWithDefault ["Default"] "AMMO" (params room)) |
|
46 |
] |
|
1862 | 47 |
|
1815 | 48 |
handleCmd_lobby clID clients _ ["CHAT", msg] = |
49 |
[AnswerOthersInRoom ["CHAT", clientNick, msg]] |
|
1804 | 50 |
where |
51 |
clientNick = nick $ clients IntMap.! clID |
|
52 |
||
1862 | 53 |
|
2352 | 54 |
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, roomPassword] |
55 |
| haveSameRoom = [Warning "Room exists"] |
|
56 |
| illegalName newRoom = [Warning "Illegal room name"] |
|
57 |
| otherwise = |
|
2126 | 58 |
[RoomRemoveThisClient "", -- leave lobby |
1804 | 59 |
AddRoom newRoom roomPassword, |
60 |
AnswerThisClient ["NOT_READY", clientNick] |
|
61 |
] |
|
62 |
where |
|
63 |
clientNick = nick $ clients IntMap.! clID |
|
64 |
haveSameRoom = isJust $ find (\room -> newRoom == name room) $ IntMap.elems rooms |
|
65 |
||
1862 | 66 |
|
1905 | 67 |
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom] = |
68 |
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, ""] |
|
1804 | 69 |
|
1862 | 70 |
|
2352 | 71 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, roomPassword] |
72 |
| noSuchRoom = [Warning "No such room"] |
|
73 |
| isRestrictedJoins jRoom = [Warning "Joining restricted"] |
|
74 |
| roomPassword /= password jRoom = [Warning "Wrong password"] |
|
75 |
| otherwise = |
|
2126 | 76 |
[RoomRemoveThisClient "", -- leave lobby |
1804 | 77 |
RoomAddThisClient rID] -- join room |
78 |
++ answerNicks |
|
79 |
++ answerReady |
|
80 |
++ [AnswerThisRoom ["NOT_READY", nick client]] |
|
1871 | 81 |
++ answerFullConfig |
1804 | 82 |
++ answerTeams |
1813 | 83 |
++ watchRound |
1804 | 84 |
where |
85 |
noSuchRoom = isNothing mbRoom |
|
2352 | 86 |
mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms |
1804 | 87 |
jRoom = fromJust mbRoom |
88 |
rID = roomUID jRoom |
|
89 |
client = clients IntMap.! clID |
|
90 |
roomClientsIDs = IntSet.elems $ playersIDs jRoom |
|
2352 | 91 |
answerNicks = |
92 |
[AnswerThisClient $ "JOINED" : |
|
93 |
map (\clID -> nick $ clients IntMap.! clID) roomClientsIDs | playersIn jRoom /= 0] |
|
94 |
answerReady = map |
|
95 |
((\ c -> |
|
96 |
AnswerThisClient |
|
97 |
[if isReady c then "READY" else "NOT_READY", nick c]) |
|
98 |
. (\ clID -> clients IntMap.! clID)) |
|
99 |
roomClientsIDs |
|
1804 | 100 |
|
101 |
toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs |
|
1871 | 102 |
|
103 |
answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart) |
|
104 |
(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom) |
|
1813 | 105 |
|
106 |
watchRound = if not $ gameinprogress jRoom then |
|
1804 | 107 |
[] |
108 |
else |
|
1813 | 109 |
[AnswerThisClient ["RUN_GAME"], |
2352 | 110 |
AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : Foldable.toList (roundMsgs jRoom)] |
1813 | 111 |
|
1804 | 112 |
answerTeams = if gameinprogress jRoom then |
113 |
answerAllTeams (teamsAtStart jRoom) |
|
114 |
else |
|
115 |
answerAllTeams (teams jRoom) |
|
116 |
||
117 |
||
1905 | 118 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] = |
119 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, ""] |
|
1862 | 120 |
|
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
121 |
--------------------------- |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
122 |
-- Administrator's stuff -- |
1862 | 123 |
|
124 |
handleCmd_lobby clID clients rooms ["KICK", kickNick] = |
|
2352 | 125 |
[KickClient kickID | isAdministrator client && (not noSuchClient) && kickID /= clID] |
1862 | 126 |
where |
127 |
client = clients IntMap.! clID |
|
128 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients |
|
129 |
noSuchClient = isNothing maybeClient |
|
130 |
kickID = clientUID $ fromJust maybeClient |
|
1866 | 131 |
|
132 |
||
133 |
handleCmd_lobby clID clients rooms ["BAN", banNick] = |
|
134 |
if not $ isAdministrator client then |
|
135 |
[] |
|
136 |
else |
|
137 |
BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick] |
|
138 |
where |
|
139 |
client = clients IntMap.! clID |
|
1862 | 140 |
|
1804 | 141 |
|
1925 | 142 |
handleCmd_lobby clID clients rooms ["SET_SERVER_MESSAGE", newMessage] = |
2352 | 143 |
[ModifyServerInfo (\si -> si{serverMessage = newMessage}) | isAdministrator client] |
1925 | 144 |
where |
145 |
client = clients IntMap.! clID |
|
146 |
||
147 |
||
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
148 |
handleCmd_lobby clID clients rooms ["CLEAR_ACCOUNTS_CACHE"] = |
2352 | 149 |
[ClearAccountsCache | isAdministrator client] |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
150 |
where |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
151 |
client = clients IntMap.! clID |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
152 |
|
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
153 |
|
1804 | 154 |
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"] |