make gameWon and gameTied return Maybe Bool instead of Bool

This commit is contained in:
Jonathan Lamothe 2019-08-30 00:43:09 -04:00
parent 45427a050e
commit 3fe2ff10f6
3 changed files with 27 additions and 29 deletions

View File

@ -30,6 +30,7 @@ module Mtlstats.Actions
, overtimeCheck , overtimeCheck
) where ) where
import Data.Maybe (fromMaybe)
import Lens.Micro (over, (^.), (&), (.~), (?~), (%~)) import Lens.Micro (over, (^.), (&), (.~), (?~), (%~))
import Mtlstats.Types import Mtlstats.Types
@ -63,10 +64,10 @@ removeChar = inputBuffer %~ \case
-- | Determines whether or not to perform a check for overtime -- | Determines whether or not to perform a check for overtime
overtimeCheck :: ProgState -> ProgState overtimeCheck :: ProgState -> ProgState
overtimeCheck s overtimeCheck s
| gameTied (s^.progMode.gameStateL) = | fromMaybe False $ gameTied $ s^.progMode.gameStateL =
s & progMode.gameStateL s & progMode.gameStateL
%~ (homeScore .~ Nothing) %~ (homeScore .~ Nothing)
. (awayScore .~ Nothing) . (awayScore .~ Nothing)
| gameWon (s^.progMode.gameStateL) = | fromMaybe False $ gameWon $ s^.progMode.gameStateL =
s & progMode.gameStateL.overtimeFlag ?~ False s & progMode.gameStateL.overtimeFlag ?~ False
| otherwise = s | otherwise = s

View File

@ -493,17 +493,13 @@ otherScore s = case s ^. gameType of
Just AwayGame -> s ^. homeScore Just AwayGame -> s ^. homeScore
Nothing -> Nothing Nothing -> Nothing
-- | Checks if the game was won (returns 'False' if unknown) -- | Checks if the game was won
gameWon :: GameState -> Bool gameWon :: GameState -> Maybe Bool
gameWon gs = case (,) <$> teamScore gs <*> otherScore gs of gameWon gs = (>) <$> teamScore gs <*> otherScore gs
Just (team, other) -> team > other
Nothing -> False
-- | Checks if the game has tied (retuns 'False' if unknown) -- | Checks if the game has tied
gameTied :: GameState -> Bool gameTied :: GameState -> Maybe Bool
gameTied gs = case (,) <$> gs^.homeScore <*> gs^.awayScore of gameTied gs = (==) <$> gs^.homeScore <*> gs^.awayScore
Just (home, away) -> home == away
Nothing -> False
-- | Calculates a player's points -- | Calculates a player's points
pPoints :: PlayerStats -> Int pPoints :: PlayerStats -> Int

View File

@ -259,20 +259,21 @@ gameWonSpec = describe "gameWon" $ mapM_
it ("should be " ++ show expected) $ it ("should be " ++ show expected) $
gameWon gs `shouldBe` expected) gameWon gs `shouldBe` expected)
-- gameType, homeScore, awayScore, expected -- gameType, homeScore, awayScore, expected
[ ( Just HomeGame, Just 1, Just 1, False ) [ ( Just HomeGame, Just 1, Just 1, Just False )
, ( Just HomeGame, Just 1, Just 2, False ) , ( Just HomeGame, Just 1, Just 2, Just False )
, ( Just HomeGame, Just 2, Just 1, True ) , ( Just HomeGame, Just 2, Just 1, Just True )
, ( Just AwayGame, Just 1, Just 1, False ) , ( Just AwayGame, Just 1, Just 1, Just False )
, ( Just AwayGame, Just 1, Just 2, True ) , ( Just AwayGame, Just 1, Just 2, Just True )
, ( Just AwayGame, Just 2, Just 1, False ) , ( Just AwayGame, Just 2, Just 1, Just False )
, ( Nothing, Just 1, Just 2, False ) , ( Nothing, Just 1, Just 2, Nothing )
, ( Just HomeGame, Nothing, Just 1, False ) , ( Just HomeGame, Nothing, Just 1, Nothing )
, ( Just AwayGame, Nothing, Just 1, False ) , ( Just AwayGame, Nothing, Just 1, Nothing )
, ( Just HomeGame, Just 1, Nothing, False ) , ( Just HomeGame, Just 1, Nothing, Nothing )
, ( Just AwayGame, Just 1, Nothing, False ) , ( Just AwayGame, Just 1, Nothing, Nothing )
, ( Nothing, Nothing, Nothing, False ) , ( Nothing, Nothing, Nothing, Nothing )
] ]
gameTiedSpec :: Spec
gameTiedSpec = describe "gameTied" $ mapM_ gameTiedSpec = describe "gameTied" $ mapM_
(\(home, away, expected) -> let (\(home, away, expected) -> let
desc = "home score: " ++ show home ++ desc = "home score: " ++ show home ++
@ -283,11 +284,11 @@ gameTiedSpec = describe "gameTied" $ mapM_
in context desc $ in context desc $
it ("should be " ++ show expected) $ it ("should be " ++ show expected) $
gameTied gs `shouldBe` expected) gameTied gs `shouldBe` expected)
[ ( Nothing, Nothing, False ) [ ( Nothing, Nothing, Nothing )
, ( Nothing, Just 1, False ) , ( Nothing, Just 1, Nothing )
, ( Just 1, Nothing, False ) , ( Just 1, Nothing, Nothing )
, ( Just 1, Just 1, True ) , ( Just 1, Just 1, Just True )
, ( Just 1, Just 2, False ) , ( Just 1, Just 2, Just False )
] ]
pPointsSpec :: Spec pPointsSpec :: Spec