diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-06-05 01:10:24 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-06-05 01:10:24 +0200 |
commit | e93892c008759957e4ee567e7e642bd8a0dd9286 (patch) | |
tree | bc2bf233a51cebe7d6525c1dfd6511986dd85cbb /src/Sequence/Contact | |
parent | 62ed6579cc1a71c4e962063999743f7fcd927f1c (diff) | |
download | 2017-01-16_17:13:37-e93892c008759957e4ee567e7e642bd8a0dd9286.tar 2017-01-16_17:13:37-e93892c008759957e4ee567e7e642bd8a0dd9286.tar.gz 2017-01-16_17:13:37-e93892c008759957e4ee567e7e642bd8a0dd9286.tar.bz2 2017-01-16_17:13:37-e93892c008759957e4ee567e7e642bd8a0dd9286.tar.xz 2017-01-16_17:13:37-e93892c008759957e4ee567e7e642bd8a0dd9286.zip |
Framework for rolling tests
Diffstat (limited to 'src/Sequence/Contact')
-rw-r--r-- | src/Sequence/Contact/Tests.hs | 100 | ||||
-rw-r--r-- | src/Sequence/Contact/Types.hs | 3 |
2 files changed, 102 insertions, 1 deletions
diff --git a/src/Sequence/Contact/Tests.hs b/src/Sequence/Contact/Tests.hs new file mode 100644 index 0000000..86f72b5 --- /dev/null +++ b/src/Sequence/Contact/Tests.hs | |||
@@ -0,0 +1,100 @@ | |||
1 | {-# LANGUAGE TemplateHaskell, OverloadedStrings, OverloadedLists, ViewPatterns, MultiParamTypeClasses, FlexibleContexts, RankNTypes, TypeSynonymInstances, FlexibleInstances, ImpredicativeTypes #-} | ||
2 | |||
3 | module Sequence.Contact.Tests | ||
4 | ( TestResult(..) | ||
5 | , Test | ||
6 | , enactTest | ||
7 | ) where | ||
8 | |||
9 | import Sequence.Formula | ||
10 | import Sequence.Types | ||
11 | import Sequence.Utils | ||
12 | import Sequence.Contact.Types | ||
13 | |||
14 | import Control.Monad | ||
15 | import Control.Monad.Reader | ||
16 | import Control.Lens | ||
17 | |||
18 | import Data.Default | ||
19 | import Data.List | ||
20 | import Data.Maybe | ||
21 | |||
22 | import Data.CaseInsensitive (CI) | ||
23 | import qualified Data.CaseInsensitive as CI | ||
24 | |||
25 | import Data.Map.Strict (Map) | ||
26 | import qualified Data.Map.Strict as Map | ||
27 | |||
28 | import Data.Traversable (mapM) | ||
29 | |||
30 | import Prelude hiding (mapM) | ||
31 | |||
32 | |||
33 | data TestResult = CritSuccess { _rWith, _rBy :: Int } | ||
34 | | Success { _rWith, _rBy :: Int } | ||
35 | | Failure { _rWith, _rBy :: Int } | ||
36 | | CritFailure { _rWith, _rBy :: Int } | ||
37 | deriving (Eq, Ord, Show) | ||
38 | makeLenses ''TestResult | ||
39 | |||
40 | data Test = Test | ||
41 | { _tCritSuccessMod | ||
42 | , _tCritFailureMod | ||
43 | , _tBaseDifficulty | ||
44 | , _tMod :: Int | ||
45 | } | ||
46 | deriving (Eq, Ord) | ||
47 | makeLenses ''Test | ||
48 | |||
49 | instance Default Test where | ||
50 | def = Test | ||
51 | { _tCritSuccessMod = 0 | ||
52 | , _tCritFailureMod = 0 | ||
53 | , _tBaseDifficulty = 50 | ||
54 | , _tMod = 0 | ||
55 | } | ||
56 | |||
57 | tests :: MonadReader Stats m => m (Map (CI String) (FormulaM Stats Test)) | ||
58 | tests = mconcat <$> sequence [ test "Stärke" (sAStrength . attributeTest) | ||
59 | ] | ||
60 | where | ||
61 | test k v = maybe mempty (Map.singleton k) <$> preview v | ||
62 | |||
63 | -- skillTest = to (\x -> flip (set tBaseDifficulty) def <$> x) | ||
64 | attributeTest = to (\x -> flip (set tBaseDifficulty) def . (* 10) <$> x) | ||
65 | |||
66 | |||
67 | getTest :: String -> Fold Stats (FormulaM Stats Test) | ||
68 | getTest (CI.mk -> str) = folding tests' | ||
69 | where | ||
70 | tests' state = Map.lookup str (tests state) -- >>= (\get -> preview get state) | ||
71 | |||
72 | instance Completion (FormulaM Stats Test) GameState where | ||
73 | completableLabel _ = "<test>" | ||
74 | complete _ st (CI.foldCase -> prefix) = return . fromMaybe [] . fmap (filter (prefix `isPrefixOf`) . map CI.foldedCase . Map.keys) $ previews (gFocus' . eStats) tests st | ||
75 | |||
76 | instance Argument (FormulaM Stats Test) GameState where | ||
77 | arg str = join <$> preuses (gFocus' . eStats) (preview (getTest str)) | ||
78 | |||
79 | enactTest :: Test -> FormulaM input TestResult | ||
80 | enactTest test = toResult <$> d 100 | ||
81 | where | ||
82 | critFailureBar = 95 - test^.tCritFailureMod | ||
83 | critSuccessBar = 5 + test^.tCritSuccessMod | ||
84 | bar = test^.tBaseDifficulty + test^.tMod | ||
85 | toResult pw = (toResult' pw) pw (abs $ bar - pw) | ||
86 | toResult' pw | ||
87 | | bar > critSuccessBar | ||
88 | , pw <= critSuccessBar = CritSuccess | ||
89 | | pw <= bar = Success | ||
90 | | pw >= critFailureBar = CritFailure | ||
91 | | otherwise = Failure | ||
92 | |||
93 | -- hasTest :: Stats -> String -> Bool | ||
94 | -- hasTest stats str = has (getTest str) stats | ||
95 | |||
96 | -- rollTest :: String -> FormulaM Stats (Maybe TestResult) | ||
97 | -- rollTest str = preview (getTest str) >>= enactTest' | ||
98 | -- where | ||
99 | -- enactTest' Nothing = return Nothing | ||
100 | -- enactTest' (Just t) = Just <$> (enactTest =<< t) | ||
diff --git a/src/Sequence/Contact/Types.hs b/src/Sequence/Contact/Types.hs index 4166812..c00a60d 100644 --- a/src/Sequence/Contact/Types.hs +++ b/src/Sequence/Contact/Types.hs | |||
@@ -1,9 +1,10 @@ | |||
1 | {-# LANGUAGE TemplateHaskell #-} | 1 | {-# LANGUAGE TemplateHaskell, RecordWildCards, OverloadedStrings, OverloadedLists, ViewPatterns, RankNTypes #-} |
2 | 2 | ||
3 | module Sequence.Contact.Types where | 3 | module Sequence.Contact.Types where |
4 | 4 | ||
5 | import Sequence.Formula | 5 | import Sequence.Formula |
6 | 6 | ||
7 | import Control.Monad | ||
7 | import Control.Lens | 8 | import Control.Lens |
8 | 9 | ||
9 | import Data.Default | 10 | import Data.Default |