summaryrefslogtreecommitdiff
path: root/src/Sequence/Utils.hs
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-06-03 00:59:08 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2016-06-03 00:59:08 +0200
commitab903fff1d08e36092d25205e061fe42e5c62d4b (patch)
tree4184b825773fa41a45470e4b7d5daa7ff3469551 /src/Sequence/Utils.hs
parent1b7f21ba636e4034f76495deafd0ac6ca9758a4e (diff)
download2017-01-16_17:13:37-ab903fff1d08e36092d25205e061fe42e5c62d4b.tar
2017-01-16_17:13:37-ab903fff1d08e36092d25205e061fe42e5c62d4b.tar.gz
2017-01-16_17:13:37-ab903fff1d08e36092d25205e061fe42e5c62d4b.tar.bz2
2017-01-16_17:13:37-ab903fff1d08e36092d25205e061fe42e5c62d4b.tar.xz
2017-01-16_17:13:37-ab903fff1d08e36092d25205e061fe42e5c62d4b.zip
align entities to factions
Diffstat (limited to 'src/Sequence/Utils.hs')
-rw-r--r--src/Sequence/Utils.hs24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/Sequence/Utils.hs b/src/Sequence/Utils.hs
index 7d28b83..aa92081 100644
--- a/src/Sequence/Utils.hs
+++ b/src/Sequence/Utils.hs
@@ -1,7 +1,7 @@
1{-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses #-} 1{-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-}
2 2
3module Sequence.Utils 3module Sequence.Utils
4 ( toName, fromName 4 ( withArg, toName, fromName
5 ) where 5 ) where
6 6
7import Sequence.Types 7import Sequence.Types
@@ -22,11 +22,21 @@ import qualified Data.Map.Strict as Map
22import Data.Function 22import Data.Function
23import Data.Default 23import Data.Default
24import Data.Maybe 24import Data.Maybe
25import Text.Read 25import Text.Read (readMaybe)
26 26
27import Data.List 27import Data.List
28 28
29import System.Console.Shell 29import System.Console.Shell
30import System.Console.Shell.ShellMonad
31
32
33class Argument a st | a -> st where
34 arg :: String -> Sh st (Maybe a)
35
36withArg :: Argument a st => (a -> Sh st ()) -> (Completable a -> Sh st ())
37withArg f (Completable str) = arg str >>= \a -> case a of
38 Nothing -> shellPutErrLn $ "Could not parse ‘" ++ str ++ "’"
39 Just a -> f a
30 40
31unaligned = view faction' def 41unaligned = view faction' def
32 42
@@ -34,13 +44,19 @@ toName :: MonadState GameState m => EntityIdentifier -> m String
34toName id = fromMaybe (show $ id ^. entityId) . fmap (CI.original . view entityName) . Bimap.lookup id <$> use gEntityNames 44toName id = fromMaybe (show $ id ^. entityId) . fmap (CI.original . view entityName) . Bimap.lookup id <$> use gEntityNames
35 45
36fromName :: MonadState GameState m => String -> m (Maybe EntityIdentifier) 46fromName :: MonadState GameState m => String -> m (Maybe EntityIdentifier)
37fromName (readMaybe -> n@(Just _)) = return $ EntityIdentifier <$> n 47fromName (readMaybe -> (Just (EntityIdentifier -> n))) = (n <$) . guard . Map.member n <$> use gEntities
38fromName (EntityName . CI.mk -> name) = Bimap.lookupR name <$> use gEntityNames 48fromName (EntityName . CI.mk -> name) = Bimap.lookupR name <$> use gEntityNames
39 49
40instance Completion EntityIdentifier GameState where 50instance Completion EntityIdentifier GameState where
41 completableLabel _ = "<entity>" 51 completableLabel _ = "<entity>"
42 complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . map ((evalState ?? st) . toName) . Map.keys $ st ^. gEntities 52 complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . map ((evalState ?? st) . toName) . Map.keys $ st ^. gEntities
43 53
54instance Argument EntityIdentifier GameState where
55 arg = fromName
56
44instance Completion Faction GameState where 57instance Completion Faction GameState where
45 completableLabel _ = "<faction>" 58 completableLabel _ = "<faction>"
46 complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . nub . sort $ unaligned : map (view faction') (st ^. inhabitedFactions) 59 complete _ st prefix = return . filter ((isPrefixOf `on` CI.foldCase) prefix) . nub . sort $ unaligned : map (view faction') (st ^. inhabitedFactions)
60
61instance Argument Faction GameState where
62 arg = return . Just . flip (set faction') def