Add listing module

This commit is contained in:
Joscha 2020-04-08 12:01:01 +00:00
parent be818ae05f
commit 3e2120f970
4 changed files with 72 additions and 3 deletions

View file

@ -2,6 +2,7 @@
## upcoming ## upcoming
- add `Haboli.Euphoria.Command` module - add `Haboli.Euphoria.Command` module
- add `Haboli.Euphoria.Listing` module
- add `Haboli.Euphoria.Util` module and move `respondingToPing` there - add `Haboli.Euphoria.Util` module and move `respondingToPing` there
- clean up project - clean up project
- fix nick of example bot in readme - fix nick of example bot in readme

View file

@ -4,7 +4,7 @@ cabal-version: 1.12
-- --
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
-- --
-- hash: e4041146c275e14c632860cfd7af7b3ac3fe6bf5493f57168143777df245a1cf -- hash: 67a0b5c2b39acab50a31b3d9efa3e4a48e13f1edd28bdbb23b99fc1c8c30bd3b
name: haboli name: haboli
version: 0.3.1.0 version: 0.3.1.0
@ -33,6 +33,7 @@ library
Haboli.Euphoria.Api Haboli.Euphoria.Api
Haboli.Euphoria.Client Haboli.Euphoria.Client
Haboli.Euphoria.Command Haboli.Euphoria.Command
Haboli.Euphoria.Listing
Haboli.Euphoria.Util Haboli.Euphoria.Util
other-modules: other-modules:
Paths_haboli Paths_haboli

View file

@ -194,7 +194,7 @@ data UserType
-- ^ The client has none of the other user types. While this value does not -- ^ The client has none of the other user types. While this value does not
-- occur nowadays, some messages in the room logs are still from a time before -- occur nowadays, some messages in the room logs are still from a time before
-- the distinction of user types were introduced. -- the distinction of user types were introduced.
deriving (Show, Eq) deriving (Show, Eq, Ord)
-- | A 'UserId' identifies a user. It consists of two parts: The type of -- | A 'UserId' identifies a user. It consists of two parts: The type of
-- session, and a unique value for that type of session. See -- session, and a unique value for that type of session. See
@ -202,7 +202,7 @@ data UserType
data UserId = UserId data UserId = UserId
{ userType :: UserType { userType :: UserType
, userSnowflake :: Snowflake , userSnowflake :: Snowflake
} deriving (Show, Eq) } deriving (Show, Eq, Ord)
instance ToJSON UserId where instance ToJSON UserId where
toJSON uid = toJSON uid =

View file

@ -0,0 +1,67 @@
{-# LANGUAGE OverloadedStrings #-}
module Haboli.Euphoria.Listing
( Listing
, newListing
, self
, others
, updateOwnNick
, updateFromList
, updateFromEvent
) where
import qualified Data.Map.Strict as Map
import Data.Maybe
import qualified Data.Text as T
import Haboli.Euphoria.Api
import Haboli.Euphoria.Client
data Listing = Listing
{ lsSelf :: SessionView
, lsOthers :: Map.Map UserId SessionView
} deriving (Show)
othersFromList :: [SessionView] -> Map.Map UserId SessionView
othersFromList sessions = Map.fromList [(svId sv, sv) | sv <- sessions]
newListing :: (HelloEvent, SnapshotEvent) -> Listing
newListing (h, s) = Listing
{ lsSelf = helloSessionView h
, lsOthers = othersFromList $ snapshotListing s
}
self :: Listing -> SessionView
self = lsSelf
others :: Listing -> Map.Map UserId SessionView
others = lsOthers
updateOwnNick :: T.Text -> Listing -> Listing
updateOwnNick name listing = listing{lsSelf = (lsSelf listing){svNick = name}}
updateFromList :: [SessionView] -> Listing -> Listing
updateFromList sessions listing =
let ownId = svId $ lsSelf listing
others' = othersFromList sessions
newSelf = fromMaybe (lsSelf listing) $ others' Map.!? ownId
newOthers = Map.filterWithKey (\k _ -> k /= ownId) others'
in Listing newSelf newOthers
onJoin :: SessionView -> Listing -> Listing
onJoin sv listing = listing{lsOthers = Map.insert (svId sv) sv $ lsOthers listing}
onPart :: SessionView -> Listing -> Listing
onPart sv listing = listing{lsOthers = Map.delete (svId sv) $ lsOthers listing}
updateFromEvent :: Event -> Listing -> Listing
updateFromEvent (EventJoin e) listing = onJoin (joinSession e) listing
updateFromEvent (EventPart e) listing = onPart (partSession e) listing
updateFromEvent (EventNetwork e) listing | networkType e == "partition" =
let sId = networkServerId e
sEra = networkServerEra e
isAffected sv = svServerId sv == sId && svServerEra sv == sEra
others' = Map.filter (not . isAffected) $ lsOthers listing
in listing{lsOthers = others'}
updateFromEvent _ listing = listing