reformatting and refectoring

This commit is contained in:
Dennis Frieberg 2020-08-25 04:02:51 +02:00
parent ddd42c1914
commit e38b240178

View file

@ -2,10 +2,9 @@
-- Module: TickLeiste -- Module: TickLeiste
-- Description: Short Implementation of Splittermonds Tickleiste -- Description: Short Implementation of Splittermonds Tickleiste
-- Stability: experimental -- Stability: experimental
module Data.TickLeiste ( module Data.TickLeiste
TickLeiste, ( TickLeiste,
newTickLeiste, newTickLeiste,
addPlayer,
addTicksToPlayer, addTicksToPlayer,
isAbwarten, isAbwarten,
isBereithalten, isBereithalten,
@ -13,27 +12,38 @@ module Data.TickLeiste (
getPlayerTick, getPlayerTick,
getTickPlayers, getTickPlayers,
getTickValue, getTickValue,
Tick (..) setPlayerTick,
) where toList,
import Data.Maybe (fromMaybe) fromList,
toMap,
fromMap,
Tick (..),
)
where
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
import qualified Data.Text as T import qualified Data.Text as T
-- | A Tick is just a number -- | A Tick is just a number
data Tick = Abwarten | Bereithalten | Tick Int data Tick = Abwarten | Bereithalten | Tick Int
deriving (Show, Eq, Ord) deriving (Show, Eq, Ord)
-- | test if 'Tick' is constructed using 'Abwarten'
isAbwarten :: Tick -> Bool isAbwarten :: Tick -> Bool
isAbwarten Abwarten = True isAbwarten Abwarten = True
isAbwarten _ = False isAbwarten _ = False
-- | test if 'Tick' is constructed using 'Bereithalten'
isBereithalten :: Tick -> Bool isBereithalten :: Tick -> Bool
isBereithalten Bereithalten = True isBereithalten Bereithalten = True
isBereithalten _ = False isBereithalten _ = False
-- | if 'Tick' is costructed with 'Tick' we get the 'Int' value, else 'Nothing'
getTickValue :: Tick -> Maybe Int getTickValue :: Tick -> Maybe Int
getTickValue (Tick n) = Just n getTickValue (Tick n) = Just n
getTickValue _ = Nothing getTickValue _ = Nothing
-- | A Player has a name as identifier -- | A Player has a name as identifier
type Player = T.Text type Player = T.Text
@ -50,62 +60,83 @@ module Data.TickLeiste (
newTickLeiste :: TickLeiste newTickLeiste :: TickLeiste
newTickLeiste = TickLeiste M.empty M.empty newTickLeiste = TickLeiste M.empty M.empty
-- |Add a 'Player' to the 'TickLeiste'
addPlayer :: Player -- ^ the Player to add
-> Tick -- ^ the Tick we add the Player to
-> TickLeiste -- ^ the TickLeiste we add the player to
-> TickLeiste -- ^ the resulting TickLeiste
addPlayer p t (TickLeiste l pl) = TickLeiste tlm pm
where
tlm :: M.Map Tick [Player]
tlm = insertPlayerToLeiste p t l
pm :: M.Map Player Tick
pm = setPlayerTickToPlayer p t pl
-- | Adds a number of ticks to the player, if the player is not on -- | Adds a number of ticks to the player, if the player is not on
-- the TickLeiste, Abwarten or Bereithalten then this function returns -- the TickLeiste, Abwarten or Bereithalten then this function returns
-- Nothing -- Nothing
-- Would it be better to return the original 'TickLeiste' instead of 'Nothing'? -- Would it be better to return the original 'TickLeiste' instead of 'Nothing'?
addTicksToPlayer :: Player -- ^ the Player we want to add ticks to addTicksToPlayer ::
-> Int -- ^ the number of Ticks we want to add -- | the Player we want to add ticks to
-> TickLeiste -- ^ the TickLeiste we want to modify Player ->
-> Maybe TickLeiste -- ^ the result -- | the number of Ticks we want to add
Int ->
addTicksToPlayer p t tl@(TickLeiste l pl) = {- fromMaybe tl $ -}do -- | the TickLeiste we want to modify
pt <- pl M.!? p TickLeiste ->
-- | the result
Maybe TickLeiste
addTicksToPlayer p t tl = {- fromMaybe tl $ -} do
pt <- getPlayerTick p tl
ptv <- getTickValue pt ptv <- getTickValue pt
let npt = Tick $ ptv + t let nt = Tick $ ptv + t
tlm = (insertPlayerToLeiste p npt . removePlayerFromLeiste p pt) l return $ setPlayerTick p nt tl
pm = setPlayerTickToPlayer p npt pl
return $ TickLeiste tlm pm
-- | We get the tick a 'Player' is on -- | We get the tick a 'Player' is on
getPlayerTick :: Player -- ^ the 'Player' getPlayerTick ::
-> TickLeiste Player ->
-> Maybe Tick TickLeiste ->
Maybe Tick
getPlayerTick p (TickLeiste _ pl) = pl M.!? p getPlayerTick p (TickLeiste _ pl) = pl M.!? p
-- | get a list of 'Player' that move at a tick in order. -- | get a list of 'Player' that move at a tick in order.
getTickPlayers :: Tick getTickPlayers ::
-> TickLeiste Tick ->
-> [Player] TickLeiste ->
[Player]
getTickPlayers t (TickLeiste l _) = fromMaybe [] $ l M.!? t getTickPlayers t (TickLeiste l _) = fromMaybe [] $ l M.!? t
-- | set the tick of a 'Player', if the 'Player' is does not exist we add them.
setPlayerTick ::
Player ->
Tick ->
TickLeiste ->
TickLeiste
setPlayerTick p t tl@(TickLeiste l pl) = TickLeiste (insertPlayerToLeiste p t l') (setPlayerTickToPlayer p t pl)
where
l' :: M.Map Tick [Player]
l' = fromMaybe l $ do
ot <- getPlayerTick p tl
return $ removePlayerFromLeiste p ot l
-- | convert the Tick[eiste to a list of 'Tick' and 'Player' list pairs. These list are ordered
toList :: TickLeiste -> [(Tick, [Player])]
toList (TickLeiste l _) = M.toAscList l
-- | convert from list to TickLeiste, if a player is at multiple 'Tick' this returns
-- 'Nothing'
-- TODO
fromList :: [(Tick, [Player])] -> Maybe TickLeiste
fromList = error "not implemented"
-- | convert from 'TickLeiste' to a map from 'Tick' to list of 'Player'
toMap :: TickLeiste -> M.Map Tick [Player]
toMap = leiste
-- TODO
fromMap :: M.Map Tick [Player] -> Maybe TickLeiste
fromMap = error "not implemented"
-- these are just internal helpers
-- this is just an internal helper
-- it removes a player from a specific tick, if the player -- it removes a player from a specific tick, if the player
-- wasn't at the tick it is the identity. -- wasn't at the tick it is the identity.
removePlayerFromLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player] removePlayerFromLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player]
removePlayerFromLeiste p t l = fromMaybe l $ do removePlayerFromLeiste p t l = fromMaybe l $ do
list <- l M.!? t list <- l M.!? t
let list' = filter (/= p) list let list' = filter (/= p) list
if null list' then if null list'
return $ M.delete t l then return $ M.delete t l
else else return $ M.insert t list' l
return $ M.insert t list' l
insertPlayerToLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player] insertPlayerToLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player]
insertPlayerToLeiste p t l = M.insert t (M.findWithDefault [] t l ++ [p]) l insertPlayerToLeiste p t l = M.insert t (M.findWithDefault [] t l ++ [p]) l