diff options
Diffstat (limited to 'src/Sequence/Contact/Tests.hs')
-rw-r--r-- | src/Sequence/Contact/Tests.hs | 100 |
1 files changed, 100 insertions, 0 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) | ||