{-# LANGUAGE ViewPatterns, RecordWildCards #-} import Control.Monad import Control.Lens hiding (Context(..)) import System.Console.Shell import System.Console.Shell.ShellMonad import System.Console.Shell.Backend.Haskeline import System.Console.ANSI (setSGRCode, SGR(..), ConsoleLayer(..), ConsoleIntensity(..), ColorIntensity(..), Color(..)) import System.Environment.XDG.BaseDir import System.FilePath import System.Directory import Data.Default import Data.CaseInsensitive (CI) import qualified Data.CaseInsensitive import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Bimap (Bimap) import qualified Data.Bimap as Bimap import Data.List import Data.List import Data.Maybe import Data.Function import Control.Monad.State.Strict import Sequence.Types import Sequence.Contact.Types import Sequence.Contact.Archetypes import Sequence.Contact.Tests import Sequence.Utils import Sequence.Formula import Text.Layout.Table import Text.Read (readMaybe) main :: IO () main = do historyFile <- getUserCacheFile "sequence" "history" createDirectoryIfMissing True $ takeDirectory historyFile let description = initialShellDescription { historyFile = Just historyFile , prompt = \st -> return $ maybe "" (++ " ") ((evalState ?? st) . toName <$> view gFocus st) ++ "→ " , beforePrompt = gets stateOutline >>= (\str -> if null str then return () else shellPutStrLn str) , commandStyle = OnlyCommands , shellCommands = [ exitCommand "exit" , helpCommand "help" , cmd "entities" listEntities "List all entities" , cmd "tip" focusTip "Focus the entity at the top of the queue" , cmd "focus" setFocus "Focus a specific entity" , cmd "blur" blur "Focus no entity" , cmd "remove" remove "Remove the focused entity from the queue" , cmd "factions" listFactions "List all inhabited factions" , cmd "members" listFaction "List all members of a faction" , cmd "align" alignEntity "Align the focused entity to a faction – creating it, if necessary" , cmd "name" nameEntity "Name the current entity overriding previous name assignments" , cmd "spawn" spawnEntity "Create a new entity from an archetype focusing on it" , cmd "roll" rollTest "Roll a test using the stats of the currently focused entity" ] } void $ runShell description haskelineBackend (def :: GameState) stateOutline :: GameState -> String stateOutline st | null pQueue = "" | otherwise = layoutTableToString rowGs (Just ("" : factions, repeat def)) (repeat def) unicodeBoldHeaderS where factions = map (view faction') $ st ^. inhabitedFactions pQueue = st ^. priorityQueue protoRows = groupBy ((==) `on` fst) pQueue faction id = fromJust $ view eFaction <$> Map.lookup id (st ^. gEntities) factionIndex id = fromJust $ elemIndex (view faction' $ faction id) factions rowGs = do rowGroup'@((seqVal', _):_) <- protoRows let rowGroup = map snd rowGroup' factionColumn i = [evalState ?? st $ toName x | x <- rowGroup, factionIndex x == i ] return . colsAllG top $ [show (seqVal' ^. seqVal)] : map factionColumn [0..(length factions - 1)] -- Query state listFactions, listEntities :: Sh GameState () listFactions = use inhabitedFactions >>= mapM_ (shellPutStrLn . view faction') listEntities = use (gEntities . to Map.keys) >>= mapM_ (shellPutStrLn <=< toName) -- Manage faction listFaction, alignEntity :: Completable Faction -> Sh GameState () listFaction = withArg $ \qFaction -> use gEntities >>= mapM_ (shellPutStrLn <=< toName) . Map.keys . Map.filter ((==) qFaction . view eFaction) alignEntity = withArg $ \nFaction -> withFocus $ \ident -> gEntities %= Map.adjust (set eFaction nFaction) ident -- Automatic focus focusTip, blur :: Sh GameState () focusTip = gFocus <~ preuse tip blur = gFocus .= Nothing -- Manual focus setFocus :: Completable EntityIdentifier -> Sh GameState () setFocus = withArg $ \ident -> gFocus ?= ident -- Drop information remove :: Sh GameState () remove = withFocus $ \ident -> do name <- toName ident confirmation <- askBool ("Are you sure you want to remove ‘" ++ name ++ "’?") False when confirmation $ do gEntities %= Map.delete ident gEntityNames %= Bimap.delete ident blur -- Manage Entity spawnEntity :: Completable Entity -> Sh GameState () spawnEntity = withArg $ \entity -> do identifier <- use gNextId' modify $ insertEntity entity gFocus ?= identifier nameEntity :: String -> Sh GameState () nameEntity ('#':_) = shellPutErrLn "We do not allow names that might shadow explicit access to entities via their number (‘#’)" nameEntity name = withFocus $ \ident -> modifying gEntityNames $ Bimap.insert ident (name ^. entityName) -- Dice rolls rollTest :: Completable (FormulaM Stats Test) -> Sh GameState () rollTest = withArg $ enactTest' >=> maybe (return ()) (shellPutStrLn . ppResult) where ppResult result = pad 3 (show $ result^.rWith) ++ ": " ++ setSGRCode (colour result) ++ name result ++ setSGRCode [] ++ " by " ++ pad 2 (show $ result^.rBy) ++ "pp" colour CritSuccess{..} = [SetColor Foreground Vivid Green, SetConsoleIntensity BoldIntensity] colour Success{..} = [SetColor Foreground Dull Green] colour Failure{..} = [SetColor Foreground Dull Red] colour CritFailure{..} = [SetColor Foreground Vivid Red, SetConsoleIntensity BoldIntensity] name CritSuccess{..} = "Critical Success" name Success{..} = "Success" name Failure{..} = "Failure" name CritFailure{..} = "Critical Failure" pad n str | length str >= n = str | otherwise = ' ' : pad (n - 1) str enactTest' :: (FormulaM Stats Test) -> Sh GameState (Maybe TestResult) enactTest' test = withFocus' $ \focus -> do (newFocus, result) <- evalFormula focus (enactTest =<< test) gFocus' .= newFocus return result