diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-06-03 01:51:28 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-06-03 01:51:28 +0200 |
| commit | e82a3a8c147b34b75a203f66f974f5b3ec21d98c (patch) | |
| tree | 1597109533e3e21251da22126e224cd5c9188d13 | |
| parent | 49847034c56b105763a9d6d369f68a7d433ca521 (diff) | |
| download | 2017-01-16_17:13:37-e82a3a8c147b34b75a203f66f974f5b3ec21d98c.tar 2017-01-16_17:13:37-e82a3a8c147b34b75a203f66f974f5b3ec21d98c.tar.gz 2017-01-16_17:13:37-e82a3a8c147b34b75a203f66f974f5b3ec21d98c.tar.bz2 2017-01-16_17:13:37-e82a3a8c147b34b75a203f66f974f5b3ec21d98c.tar.xz 2017-01-16_17:13:37-e82a3a8c147b34b75a203f66f974f5b3ec21d98c.zip | |
focus
| -rw-r--r-- | src/Main.hs | 34 | ||||
| -rw-r--r-- | src/Sequence/Types.hs | 4 | ||||
| -rw-r--r-- | src/Sequence/Utils.hs | 17 |
3 files changed, 48 insertions, 7 deletions
diff --git a/src/Main.hs b/src/Main.hs index ea663e8..258e230 100644 --- a/src/Main.hs +++ b/src/Main.hs | |||
| @@ -19,6 +19,11 @@ import qualified Data.CaseInsensitive | |||
| 19 | import Data.Map.Strict (Map) | 19 | import Data.Map.Strict (Map) |
| 20 | import qualified Data.Map.Strict as Map | 20 | import qualified Data.Map.Strict as Map |
| 21 | 21 | ||
| 22 | import Data.Bimap (Bimap) | ||
| 23 | import qualified Data.Bimap as Bimap | ||
| 24 | |||
| 25 | import Data.List | ||
| 26 | |||
| 22 | import Data.List | 27 | import Data.List |
| 23 | import Data.Maybe | 28 | import Data.Maybe |
| 24 | 29 | ||
| @@ -43,10 +48,14 @@ main = do | |||
| 43 | , commandStyle = OnlyCommands | 48 | , commandStyle = OnlyCommands |
| 44 | , shellCommands = [ exitCommand "exit" | 49 | , shellCommands = [ exitCommand "exit" |
| 45 | , helpCommand "help" | 50 | , helpCommand "help" |
| 51 | , cmd "entities" listEntities "List all entities" | ||
| 52 | , cmd "tip" focusTip "Focus the entity at the top of the queue" | ||
| 53 | , cmd "focus" setFocus "Focus a specific entity" | ||
| 54 | , cmd "blur" blur "Focus no entity" | ||
| 55 | , cmd "remove" remove "Remove the focused entity from the queue" | ||
| 46 | , cmd "factions" listFactions "List all inhabited factions" | 56 | , cmd "factions" listFactions "List all inhabited factions" |
| 47 | , cmd "members" listFaction "List all members of a faction" | 57 | , cmd "members" listFaction "List all members of a faction" |
| 48 | , cmd "entities" listEntities "List all entities" | 58 | , cmd "align" alignEntity "Align the focused entity to a faction – creating it, if necessary" |
| 49 | , cmd "align" alignEntity "Align an entity to a faction creating it, if necessary" | ||
| 50 | ] | 59 | ] |
| 51 | } | 60 | } |
| 52 | void $ runShell description haskelineBackend (def :: GameState) | 61 | void $ runShell description haskelineBackend (def :: GameState) |
| @@ -77,6 +86,21 @@ listFactions = use inhabitedFactions >>= mapM_ (shellPutStrLn . view faction') | |||
| 77 | listEntities :: Sh GameState () | 86 | listEntities :: Sh GameState () |
| 78 | listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName) | 87 | listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName) |
| 79 | 88 | ||
| 80 | alignEntity :: Completable EntityIdentifier -> Completable Faction -> Sh GameState () | 89 | alignEntity :: Completable Faction -> Sh GameState () |
| 81 | alignEntity ident' nFaction' = flip withArg nFaction' $ \nFaction -> flip withArg ident' $ \ident -> do | 90 | alignEntity = withArg $ \nFaction -> withFocus $ \ident -> gEntities %= Map.adjust (set eFaction nFaction) ident |
| 82 | gEntities %= Map.adjust (set eFaction nFaction) ident | 91 | |
| 92 | focusTip, blur :: Sh GameState () | ||
| 93 | focusTip = gFocus <~ use tip | ||
| 94 | blur = gFocus .= Nothing | ||
| 95 | |||
| 96 | setFocus :: Completable EntityIdentifier -> Sh GameState () | ||
| 97 | setFocus = withArg $ \ident -> gFocus .= Just ident | ||
| 98 | |||
| 99 | remove :: Sh GameState () | ||
| 100 | remove = withFocus $ \ident -> do | ||
| 101 | name <- toName ident | ||
| 102 | confirmation <- askBool ("Are you sure you want to remove ‘" ++ name ++ "’? ") False | ||
| 103 | when confirmation $ do | ||
| 104 | gEntities %= Map.delete ident | ||
| 105 | gEntityNames %= Bimap.delete ident | ||
| 106 | blur | ||
diff --git a/src/Sequence/Types.hs b/src/Sequence/Types.hs index fd8d7ab..8895569 100644 --- a/src/Sequence/Types.hs +++ b/src/Sequence/Types.hs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeSynonymInstances, ViewPatterns, OverloadedStrings, TemplateHaskell, GeneralizedNewtypeDeriving #-} | 1 | {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeSynonymInstances, ViewPatterns, OverloadedStrings, TemplateHaskell, GeneralizedNewtypeDeriving #-} |
| 2 | 2 | ||
| 3 | module Sequence.Types | 3 | module Sequence.Types |
| 4 | ( GameState(..), gEntities, gEntityNames | 4 | ( GameState(..), gEntities, gEntityNames, gFocus |
| 5 | , Faction, faction, faction' | 5 | , Faction, faction, faction' |
| 6 | , SeqVal(..), seqVal | 6 | , SeqVal(..), seqVal |
| 7 | , Entity(..), eFaction, eSeqVal | 7 | , Entity(..), eFaction, eSeqVal |
| @@ -81,6 +81,7 @@ makeLenses ''EntityIdentifier | |||
| 81 | data GameState = GameState | 81 | data GameState = GameState |
| 82 | { _gEntities :: Map EntityIdentifier Entity | 82 | { _gEntities :: Map EntityIdentifier Entity |
| 83 | , _gEntityNames :: Bimap EntityIdentifier EntityName | 83 | , _gEntityNames :: Bimap EntityIdentifier EntityName |
| 84 | , _gFocus :: Maybe EntityIdentifier | ||
| 84 | } | 85 | } |
| 85 | makeLenses ''GameState | 86 | makeLenses ''GameState |
| 86 | 87 | ||
| @@ -88,6 +89,7 @@ instance Default GameState where | |||
| 88 | def = GameState | 89 | def = GameState |
| 89 | { _gEntities = def | 90 | { _gEntities = def |
| 90 | , _gEntityNames = Bimap.empty | 91 | , _gEntityNames = Bimap.empty |
| 92 | , _gFocus = Nothing | ||
| 91 | } | 93 | } |
| 92 | 94 | ||
| 93 | inhabitedFactions :: Getter GameState [Faction] | 95 | inhabitedFactions :: Getter GameState [Faction] |
diff --git a/src/Sequence/Utils.hs b/src/Sequence/Utils.hs index aa92081..aea853d 100644 --- a/src/Sequence/Utils.hs +++ b/src/Sequence/Utils.hs | |||
| @@ -1,7 +1,9 @@ | |||
| 1 | {-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-} | 1 | {-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-} |
| 2 | 2 | ||
| 3 | module Sequence.Utils | 3 | module Sequence.Utils |
| 4 | ( withArg, toName, fromName | 4 | ( withArg, withFocus |
| 5 | , askBool | ||
| 6 | , toName, fromName | ||
| 5 | ) where | 7 | ) where |
| 6 | 8 | ||
| 7 | import Sequence.Types | 9 | import Sequence.Types |
| @@ -28,6 +30,7 @@ import Data.List | |||
| 28 | 30 | ||
| 29 | import System.Console.Shell | 31 | import System.Console.Shell |
| 30 | import System.Console.Shell.ShellMonad | 32 | import System.Console.Shell.ShellMonad |
| 33 | import System.Console.Shell.Backend.Haskeline | ||
| 31 | 34 | ||
| 32 | 35 | ||
| 33 | class Argument a st | a -> st where | 36 | class Argument a st | a -> st where |
| @@ -38,6 +41,18 @@ withArg f (Completable str) = arg str >>= \a -> case a of | |||
| 38 | Nothing -> shellPutErrLn $ "Could not parse ‘" ++ str ++ "’" | 41 | Nothing -> shellPutErrLn $ "Could not parse ‘" ++ str ++ "’" |
| 39 | Just a -> f a | 42 | Just a -> f a |
| 40 | 43 | ||
| 44 | withFocus :: (EntityIdentifier -> Sh GameState ()) -> Sh GameState () | ||
| 45 | withFocus f = use gFocus >>= \focus -> case focus of | ||
| 46 | Nothing -> shellPutErrLn $ "Currently not focusing any entity" | ||
| 47 | Just id -> f id | ||
| 48 | |||
| 49 | askBool :: String -> Bool -> Sh st Bool | ||
| 50 | askBool prompt initial = liftIO $ runShell (initialShellDescription { shellCommands = [cmd "y" yes "yes", cmd "n" no "no"], commandStyle = SingleCharCommands, prompt = const $ return prompt, historyEnabled = False }) haskelineBackend initial | ||
| 51 | where | ||
| 52 | yes, no :: Sh Bool () | ||
| 53 | yes = put True | ||
| 54 | no = put False | ||
| 55 | |||
| 41 | unaligned = view faction' def | 56 | unaligned = view faction' def |
| 42 | 57 | ||
| 43 | toName :: MonadState GameState m => EntityIdentifier -> m String | 58 | toName :: MonadState GameState m => EntityIdentifier -> m String |
