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,113 +2,144 @@
-- 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, Player,
Player, getPlayerTick,
getPlayerTick, getTickPlayers,
getTickPlayers, getTickValue,
getTickValue, setPlayerTick,
Tick (..) toList,
) where fromList,
import Data.Maybe (fromMaybe) toMap,
import qualified Data.Map.Strict as M fromMap,
import qualified Data.Text as T Tick (..),
)
where
-- |A Tick is just a number import qualified Data.Map.Strict as M
data Tick = Abwarten | Bereithalten | Tick Int import Data.Maybe (fromMaybe)
deriving (Show, Eq, Ord) import qualified Data.Text as T
isAbwarten :: Tick -> Bool -- | A Tick is just a number
isAbwarten Abwarten = True data Tick = Abwarten | Bereithalten | Tick Int
isAbwarten _ = False deriving (Show, Eq, Ord)
isBereithalten :: Tick -> Bool -- | test if 'Tick' is constructed using 'Abwarten'
isBereithalten Bereithalten = True isAbwarten :: Tick -> Bool
isBereithalten _ = False isAbwarten Abwarten = True
isAbwarten _ = False
getTickValue :: Tick -> Maybe Int -- | test if 'Tick' is constructed using 'Bereithalten'
getTickValue (Tick n) = Just n isBereithalten :: Tick -> Bool
getTickValue _ = Nothing isBereithalten Bereithalten = True
-- |A Player has a name as identifier isBereithalten _ = False
type Player = T.Text
-- |The TickLeiste consists of a List of Ticks and for each Tick a -- | if 'Tick' is costructed with 'Tick' we get the 'Int' value, else 'Nothing'
-- Queue of players, that play at that turn. getTickValue :: Tick -> Maybe Int
getTickValue (Tick n) = Just n
getTickValue _ = Nothing
-- This is a bit wierd as it introduces redundancy, but I don't have a -- | A Player has a name as identifier
-- better idea. type Player = T.Text
-- NOTE: the first player in the list is the first to move.
data TickLeiste = TickLeiste { leiste :: M.Map Tick [Player], player :: M.Map Player Tick }
deriving (Show)
-- |Empty Tickleiste -- | The TickLeiste consists of a List of Ticks and for each Tick a
newTickLeiste :: TickLeiste -- Queue of players, that play at that turn.
newTickLeiste = TickLeiste M.empty M.empty
-- |Add a 'Player' to the 'TickLeiste' -- This is a bit wierd as it introduces redundancy, but I don't have a
addPlayer :: Player -- ^ the Player to add -- better idea.
-> Tick -- ^ the Tick we add the Player to -- NOTE: the first player in the list is the first to move.
-> TickLeiste -- ^ the TickLeiste we add the player to data TickLeiste = TickLeiste {leiste :: M.Map Tick [Player], player :: M.Map Player Tick}
-> TickLeiste -- ^ the resulting TickLeiste deriving (Show)
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 -- | Empty Tickleiste
-- the TickLeiste, Abwarten or Bereithalten then this function returns newTickLeiste :: TickLeiste
-- Nothing newTickLeiste = TickLeiste M.empty M.empty
-- Would it be better to return the original 'TickLeiste' instead of 'Nothing'? -- | Adds a number of ticks to the player, if the player is not on
addTicksToPlayer :: Player -- ^ the Player we want to add ticks to -- the TickLeiste, Abwarten or Bereithalten then this function returns
-> Int -- ^ the number of Ticks we want to add -- Nothing
-> TickLeiste -- ^ the TickLeiste we want to modify
-> Maybe TickLeiste -- ^ the result
addTicksToPlayer p t tl@(TickLeiste l pl) = {- fromMaybe tl $ -}do -- Would it be better to return the original 'TickLeiste' instead of 'Nothing'?
pt <- pl M.!? p addTicksToPlayer ::
ptv <- getTickValue pt -- | the Player we want to add ticks to
let npt = Tick $ ptv + t Player ->
tlm = (insertPlayerToLeiste p npt . removePlayerFromLeiste p pt) l -- | the number of Ticks we want to add
pm = setPlayerTickToPlayer p npt pl Int ->
return $ TickLeiste tlm pm -- | 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 nt = Tick $ ptv + t
return $ setPlayerTick p nt tl
-- | We get the tick a 'Player' is on
getPlayerTick ::
Player ->
TickLeiste ->
Maybe Tick
getPlayerTick p (TickLeiste _ pl) = pl M.!? p
-- |We get the tick a 'Player' is on -- | get a list of 'Player' that move at a tick in order.
getPlayerTick :: Player -- ^ the 'Player' getTickPlayers ::
-> TickLeiste Tick ->
-> Maybe Tick TickLeiste ->
getPlayerTick p (TickLeiste _ pl) = pl M.!? p [Player]
getTickPlayers t (TickLeiste l _) = fromMaybe [] $ l M.!? t
-- |get a list of 'Player' that move at a tick in order. -- | set the tick of a 'Player', if the 'Player' is does not exist we add them.
getTickPlayers :: Tick setPlayerTick ::
-> TickLeiste Player ->
-> [Player] Tick ->
getTickPlayers t (TickLeiste l _) = fromMaybe [] $ l M.!? t 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
-- this is just an internal helper toList :: TickLeiste -> [(Tick, [Player])]
-- it removes a player from a specific tick, if the player toList (TickLeiste l _) = M.toAscList l
-- 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
insertPlayerToLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player] -- | convert from list to TickLeiste, if a player is at multiple 'Tick' this returns
insertPlayerToLeiste p t l = M.insert t (M.findWithDefault [] t l ++ [p]) l -- 'Nothing'
setPlayerTickToPlayer :: Player -> Tick -> M.Map Player Tick -> M.Map Player Tick -- TODO
setPlayerTickToPlayer = M.insert 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
-- 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
insertPlayerToLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player]
insertPlayerToLeiste p t l = M.insert t (M.findWithDefault [] t l ++ [p]) l
setPlayerTickToPlayer :: Player -> Tick -> M.Map Player Tick -> M.Map Player Tick
setPlayerTickToPlayer = M.insert