4904
|
1 |
{-# LANGUAGE OverloadedStrings #-}
|
|
2 |
module Actions where
|
|
3 |
|
|
4 |
import Control.Concurrent
|
|
5 |
import Control.Concurrent.Chan
|
|
6 |
import qualified Data.IntSet as IntSet
|
|
7 |
import qualified Data.Set as Set
|
|
8 |
import qualified Data.Sequence as Seq
|
|
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 |
-----------------------------
|
|
18 |
import CoreTypes
|
|
19 |
import Utils
|
|
20 |
import ClientIO
|
|
21 |
import ServerState
|
|
22 |
|
|
23 |
data Action =
|
|
24 |
AnswerClients ![ClientChan] ![B.ByteString]
|
|
25 |
| SendServerMessage
|
|
26 |
| SendServerVars
|
|
27 |
| MoveToRoom RoomIndex
|
|
28 |
| MoveToLobby B.ByteString
|
|
29 |
| RemoveTeam B.ByteString
|
|
30 |
| RemoveRoom
|
|
31 |
| UnreadyRoomClients
|
|
32 |
| JoinLobby
|
|
33 |
| ProtocolError B.ByteString
|
|
34 |
| Warning B.ByteString
|
|
35 |
| NoticeMessage Notice
|
|
36 |
| ByeClient B.ByteString
|
|
37 |
| KickClient ClientIndex
|
|
38 |
| KickRoomClient ClientIndex
|
|
39 |
| BanClient B.ByteString
|
|
40 |
| ChangeMaster
|
|
41 |
| RemoveClientTeams ClientIndex
|
|
42 |
| ModifyClient (ClientInfo -> ClientInfo)
|
|
43 |
| ModifyClient2 ClientIndex (ClientInfo -> ClientInfo)
|
|
44 |
| ModifyRoom (RoomInfo -> RoomInfo)
|
|
45 |
| ModifyServerInfo (ServerInfo -> ServerInfo)
|
|
46 |
| AddRoom B.ByteString B.ByteString
|
|
47 |
| CheckRegistered
|
|
48 |
| ClearAccountsCache
|
|
49 |
| ProcessAccountInfo AccountInfo
|
|
50 |
| AddClient ClientInfo
|
|
51 |
| DeleteClient ClientIndex
|
|
52 |
| PingAll
|
|
53 |
| StatsAction
|
|
54 |
|
|
55 |
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action]
|
|
56 |
|
|
57 |
instance NFData Action where
|
|
58 |
rnf (AnswerClients chans msg) = chans `deepseq` msg `deepseq` ()
|
|
59 |
rnf a = a `seq` ()
|
|
60 |
|
|
61 |
instance NFData B.ByteString
|
|
62 |
instance NFData (Chan a)
|
|
63 |
|
|
64 |
othersChans = do
|
|
65 |
cl <- client's id
|
|
66 |
ri <- clientRoomA
|
|
67 |
liftM (map sendChan . filter (/= cl)) $ roomClientsS ri
|
|
68 |
|
|
69 |
processAction :: Action -> StateT ServerState IO ()
|
|
70 |
|
|
71 |
|
|
72 |
processAction (AnswerClients chans msg) = do
|
|
73 |
io $ mapM_ (flip writeChan (msg `deepseq` msg)) (chans `deepseq` chans)
|
|
74 |
|
|
75 |
|
|
76 |
processAction SendServerMessage = do
|
|
77 |
chan <- client's sendChan
|
|
78 |
protonum <- client's clientProto
|
|
79 |
si <- liftM serverInfo get
|
|
80 |
let message = if protonum < latestReleaseVersion si then
|
|
81 |
serverMessageForOldVersions si
|
|
82 |
else
|
|
83 |
serverMessage si
|
|
84 |
processAction $ AnswerClients [chan] ["SERVER_MESSAGE", message]
|
|
85 |
|
|
86 |
|
|
87 |
processAction SendServerVars = do
|
|
88 |
chan <- client's sendChan
|
|
89 |
si <- gets serverInfo
|
|
90 |
io $ writeChan chan ("SERVER_VARS" : vars si)
|
|
91 |
where
|
|
92 |
vars si = [
|
|
93 |
"MOTD_NEW", serverMessage si,
|
|
94 |
"MOTD_OLD", serverMessageForOldVersions si,
|
|
95 |
"LATEST_PROTO", B.pack . show $ latestReleaseVersion si
|
|
96 |
]
|
|
97 |
|
|
98 |
|
|
99 |
processAction (ProtocolError msg) = do
|
|
100 |
chan <- client's sendChan
|
|
101 |
processAction $ AnswerClients [chan] ["ERROR", msg]
|
|
102 |
|
|
103 |
|
|
104 |
processAction (Warning msg) = do
|
|
105 |
chan <- client's sendChan
|
|
106 |
processAction $ AnswerClients [chan] ["WARNING", msg]
|
|
107 |
|
|
108 |
processAction (NoticeMessage n) = do
|
|
109 |
chan <- client's sendChan
|
|
110 |
processAction $ AnswerClients [chan] ["NOTICE", B.pack . show . fromEnum $ n]
|
|
111 |
|
|
112 |
processAction (ByeClient msg) = do
|
|
113 |
(Just ci) <- gets clientIndex
|
|
114 |
rnc <- gets roomsClients
|
|
115 |
ri <- clientRoomA
|
|
116 |
|
|
117 |
chan <- client's sendChan
|
|
118 |
clNick <- client's nick
|
|
119 |
|
|
120 |
when (ri /= lobbyId) $ do
|
|
121 |
processAction $ MoveToLobby ("quit: " `B.append` msg)
|
|
122 |
return ()
|
|
123 |
|
|
124 |
clientsChans <- liftM (Prelude.map sendChan . Prelude.filter logonPassed) $! allClientsS
|
|
125 |
io $ do
|
|
126 |
infoM "Clients" (show ci ++ " quits: " ++ (B.unpack msg))
|
|
127 |
|
|
128 |
processAction $ AnswerClients [chan] ["BYE", msg]
|
|
129 |
processAction $ AnswerClients clientsChans ["LOBBY:LEFT", clNick, msg]
|
|
130 |
|
|
131 |
s <- get
|
|
132 |
put $! s{removedClients = ci `Set.insert` removedClients s}
|
|
133 |
|
|
134 |
processAction (DeleteClient ci) = do
|
|
135 |
rnc <- gets roomsClients
|
|
136 |
io $ removeClient rnc ci
|
|
137 |
|
|
138 |
s <- get
|
|
139 |
put $! s{removedClients = ci `Set.delete` removedClients s}
|
|
140 |
|
|
141 |
processAction (ModifyClient f) = do
|
|
142 |
(Just ci) <- gets clientIndex
|
|
143 |
rnc <- gets roomsClients
|
|
144 |
io $ modifyClient rnc f ci
|
|
145 |
return ()
|
|
146 |
|
|
147 |
processAction (ModifyClient2 ci f) = do
|
|
148 |
rnc <- gets roomsClients
|
|
149 |
io $ modifyClient rnc f ci
|
|
150 |
return ()
|
|
151 |
|
|
152 |
|
|
153 |
processAction (ModifyRoom f) = do
|
|
154 |
rnc <- gets roomsClients
|
|
155 |
ri <- clientRoomA
|
|
156 |
io $ modifyRoom rnc f ri
|
|
157 |
return ()
|
|
158 |
|
|
159 |
|
|
160 |
processAction (ModifyServerInfo f) =
|
|
161 |
modify (\s -> s{serverInfo = f $ serverInfo s})
|
|
162 |
|
|
163 |
|
|
164 |
processAction (MoveToRoom ri) = do
|
|
165 |
(Just ci) <- gets clientIndex
|
|
166 |
rnc <- gets roomsClients
|
|
167 |
|
|
168 |
io $ do
|
|
169 |
modifyClient rnc (\cl -> cl{teamsInGame = 0, isReady = False, isMaster = False}) ci
|
|
170 |
modifyRoom rnc (\r -> r{playersIn = (playersIn r) + 1}) ri
|
|
171 |
moveClientToRoom rnc ri ci
|
|
172 |
|
|
173 |
chans <- liftM (map sendChan) $ roomClientsS ri
|
|
174 |
clNick <- client's nick
|
|
175 |
|
|
176 |
processAction $ AnswerClients chans ["JOINED", clNick]
|
|
177 |
|
|
178 |
|
|
179 |
processAction (MoveToLobby msg) = do
|
|
180 |
(Just ci) <- gets clientIndex
|
|
181 |
ri <- clientRoomA
|
|
182 |
rnc <- gets roomsClients
|
|
183 |
(gameProgress, playersNum) <- io $ room'sM rnc (\r -> (gameinprogress r, playersIn r)) ri
|
|
184 |
ready <- client's isReady
|
|
185 |
master <- client's isMaster
|
|
186 |
-- client <- client's id
|
|
187 |
clNick <- client's nick
|
|
188 |
chans <- othersChans
|
|
189 |
|
|
190 |
if master then
|
|
191 |
if gameProgress && playersNum > 1 then
|
|
192 |
mapM_ processAction [ChangeMaster, AnswerClients chans ["LEFT", clNick, msg], NoticeMessage AdminLeft, RemoveClientTeams ci]
|
|
193 |
else
|
|
194 |
processAction RemoveRoom
|
|
195 |
else
|
|
196 |
mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci]
|
|
197 |
|
|
198 |
io $ do
|
|
199 |
modifyRoom rnc (\r -> r{
|
|
200 |
playersIn = (playersIn r) - 1,
|
|
201 |
readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
|
|
202 |
}) ri
|
|
203 |
moveClientToLobby rnc ci
|
|
204 |
|
|
205 |
processAction ChangeMaster = do
|
|
206 |
ri <- clientRoomA
|
|
207 |
rnc <- gets roomsClients
|
|
208 |
newMasterId <- liftM head . io $ roomClientsIndicesM rnc ri
|
|
209 |
newMaster <- io $ client'sM rnc id newMasterId
|
|
210 |
let newRoomName = nick newMaster
|
|
211 |
mapM_ processAction [
|
|
212 |
ModifyRoom (\r -> r{masterID = newMasterId, name = newRoomName}),
|
|
213 |
ModifyClient2 newMasterId (\c -> c{isMaster = True}),
|
|
214 |
AnswerClients [sendChan newMaster] ["ROOM_CONTROL_ACCESS", "1"]
|
|
215 |
]
|
|
216 |
|
|
217 |
processAction (AddRoom roomName roomPassword) = do
|
|
218 |
Just clId <- gets clientIndex
|
|
219 |
rnc <- gets roomsClients
|
|
220 |
proto <- io $ client'sM rnc clientProto clId
|
|
221 |
|
|
222 |
let room = newRoom{
|
|
223 |
masterID = clId,
|
|
224 |
name = roomName,
|
|
225 |
password = roomPassword,
|
|
226 |
roomProto = proto
|
|
227 |
}
|
|
228 |
|
|
229 |
rId <- io $ addRoom rnc room
|
|
230 |
|
|
231 |
processAction $ MoveToRoom rId
|
|
232 |
|
|
233 |
chans <- liftM (map sendChan) $! roomClientsS lobbyId
|
|
234 |
|
|
235 |
mapM_ processAction [
|
|
236 |
AnswerClients chans ["ROOM", "ADD", roomName]
|
|
237 |
, ModifyClient (\cl -> cl{isMaster = True})
|
|
238 |
]
|
|
239 |
|
|
240 |
|
|
241 |
processAction RemoveRoom = do
|
|
242 |
Just clId <- gets clientIndex
|
|
243 |
rnc <- gets roomsClients
|
|
244 |
ri <- io $ clientRoomM rnc clId
|
|
245 |
roomName <- io $ room'sM rnc name ri
|
|
246 |
others <- othersChans
|
|
247 |
lobbyChans <- liftM (map sendChan) $! roomClientsS lobbyId
|
|
248 |
|
|
249 |
mapM_ processAction [
|
|
250 |
AnswerClients lobbyChans ["ROOM", "DEL", roomName],
|
|
251 |
AnswerClients others ["ROOMABANDONED", roomName]
|
|
252 |
]
|
|
253 |
|
|
254 |
io $ removeRoom rnc ri
|
|
255 |
|
|
256 |
|
|
257 |
processAction (UnreadyRoomClients) = do
|
|
258 |
rnc <- gets roomsClients
|
|
259 |
ri <- clientRoomA
|
|
260 |
roomPlayers <- roomClientsS ri
|
|
261 |
roomClIDs <- io $ roomClientsIndicesM rnc ri
|
|
262 |
processAction $ AnswerClients (map sendChan roomPlayers) ("NOT_READY" : map nick roomPlayers)
|
|
263 |
io $ mapM_ (modifyClient rnc (\cl -> cl{isReady = False})) roomClIDs
|
|
264 |
processAction $ ModifyRoom (\r -> r{readyPlayers = 0})
|
|
265 |
|
|
266 |
|
|
267 |
processAction (RemoveTeam teamName) = do
|
|
268 |
rnc <- gets roomsClients
|
|
269 |
cl <- client's id
|
|
270 |
ri <- clientRoomA
|
|
271 |
inGame <- io $ room'sM rnc gameinprogress ri
|
|
272 |
chans <- othersChans
|
|
273 |
if inGame then
|
|
274 |
mapM_ processAction [
|
|
275 |
AnswerClients chans ["REMOVE_TEAM", teamName],
|
|
276 |
ModifyRoom (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r})
|
|
277 |
]
|
|
278 |
else
|
|
279 |
mapM_ processAction [
|
|
280 |
AnswerClients chans ["EM", rmTeamMsg],
|
|
281 |
ModifyRoom (\r -> r{
|
|
282 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r,
|
|
283 |
leftTeams = teamName : leftTeams r,
|
|
284 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg
|
|
285 |
})
|
|
286 |
]
|
|
287 |
where
|
|
288 |
rmTeamMsg = toEngineMsg $ (B.singleton 'F') `B.append` teamName
|
|
289 |
|
|
290 |
|
|
291 |
processAction (RemoveClientTeams clId) = do
|
|
292 |
rnc <- gets roomsClients
|
|
293 |
|
|
294 |
removeTeamActions <- io $ do
|
|
295 |
clNick <- client'sM rnc nick clId
|
|
296 |
rId <- clientRoomM rnc clId
|
|
297 |
roomTeams <- room'sM rnc teams rId
|
|
298 |
return . Prelude.map (RemoveTeam . teamname) . Prelude.filter (\t -> teamowner t == clNick) $ roomTeams
|
|
299 |
|
|
300 |
mapM_ processAction removeTeamActions
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
processAction CheckRegistered = do
|
|
305 |
(Just ci) <- gets clientIndex
|
|
306 |
n <- client's nick
|
|
307 |
h <- client's host
|
|
308 |
db <- gets (dbQueries . serverInfo)
|
|
309 |
io $ writeChan db $ CheckAccount ci n h
|
|
310 |
return ()
|
|
311 |
|
|
312 |
|
|
313 |
processAction ClearAccountsCache = do
|
|
314 |
dbq <- gets (dbQueries . serverInfo)
|
|
315 |
io $ writeChan dbq ClearCache
|
|
316 |
return ()
|
|
317 |
|
|
318 |
|
|
319 |
processAction (ProcessAccountInfo info) =
|
|
320 |
case info of
|
|
321 |
HasAccount passwd isAdmin -> do
|
|
322 |
chan <- client's sendChan
|
|
323 |
processAction $ AnswerClients [chan] ["ASKPASSWORD"]
|
|
324 |
Guest -> do
|
|
325 |
processAction JoinLobby
|
|
326 |
Admin -> do
|
|
327 |
mapM processAction [ModifyClient (\cl -> cl{isAdministrator = True}), JoinLobby]
|
|
328 |
chan <- client's sendChan
|
|
329 |
processAction $ AnswerClients [chan] ["ADMIN_ACCESS"]
|
|
330 |
|
|
331 |
|
|
332 |
processAction JoinLobby = do
|
|
333 |
chan <- client's sendChan
|
|
334 |
clientNick <- client's nick
|
|
335 |
(lobbyNicks, clientsChans) <- liftM (unzip . Prelude.map (\c -> (nick c, sendChan c)) . Prelude.filter logonPassed) $! allClientsS
|
|
336 |
mapM_ processAction $
|
|
337 |
(AnswerClients clientsChans ["LOBBY:JOINED", clientNick])
|
|
338 |
: [AnswerClients [chan] ("LOBBY:JOINED" : clientNick : lobbyNicks)]
|
|
339 |
++ [ModifyClient (\cl -> cl{logonPassed = True}), SendServerMessage]
|
|
340 |
|
|
341 |
{-
|
|
342 |
processAction (clID, serverInfo, rnc) (RoomAddThisClient rID) =
|
|
343 |
processAction (
|
|
344 |
clID,
|
|
345 |
serverInfo,
|
|
346 |
adjust (\cl -> cl{roomID = rID, teamsInGame = if rID == 0 then teamsInGame cl else 0}) clID clients,
|
|
347 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $
|
|
348 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms
|
|
349 |
) joinMsg
|
|
350 |
where
|
|
351 |
client = clients ! clID
|
|
352 |
joinMsg = if rID == 0 then
|
|
353 |
AnswerAllOthers ["LOBBY:JOINED", nick client]
|
|
354 |
else
|
|
355 |
AnswerThisRoom ["JOINED", nick client]
|
|
356 |
|
|
357 |
processAction (clID, serverInfo, rnc) (KickClient kickID) =
|
|
358 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, rnc) $ ByeClient "Kicked")
|
|
359 |
|
|
360 |
|
|
361 |
processAction (clID, serverInfo, rnc) (BanClient banNick) =
|
|
362 |
return (clID, serverInfo, rnc)
|
|
363 |
|
|
364 |
|
|
365 |
processAction (clID, serverInfo, rnc) (KickRoomClient kickID) = do
|
|
366 |
writeChan (sendChan $ clients ! kickID) ["KICKED"]
|
|
367 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, rnc) $ RoomRemoveThisClient "kicked")
|
|
368 |
|
|
369 |
-}
|
|
370 |
|
|
371 |
processAction (AddClient client) = do
|
|
372 |
rnc <- gets roomsClients
|
|
373 |
si <- gets serverInfo
|
|
374 |
io $ do
|
|
375 |
ci <- addClient rnc client
|
|
376 |
t <- forkIO $ clientRecvLoop (clientSocket client) (coreChan si) ci
|
|
377 |
forkIO $ clientSendLoop (clientSocket client) t (coreChan si) (sendChan client) ci
|
|
378 |
|
|
379 |
infoM "Clients" (show ci ++ ": New client. Time: " ++ show (connectTime client))
|
|
380 |
|
|
381 |
processAction $ AnswerClients [sendChan client] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"]
|
|
382 |
{- let newLogins = takeWhile (\(_ , time) -> (connectTime client) `diffUTCTime` time <= 11) $ lastLogins serverInfo
|
|
383 |
|
|
384 |
if False && (isJust $ host client `Prelude.lookup` newLogins) then
|
|
385 |
processAction (ci, serverInfo{lastLogins = newLogins}, rnc) $ ByeClient "Reconnected too fast"
|
|
386 |
else
|
|
387 |
return (ci, serverInfo)
|
|
388 |
-}
|
|
389 |
|
|
390 |
|
|
391 |
|
|
392 |
processAction PingAll = do
|
|
393 |
rnc <- gets roomsClients
|
|
394 |
io (allClientsM rnc) >>= mapM_ (kickTimeouted rnc)
|
|
395 |
cis <- io $ allClientsM rnc
|
|
396 |
chans <- io $ mapM (client'sM rnc sendChan) cis
|
|
397 |
io $ mapM_ (modifyClient rnc (\cl -> cl{pingsQueue = pingsQueue cl + 1})) cis
|
|
398 |
processAction $ AnswerClients chans ["PING"]
|
|
399 |
where
|
|
400 |
kickTimeouted rnc ci = do
|
|
401 |
pq <- io $ client'sM rnc pingsQueue ci
|
|
402 |
when (pq > 0) $
|
|
403 |
withStateT (\as -> as{clientIndex = Just ci}) $
|
|
404 |
processAction (ByeClient "Ping timeout")
|
|
405 |
|
|
406 |
|
|
407 |
processAction (StatsAction) = do
|
|
408 |
rnc <- gets roomsClients
|
|
409 |
si <- gets serverInfo
|
|
410 |
(roomsNum, clientsNum) <- io $ withRoomsAndClients rnc stats
|
|
411 |
io $ writeChan (dbQueries si) $ SendStats clientsNum (roomsNum - 1)
|
|
412 |
where
|
|
413 |
stats irnc = (length $ allRooms irnc, length $ allClients irnc)
|
|
414 |
|