author | nemo |
Tue, 17 Aug 2010 19:43:17 -0400 | |
changeset 3742 | 8461f0cef2e6 |
parent 3645 | c0b3f1bb9316 |
child 4242 | 5e3c5fe2cb14 |
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 |
3566 | 36 |
roomInfo irnc room = [ |
3501 | 37 |
showB $ gameinprogress room, |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
38 |
name room, |
3501 | 39 |
showB $ playersIn room, |
40 |
showB $ length $ teams room, |
|
3555 | 41 |
nick $ irnc `client` masterID room, |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
42 |
head (Map.findWithDefault ["+gen+"] "MAP" (params room)), |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
43 |
head (Map.findWithDefault ["Default"] "SCHEME" (params room)), |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
44 |
head (Map.findWithDefault ["Default"] "AMMO" (params room)) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
45 |
] |
3501 | 46 |
|
1862 | 47 |
|
3435 | 48 |
handleCmd_lobby ["CHAT", msg] = do |
49 |
n <- clientNick |
|
50 |
s <- roomOthersChans |
|
51 |
return [AnswerClients s ["CHAT", n, msg]] |
|
1804 | 52 |
|
3502 | 53 |
handleCmd_lobby ["CREATE_ROOM", newRoom, roomPassword] |
54 |
| illegalName newRoom = return [Warning "Illegal room name"] |
|
55 |
| otherwise = do |
|
56 |
rs <- allRoomInfos |
|
3645 | 57 |
cl <- thisClient |
3502 | 58 |
return $ if isJust $ find (\room -> newRoom == name room) rs then |
59 |
[Warning "Room exists"] |
|
60 |
else |
|
61 |
[ |
|
62 |
AddRoom newRoom roomPassword, |
|
63 |
AnswerClients [sendChan cl] ["NOT_READY", nick cl] |
|
64 |
] |
|
1804 | 65 |
|
1862 | 66 |
|
3502 | 67 |
handleCmd_lobby ["CREATE_ROOM", newRoom] = |
68 |
handleCmd_lobby ["CREATE_ROOM", newRoom, ""] |
|
1804 | 69 |
|
3536 | 70 |
|
71 |
handleCmd_lobby ["JOIN_ROOM", roomName, roomPassword] = do |
|
72 |
(ci, irnc) <- ask |
|
73 |
let ris = allRooms irnc |
|
3540 | 74 |
cl <- thisClient |
3536 | 75 |
let maybeRI = find (\ri -> roomName == name (irnc `room` ri)) ris |
76 |
let jRI = fromJust maybeRI |
|
77 |
let jRoom = irnc `room` jRI |
|
78 |
let jRoomClients = map (client irnc) $! roomClients irnc jRI -- no lazyness here! |
|
79 |
return $ |
|
80 |
if isNothing maybeRI then |
|
81 |
[Warning "No such rooms"] |
|
82 |
else if isRestrictedJoins jRoom then |
|
83 |
[Warning "Joining restricted"] |
|
84 |
else if roomPassword /= password jRoom then |
|
85 |
[Warning "Wrong password"] |
|
86 |
else |
|
87 |
[ |
|
88 |
MoveToRoom jRI, |
|
89 |
AnswerClients (map sendChan $ cl : jRoomClients) ["NOT_READY", nick cl] |
|
90 |
] |
|
91 |
++ [ AnswerClients [sendChan cl] $ "JOINED" : map nick jRoomClients | playersIn jRoom /= 0] |
|
92 |
++ (map (readynessMessage cl) jRoomClients) |
|
93 |
||
94 |
where |
|
95 |
readynessMessage cl c = AnswerClients [sendChan cl] [if isReady c then "READY" else "NOT_READY", nick c] |
|
96 |
||
97 |
||
98 |
||
3502 | 99 |
{- |
1862 | 100 |
|
2352 | 101 |
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
|
102 |
| noSuchRoom = [Warning "No such room"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
103 |
| isRestrictedJoins jRoom = [Warning "Joining restricted"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
104 |
| roomPassword /= password jRoom = [Warning "Wrong password"] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
105 |
| otherwise = |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
106 |
[RoomRemoveThisClient "", -- leave lobby |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
107 |
RoomAddThisClient rID] -- join room |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
108 |
++ answerNicks |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
109 |
++ answerReady |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
110 |
++ [AnswerThisRoom ["NOT_READY", nick client]] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
111 |
++ answerFullConfig |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
112 |
++ answerTeams |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
113 |
++ watchRound |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
114 |
where |
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 |
[AnswerThisClient $ "JOINED" : |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
117 |
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
|
118 |
answerReady = map |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
119 |
((\ c -> |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
120 |
AnswerThisClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
121 |
[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
|
122 |
. (\ clID -> clients IntMap.! clID)) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
123 |
roomClientsIDs |
1804 | 124 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
125 |
toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs |
3425 | 126 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
127 |
answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
128 |
(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom) |
1813 | 129 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
130 |
watchRound = if not $ gameinprogress jRoom then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
131 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
132 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
133 |
[AnswerThisClient ["RUN_GAME"], |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
134 |
AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : Foldable.toList (roundMsgs jRoom)] |
1813 | 135 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
136 |
answerTeams = if gameinprogress jRoom then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
137 |
answerAllTeams (clientProto client) (teamsAtStart jRoom) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
138 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
139 |
answerAllTeams (clientProto client) (teams jRoom) |
3536 | 140 |
-} |
1804 | 141 |
|
3536 | 142 |
handleCmd_lobby ["JOIN_ROOM", roomName] = |
143 |
handleCmd_lobby ["JOIN_ROOM", roomName, ""] |
|
3425 | 144 |
|
3536 | 145 |
{- |
2961
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
146 |
handleCmd_lobby clID clients rooms ["FOLLOW", asknick] = |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
147 |
if noSuchClient || roomID followClient == 0 then |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
148 |
[] |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
149 |
else |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
150 |
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
151 |
where |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
152 |
maybeClient = Foldable.find (\cl -> asknick == nick cl) clients |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
153 |
noSuchClient = isNothing maybeClient |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
154 |
followClient = fromJust maybeClient |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
155 |
roomName = name $ rooms IntMap.! roomID followClient |
3e057dfa601f
Fix "FOLLOW" command handler and place it into proper file
unc0rr
parents:
2867
diff
changeset
|
156 |
|
1862 | 157 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
158 |
--------------------------- |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
159 |
-- Administrator's stuff -- |
1862 | 160 |
|
161 |
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
|
162 |
[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
|
163 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
164 |
client = clients IntMap.! clID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
165 |
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
|
166 |
noSuchClient = isNothing maybeClient |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
167 |
kickID = clientUID $ fromJust maybeClient |
1866 | 168 |
|
169 |
||
170 |
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
|
171 |
if not $ isAdministrator client then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
172 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
173 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
174 |
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
|
175 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
176 |
client = clients IntMap.! clID |
1862 | 177 |
|
1804 | 178 |
|
3283 | 179 |
|
180 |
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
|
181 |
[ModifyServerInfo (\si -> si{serverMessage = newMessage}) | isAdministrator client] |
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 |
1925 | 184 |
|
3283 | 185 |
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
|
186 |
[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
|
187 |
where |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
188 |
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
|
189 |
|
3283 | 190 |
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
|
191 |
[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
|
192 |
where |
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
193 |
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
|
194 |
readNum = maybeRead protoNum :: Maybe Word16 |
1925 | 195 |
|
3283 | 196 |
handleCmd_lobby clID clients rooms ["GET_SERVER_VAR"] = |
197 |
[SendServerVars | isAdministrator client] |
|
198 |
where |
|
199 |
client = clients IntMap.! clID |
|
200 |
||
201 |
||
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
202 |
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
|
203 |
[ClearAccountsCache | isAdministrator client] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
204 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
205 |
client = clients IntMap.! clID |
3502 | 206 |
-} |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
207 |
|
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2150
diff
changeset
|
208 |
|
3502 | 209 |
handleCmd_lobby _ = return [ProtocolError "Incorrect command (state: in lobby)"] |