summaryrefslogtreecommitdiff
path: root/src/Sequence/Contact/Tests.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Sequence/Contact/Tests.hs')
-rw-r--r--src/Sequence/Contact/Tests.hs100
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
3module Sequence.Contact.Tests
4 ( TestResult(..)
5 , Test
6 , enactTest
7 ) where
8
9import Sequence.Formula
10import Sequence.Types
11import Sequence.Utils
12import Sequence.Contact.Types
13
14import Control.Monad
15import Control.Monad.Reader
16import Control.Lens
17
18import Data.Default
19import Data.List
20import Data.Maybe
21
22import Data.CaseInsensitive (CI)
23import qualified Data.CaseInsensitive as CI
24
25import Data.Map.Strict (Map)
26import qualified Data.Map.Strict as Map
27
28import Data.Traversable (mapM)
29
30import Prelude hiding (mapM)
31
32
33data 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)
38makeLenses ''TestResult
39
40data Test = Test
41 { _tCritSuccessMod
42 , _tCritFailureMod
43 , _tBaseDifficulty
44 , _tMod :: Int
45 }
46 deriving (Eq, Ord)
47makeLenses ''Test
48
49instance Default Test where
50 def = Test
51 { _tCritSuccessMod = 0
52 , _tCritFailureMod = 0
53 , _tBaseDifficulty = 50
54 , _tMod = 0
55 }
56
57tests :: MonadReader Stats m => m (Map (CI String) (FormulaM Stats Test))
58tests = 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
67getTest :: String -> Fold Stats (FormulaM Stats Test)
68getTest (CI.mk -> str) = folding tests'
69 where
70 tests' state = Map.lookup str (tests state) -- >>= (\get -> preview get state)
71
72instance 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
76instance Argument (FormulaM Stats Test) GameState where
77 arg str = join <$> preuses (gFocus' . eStats) (preview (getTest str))
78
79enactTest :: Test -> FormulaM input TestResult
80enactTest 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)