From cc4e079be3d0e918119c08301595460c3e91ef3c Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 2 Dec 2016 13:26:55 +0100 Subject: Print all applicable values --- src/Main.hs | 14 ++++++++++++++ src/Sequence/Formula.hs | 4 ++++ src/Sequence/Utils.hs | 1 + 3 files changed, 19 insertions(+) diff --git a/src/Main.hs b/src/Main.hs index 9a51332..1c2afd4 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -114,6 +114,7 @@ main = do , cmd "heal'" healFatigue "Heal the focused entity of fatigue" , cmd "log" dumpLog "Print the combat log" , cmd "val" printVal "Find the distribution of a specific value of the current entities" + , cmd "summary" printVals "Find the averages of applicable all values" ] , wordBreakChars = wordBreakChars initialShellDescription \\ [',', '\''] } @@ -572,3 +573,16 @@ printVal = withArg $ \formula -> withFocus $ \focusId -> do lengths = map (length . show . fst) vals -- normalize p = p / maximum (map snd vals) normalize = id + +printVals :: Sh GameState () +printVals = withFocus $ \focusId -> do + name <- toName focusId + sheet <- Map.mapMaybe id <$> mapM (\l -> preuse $ gFocus' . eStats . to l . _Just) statAccessors + let + maxLength = maximum . map (length . CI.original) $ Map.keys sheet + printAvg (str, formula) = do + result <- focusState (gFocus' . eStats) (findAverage [name] formula) + case result of + Just avg -> shellPutStrLn $ printf "%*s: %.2f" maxLength (CI.original str) (fromRational avg :: Double) + Nothing -> return () + mapM_ printAvg $ Map.toList sheet diff --git a/src/Sequence/Formula.hs b/src/Sequence/Formula.hs index 2ac1210..878ec7f 100644 --- a/src/Sequence/Formula.hs +++ b/src/Sequence/Formula.hs @@ -5,6 +5,7 @@ module Sequence.Formula , (:<:)(..), Context(..), ctx , evalFormula, evalFormula' , findDistribution, findDistribution' + , findAverage , val , d, z , Table, table @@ -137,6 +138,9 @@ evalFormula' promptPref formula = uncurry (<$) . over _2 put . swap =<< flip (ev findDistribution' :: (MonadIO m, sInput :<: lInput, MonadState lInput m, Ord a) => [String] -> FormulaM sInput a -> m (Map a Rational) findDistribution' promptPref formula = uncurry (<$) . over _2 put . swap =<< flip (findDistribution promptPref) formula =<< get +findAverage :: (MonadIO m, sInput :<: lInput, MonadState lInput m, Real a) => [String] -> FormulaM sInput a -> m Rational +findAverage promptPref formula = sum . map (\(val, prob) -> toRational val * prob) . Map.toList <$> findDistribution' promptPref formula + val :: Traversal' input (Formula input) -> [String] -> Bool -> Formula input val answer prompt keepResult = do gets (Set.member prompt) >>= bool (modify $ Set.insert prompt) (modify (Set.delete prompt) >> throwError Question{..}) diff --git a/src/Sequence/Utils.hs b/src/Sequence/Utils.hs index f51bcd8..9f03ca7 100644 --- a/src/Sequence/Utils.hs +++ b/src/Sequence/Utils.hs @@ -7,6 +7,7 @@ module Sequence.Utils , toName, toDesc , outputLogged , scaleTimer + , statAccessors , Argument(..) , Completion(..) , module Sequence.Utils.Ask -- cgit v1.2.3