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
-- Description: Short Implementation of Splittermonds Tickleiste
-- Stability: experimental
module Data.TickLeiste (
TickLeiste,
module Data.TickLeiste
( TickLeiste,
newTickLeiste,
addPlayer,
addTicksToPlayer,
isAbwarten,
isBereithalten,
@ -13,27 +12,38 @@ module Data.TickLeiste (
getPlayerTick,
getTickPlayers,
getTickValue,
Tick (..)
) where
import Data.Maybe (fromMaybe)
setPlayerTick,
toList,
fromList,
toMap,
fromMap,
Tick (..),
)
where
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
-- | A Tick is just a number
data Tick = Abwarten | Bereithalten | Tick Int
deriving (Show, Eq, Ord)
-- | test if 'Tick' is constructed using 'Abwarten'
isAbwarten :: Tick -> Bool
isAbwarten Abwarten = True
isAbwarten _ = False
-- | test if 'Tick' is constructed using 'Bereithalten'
isBereithalten :: Tick -> Bool
isBereithalten Bereithalten = True
isBereithalten _ = False
-- | if 'Tick' is costructed with 'Tick' we get the 'Int' value, else 'Nothing'
getTickValue :: Tick -> Maybe Int
getTickValue (Tick n) = Just n
getTickValue _ = Nothing
-- | A Player has a name as identifier
type Player = T.Text
@ -50,62 +60,83 @@ module Data.TickLeiste (
newTickLeiste :: TickLeiste
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
-- the TickLeiste, Abwarten or Bereithalten then this function returns
-- Nothing
-- Would it be better to return the original 'TickLeiste' instead of 'Nothing'?
addTicksToPlayer :: Player -- ^ the Player we want to add ticks to
-> Int -- ^ the number of Ticks we want to add
-> TickLeiste -- ^ the TickLeiste we want to modify
-> Maybe TickLeiste -- ^ the result
addTicksToPlayer p t tl@(TickLeiste l pl) = {- fromMaybe tl $ -}do
pt <- pl M.!? p
addTicksToPlayer ::
-- | the Player we want to add ticks to
Player ->
-- | the number of Ticks we want to add
Int ->
-- | the TickLeiste we want to modify
TickLeiste ->
-- | the result
Maybe TickLeiste
addTicksToPlayer p t tl = {- fromMaybe tl $ -} do
pt <- getPlayerTick p tl
ptv <- getTickValue pt
let npt = Tick $ ptv + t
tlm = (insertPlayerToLeiste p npt . removePlayerFromLeiste p pt) l
pm = setPlayerTickToPlayer p npt pl
return $ TickLeiste tlm pm
let nt = Tick $ ptv + t
return $ setPlayerTick p nt tl
-- | We get the tick a 'Player' is on
getPlayerTick :: Player -- ^ the 'Player'
-> TickLeiste
-> Maybe Tick
getPlayerTick ::
Player ->
TickLeiste ->
Maybe Tick
getPlayerTick p (TickLeiste _ pl) = pl M.!? p
-- | get a list of 'Player' that move at a tick in order.
getTickPlayers :: Tick
-> TickLeiste
-> [Player]
getTickPlayers ::
Tick ->
TickLeiste ->
[Player]
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
-- wasn't at the tick it is the identity.
removePlayerFromLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player]
removePlayerFromLeiste p t l = fromMaybe l $ do
list <- l M.!? t
let list' = filter (/= p) list
if null list' then
return $ M.delete t l
else
return $ M.insert t list' l
if null list'
then return $ M.delete t l
else return $ M.insert t list' l
insertPlayerToLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player]
insertPlayerToLeiste p t l = M.insert t (M.findWithDefault [] t l ++ [p]) l