diff -r d3a2fe9f04f2 -r 003fc694c0c3 gameServer/FloodDetection.hs --- a/gameServer/FloodDetection.hs Fri Jan 31 23:36:02 2014 +0400 +++ b/gameServer/FloodDetection.hs Sun Feb 02 00:37:00 2014 +0400 @@ -1,3 +1,4 @@ +{-# LANGUAGE OverloadedStrings, BangPatterns #-} module FloodDetection where import Control.Monad.State.Strict @@ -6,35 +7,51 @@ ---------------- import ServerState import CoreTypes +import Utils registerEvent :: Event -> StateT ServerState IO [Action] registerEvent e = do eventInfo <- client's $ einfo e if (null eventInfo) || 0 == (fst $ head eventInfo) then doCheck eventInfo else updateInfo + where einfo LobbyChatMessage = eiLobbyChat einfo EngineMessage = eiEM einfo RoomJoin = eiJoin transformField LobbyChatMessage f = \c -> c{eiLobbyChat = f $ eiLobbyChat c} - transformField EngineMessage f = \c -> c{eiLobbyChat = f $ eiEM c} - transformField RoomJoin f = \c -> c{eiLobbyChat = f $ eiJoin c} + transformField EngineMessage f = \c -> c{eiEM = f $ eiEM c} + transformField RoomJoin f = \c -> c{eiJoin = f $ eiJoin c} - boundaries :: Event -> (Int, (NominalDiffTime, Int, [Action]), (NominalDiffTime, Int, [Action])) - boundaries LobbyChatMessage = (3, (10, 2, []), (30, 3, [])) - boundaries EngineMessage = (10, (10, 3, []), (30, 4, undefined)) - boundaries RoomJoin = (2, (10, 2, []), (35, 3, [])) + boundaries :: Event -> (Int, (NominalDiffTime, Int), (NominalDiffTime, Int), ([Action], [Action])) + boundaries LobbyChatMessage = (3, (10, 2), (30, 3), (chat1, chat2)) + boundaries EngineMessage = (8, (10, 4), (25, 5), (em1, em2)) + boundaries RoomJoin = (2, (10, 2), (35, 3), (join1, join2)) + + chat1 = [Warning $ loc "Warning! Chat flood protection activated"] + chat2 = [ByeClient $ loc "Excess flood"] + em1 = [Warning $ loc "Game messages flood detected - 1"] + em2 = [Warning $ loc "Game messages flood detected - 2"] + join1 = [Warning $ loc "Warning! Joins flood protection activated"] + join2 = [ByeClient $ loc "Excess flood"] doCheck ei = do curTime <- io getCurrentTime - let (numPerEntry, (sec1, num1, ac1), (sec2, num2, ac2)) = boundaries e + let (numPerEntry, (sec1, num1), (sec2, num2), (ac1, ac2)) = boundaries e + + let nei = takeWhile ((>=) sec2 . diffUTCTime curTime . snd) ei + let l2 = length nei + let l1 = length $ takeWhile ((>=) sec1 . diffUTCTime curTime . snd) nei - let nei2 = takeWhile ((>=) sec2 . diffUTCTime curTime . snd) ei - let nei1 = takeWhile ((>=) sec1 . diffUTCTime curTime . snd) nei1 + let actions = if l2 >= num2 + 1 || l1 >= num1 + 1 then + ac2 + else + if l1 >= num1 || l2 >= num2 then + ac1 + else + [] - let actions = if length nei2 >= num2 then ac2 else if length nei1 >= num1 then ac1 else [] - - return $ (ModifyClient . transformField e . const $ (numPerEntry, curTime) : nei2) : actions + return $ (ModifyClient . transformField e . const $ (numPerEntry, curTime) : nei) : actions updateInfo = return [ ModifyClient $ transformField e