@@ -3,17 +3,23 @@ module FOMObot.Helpers.Bot
33 , printMessage
44 , sendMessage
55 , alertFOMOChannel
6+ , updateState
67 ) where
78
89import Control.Monad.Trans (liftIO )
10+ import Control.Monad.State (modify )
911import Control.Monad.Reader (ask )
1012import Control.Monad.Loops (untilJust )
1113import Data.Aeson (decode , encode )
14+ import Data.HashMap (member , insert , adjust )
1215import qualified Network.WebSockets as WS
1316
1417import FOMObot.Types.Message
1518import FOMObot.Types.Bot
1619import FOMObot.Types.BotConfig
20+ import FOMObot.Types.ChannelState
21+ import FOMObot.Helpers.Time
22+ import FOMObot.Helpers.MovingAverage
1723
1824receiveMessage :: Bot Message
1925receiveMessage = do
@@ -43,3 +49,22 @@ alertFOMOChannel message = (sendMessage message) =<< _channelID <$> ask
4349
4450connection :: Bot WS. Connection
4551connection = _connection <$> ask
52+
53+ updateState :: Message -> Bot ()
54+ updateState message = modify =<< (alterState message) <$> ask
55+
56+ alterState :: Message -> BotConfig -> BotState -> BotState
57+ alterState Message {.. } config state
58+ | member _channel state = adjust (updateChannelState config _ts) _channel state
59+ | otherwise = insert _channel newChannelState state
60+ where
61+ newChannelState = ChannelState 1 0 0 _ts
62+
63+ updateChannelState :: BotConfig -> String -> ChannelState -> ChannelState
64+ updateChannelState BotConfig {.. } ts ChannelState {.. } = ChannelState (_count + 1 ) longAvg shortAvg ts
65+ where
66+ longAvg = singleExpSmoothing longAlpha _longAvg diff
67+ shortAvg = singleExpSmoothing shortAlpha _shortAvg diff
68+ longAlpha = realToFrac _longAlpha
69+ shortAlpha = realToFrac _shortAlpha
70+ diff = diffTime ts _lastTimeStamp
0 commit comments