From 1b7f21ba636e4034f76495deafd0ac6ca9758a4e Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 2 Jun 2016 23:29:36 +0200 Subject: rudimentary state --- src/Sequence/Utils.hs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Sequence/Utils.hs (limited to 'src/Sequence/Utils.hs') diff --git a/src/Sequence/Utils.hs b/src/Sequence/Utils.hs new file mode 100644 index 0000000..7d28b83 --- /dev/null +++ b/src/Sequence/Utils.hs @@ -0,0 +1,46 @@ +{-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses #-} + +module Sequence.Utils + ( toName, fromName + ) where + +import Sequence.Types + +import Control.Monad.State.Strict + +import Control.Lens + +import Data.Bimap (Bimap) +import qualified Data.Bimap as Bimap + +import Data.CaseInsensitive (CI) +import qualified Data.CaseInsensitive as CI + +import Data.Map.Strict (Map) +import qualified Data.Map.Strict as Map + +import Data.Function +import Data.Default +import Data.Maybe +import Text.Read + +import Data.List + +import System.Console.Shell + +unaligned = view faction' def + +toName :: MonadState GameState m => EntityIdentifier -> m String +toName id = fromMaybe (show $ id ^. entityId) . fmap (CI.original . view entityName) . Bimap.lookup id <$> use gEntityNames + +fromName :: MonadState GameState m => String -> m (Maybe EntityIdentifier) +fromName (readMaybe -> n@(Just _)) = return $ EntityIdentifier <$> n +fromName (EntityName . CI.mk -> name) = Bimap.lookupR name <$> use gEntityNames + +instance Completion EntityIdentifier GameState where + completableLabel _ = "" + complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . map ((evalState ?? st) . toName) . Map.keys $ st ^. gEntities + +instance Completion Faction GameState where + completableLabel _ = "" + complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . nub . sort $ unaligned : map (view faction') (st ^. inhabitedFactions) -- cgit v1.2.3