summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Main.hs7
-rw-r--r--src/Sequence/Utils.hs24
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
70listFaction :: Completable Faction -> Sh GameState () 71listFaction :: Completable Faction -> Sh GameState ()
71listFaction (Completable (flip (set faction') def -> qFaction)) = use gEntities >>= mapM_ (shellPutStrLn <=< toName) . Map.keys . Map.filter ((==) qFaction . view eFaction) 72listFaction = withArg $ \qFaction -> use gEntities >>= mapM_ (shellPutStrLn <=< toName) . Map.keys . Map.filter ((==) qFaction . view eFaction)
72 73
73listFactions :: Sh GameState () 74listFactions :: Sh GameState ()
74listFactions = use inhabitedFactions >>= mapM_ (shellPutStrLn . view faction') 75listFactions = use inhabitedFactions >>= mapM_ (shellPutStrLn . view faction')
75 76
76listEntities :: Sh GameState () 77listEntities :: Sh GameState ()
77listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName) 78listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName)
79
80alignEntity :: Completable EntityIdentifier -> Completable Faction -> Sh GameState ()
81alignEntity 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
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