author | unc0rr |
Fri, 25 Jun 2010 10:05:42 +0400 | |
changeset 3555 | 4c5ca656d1bb |
parent 3540 | b602a57ba0fb |
child 3566 | 772a46ef8288 |
permissions | -rw-r--r-- |
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
1 |
{-# LANGUAGE OverloadedStrings #-} |
1804 | 2 |
module HWProtoLobbyState where |
3 |
||
4 |
import qualified Data.Map as Map |
|
5 |
import qualified Data.IntSet as IntSet |
|
1813 | 6 |
import qualified Data.Foldable as Foldable |
3555 | 7 |
import Data.Maybe |
1804 | 8 |
import Data.List |
3260
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
9 |
import Data.Word |
3501 | 10 |
import Control.Monad.Reader |
11 |
import qualified Data.ByteString.Char8 as B |
|
1804 | 12 |
-------------------------------------- |
13 |
import CoreTypes |
|
14 |
import Actions |
|
15 |
import Utils |
|
3435 | 16 |
import HandlerUtils |
3501 | 17 |
import RoomsAndClients |
1804 | 18 |
|
3435 | 19 |
{-answerAllTeams protocol teams = concatMap toAnswer teams |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
20 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
21 |
toAnswer team = |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
22 |
[AnswerThisClient $ teamToNet protocol team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
23 |
AnswerThisClient ["TEAM_COLOR", teamname team, teamcolor team], |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
24 |
AnswerThisClient ["HH_NUM", teamname team, show $ hhnum team]] |
3435 | 25 |
-} |
1804 | 26 |
handleCmd_lobby :: CmdHandler |
27 |
||
3501 | 28 |
|
29 |
handleCmd_lobby ["LIST"] = do |
|
30 |
(ci, irnc) <- ask |
|
31 |
let cl = irnc `client` ci |
|
32 |
rooms <- allRoomInfos |
|
33 |
let roomsInfoList = concatMap (roomInfo irnc) . filter (\r -> (roomProto r == clientProto cl) && not (isRestrictedJoins r)) |
|
34 |
return [AnswerClients [sendChan cl] ("ROOMS" : roomsInfoList rooms)] |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
35 |
where |
3501 | 36 |
roomInfo irnc room |
37 |
| roomProto room < 28 = [ |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
38 |
name room, |
3501 | 39 |
B.pack $ show (playersIn room) ++ "(" ++ show (length $ teams room) ++ ")", |
40 |
B.pack $ show $ gameinprogress room |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
41 |
] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
42 |
| otherwise = [ |
3501 | 43 |
showB $ gameinprogress room, |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
44 |
name room, |
3501 | 45 |
showB $ playersIn room, |
46 |
showB $ length $ teams room, |
|
3555 | 47 |
nick $ irnc `client` masterID room, |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
48 |
head (Map.findWithDefault ["+gen+"] "MAP" (params room)), |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
49 |
head (Map.findWithDefault ["Default"] "SCHEME" (params room)), |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
50 |
head (Map.findWithDefault ["Default"] "AMMO" (params room)) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
51 |
] |
3501 | 52 |
|
1862 | 53 |
|
3435 | 54 |
handleCmd_lobby ["CHAT", msg] = do |
55 |
n <- clientNick |
|
56 |
s <- roomOthersChans |
|
57 |
return [AnswerClients s ["CHAT", n, msg]] |
|
1804 | 58 |
|
3502 | 59 |
handleCmd_lobby ["CREATE_ROOM", newRoom, roomPassword] |
60 |
| illegalName newRoom = return [Warning "Illegal room name"] |
|
61 |
| otherwise = do |
|
62 |
rs <- allRoomInfos |
|
63 |
(ci, irnc) <- ask |
|
64 |
let cl = irnc `client` ci |
|
65 |
return $ if isJust $ find (\room -> newRoom == name room) rs then |
|
66 |
[Warning "Room exists"] |
|
67 |
else |
|
68 |
[ |
|
69 |
AddRoom newRoom roomPassword, |
|
70 |
AnswerClients [sendChan cl] ["NOT_READY", nick cl] |
|
71 |
] |
|
1804 | 72 |
|
1862 | 73 |
|
3502 | 74 |
handleCmd_lobby ["CREATE_ROOM", newRoom] = |
75 |
handleCmd_lobby ["CREATE_ROOM", newRoom, ""] |
|
1804 | 76 |
|
3536 | 77 |
|
78 |
handleCmd_lobby ["JOIN_ROOM", roomName, roomPassword] = do |
|
79 |
(ci, irnc) <- ask |
|
80 |
let ris = allRooms irnc |
|
3540 | 81 |
cl <- thisClient |
3536 | 82 |
let maybeRI = find (\ri -> roomName == name (irnc `room` ri)) ris |
83 |
let jRI = fromJust maybeRI |
|
84 |
let jRoom = irnc `room` jRI |
|
85 |
let jRoomClients = map (client irnc) $! roomClients irnc jRI -- no lazyness here! |
|
86 |
return $ |
|
87 |
if isNothing maybeRI then |
|
88 |
[Warning "No such rooms"] |
|
89 |
else if isRestrictedJoins jRoom then |
|
90 |
[Warning "Joining restricted"] |
|
91 |
else if roomPassword /= password jRoom then |
|
92 |
[Warning "Wrong password"] |
|
93 |
else |
|
94 |
[ |
|
95 |
MoveToRoom jRI, |
|
96 |
AnswerClients (map sendChan $ cl : jRoomClients) ["NOT_READY", nick cl] |
|
97 |
] |
|
98 |
++ [ AnswerClients [sendChan cl] $ "JOINED" : map nick jRoomClients | playersIn jRoom /= 0] |
|
99 |
++ (map (readynessMessage cl) jRoomClients) |
|
100 |
||
101 |
where |
|
102 |
readynessMessage cl c = AnswerClients [sendChan cl] [if isReady c then "READY" else "NOT_READY", nick c] |
|
103 |
||
104 |
||
105 |
||
3502 | 106 |
{- |
1862 | 107 |
|
2352 | 108 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, roomPassword] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
109 |
| noSuchRoom = [Warning "No such room"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
110 |
| isRestrictedJoins jRoom = [Warning "Joining restricted"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
111 |
| roomPassword /= password jRoom = [Warning "Wrong password"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
112 |
| otherwise = |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
113 |
[RoomRemoveThisClient "", -- leave lobby |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
114 |
RoomAddThisClient rID] -- join room |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
115 |
++ answerNicks |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
116 |
++ answerReady |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
117 |
++ [AnswerThisRoom ["NOT_READY", nick client]] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
118 |
++ answerFullConfig |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
119 |
++ answerTeams |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
120 |
++ watchRound |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
121 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
122 |
answerNicks = |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
123 |
[AnswerThisClient $ "JOINED" : |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
124 |
map (\clID -> nick $ clients IntMap.! clID) roomClientsIDs | playersIn jRoom /= 0] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
125 |
answerReady = map |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
126 |
((\ c -> |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
127 |
AnswerThisClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
128 |
[if isReady c then "READY" else "NOT_READY", nick c]) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
129 |
. (\ clID -> clients IntMap.! clID)) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
130 |
roomClientsIDs |
1804 | 131 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
132 |
toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs |
3425 | 133 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
134 |
answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
135 |
(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom) |
1813 | 136 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
137 |
watchRound = if not $ gameinprogress jRoom then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
138 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
139 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
140 |
[AnswerThisClient ["RUN_GAME"], |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
141 |
AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : Foldable.toList (roundMsgs jRoom)] |
1813 | 142 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
143 |
answerTeams = if gameinprogress jRoom then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
144 |
answerAllTeams (clientProto client) (teamsAtStart jRoom) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
145 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
146 |
answerAllTeams (clientProto client) (teams jRoom) |
3536 | 147 |
-} |
1804 | 148 |
|
3536 | 149 |
handleCmd_lobby ["JOIN_ROOM", roomName] = |
150 |
handleCmd_lobby ["JOIN_ROOM", roomName, ""] |
|
3425 | 151 |
|
3536 | 152 |
{- |
2961
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
153 |
handleCmd_lobby clID clients rooms ["FOLLOW", asknick] = |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
154 |
if noSuchClient || roomID followClient == 0 then |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
155 |
[] |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
156 |
else |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
157 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
158 |
where |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
159 |
maybeClient = Foldable.find (\cl -> asknick == nick cl) clients |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
160 |
noSuchClient = isNothing maybeClient |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
161 |
followClient = fromJust maybeClient |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
162 |
roomName = name $ rooms IntMap.! roomID followClient |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
163 |
|
1862 | 164 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
165 |
--------------------------- |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
166 |
-- Administrator's stuff -- |
1862 | 167 |
|
168 |
handleCmd_lobby clID clients rooms ["KICK", kickNick] = |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
169 |
[KickClient kickID | isAdministrator client && (not noSuchClient) && kickID /= clID] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
170 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
171 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
172 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
173 |
noSuchClient = isNothing maybeClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
174 |
kickID = clientUID $ fromJust maybeClient |
1866 | 175 |
|
176 |
||
177 |
handleCmd_lobby clID clients rooms ["BAN", banNick] = |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
178 |
if not $ isAdministrator client then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
179 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
180 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
181 |
BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
182 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
183 |
client = clients IntMap.! clID |
1862 | 184 |
|
1804 | 185 |
|
3283 | 186 |
|
187 |
handleCmd_lobby clID clients rooms ["SET_SERVER_VAR", "MOTD_NEW", newMessage] = |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
188 |
[ModifyServerInfo (\si -> si{serverMessage = newMessage}) | isAdministrator client] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
189 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
190 |
client = clients IntMap.! clID |
1925 | 191 |
|
3283 | 192 |
handleCmd_lobby clID clients rooms ["SET_SERVER_VAR", "MOTD_OLD", newMessage] = |
3260
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
193 |
[ModifyServerInfo (\si -> si{serverMessageForOldVersions = newMessage}) | isAdministrator client] |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
194 |
where |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
195 |
client = clients IntMap.! clID |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
196 |
|
3283 | 197 |
handleCmd_lobby clID clients rooms ["SET_SERVER_VAR", "LATEST_PROTO", protoNum] = |
3260
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
198 |
[ModifyServerInfo (\si -> si{latestReleaseVersion = fromJust readNum}) | isAdministrator client && isJust readNum] |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
199 |
where |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
200 |
client = clients IntMap.! clID |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
201 |
readNum = maybeRead protoNum :: Maybe Word16 |
1925 | 202 |
|
3283 | 203 |
handleCmd_lobby clID clients rooms ["GET_SERVER_VAR"] = |
204 |
[SendServerVars | isAdministrator client] |
|
205 |
where |
|
206 |
client = clients IntMap.! clID |
|
207 |
||
208 |
||
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
209 |
handleCmd_lobby clID clients rooms ["CLEAR_ACCOUNTS_CACHE"] = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
210 |
[ClearAccountsCache | isAdministrator client] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
211 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
212 |
client = clients IntMap.! clID |
3502 | 213 |
-} |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
214 |
|
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
215 |
|
3502 | 216 |
handleCmd_lobby _ = return [ProtocolError "Incorrect command (state: in lobby)"] |