added removePlayer

This commit is contained in:
Dennis Frieberg 2021-03-30 14:21:28 +02:00
parent 10318ade8c
commit 3a9592f958

View file

@ -22,7 +22,7 @@ module Data.TickLeiste
where
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe,maybe)
import qualified Data.Text as T
import qualified Data.UUID as U
@ -110,7 +110,14 @@ setPlayerTick p t tl@(TickLeiste l pl) = TickLeiste (insertPlayerToLeiste p t l'
-- TODO
removePlayer :: Player -> TickLeiste -> TickLeiste
removePlayer = error "removePlayer not yet implemented (Franzi?)"
removePlayer p tl@(TickLeiste {leiste = l, player = pm}) = TickLeiste l' pm'
where
pt = getPlayerTick p tl
l' = maybe
l
(flip (removePlayerFromLeiste p) l)
pt
pm' = removePlayerFromPlayerMap p pm
-- | convert the Tick[eiste to a list of 'Tick' and 'Player' list pairs. These list are ordered
toList :: TickLeiste -> [(Tick, [Player])]
@ -120,7 +127,7 @@ toList (TickLeiste l _) = M.toAscList l
-- TODO
fromList :: [(Tick, [Player])] -> TickLeiste
fromList = error "not implemented"
fromList = error "not implemented (Franzi?)"
-- | convert from 'TickLeiste' to a map from 'Tick' to list of 'Player'
toMap :: TickLeiste -> M.Map Tick [Player]
@ -130,6 +137,7 @@ toMap = leiste
-- it removes a player from a specific tick, if the player
-- wasn't at the tick it is the identity.
-- TODO this can be written more beautifully with something like M.update
removePlayerFromLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player]
removePlayerFromLeiste p t l = fromMaybe l $ do
list <- l M.!? t
@ -138,6 +146,9 @@ removePlayerFromLeiste p t l = fromMaybe l $ do
then return $ M.delete t l
else return $ M.insert t list' l
removePlayerFromPlayerMap :: Player -> M.Map Player Tick -> M.Map Player Tick
removePlayerFromPlayerMap = M.delete
insertPlayerToLeiste :: Player -> Tick -> M.Map Tick [Player] -> M.Map Tick [Player]
insertPlayerToLeiste p t l = M.insert t (M.findWithDefault [] t l ++ [p]) l