From 16f6c8689ae4e09e98413928e0f4a7b0774e8f02 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Mon, 6 Jun 2016 17:48:24 +0200 Subject: More faction utilities --- src/Sequence/Types.hs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/Sequence') diff --git a/src/Sequence/Types.hs b/src/Sequence/Types.hs index 3d95b9a..02389a1 100644 --- a/src/Sequence/Types.hs +++ b/src/Sequence/Types.hs @@ -3,7 +3,7 @@ module Sequence.Types ( GameState, gEntities, gEntityNames, gFocus, gNextId' , Faction, faction, faction' - , SeqVal(..), seqVal + , SeqVal(..), seqVal, seqVal' , Entity(..), eFaction, eSeqVal, eStats , EntityName(..), entityName , EntityIdentifier(..), entityId, entityId' @@ -61,8 +61,13 @@ faction' = lens (CI.original . fromMaybe unaligned . view faction) (\s a -> s { newtype SeqVal = SeqVal { _seqVal :: Int } deriving (Show, Ord, Eq, Num, Integral, Enum, Real) -makeLenses ''SeqVal - + +seqVal :: Integral a => Iso' a SeqVal +seqVal = iso (SeqVal . fromIntegral) (fromIntegral . _seqVal) + +seqVal' :: Prism' String SeqVal +seqVal' = _Show . seqVal + data Entity = Entity { _eSeqVal :: Maybe SeqVal @@ -90,11 +95,12 @@ entityName = iso (EntityName . CI.mk) (CI.original . _entityName) newtype EntityIdentifier = EntityIdentifier { _entityId :: Integer } deriving (Show, Eq, Ord, Enum) -entityId :: Iso' Integer EntityIdentifier -entityId = iso EntityIdentifier _entityId +entityId :: Integral a => Iso' a EntityIdentifier +entityId = iso (EntityIdentifier . fromIntegral) (fromIntegral . _entityId) entityId' :: Prism' String EntityIdentifier -entityId' = prism' (show . _entityId) (fmap EntityIdentifier . readMaybe) +entityId' = _Show . entityId + data GameState = GameState { _gEntities :: Map EntityIdentifier Entity @@ -118,7 +124,7 @@ inhabitedFactions = to $ nub . sort . Map.elems . fmap (view eFaction) . view gE priorityQueue :: Getter GameState [(SeqVal, EntityIdentifier)] priorityQueue = to priorityQueue' where - priorityQueue' (Map.toAscList . view gEntities -> entities) = sortBy (comparing $ Down . snd) . concat . map filter . map (over _1 $ view eSeqVal) . map swap $ entities + priorityQueue' (Map.toAscList . view gEntities -> entities) = sortBy (comparing $ Down . fst) . concat . map filter . map (over _1 $ view eSeqVal) . map swap $ entities filter (Nothing, _) = mempty filter (Just val, id) = pure (val, id) -- cgit v1.2.3