author | Xeli |
Thu, 04 Aug 2011 17:19:27 +0200 | |
branch | hedgeroid |
changeset 5454 | 7ff6325d0055 |
parent 5214 | d2ad737891b0 |
child 5426 | 109e9b5761c2 |
permissions | -rw-r--r-- |
5184 | 1 |
{-# LANGUAGE OverloadedStrings #-} |
2 |
module Actions where |
|
3 |
||
4 |
import Control.Concurrent |
|
5 |
import qualified Data.Set as Set |
|
6 |
import qualified Data.Sequence as Seq |
|
7 |
import qualified Data.List as L |
|
8 |
import qualified Control.Exception as Exception |
|
9 |
import System.Log.Logger |
|
10 |
import Control.Monad |
|
11 |
import Data.Time |
|
12 |
import Data.Maybe |
|
13 |
import Control.Monad.Reader |
|
14 |
import Control.Monad.State.Strict |
|
15 |
import qualified Data.ByteString.Char8 as B |
|
16 |
import Control.DeepSeq |
|
17 |
import Data.Unique |
|
18 |
import Control.Arrow |
|
19 |
import Control.Exception |
|
20 |
import OfficialServer.GameReplayStore |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
21 |
import System.Process |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
22 |
import Network.Socket |
5184 | 23 |
----------------------------- |
24 |
import CoreTypes |
|
25 |
import Utils |
|
26 |
import ClientIO |
|
27 |
import ServerState |
|
28 |
import Consts |
|
29 |
import ConfigFile |
|
30 |
||
31 |
data Action = |
|
32 |
AnswerClients ![ClientChan] ![B.ByteString] |
|
33 |
| SendServerMessage |
|
34 |
| SendServerVars |
|
35 |
| MoveToRoom RoomIndex |
|
36 |
| MoveToLobby B.ByteString |
|
37 |
| RemoveTeam B.ByteString |
|
38 |
| RemoveRoom |
|
39 |
| UnreadyRoomClients |
|
40 |
| JoinLobby |
|
41 |
| ProtocolError B.ByteString |
|
42 |
| Warning B.ByteString |
|
43 |
| NoticeMessage Notice |
|
44 |
| ByeClient B.ByteString |
|
45 |
| KickClient ClientIndex |
|
46 |
| KickRoomClient ClientIndex |
|
47 |
| BanClient NominalDiffTime B.ByteString ClientIndex |
|
48 |
| ChangeMaster |
|
49 |
| RemoveClientTeams ClientIndex |
|
50 |
| ModifyClient (ClientInfo -> ClientInfo) |
|
51 |
| ModifyClient2 ClientIndex (ClientInfo -> ClientInfo) |
|
52 |
| ModifyRoom (RoomInfo -> RoomInfo) |
|
53 |
| ModifyServerInfo (ServerInfo -> ServerInfo) |
|
54 |
| AddRoom B.ByteString B.ByteString |
|
55 |
| CheckRegistered |
|
56 |
| ClearAccountsCache |
|
57 |
| ProcessAccountInfo AccountInfo |
|
58 |
| AddClient ClientInfo |
|
59 |
| DeleteClient ClientIndex |
|
60 |
| PingAll |
|
61 |
| StatsAction |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
62 |
| RestartServer |
5184 | 63 |
| AddNick2Bans B.ByteString B.ByteString UTCTime |
64 |
| AddIP2Bans B.ByteString B.ByteString UTCTime |
|
65 |
| CheckBanned |
|
66 |
| SaveReplay |
|
67 |
||
68 |
||
69 |
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
|
70 |
||
71 |
instance NFData Action where |
|
72 |
rnf (AnswerClients chans msg) = chans `deepseq` msg `deepseq` () |
|
73 |
rnf a = a `seq` () |
|
74 |
||
75 |
instance NFData B.ByteString |
|
76 |
instance NFData (Chan a) |
|
77 |
||
78 |
||
79 |
othersChans :: StateT ServerState IO [ClientChan] |
|
80 |
othersChans = do |
|
81 |
cl <- client's id |
|
82 |
ri <- clientRoomA |
|
83 |
liftM (map sendChan . filter (/= cl)) $ roomClientsS ri |
|
84 |
||
85 |
processAction :: Action -> StateT ServerState IO () |
|
86 |
||
87 |
||
88 |
processAction (AnswerClients chans msg) = |
|
89 |
io $ mapM_ (`writeChan` (msg `deepseq` msg)) (chans `deepseq` chans) |
|
90 |
||
91 |
||
92 |
processAction SendServerMessage = do |
|
93 |
chan <- client's sendChan |
|
94 |
protonum <- client's clientProto |
|
95 |
si <- liftM serverInfo get |
|
96 |
let message = if protonum < latestReleaseVersion si then |
|
97 |
serverMessageForOldVersions si |
|
98 |
else |
|
99 |
serverMessage si |
|
100 |
processAction $ AnswerClients [chan] ["SERVER_MESSAGE", message] |
|
101 |
||
102 |
||
103 |
processAction SendServerVars = do |
|
104 |
chan <- client's sendChan |
|
105 |
si <- gets serverInfo |
|
106 |
io $ writeChan chan ("SERVER_VARS" : vars si) |
|
107 |
where |
|
108 |
vars si = [ |
|
109 |
"MOTD_NEW", serverMessage si, |
|
110 |
"MOTD_OLD", serverMessageForOldVersions si, |
|
111 |
"LATEST_PROTO", showB $ latestReleaseVersion si |
|
112 |
] |
|
113 |
||
114 |
||
115 |
processAction (ProtocolError msg) = do |
|
116 |
chan <- client's sendChan |
|
117 |
processAction $ AnswerClients [chan] ["ERROR", msg] |
|
118 |
||
119 |
||
120 |
processAction (Warning msg) = do |
|
121 |
chan <- client's sendChan |
|
122 |
processAction $ AnswerClients [chan] ["WARNING", msg] |
|
123 |
||
124 |
processAction (NoticeMessage n) = do |
|
125 |
chan <- client's sendChan |
|
126 |
processAction $ AnswerClients [chan] ["NOTICE", showB . fromEnum $ n] |
|
127 |
||
128 |
processAction (ByeClient msg) = do |
|
129 |
(Just ci) <- gets clientIndex |
|
130 |
ri <- clientRoomA |
|
131 |
||
132 |
chan <- client's sendChan |
|
133 |
clNick <- client's nick |
|
134 |
loggedIn <- client's logonPassed |
|
135 |
||
136 |
when (ri /= lobbyId) $ do |
|
137 |
processAction $ MoveToLobby ("quit: " `B.append` msg) |
|
138 |
return () |
|
139 |
||
140 |
clientsChans <- liftM (Prelude.map sendChan . Prelude.filter logonPassed) $! allClientsS |
|
141 |
io $ |
|
142 |
infoM "Clients" (show ci ++ " quits: " ++ B.unpack msg) |
|
143 |
||
144 |
processAction $ AnswerClients [chan] ["BYE", msg] |
|
145 |
when loggedIn $ processAction $ AnswerClients clientsChans ["LOBBY:LEFT", clNick, msg] |
|
146 |
||
147 |
s <- get |
|
148 |
put $! s{removedClients = ci `Set.insert` removedClients s} |
|
149 |
||
150 |
processAction (DeleteClient ci) = do |
|
151 |
io $ debugM "Clients" $ "DeleteClient: " ++ show ci |
|
152 |
||
153 |
rnc <- gets roomsClients |
|
154 |
io $ removeClient rnc ci |
|
155 |
||
156 |
s <- get |
|
157 |
put $! s{removedClients = ci `Set.delete` removedClients s} |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
158 |
|
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
159 |
sp <- gets (shutdownPending . serverInfo) |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
160 |
cls <- allClientsS |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5184
diff
changeset
|
161 |
io $ when (sp && null cls) $ throwIO ShutdownException |
5184 | 162 |
|
163 |
processAction (ModifyClient f) = do |
|
164 |
(Just ci) <- gets clientIndex |
|
165 |
rnc <- gets roomsClients |
|
166 |
io $ modifyClient rnc f ci |
|
167 |
return () |
|
168 |
||
169 |
processAction (ModifyClient2 ci f) = do |
|
170 |
rnc <- gets roomsClients |
|
171 |
io $ modifyClient rnc f ci |
|
172 |
return () |
|
173 |
||
174 |
||
175 |
processAction (ModifyRoom f) = do |
|
176 |
rnc <- gets roomsClients |
|
177 |
ri <- clientRoomA |
|
178 |
io $ modifyRoom rnc f ri |
|
179 |
return () |
|
180 |
||
181 |
||
182 |
processAction (ModifyServerInfo f) = do |
|
183 |
modify (\s -> s{serverInfo = f $ serverInfo s}) |
|
184 |
si <- gets serverInfo |
|
185 |
io $ writeServerConfig si |
|
186 |
||
187 |
||
188 |
processAction (MoveToRoom ri) = do |
|
189 |
(Just ci) <- gets clientIndex |
|
190 |
rnc <- gets roomsClients |
|
191 |
||
192 |
io $ do |
|
193 |
modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False}) ci |
|
194 |
modifyRoom rnc (\r -> r{playersIn = playersIn r + 1}) ri |
|
195 |
moveClientToRoom rnc ri ci |
|
196 |
||
197 |
chans <- liftM (map sendChan) $ roomClientsS ri |
|
198 |
clNick <- client's nick |
|
199 |
||
200 |
processAction $ AnswerClients chans ["JOINED", clNick] |
|
201 |
||
202 |
||
203 |
processAction (MoveToLobby msg) = do |
|
204 |
(Just ci) <- gets clientIndex |
|
205 |
ri <- clientRoomA |
|
206 |
rnc <- gets roomsClients |
|
207 |
(gameProgress, playersNum) <- io $ room'sM rnc (gameinprogress &&& playersIn) ri |
|
208 |
ready <- client's isReady |
|
209 |
master <- client's isMaster |
|
210 |
-- client <- client's id |
|
211 |
clNick <- client's nick |
|
212 |
chans <- othersChans |
|
213 |
||
214 |
if master then |
|
215 |
if gameProgress && playersNum > 1 then |
|
216 |
mapM_ processAction [ChangeMaster, AnswerClients chans ["LEFT", clNick, msg], NoticeMessage AdminLeft, RemoveClientTeams ci] |
|
217 |
else |
|
218 |
processAction RemoveRoom |
|
219 |
else |
|
220 |
mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci] |
|
221 |
||
222 |
-- when not removing room |
|
223 |
when (not master || (gameProgress && playersNum > 1)) . io $ do |
|
224 |
modifyRoom rnc (\r -> r{ |
|
225 |
playersIn = playersIn r - 1, |
|
226 |
readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r |
|
227 |
}) ri |
|
228 |
moveClientToLobby rnc ci |
|
229 |
||
230 |
processAction ChangeMaster = do |
|
231 |
(Just ci) <- gets clientIndex |
|
232 |
ri <- clientRoomA |
|
233 |
rnc <- gets roomsClients |
|
234 |
newMasterId <- liftM (head . filter (/= ci)) . io $ roomClientsIndicesM rnc ri |
|
235 |
newMaster <- io $ client'sM rnc id newMasterId |
|
236 |
let newRoomName = nick newMaster |
|
237 |
mapM_ processAction [ |
|
238 |
ModifyRoom (\r -> r{masterID = newMasterId, name = newRoomName}), |
|
239 |
ModifyClient2 newMasterId (\c -> c{isMaster = True}), |
|
240 |
AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"] |
|
241 |
] |
|
242 |
||
243 |
processAction (AddRoom roomName roomPassword) = do |
|
244 |
Just clId <- gets clientIndex |
|
245 |
rnc <- gets roomsClients |
|
246 |
proto <- io $ client'sM rnc clientProto clId |
|
247 |
||
248 |
let rm = newRoom{ |
|
249 |
masterID = clId, |
|
250 |
name = roomName, |
|
251 |
password = roomPassword, |
|
252 |
roomProto = proto |
|
253 |
} |
|
254 |
||
255 |
rId <- io $ addRoom rnc rm |
|
256 |
||
257 |
processAction $ MoveToRoom rId |
|
258 |
||
259 |
chans <- liftM (map sendChan) $! roomClientsS lobbyId |
|
260 |
||
261 |
mapM_ processAction [ |
|
262 |
AnswerClients chans ["ROOM", "ADD", roomName] |
|
263 |
, ModifyClient (\cl -> cl{isMaster = True}) |
|
264 |
] |
|
265 |
||
266 |
||
267 |
processAction RemoveRoom = do |
|
268 |
Just clId <- gets clientIndex |
|
269 |
rnc <- gets roomsClients |
|
270 |
ri <- io $ clientRoomM rnc clId |
|
271 |
roomName <- io $ room'sM rnc name ri |
|
272 |
others <- othersChans |
|
273 |
lobbyChans <- liftM (map sendChan) $! roomClientsS lobbyId |
|
274 |
||
275 |
mapM_ processAction [ |
|
276 |
AnswerClients lobbyChans ["ROOM", "DEL", roomName], |
|
277 |
AnswerClients others ["ROOMABANDONED", roomName] |
|
278 |
] |
|
279 |
||
280 |
io $ removeRoom rnc ri |
|
281 |
||
282 |
||
283 |
processAction (UnreadyRoomClients) = do |
|
284 |
rnc <- gets roomsClients |
|
285 |
ri <- clientRoomA |
|
286 |
roomPlayers <- roomClientsS ri |
|
287 |
roomClIDs <- io $ roomClientsIndicesM rnc ri |
|
288 |
pr <- client's clientProto |
|
289 |
processAction $ AnswerClients (map sendChan roomPlayers) $ notReadyMessage pr (map nick roomPlayers) |
|
290 |
io $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs |
|
291 |
processAction $ ModifyRoom (\r -> r{readyPlayers = 0}) |
|
292 |
where |
|
293 |
notReadyMessage p nicks = if p < 38 then "NOT_READY" : nicks else "CLIENT_FLAGS" : "-r" : nicks |
|
294 |
||
295 |
||
296 |
processAction (RemoveTeam teamName) = do |
|
297 |
rnc <- gets roomsClients |
|
298 |
ri <- clientRoomA |
|
299 |
inGame <- io $ room'sM rnc gameinprogress ri |
|
300 |
chans <- othersChans |
|
301 |
if not $ inGame then |
|
302 |
mapM_ processAction [ |
|
303 |
AnswerClients chans ["REMOVE_TEAM", teamName], |
|
304 |
ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) |
|
305 |
] |
|
306 |
else |
|
307 |
mapM_ processAction [ |
|
308 |
AnswerClients chans ["EM", rmTeamMsg], |
|
309 |
ModifyRoom (\r -> r{ |
|
310 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r, |
|
311 |
leftTeams = teamName : leftTeams r, |
|
312 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg |
|
313 |
}) |
|
314 |
] |
|
315 |
where |
|
316 |
rmTeamMsg = toEngineMsg $ 'F' `B.cons` teamName |
|
317 |
||
318 |
||
319 |
processAction (RemoveClientTeams clId) = do |
|
320 |
rnc <- gets roomsClients |
|
321 |
||
322 |
removeTeamActions <- io $ do |
|
323 |
clNick <- client'sM rnc nick clId |
|
324 |
rId <- clientRoomM rnc clId |
|
325 |
roomTeams <- room'sM rnc teams rId |
|
326 |
return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams |
|
327 |
||
328 |
mapM_ processAction removeTeamActions |
|
329 |
||
330 |
||
331 |
||
332 |
processAction CheckRegistered = do |
|
333 |
(Just ci) <- gets clientIndex |
|
334 |
n <- client's nick |
|
335 |
h <- client's host |
|
336 |
p <- client's clientProto |
|
337 |
uid <- client's clUID |
|
338 |
haveSameNick <- liftM (not . null . tail . filter (\c -> nick c == n)) allClientsS |
|
339 |
if haveSameNick then |
|
340 |
if p < 38 then |
|
341 |
mapM_ processAction [ByeClient "Nickname is already in use", removeNick] |
|
342 |
else |
|
343 |
mapM_ processAction [NoticeMessage NickAlreadyInUse, removeNick] |
|
344 |
else |
|
345 |
do |
|
346 |
db <- gets (dbQueries . serverInfo) |
|
347 |
io $ writeChan db $ CheckAccount ci (hashUnique uid) n h |
|
348 |
return () |
|
349 |
where |
|
350 |
removeNick = ModifyClient (\c -> c{nick = ""}) |
|
351 |
||
352 |
||
353 |
processAction ClearAccountsCache = do |
|
354 |
dbq <- gets (dbQueries . serverInfo) |
|
355 |
io $ writeChan dbq ClearCache |
|
356 |
return () |
|
357 |
||
358 |
||
359 |
processAction (ProcessAccountInfo info) = |
|
360 |
case info of |
|
361 |
HasAccount passwd isAdmin -> do |
|
362 |
chan <- client's sendChan |
|
363 |
mapM_ processAction [AnswerClients [chan] ["ASKPASSWORD"], ModifyClient (\c -> c{webPassword = passwd, isAdministrator = isAdmin})] |
|
364 |
Guest -> |
|
365 |
processAction JoinLobby |
|
366 |
Admin -> do |
|
367 |
mapM_ processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby] |
|
368 |
chan <- client's sendChan |
|
369 |
processAction $ AnswerClients [chan] ["ADMIN_ACCESS"] |
|
370 |
||
371 |
||
372 |
processAction JoinLobby = do |
|
373 |
chan <- client's sendChan |
|
374 |
clientNick <- client's nick |
|
375 |
(lobbyNicks, clientsChans) <- liftM (unzip . Prelude.map (nick &&& sendChan) . Prelude.filter logonPassed) $! allClientsS |
|
376 |
mapM_ processAction $ |
|
377 |
AnswerClients clientsChans ["LOBBY:JOINED", clientNick] |
|
378 |
: AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks) |
|
379 |
: [ModifyClient (\cl -> cl{logonPassed = True}), SendServerMessage] |
|
380 |
||
381 |
||
382 |
processAction (KickClient kickId) = do |
|
383 |
modify (\s -> s{clientIndex = Just kickId}) |
|
5214 | 384 |
clHost <- client's host |
385 |
currentTime <- io getCurrentTime |
|
386 |
mapM_ processAction [ |
|
387 |
AddIP2Bans clHost "60 seconds cooldown after kick" (addUTCTime 60 currentTime), |
|
388 |
ByeClient "Kicked" |
|
389 |
] |
|
5184 | 390 |
|
391 |
||
392 |
processAction (BanClient seconds reason banId) = do |
|
393 |
modify (\s -> s{clientIndex = Just banId}) |
|
394 |
clHost <- client's host |
|
395 |
currentTime <- io getCurrentTime |
|
396 |
let msg = B.concat ["Ban for ", B.pack . show $ seconds, "seconds (", reason, ")"] |
|
397 |
mapM_ processAction [ |
|
398 |
AddIP2Bans clHost msg (addUTCTime seconds currentTime) |
|
399 |
, KickClient banId |
|
400 |
] |
|
401 |
||
402 |
||
403 |
processAction (KickRoomClient kickId) = do |
|
404 |
modify (\s -> s{clientIndex = Just kickId}) |
|
405 |
ch <- client's sendChan |
|
406 |
mapM_ processAction [AnswerClients [ch] ["KICKED"], MoveToLobby "kicked"] |
|
407 |
||
408 |
||
409 |
processAction (AddClient cl) = do |
|
410 |
rnc <- gets roomsClients |
|
411 |
si <- gets serverInfo |
|
412 |
newClId <- io $ do |
|
413 |
ci <- addClient rnc cl |
|
414 |
_ <- Exception.mask (forkIO . clientRecvLoop (clientSocket cl) (coreChan si) (sendChan cl) ci) |
|
415 |
||
416 |
infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime cl)) |
|
417 |
||
418 |
return ci |
|
419 |
||
420 |
modify (\s -> s{clientIndex = Just newClId}) |
|
421 |
mapM_ processAction |
|
422 |
[ |
|
423 |
AnswerClients [sendChan cl] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/", serverVersion] |
|
424 |
, CheckBanned |
|
425 |
, AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl) |
|
426 |
] |
|
427 |
||
428 |
||
429 |
processAction (AddNick2Bans n reason expiring) = do |
|
430 |
processAction $ ModifyServerInfo (\s -> s{bans = BanByNick n reason expiring : bans s}) |
|
431 |
||
432 |
processAction (AddIP2Bans ip reason expiring) = do |
|
433 |
(Just ci) <- gets clientIndex |
|
434 |
rc <- gets removedClients |
|
435 |
when (not $ ci `Set.member` rc) |
|
436 |
$ processAction $ ModifyServerInfo (\s -> s{bans = BanByIP ip reason expiring : bans s}) |
|
437 |
||
438 |
processAction CheckBanned = do |
|
439 |
clTime <- client's connectTime |
|
440 |
clNick <- client's nick |
|
441 |
clHost <- client's host |
|
442 |
si <- gets serverInfo |
|
443 |
let validBans = filter (checkNotExpired clTime) $ bans si |
|
444 |
let ban = L.find (checkBan clHost clNick) $ validBans |
|
445 |
when (isJust ban) $ |
|
446 |
mapM_ processAction [ |
|
447 |
ModifyServerInfo (\s -> s{bans = validBans}) |
|
448 |
, ByeClient (getBanReason $ fromJust ban) |
|
449 |
] |
|
450 |
where |
|
451 |
checkNotExpired testTime (BanByIP _ _ time) = testTime `diffUTCTime` time <= 0 |
|
452 |
checkNotExpired testTime (BanByNick _ _ time) = testTime `diffUTCTime` time <= 0 |
|
453 |
checkBan ip _ (BanByIP bip _ _) = bip == ip |
|
454 |
checkBan _ n (BanByNick bn _ _) = bn == n |
|
455 |
getBanReason (BanByIP _ msg _) = msg |
|
456 |
getBanReason (BanByNick _ msg _) = msg |
|
457 |
||
458 |
processAction PingAll = do |
|
459 |
rnc <- gets roomsClients |
|
460 |
io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc) |
|
461 |
cis <- io $ allClientsM rnc |
|
462 |
chans <- io $ mapM (client'sM rnc sendChan) cis |
|
463 |
io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis |
|
464 |
processAction $ AnswerClients chans ["PING"] |
|
465 |
where |
|
466 |
kickTimeouted rnc ci = do |
|
467 |
pq <- io $ client'sM rnc pingsQueue ci |
|
468 |
when (pq > 0) $ |
|
469 |
withStateT (\as -> as{clientIndex = Just ci}) $ |
|
470 |
processAction (ByeClient "Ping timeout") |
|
471 |
||
472 |
||
473 |
processAction StatsAction = do |
|
5211 | 474 |
si <- gets serverInfo |
475 |
when (not $ shutdownPending si) $ do |
|
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
476 |
rnc <- gets roomsClients |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
477 |
(roomsNum, clientsNum) <- io $ withRoomsAndClients rnc st |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
478 |
io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1) |
5184 | 479 |
where |
480 |
st irnc = (length $ allRooms irnc, length $ allClients irnc) |
|
481 |
||
5212
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
482 |
processAction RestartServer = do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
483 |
sp <- gets (shutdownPending . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
484 |
when (not sp) $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
485 |
sock <- gets (fromJust . serverSocket . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
486 |
args <- gets (runArgs . serverInfo) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
487 |
io $ do |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
488 |
noticeM "Core" "Closing listening socket" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
489 |
sClose sock |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
490 |
noticeM "Core" "Spawning new server" |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
491 |
_ <- createProcess (proc "./hedgewars-server" args) |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
492 |
return () |
eaffb02f0053
Don't perform RestartServer action when already did it once
unc0rr
parents:
5211
diff
changeset
|
493 |
processAction $ ModifyServerInfo (\s -> s{shutdownPending = True}) |
5184 | 494 |
|
495 |
processAction SaveReplay = do |
|
496 |
ri <- clientRoomA |
|
497 |
rnc <- gets roomsClients |
|
498 |
io $ do |
|
499 |
r <- room'sM rnc id ri |
|
500 |
saveReplay r |