author | koda |
Sun, 25 Oct 2009 10:39:12 +0000 | |
changeset 2582 | e16668bac20f |
parent 2551 | 01eb81cd3198 |
child 2662 | 12dc696f1c81 |
permissions | -rw-r--r-- |
1804 | 1 |
module Actions where |
2 |
||
3 |
import Control.Concurrent.STM |
|
4 |
import Control.Concurrent.Chan |
|
5 |
import Data.IntMap |
|
6 |
import qualified Data.IntSet as IntSet |
|
1813 | 7 |
import qualified Data.Sequence as Seq |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
8 |
import System.Log.Logger |
1804 | 9 |
import Monad |
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
10 |
import Data.Time |
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
11 |
import Maybe |
1804 | 12 |
----------------------------- |
13 |
import CoreTypes |
|
1813 | 14 |
import Utils |
1804 | 15 |
|
16 |
data Action = |
|
17 |
AnswerThisClient [String] |
|
18 |
| AnswerAll [String] |
|
19 |
| AnswerAllOthers [String] |
|
20 |
| AnswerThisRoom [String] |
|
21 |
| AnswerOthersInRoom [String] |
|
2403 | 22 |
| AnswerSameClan [String] |
1804 | 23 |
| AnswerLobby [String] |
1923 | 24 |
| SendServerMessage |
1804 | 25 |
| RoomAddThisClient Int -- roomID |
2126 | 26 |
| RoomRemoveThisClient String |
1813 | 27 |
| RemoveTeam String |
1804 | 28 |
| RemoveRoom |
1811 | 29 |
| UnreadyRoomClients |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
30 |
| MoveToLobby |
1804 | 31 |
| ProtocolError String |
32 |
| Warning String |
|
33 |
| ByeClient String |
|
1862 | 34 |
| KickClient Int -- clID |
1879 | 35 |
| KickRoomClient Int -- clID |
1921 | 36 |
| BanClient String -- nick |
1879 | 37 |
| RemoveClientTeams Int -- clID |
1804 | 38 |
| ModifyClient (ClientInfo -> ClientInfo) |
2403 | 39 |
| ModifyClient2 Int (ClientInfo -> ClientInfo) |
1804 | 40 |
| ModifyRoom (RoomInfo -> RoomInfo) |
1925 | 41 |
| ModifyServerInfo (ServerInfo -> ServerInfo) |
1804 | 42 |
| AddRoom String String |
1834 | 43 |
| CheckRegistered |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2126
diff
changeset
|
44 |
| ClearAccountsCache |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
45 |
| ProcessAccountInfo AccountInfo |
1804 | 46 |
| Dump |
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
47 |
| AddClient ClientInfo |
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
48 |
| PingAll |
2172 | 49 |
| StatsAction |
1804 | 50 |
|
51 |
type CmdHandler = Int -> Clients -> Rooms -> [String] -> [Action] |
|
52 |
||
1862 | 53 |
replaceID a (b, c, d, e) = (a, c, d, e) |
1804 | 54 |
|
55 |
processAction :: (Int, ServerInfo, Clients, Rooms) -> Action -> IO (Int, ServerInfo, Clients, Rooms) |
|
56 |
||
57 |
||
58 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisClient msg) = do |
|
59 |
writeChan (sendChan $ clients ! clID) msg |
|
60 |
return (clID, serverInfo, clients, rooms) |
|
61 |
||
62 |
||
63 |
processAction (clID, serverInfo, clients, rooms) (AnswerAll msg) = do |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
64 |
mapM_ (\cl -> writeChan (sendChan cl) msg) (elems clients) |
1804 | 65 |
return (clID, serverInfo, clients, rooms) |
66 |
||
67 |
||
68 |
processAction (clID, serverInfo, clients, rooms) (AnswerAllOthers msg) = do |
|
2403 | 69 |
mapM_ (\id' -> writeChan (sendChan $ clients ! id') msg) $ |
2352 | 70 |
Prelude.filter (\id' -> (id' /= clID) && logonPassed (clients ! id')) (keys clients) |
1804 | 71 |
return (clID, serverInfo, clients, rooms) |
72 |
||
73 |
||
74 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisRoom msg) = do |
|
2403 | 75 |
mapM_ (\id' -> writeChan (sendChan $ clients ! id') msg) roomClients |
1804 | 76 |
return (clID, serverInfo, clients, rooms) |
77 |
where |
|
78 |
roomClients = IntSet.elems $ playersIDs room |
|
79 |
room = rooms ! rID |
|
80 |
rID = roomID client |
|
81 |
client = clients ! clID |
|
82 |
||
83 |
||
84 |
processAction (clID, serverInfo, clients, rooms) (AnswerOthersInRoom msg) = do |
|
2403 | 85 |
mapM_ (\id' -> writeChan (sendChan $ clients ! id') msg) $ Prelude.filter (/= clID) roomClients |
1804 | 86 |
return (clID, serverInfo, clients, rooms) |
87 |
where |
|
88 |
roomClients = IntSet.elems $ playersIDs room |
|
89 |
room = rooms ! rID |
|
90 |
rID = roomID client |
|
91 |
client = clients ! clID |
|
92 |
||
93 |
||
94 |
processAction (clID, serverInfo, clients, rooms) (AnswerLobby msg) = do |
|
2403 | 95 |
mapM_ (\id' -> writeChan (sendChan $ clients ! id') msg) roomClients |
1804 | 96 |
return (clID, serverInfo, clients, rooms) |
97 |
where |
|
98 |
roomClients = IntSet.elems $ playersIDs room |
|
99 |
room = rooms ! 0 |
|
100 |
||
101 |
||
2403 | 102 |
processAction (clID, serverInfo, clients, rooms) (AnswerSameClan msg) = do |
103 |
mapM_ (\cl -> writeChan (sendChan cl) msg) sameClanClients |
|
104 |
return (clID, serverInfo, clients, rooms) |
|
105 |
where |
|
106 |
otherRoomClients = Prelude.map ((!) clients) $ IntSet.elems $ clID `IntSet.delete` (playersIDs room) |
|
107 |
sameClanClients = Prelude.filter (\cl -> teamsInGame cl > 0 && clientClan cl == thisClan) otherRoomClients |
|
108 |
thisClan = clientClan client |
|
109 |
room = rooms ! rID |
|
110 |
rID = roomID client |
|
111 |
client = clients ! clID |
|
112 |
||
113 |
||
1923 | 114 |
processAction (clID, serverInfo, clients, rooms) SendServerMessage = do |
2352 | 115 |
writeChan (sendChan $ clients ! clID) ["SERVER_MESSAGE", message serverInfo] |
1923 | 116 |
return (clID, serverInfo, clients, rooms) |
1953 | 117 |
where |
118 |
client = clients ! clID |
|
2551 | 119 |
message = if clientProto client < 29 then |
1953 | 120 |
serverMessageForOldVersions |
121 |
else |
|
1994 | 122 |
serverMessage |
1923 | 123 |
|
124 |
||
1804 | 125 |
processAction (clID, serverInfo, clients, rooms) (ProtocolError msg) = do |
126 |
writeChan (sendChan $ clients ! clID) ["ERROR", msg] |
|
127 |
return (clID, serverInfo, clients, rooms) |
|
128 |
||
129 |
||
130 |
processAction (clID, serverInfo, clients, rooms) (Warning msg) = do |
|
131 |
writeChan (sendChan $ clients ! clID) ["WARNING", msg] |
|
132 |
return (clID, serverInfo, clients, rooms) |
|
133 |
||
134 |
||
135 |
processAction (clID, serverInfo, clients, rooms) (ByeClient msg) = do |
|
2352 | 136 |
infoM "Clients" (show (clientUID client) ++ " quits: " ++ msg) |
1929 | 137 |
(_, _, newClients, newRooms) <- |
1930 | 138 |
if roomID client /= 0 then |
2337
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
139 |
processAction (clID, serverInfo, clients, rooms) $ RoomRemoveThisClient "quit" |
1930 | 140 |
else |
141 |
return (clID, serverInfo, clients, rooms) |
|
1929 | 142 |
|
143 |
mapM_ (processAction (clID, serverInfo, newClients, newRooms)) $ answerOthersQuit ++ answerInformRoom |
|
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
144 |
writeChan (sendChan $ clients ! clID) ["BYE", msg] |
1804 | 145 |
return ( |
146 |
0, |
|
147 |
serverInfo, |
|
1929 | 148 |
delete clID newClients, |
1823 | 149 |
adjust (\r -> r{ |
150 |
playersIDs = IntSet.delete clID (playersIDs r), |
|
151 |
playersIn = (playersIn r) - 1, |
|
152 |
readyPlayers = if isReady client then readyPlayers r - 1 else readyPlayers r |
|
1930 | 153 |
}) (roomID $ newClients ! clID) newRooms |
1804 | 154 |
) |
155 |
where |
|
156 |
client = clients ! clID |
|
157 |
clientNick = nick client |
|
158 |
answerInformRoom = |
|
159 |
if roomID client /= 0 then |
|
160 |
if not $ Prelude.null msg then |
|
161 |
[AnswerThisRoom ["LEFT", clientNick, msg]] |
|
162 |
else |
|
163 |
[AnswerThisRoom ["LEFT", clientNick]] |
|
164 |
else |
|
165 |
[] |
|
166 |
answerOthersQuit = |
|
1846 | 167 |
if logonPassed client then |
1804 | 168 |
if not $ Prelude.null msg then |
169 |
[AnswerAll ["LOBBY:LEFT", clientNick, msg]] |
|
170 |
else |
|
171 |
[AnswerAll ["LOBBY:LEFT", clientNick]] |
|
172 |
else |
|
173 |
[] |
|
174 |
||
175 |
||
2352 | 176 |
processAction (clID, serverInfo, clients, rooms) (ModifyClient func) = |
1804 | 177 |
return (clID, serverInfo, adjust func clID clients, rooms) |
178 |
||
179 |
||
2403 | 180 |
processAction (clID, serverInfo, clients, rooms) (ModifyClient2 cl2ID func) = |
181 |
return (clID, serverInfo, adjust func cl2ID clients, rooms) |
|
182 |
||
183 |
||
2352 | 184 |
processAction (clID, serverInfo, clients, rooms) (ModifyRoom func) = |
1804 | 185 |
return (clID, serverInfo, clients, adjust func rID rooms) |
186 |
where |
|
187 |
rID = roomID $ clients ! clID |
|
188 |
||
189 |
||
2352 | 190 |
processAction (clID, serverInfo, clients, rooms) (ModifyServerInfo func) = |
1925 | 191 |
return (clID, func serverInfo, clients, rooms) |
192 |
||
193 |
||
2352 | 194 |
processAction (clID, serverInfo, clients, rooms) (RoomAddThisClient rID) = |
1804 | 195 |
processAction ( |
196 |
clID, |
|
197 |
serverInfo, |
|
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2172
diff
changeset
|
198 |
adjust (\cl -> cl{roomID = rID, teamsInGame = if rID == 0 then teamsInGame cl else 0}) clID clients, |
1804 | 199 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $ |
200 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms |
|
201 |
) joinMsg |
|
202 |
where |
|
203 |
client = clients ! clID |
|
204 |
joinMsg = if rID == 0 then |
|
205 |
AnswerAllOthers ["LOBBY:JOINED", nick client] |
|
206 |
else |
|
207 |
AnswerThisRoom ["JOINED", nick client] |
|
208 |
||
209 |
||
2126 | 210 |
processAction (clID, serverInfo, clients, rooms) (RoomRemoveThisClient msg) = do |
1977 | 211 |
(_, _, newClients, newRooms) <- |
2337
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
212 |
if roomID client /= 0 then |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
213 |
if isMaster client then |
2341
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
214 |
if (gameinprogress room) && (playersIn room > 1) then |
2343
3ab763dc14a3
Send leaving message and remove room admin's teams when he exits
unc0rr
parents:
2341
diff
changeset
|
215 |
(changeMaster >>= (\state -> foldM processAction state |
3ab763dc14a3
Send leaving message and remove room admin's teams when he exits
unc0rr
parents:
2341
diff
changeset
|
216 |
[AnswerOthersInRoom ["LEFT", nick client, msg], |
2346 | 217 |
AnswerOthersInRoom ["WARNING", "Admin left the room"], |
2343
3ab763dc14a3
Send leaving message and remove room admin's teams when he exits
unc0rr
parents:
2341
diff
changeset
|
218 |
RemoveClientTeams clID])) |
2337
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
219 |
else -- not in game |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
220 |
processAction (clID, serverInfo, clients, rooms) RemoveRoom |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
221 |
else -- not master |
1977 | 222 |
foldM |
223 |
processAction |
|
224 |
(clID, serverInfo, clients, rooms) |
|
2126 | 225 |
[AnswerOthersInRoom ["LEFT", nick client, msg], |
1977 | 226 |
RemoveClientTeams clID] |
2337
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
227 |
else -- in lobby |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
228 |
return (clID, serverInfo, clients, rooms) |
1977 | 229 |
|
1804 | 230 |
return ( |
231 |
clID, |
|
232 |
serverInfo, |
|
2337
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
233 |
adjust resetClientFlags clID newClients, |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
234 |
adjust removeClientFromRoom rID $ adjust insertClientToRoom 0 newRooms |
1804 | 235 |
) |
236 |
where |
|
237 |
rID = roomID client |
|
238 |
client = clients ! clID |
|
2337
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
239 |
room = rooms ! rID |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
240 |
resetClientFlags cl = cl{roomID = 0, isMaster = False, isReady = False, teamsInGame = undefined} |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
241 |
removeClientFromRoom r = r{ |
2341
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
242 |
playersIDs = otherPlayersSet, |
2337
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
243 |
playersIn = (playersIn r) - 1, |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
244 |
readyPlayers = if isReady client then (readyPlayers r) - 1 else readyPlayers r |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
245 |
} |
723f1cbe2ef3
Some preparation for room control delegation support
unc0rr
parents:
2245
diff
changeset
|
246 |
insertClientToRoom r = r{playersIDs = IntSet.insert clID (playersIDs r)} |
2341
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
247 |
changeMaster = do |
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
248 |
processAction (newMasterId, serverInfo, clients, rooms) $ AnswerThisClient ["ROOM_CONTROL_ACCESS", "1"] |
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
249 |
return ( |
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
250 |
clID, |
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
251 |
serverInfo, |
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
252 |
adjust (\cl -> cl{isMaster = True}) newMasterId clients, |
2408 | 253 |
adjust (\r -> r{masterID = newMasterId, name = newRoomName}) rID rooms |
2341
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
254 |
) |
2345
daf1785f2337
- Frontend: reorganize code controlling widgets state, fix problems getting room admin status
unc0rr
parents:
2343
diff
changeset
|
255 |
newRoomName = nick newMasterClient |
2341
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
256 |
otherPlayersSet = IntSet.delete clID (playersIDs room) |
408edb2f254c
Implement room delegation (not tested, only one predefined name to rename room)
unc0rr
parents:
2337
diff
changeset
|
257 |
newMasterId = IntSet.findMin otherPlayersSet |
2345
daf1785f2337
- Frontend: reorganize code controlling widgets state, fix problems getting room admin status
unc0rr
parents:
2343
diff
changeset
|
258 |
newMasterClient = clients ! newMasterId |
1804 | 259 |
|
260 |
||
261 |
processAction (clID, serverInfo, clients, rooms) (AddRoom roomName roomPassword) = do |
|
262 |
let newServerInfo = serverInfo {nextRoomID = newID} |
|
263 |
let room = newRoom{ |
|
264 |
roomUID = newID, |
|
2408 | 265 |
masterID = clID, |
1804 | 266 |
name = roomName, |
267 |
password = roomPassword, |
|
268 |
roomProto = (clientProto client) |
|
269 |
} |
|
270 |
||
271 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "ADD", roomName] |
|
272 |
||
273 |
processAction ( |
|
274 |
clID, |
|
275 |
newServerInfo, |
|
276 |
adjust (\cl -> cl{isMaster = True}) clID clients, |
|
277 |
insert newID room rooms |
|
278 |
) $ RoomAddThisClient newID |
|
279 |
where |
|
280 |
newID = (nextRoomID serverInfo) - 1 |
|
281 |
client = clients ! clID |
|
282 |
||
283 |
||
284 |
processAction (clID, serverInfo, clients, rooms) (RemoveRoom) = do |
|
1811 | 285 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "DEL", name room] |
286 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["ROOMABANDONED", name room] |
|
1804 | 287 |
return (clID, |
288 |
serverInfo, |
|
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2172
diff
changeset
|
289 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{roomID = 0, isMaster = False, isReady = False, teamsInGame = undefined} else cl) clients, |
1811 | 290 |
delete rID $ adjust (\r -> r{playersIDs = IntSet.union (playersIDs room) (playersIDs r)}) 0 rooms |
1804 | 291 |
) |
292 |
where |
|
1811 | 293 |
room = rooms ! rID |
1804 | 294 |
rID = roomID client |
295 |
client = clients ! clID |
|
296 |
||
1813 | 297 |
|
1811 | 298 |
processAction (clID, serverInfo, clients, rooms) (UnreadyRoomClients) = do |
299 |
processAction (clID, serverInfo, clients, rooms) $ AnswerThisRoom ("NOT_READY" : roomPlayers) |
|
300 |
return (clID, |
|
301 |
serverInfo, |
|
302 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{isReady = False} else cl) clients, |
|
1827 | 303 |
adjust (\r -> r{readyPlayers = 0}) rID rooms) |
1811 | 304 |
where |
305 |
room = rooms ! rID |
|
306 |
rID = roomID client |
|
307 |
client = clients ! clID |
|
308 |
roomPlayers = Prelude.map (nick . (clients !)) roomPlayersIDs |
|
309 |
roomPlayersIDs = IntSet.elems $ playersIDs room |
|
310 |
||
311 |
||
1813 | 312 |
processAction (clID, serverInfo, clients, rooms) (RemoveTeam teamName) = do |
313 |
newRooms <- if not $ gameinprogress room then |
|
314 |
do |
|
315 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["REMOVE_TEAM", teamName] |
|
316 |
return $ |
|
317 |
adjust (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) rID rooms |
|
318 |
else |
|
319 |
do |
|
1866 | 320 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["EM", rmTeamMsg] |
1813 | 321 |
return $ |
322 |
adjust (\r -> r{ |
|
323 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r, |
|
324 |
leftTeams = teamName : leftTeams r, |
|
325 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg |
|
326 |
}) rID rooms |
|
327 |
return (clID, serverInfo, clients, newRooms) |
|
328 |
where |
|
329 |
room = rooms ! rID |
|
330 |
rID = roomID client |
|
331 |
client = clients ! clID |
|
332 |
rmTeamMsg = toEngineMsg $ 'F' : teamName |
|
333 |
||
334 |
||
1834 | 335 |
processAction (clID, serverInfo, clients, rooms) (CheckRegistered) = do |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
2104
diff
changeset
|
336 |
writeChan (dbQueries serverInfo) $ CheckAccount (clientUID client) (nick client) (host client) |
1834 | 337 |
return (clID, serverInfo, clients, rooms) |
338 |
where |
|
339 |
client = clients ! clID |
|
340 |
||
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
341 |
|
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2126
diff
changeset
|
342 |
processAction (clID, serverInfo, clients, rooms) (ClearAccountsCache) = do |
2352 | 343 |
writeChan (dbQueries serverInfo) ClearCache |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2126
diff
changeset
|
344 |
return (clID, serverInfo, clients, rooms) |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2126
diff
changeset
|
345 |
where |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2126
diff
changeset
|
346 |
client = clients ! clID |
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2126
diff
changeset
|
347 |
|
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2126
diff
changeset
|
348 |
|
1804 | 349 |
processAction (clID, serverInfo, clients, rooms) (Dump) = do |
350 |
writeChan (sendChan $ clients ! clID) ["DUMP", show serverInfo, showTree clients, showTree rooms] |
|
351 |
return (clID, serverInfo, clients, rooms) |
|
352 |
||
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
353 |
|
2352 | 354 |
processAction (clID, serverInfo, clients, rooms) (ProcessAccountInfo info) = |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
355 |
case info of |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1846
diff
changeset
|
356 |
HasAccount passwd isAdmin -> do |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
357 |
infoM "Clients" $ show clID ++ " has account" |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
358 |
writeChan (sendChan $ clients ! clID) ["ASKPASSWORD"] |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1846
diff
changeset
|
359 |
return (clID, serverInfo, adjust (\cl -> cl{webPassword = passwd, isAdministrator = isAdmin}) clID clients, rooms) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
360 |
Guest -> do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
361 |
infoM "Clients" $ show clID ++ " is guest" |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
362 |
processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True}) clID clients, rooms) MoveToLobby |
1921 | 363 |
Admin -> do |
364 |
infoM "Clients" $ show clID ++ " is admin" |
|
365 |
foldM processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True, isAdministrator = True}) clID clients, rooms) [MoveToLobby, AnswerThisClient ["ADMIN_ACCESS"]] |
|
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
366 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
367 |
|
2352 | 368 |
processAction (clID, serverInfo, clients, rooms) (MoveToLobby) = |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
369 |
foldM processAction (clID, serverInfo, clients, rooms) $ |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
370 |
(RoomAddThisClient 0) |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
371 |
: answerLobbyNicks |
2118
0ebcc98ebc1a
Send server message after nicks info (more chance for it to be seen)
unc0rr
parents:
2116
diff
changeset
|
372 |
++ [SendServerMessage] |
0ebcc98ebc1a
Send server message after nicks info (more chance for it to be seen)
unc0rr
parents:
2116
diff
changeset
|
373 |
|
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
374 |
-- ++ (answerServerMessage client clients) |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
375 |
where |
1846 | 376 |
lobbyNicks = Prelude.map nick $ Prelude.filter logonPassed $ elems clients |
2352 | 377 |
answerLobbyNicks = [AnswerThisClient ("LOBBY:JOINED": lobbyNicks) | not $ Prelude.null lobbyNicks] |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
378 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
379 |
|
2352 | 380 |
processAction (clID, serverInfo, clients, rooms) (KickClient kickID) = |
1879 | 381 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ ByeClient "Kicked") |
382 |
||
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
383 |
|
2352 | 384 |
processAction (clID, serverInfo, clients, rooms) (BanClient banNick) = |
1921 | 385 |
return (clID, serverInfo, clients, rooms) |
386 |
||
1879 | 387 |
|
388 |
processAction (clID, serverInfo, clients, rooms) (KickRoomClient kickID) = do |
|
389 |
writeChan (sendChan $ clients ! kickID) ["KICKED"] |
|
2126 | 390 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ RoomRemoveThisClient "kicked") |
1879 | 391 |
|
392 |
||
2352 | 393 |
processAction (clID, serverInfo, clients, rooms) (RemoveClientTeams teamsClID) = |
1879 | 394 |
liftM2 replaceID (return clID) $ |
2352 | 395 |
foldM processAction (teamsClID, serverInfo, clients, rooms) removeTeamsActions |
1879 | 396 |
where |
397 |
client = clients ! teamsClID |
|
398 |
room = rooms ! (roomID client) |
|
399 |
teamsToRemove = Prelude.filter (\t -> teamowner t == nick client) $ teams room |
|
400 |
removeTeamsActions = Prelude.map (RemoveTeam . teamname) teamsToRemove |
|
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
401 |
|
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
402 |
|
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
403 |
processAction (clID, serverInfo, clients, rooms) (AddClient client) = do |
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
404 |
let updatedClients = insert (clientUID client) client clients |
2352 | 405 |
infoM "Clients" (show (clientUID client) ++ ": New client. Time: " ++ show (connectTime client)) |
406 |
writeChan (sendChan client) ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"] |
|
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
407 |
|
1977 | 408 |
let newLogins = takeWhile (\(_ , time) -> (connectTime client) `diffUTCTime` time <= 11) $ lastLogins serverInfo |
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
409 |
|
1931 | 410 |
if isJust $ host client `Prelude.lookup` newLogins then |
411 |
processAction (clID, serverInfo{lastLogins = newLogins}, updatedClients, rooms) $ ByeClient "Reconnected too fast" |
|
412 |
else |
|
413 |
return (clID, serverInfo{lastLogins = (host client, connectTime client) : newLogins}, updatedClients, rooms) |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
414 |
|
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
415 |
|
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
416 |
processAction (clID, serverInfo, clients, rooms) PingAll = do |
1928 | 417 |
(_, _, newClients, newRooms) <- foldM kickTimeouted (clID, serverInfo, clients, rooms) $ elems clients |
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
418 |
processAction (clID, |
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
419 |
serverInfo, |
1928 | 420 |
Data.IntMap.map (\cl -> cl{pingsQueue = pingsQueue cl + 1}) newClients, |
421 |
newRooms) $ AnswerAll ["PING"] |
|
422 |
where |
|
423 |
kickTimeouted (clID, serverInfo, clients, rooms) client = |
|
424 |
if pingsQueue client > 0 then |
|
425 |
processAction (clientUID client, serverInfo, clients, rooms) $ ByeClient "Ping timeout" |
|
426 |
else |
|
427 |
return (clID, serverInfo, clients, rooms) |
|
2172 | 428 |
|
429 |
||
430 |
processAction (clID, serverInfo, clients, rooms) (StatsAction) = do |
|
431 |
writeChan (dbQueries serverInfo) $ SendStats (size clients) (size rooms - 1) |
|
432 |
return (clID, serverInfo, clients, rooms) |