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