summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-06-03 01:51:28 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2016-06-03 01:51:28 +0200
commite82a3a8c147b34b75a203f66f974f5b3ec21d98c (patch)
tree1597109533e3e21251da22126e224cd5c9188d13 /src
parent49847034c56b105763a9d6d369f68a7d433ca521 (diff)
download2017-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
Diffstat (limited to 'src')
-rw-r--r--src/Main.hs34
-rw-r--r--src/Sequence/Types.hs4
-rw-r--r--src/Sequence/Utils.hs17
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
19import Data.Map.Strict (Map) 19import Data.Map.Strict (Map)
20import qualified Data.Map.Strict as Map 20import qualified Data.Map.Strict as Map
21 21
22import Data.Bimap (Bimap)
23import qualified Data.Bimap as Bimap
24
25import Data.List
26
22import Data.List 27import Data.List
23import Data.Maybe 28import 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')
77listEntities :: Sh GameState () 86listEntities :: Sh GameState ()
78listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName) 87listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName)
79 88
80alignEntity :: Completable EntityIdentifier -> Completable Faction -> Sh GameState () 89alignEntity :: Completable Faction -> Sh GameState ()
81alignEntity ident' nFaction' = flip withArg nFaction' $ \nFaction -> flip withArg ident' $ \ident -> do 90alignEntity = withArg $ \nFaction -> withFocus $ \ident -> gEntities %= Map.adjust (set eFaction nFaction) ident
82 gEntities %= Map.adjust (set eFaction nFaction) ident 91
92focusTip, blur :: Sh GameState ()
93focusTip = gFocus <~ use tip
94blur = gFocus .= Nothing
95
96setFocus :: Completable EntityIdentifier -> Sh GameState ()
97setFocus = withArg $ \ident -> gFocus .= Just ident
98
99remove :: Sh GameState ()
100remove = 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
3module Sequence.Types 3module 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
81data GameState = GameState 81data 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 }
85makeLenses ''GameState 86makeLenses ''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
93inhabitedFactions :: Getter GameState [Faction] 95inhabitedFactions :: 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
3module Sequence.Utils 3module Sequence.Utils
4 ( withArg, toName, fromName 4 ( withArg, withFocus
5 , askBool
6 , toName, fromName
5 ) where 7 ) where
6 8
7import Sequence.Types 9import Sequence.Types
@@ -28,6 +30,7 @@ import Data.List
28 30
29import System.Console.Shell 31import System.Console.Shell
30import System.Console.Shell.ShellMonad 32import System.Console.Shell.ShellMonad
33import System.Console.Shell.Backend.Haskeline
31 34
32 35
33class Argument a st | a -> st where 36class 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
44withFocus :: (EntityIdentifier -> Sh GameState ()) -> Sh GameState ()
45withFocus f = use gFocus >>= \focus -> case focus of
46 Nothing -> shellPutErrLn $ "Currently not focusing any entity"
47 Just id -> f id
48
49askBool :: String -> Bool -> Sh st Bool
50askBool 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
41unaligned = view faction' def 56unaligned = view faction' def
42 57
43toName :: MonadState GameState m => EntityIdentifier -> m String 58toName :: MonadState GameState m => EntityIdentifier -> m String