gameServer/FloodDetection.hs
author unc0rr
Fri, 31 Jan 2014 23:36:02 +0400
changeset 10094 d3a2fe9f04f2
parent 10093 ada172d33988
child 10095 003fc694c0c3
permissions -rw-r--r--
Define some boundaries to detect events
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10090
a471a7bbc339 - Start work on flood detector
unc0rr
parents:
diff changeset
     1
module FloodDetection where
a471a7bbc339 - Start work on flood detector
unc0rr
parents:
diff changeset
     2
10093
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
     3
import Control.Monad.State.Strict
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
     4
import Data.Time
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
     5
import Control.Arrow
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
     6
----------------
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
     7
import ServerState
10090
a471a7bbc339 - Start work on flood detector
unc0rr
parents:
diff changeset
     8
import CoreTypes
a471a7bbc339 - Start work on flood detector
unc0rr
parents:
diff changeset
     9
10093
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    10
registerEvent :: Event -> StateT ServerState IO [Action]
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    11
registerEvent e = do
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    12
    eventInfo <- client's $ einfo e
10094
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    13
    if (null eventInfo) || 0 == (fst $ head eventInfo) then doCheck eventInfo else updateInfo
10093
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    14
    where
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    15
    einfo LobbyChatMessage = eiLobbyChat
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    16
    einfo EngineMessage = eiEM
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    17
    einfo RoomJoin = eiJoin
10094
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    18
10093
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    19
    transformField LobbyChatMessage f = \c -> c{eiLobbyChat = f $ eiLobbyChat c}
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    20
    transformField EngineMessage f = \c -> c{eiLobbyChat = f $ eiEM c}
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    21
    transformField RoomJoin f = \c -> c{eiLobbyChat = f $ eiJoin c}
10094
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    22
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    23
    boundaries :: Event -> (Int, (NominalDiffTime, Int, [Action]), (NominalDiffTime, Int, [Action]))
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    24
    boundaries LobbyChatMessage = (3, (10, 2, []), (30, 3, []))
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    25
    boundaries EngineMessage = (10, (10, 3, []), (30, 4, undefined))
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    26
    boundaries RoomJoin = (2, (10, 2, []), (35, 3, []))
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    27
10093
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    28
    doCheck ei = do
10094
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    29
        curTime <- io getCurrentTime
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    30
        let (numPerEntry, (sec1, num1, ac1), (sec2, num2, ac2)) = boundaries e
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    31
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    32
        let nei2 = takeWhile ((>=) sec2 . diffUTCTime curTime . snd) ei
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    33
        let nei1 = takeWhile ((>=) sec1 . diffUTCTime curTime . snd) nei1
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    34
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    35
        let actions = if length nei2 >= num2 then ac2 else if length nei1 >= num1 then ac1 else []
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    36
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    37
        return $ (ModifyClient . transformField e . const $ (numPerEntry, curTime) : nei2) : actions
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    38
10093
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    39
    updateInfo = return [
10094
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    40
        ModifyClient $ transformField e
d3a2fe9f04f2 Define some boundaries to detect events
unc0rr
parents: 10093
diff changeset
    41
            $ \(h:hs) -> first (flip (-) 1) h : hs
10093
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    42
        ]