diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-06-03 00:59:08 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-06-03 00:59:08 +0200 |
commit | ab903fff1d08e36092d25205e061fe42e5c62d4b (patch) | |
tree | 4184b825773fa41a45470e4b7d5daa7ff3469551 /src | |
parent | 1b7f21ba636e4034f76495deafd0ac6ca9758a4e (diff) | |
download | 2017-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')
-rw-r--r-- | src/Main.hs | 7 | ||||
-rw-r--r-- | src/Sequence/Utils.hs | 24 |
2 files changed, 26 insertions, 5 deletions
diff --git a/src/Main.hs b/src/Main.hs index 639673c..903da05 100644 --- a/src/Main.hs +++ b/src/Main.hs | |||
@@ -46,6 +46,7 @@ main = do | |||
46 | , cmd "factions" listFactions "List all inhabited factions" | 46 | , cmd "factions" listFactions "List all inhabited factions" |
47 | , cmd "members" listFaction "List all members of a faction" | 47 | , cmd "members" listFaction "List all members of a faction" |
48 | , cmd "entities" listEntities "List all entities" | 48 | , cmd "entities" listEntities "List all entities" |
49 | , cmd "align" alignEntity "Align an entity to a faction creating it, if necessary" | ||
49 | ] | 50 | ] |
50 | } | 51 | } |
51 | void $ runShell description haskelineBackend (def :: GameState) | 52 | void $ runShell description haskelineBackend (def :: GameState) |
@@ -68,10 +69,14 @@ stateOutline st | |||
68 | return . colsAllG top $ [show (seqVal' ^. seqVal)] : map factionColumn [0..(length factions - 1)] | 69 | return . colsAllG top $ [show (seqVal' ^. seqVal)] : map factionColumn [0..(length factions - 1)] |
69 | 70 | ||
70 | listFaction :: Completable Faction -> Sh GameState () | 71 | listFaction :: Completable Faction -> Sh GameState () |
71 | listFaction (Completable (flip (set faction') def -> qFaction)) = use gEntities >>= mapM_ (shellPutStrLn <=< toName) . Map.keys . Map.filter ((==) qFaction . view eFaction) | 72 | listFaction = withArg $ \qFaction -> use gEntities >>= mapM_ (shellPutStrLn <=< toName) . Map.keys . Map.filter ((==) qFaction . view eFaction) |
72 | 73 | ||
73 | listFactions :: Sh GameState () | 74 | listFactions :: Sh GameState () |
74 | listFactions = use inhabitedFactions >>= mapM_ (shellPutStrLn . view faction') | 75 | listFactions = use inhabitedFactions >>= mapM_ (shellPutStrLn . view faction') |
75 | 76 | ||
76 | listEntities :: Sh GameState () | 77 | listEntities :: Sh GameState () |
77 | listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName) | 78 | listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName) |
79 | |||
80 | alignEntity :: Completable EntityIdentifier -> Completable Faction -> Sh GameState () | ||
81 | alignEntity ident' nFaction' = flip withArg nFaction' $ \nFaction -> flip withArg ident' $ \ident -> do | ||
82 | gEntities %= Map.adjust (set eFaction nFaction) ident | ||
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 | ||
3 | module Sequence.Utils | 3 | module Sequence.Utils |
4 | ( toName, fromName | 4 | ( withArg, toName, fromName |
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import Sequence.Types | 7 | import Sequence.Types |
@@ -22,11 +22,21 @@ import qualified Data.Map.Strict as Map | |||
22 | import Data.Function | 22 | import Data.Function |
23 | import Data.Default | 23 | import Data.Default |
24 | import Data.Maybe | 24 | import Data.Maybe |
25 | import Text.Read | 25 | import Text.Read (readMaybe) |
26 | 26 | ||
27 | import Data.List | 27 | import Data.List |
28 | 28 | ||
29 | import System.Console.Shell | 29 | import System.Console.Shell |
30 | import System.Console.Shell.ShellMonad | ||
31 | |||
32 | |||
33 | class Argument a st | a -> st where | ||
34 | arg :: String -> Sh st (Maybe a) | ||
35 | |||
36 | withArg :: Argument a st => (a -> Sh st ()) -> (Completable a -> Sh st ()) | ||
37 | withArg f (Completable str) = arg str >>= \a -> case a of | ||
38 | Nothing -> shellPutErrLn $ "Could not parse ‘" ++ str ++ "’" | ||
39 | Just a -> f a | ||
30 | 40 | ||
31 | unaligned = view faction' def | 41 | unaligned = view faction' def |
32 | 42 | ||
@@ -34,13 +44,19 @@ toName :: MonadState GameState m => EntityIdentifier -> m String | |||
34 | toName id = fromMaybe (show $ id ^. entityId) . fmap (CI.original . view entityName) . Bimap.lookup id <$> use gEntityNames | 44 | toName id = fromMaybe (show $ id ^. entityId) . fmap (CI.original . view entityName) . Bimap.lookup id <$> use gEntityNames |
35 | 45 | ||
36 | fromName :: MonadState GameState m => String -> m (Maybe EntityIdentifier) | 46 | fromName :: MonadState GameState m => String -> m (Maybe EntityIdentifier) |
37 | fromName (readMaybe -> n@(Just _)) = return $ EntityIdentifier <$> n | 47 | fromName (readMaybe -> (Just (EntityIdentifier -> n))) = (n <$) . guard . Map.member n <$> use gEntities |
38 | fromName (EntityName . CI.mk -> name) = Bimap.lookupR name <$> use gEntityNames | 48 | fromName (EntityName . CI.mk -> name) = Bimap.lookupR name <$> use gEntityNames |
39 | 49 | ||
40 | instance Completion EntityIdentifier GameState where | 50 | instance 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 | ||
54 | instance Argument EntityIdentifier GameState where | ||
55 | arg = fromName | ||
56 | |||
44 | instance Completion Faction GameState where | 57 | instance 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 | |||
61 | instance Argument Faction GameState where | ||
62 | arg = return . Just . flip (set faction') def | ||