gameServer/FloodDetection.hs
author unc0rr
Fri, 31 Jan 2014 16:51:20 +0400
changeset 10093 ada172d33988
parent 10090 a471a7bbc339
child 10094 d3a2fe9f04f2
permissions -rw-r--r--
More work on flood detector
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
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    13
    if (not $ null eventInfo) && 0 == (fst $ head eventInfo) then doCheck eventInfo else updateInfo
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
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    18
     
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}
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    22
    
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    23
    doCheck ei = do
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    24
        liftM Just $ io getCurrentTime
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    25
        return []
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    26
    updateInfo = return [
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    27
        ModifyClient $ transformField e 
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    28
            $ \ei -> if null ei then 
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    29
                [] 
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    30
                else 
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    31
                let (h:hs) = ei in first (flip (-) 1) h : hs
ada172d33988 More work on flood detector
unc0rr
parents: 10090
diff changeset
    32
        ]