Merge pull request #22 from mtlstats/confirm-ga
Confirm goal/assist data
This commit is contained in:
commit
9977a73da4
|
@ -35,6 +35,7 @@ module Mtlstats.Actions
|
|||
, recordGoalAssists
|
||||
, awardGoal
|
||||
, awardAssist
|
||||
, resetGoalData
|
||||
) where
|
||||
|
||||
import Control.Monad.Trans.State (modify)
|
||||
|
@ -164,6 +165,7 @@ recordGoalAssists ps = fromMaybe ps $ do
|
|||
%~ (goalBy .~ Nothing)
|
||||
. (assistsBy .~ [])
|
||||
. (pointsAccounted %~ succ)
|
||||
. (confirmGoalDataFlag .~ False)
|
||||
|
||||
-- | Awards a goal to a player
|
||||
awardGoal
|
||||
|
@ -194,3 +196,10 @@ awardAssist n ps = ps
|
|||
& pYtd.psAssists %~ succ
|
||||
& pLifetime.psAssists %~ succ
|
||||
else p) . zip [0..]
|
||||
|
||||
-- | Resets the entered data for the current goal
|
||||
resetGoalData :: ProgState -> ProgState
|
||||
resetGoalData ps = ps & progMode.gameStateL
|
||||
%~ (goalBy .~ Nothing)
|
||||
. (assistsBy .~ [])
|
||||
. (confirmGoalDataFlag .~ False)
|
||||
|
|
|
@ -36,6 +36,7 @@ import Mtlstats.Menu
|
|||
import Mtlstats.Prompt
|
||||
import Mtlstats.Report
|
||||
import Mtlstats.Types
|
||||
import Mtlstats.Util
|
||||
|
||||
-- | Reads the program state and returs the apropriate controller to
|
||||
-- run
|
||||
|
@ -185,7 +186,8 @@ verifyDataC = Controller
|
|||
goalInput :: GameState -> Controller
|
||||
goalInput gs
|
||||
| null (gs^.goalBy ) = recordGoalC
|
||||
| otherwise = recordAssistC
|
||||
| not (gs^.confirmGoalDataFlag) = recordAssistC
|
||||
| otherwise = confirmGoalDataC
|
||||
|
||||
recordGoalC :: Controller
|
||||
recordGoalC = Controller
|
||||
|
@ -209,6 +211,36 @@ recordAssistC = Controller
|
|||
return True
|
||||
}
|
||||
|
||||
confirmGoalDataC :: Controller
|
||||
confirmGoalDataC = Controller
|
||||
{ drawController = \s -> do
|
||||
let
|
||||
(game, goal) = gameGoal s
|
||||
gs = s^.progMode.gameStateL
|
||||
players = s^.database.dbPlayers
|
||||
msg = unlines $
|
||||
[ " Game: " ++ padNum 2 game
|
||||
, " Goal: " ++ show goal
|
||||
, "Goal scored by: " ++
|
||||
playerSummary (fromJust $ gs^.goalBy >>= flip nth players)
|
||||
] ++
|
||||
map
|
||||
(\pid -> " Assisted by: " ++
|
||||
playerSummary (fromJust $ nth pid players))
|
||||
(gs^.assistsBy) ++
|
||||
[ ""
|
||||
, "Is the above information correct? (Y/N)"
|
||||
]
|
||||
C.drawString msg
|
||||
return C.CursorInvisible
|
||||
, handleController = \e -> do
|
||||
case ynHandler e of
|
||||
Just True -> modify recordGoalAssists
|
||||
Just False -> modify resetGoalData
|
||||
Nothing -> return ()
|
||||
return True
|
||||
}
|
||||
|
||||
reportC :: Controller
|
||||
reportC = Controller
|
||||
{ drawController = \s -> do
|
||||
|
|
|
@ -227,12 +227,12 @@ recordAssistPrompt game goal assist = selectPlayerPrompt
|
|||
++ "Goal: " ++ show goal ++ "\n"
|
||||
++ "Assist #" ++ show assist ++ ": "
|
||||
) $ \case
|
||||
Nothing -> modify recordGoalAssists
|
||||
Nothing -> modify $ progMode.gameStateL.confirmGoalDataFlag .~ True
|
||||
Just n -> do
|
||||
modify $ progMode.gameStateL.assistsBy %~ (++[n])
|
||||
nAssists <- length <$> gets (view $ progMode.gameStateL.assistsBy)
|
||||
when (nAssists >= maxAssists) $
|
||||
modify recordGoalAssists
|
||||
modify $ progMode.gameStateL.confirmGoalDataFlag .~ True
|
||||
|
||||
drawSimplePrompt :: String -> ProgState -> C.Update ()
|
||||
drawSimplePrompt pStr s = C.drawString $ pStr ++ s^.inputBuffer
|
||||
|
|
|
@ -58,6 +58,7 @@ module Mtlstats.Types (
|
|||
pointsAccounted,
|
||||
goalBy,
|
||||
assistsBy,
|
||||
confirmGoalDataFlag,
|
||||
-- ** CreatePlayerState Lenses
|
||||
cpsNumber,
|
||||
cpsName,
|
||||
|
@ -127,7 +128,8 @@ module Mtlstats.Types (
|
|||
pPoints,
|
||||
playerSearch,
|
||||
playerSearchExact,
|
||||
modifyPlayer
|
||||
modifyPlayer,
|
||||
playerSummary
|
||||
) where
|
||||
|
||||
import Control.Monad.Trans.State (StateT)
|
||||
|
@ -213,6 +215,7 @@ data GameState = GameState
|
|||
, _assistsBy :: [Int]
|
||||
-- ^ The index numbers of the players who have assisted the most
|
||||
-- recently entered goal
|
||||
, _confirmGoalDataFlag :: Bool
|
||||
} deriving (Eq, Show)
|
||||
|
||||
-- | The type of game
|
||||
|
@ -511,6 +514,7 @@ newGameState = GameState
|
|||
, _pointsAccounted = 0
|
||||
, _goalBy = Nothing
|
||||
, _assistsBy = []
|
||||
, _confirmGoalDataFlag = False
|
||||
}
|
||||
|
||||
-- | Constructor for a 'CreatePlayerState'
|
||||
|
@ -710,3 +714,8 @@ modifyPlayer f n = map
|
|||
(\p -> if p^.pName == n
|
||||
then f p
|
||||
else p)
|
||||
|
||||
-- | Provides a short summary string for a player
|
||||
playerSummary :: Player -> String
|
||||
playerSummary p =
|
||||
p^.pName ++ " (" ++ show (p^.pNumber) ++ ") " ++ p^.pPosition
|
||||
|
|
|
@ -24,7 +24,7 @@ module ActionsSpec (spec) where
|
|||
import Control.Monad (replicateM)
|
||||
import Lens.Micro ((^.), (&), (.~), (?~), (%~))
|
||||
import System.Random (randomRIO)
|
||||
import Test.Hspec (Spec, context, describe, it, shouldBe, shouldNotBe)
|
||||
import Test.Hspec (Spec, context, describe, it, runIO, shouldBe, shouldNotBe)
|
||||
|
||||
import Mtlstats.Actions
|
||||
import Mtlstats.Types
|
||||
|
@ -44,6 +44,7 @@ spec = describe "Mtlstats.Actions" $ do
|
|||
recordGoalAssistsSpec
|
||||
awardGoalSpec
|
||||
awardAssistSpec
|
||||
resetGoalDataSpec
|
||||
|
||||
startNewSeasonSpec :: Spec
|
||||
startNewSeasonSpec = describe "startNewSeason" $ do
|
||||
|
@ -373,6 +374,7 @@ recordGoalAssistsSpec = describe "recordGoalAssists" $ do
|
|||
& progMode.gameStateL
|
||||
%~ (goalBy ?~ 0)
|
||||
. (assistsBy .~ [1, 2])
|
||||
. (confirmGoalDataFlag .~ True)
|
||||
& recordGoalAssists
|
||||
|
||||
mapM_
|
||||
|
@ -407,6 +409,9 @@ recordGoalAssistsSpec = describe "recordGoalAssists" $ do
|
|||
it "should increment the pointsAccounted counter" $
|
||||
ps^.progMode.gameStateL.pointsAccounted `shouldBe` 1
|
||||
|
||||
it "should clear the confirmGoalDataFlag" $
|
||||
ps^.progMode.gameStateL.confirmGoalDataFlag `shouldBe` False
|
||||
|
||||
awardGoalSpec :: Spec
|
||||
awardGoalSpec = describe "awardGoal" $ do
|
||||
let
|
||||
|
@ -513,6 +518,30 @@ awardAssistSpec = describe "awardAssist" $ do
|
|||
in it "should not change anything" $
|
||||
ps'^.database.dbPlayers `shouldBe` ps^.database.dbPlayers
|
||||
|
||||
resetGoalDataSpec :: Spec
|
||||
resetGoalDataSpec = describe "resetGoalData" $ do
|
||||
players <- runIO $ replicateM 5 makePlayer
|
||||
let
|
||||
gs
|
||||
= newGameState
|
||||
& goalBy ?~ 1
|
||||
& assistsBy .~ [2, 3]
|
||||
& confirmGoalDataFlag .~ True
|
||||
ps
|
||||
= newProgState
|
||||
& database.dbPlayers .~ players
|
||||
& progMode.gameStateL .~ gs
|
||||
& resetGoalData
|
||||
|
||||
it "should clear the goalBy value" $
|
||||
ps^.progMode.gameStateL.goalBy `shouldBe` Nothing
|
||||
|
||||
it "should clear the assists by list" $
|
||||
ps^.progMode.gameStateL.assistsBy `shouldBe` []
|
||||
|
||||
it "should clear confirmGoalDataFlag" $
|
||||
ps^.progMode.gameStateL.confirmGoalDataFlag `shouldBe` False
|
||||
|
||||
makePlayer :: IO Player
|
||||
makePlayer = Player
|
||||
<$> makeNum
|
||||
|
|
|
@ -58,6 +58,7 @@ spec = describe "Mtlstats.Types" $ do
|
|||
playerSearchSpec
|
||||
playerSearchExactSpec
|
||||
modifyPlayerSpec
|
||||
playerSummarySpec
|
||||
Menu.spec
|
||||
|
||||
playerSpec :: Spec
|
||||
|
@ -578,6 +579,11 @@ modifyPlayerSpec = describe "modifyPlayer" $ mapM_
|
|||
, ( "Sam", 0, 0, 0 )
|
||||
]
|
||||
|
||||
playerSummarySpec :: Spec
|
||||
playerSummarySpec = describe "playerSummary" $
|
||||
it "should be \"Joe (2) center\"" $
|
||||
playerSummary joe `shouldBe` "Joe (2) center"
|
||||
|
||||
joe :: Player
|
||||
joe = newPlayer 2 "Joe" "center"
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user