From 3fe2ff10f66839c5ecd9ef67300e5e80560c62e3 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Fri, 30 Aug 2019 00:43:09 -0400 Subject: [PATCH] make gameWon and gameTied return Maybe Bool instead of Bool --- src/Mtlstats/Actions.hs | 5 +++-- src/Mtlstats/Types.hs | 16 ++++++---------- test/TypesSpec.hs | 35 ++++++++++++++++++----------------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/Mtlstats/Actions.hs b/src/Mtlstats/Actions.hs index 8b895d3..482db98 100644 --- a/src/Mtlstats/Actions.hs +++ b/src/Mtlstats/Actions.hs @@ -30,6 +30,7 @@ module Mtlstats.Actions , overtimeCheck ) where +import Data.Maybe (fromMaybe) import Lens.Micro (over, (^.), (&), (.~), (?~), (%~)) import Mtlstats.Types @@ -63,10 +64,10 @@ removeChar = inputBuffer %~ \case -- | Determines whether or not to perform a check for overtime overtimeCheck :: ProgState -> ProgState overtimeCheck s - | gameTied (s^.progMode.gameStateL) = + | fromMaybe False $ gameTied $ s^.progMode.gameStateL = s & progMode.gameStateL %~ (homeScore .~ Nothing) . (awayScore .~ Nothing) - | gameWon (s^.progMode.gameStateL) = + | fromMaybe False $ gameWon $ s^.progMode.gameStateL = s & progMode.gameStateL.overtimeFlag ?~ False | otherwise = s diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 5b504d4..49c05fc 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -493,17 +493,13 @@ otherScore s = case s ^. gameType of Just AwayGame -> s ^. homeScore Nothing -> Nothing --- | Checks if the game was won (returns 'False' if unknown) -gameWon :: GameState -> Bool -gameWon gs = case (,) <$> teamScore gs <*> otherScore gs of - Just (team, other) -> team > other - Nothing -> False +-- | Checks if the game was won +gameWon :: GameState -> Maybe Bool +gameWon gs = (>) <$> teamScore gs <*> otherScore gs --- | Checks if the game has tied (retuns 'False' if unknown) -gameTied :: GameState -> Bool -gameTied gs = case (,) <$> gs^.homeScore <*> gs^.awayScore of - Just (home, away) -> home == away - Nothing -> False +-- | Checks if the game has tied +gameTied :: GameState -> Maybe Bool +gameTied gs = (==) <$> gs^.homeScore <*> gs^.awayScore -- | Calculates a player's points pPoints :: PlayerStats -> Int diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index b37d4f4..1af9999 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -259,20 +259,21 @@ gameWonSpec = describe "gameWon" $ mapM_ it ("should be " ++ show expected) $ gameWon gs `shouldBe` expected) -- gameType, homeScore, awayScore, expected - [ ( Just HomeGame, Just 1, Just 1, False ) - , ( Just HomeGame, Just 1, Just 2, False ) - , ( Just HomeGame, Just 2, Just 1, True ) - , ( Just AwayGame, Just 1, Just 1, False ) - , ( Just AwayGame, Just 1, Just 2, True ) - , ( Just AwayGame, Just 2, Just 1, False ) - , ( Nothing, Just 1, Just 2, False ) - , ( Just HomeGame, Nothing, Just 1, False ) - , ( Just AwayGame, Nothing, Just 1, False ) - , ( Just HomeGame, Just 1, Nothing, False ) - , ( Just AwayGame, Just 1, Nothing, False ) - , ( Nothing, Nothing, Nothing, False ) + [ ( Just HomeGame, Just 1, Just 1, Just False ) + , ( Just HomeGame, Just 1, Just 2, Just False ) + , ( Just HomeGame, Just 2, Just 1, Just True ) + , ( Just AwayGame, Just 1, Just 1, Just False ) + , ( Just AwayGame, Just 1, Just 2, Just True ) + , ( Just AwayGame, Just 2, Just 1, Just False ) + , ( Nothing, Just 1, Just 2, Nothing ) + , ( Just HomeGame, Nothing, Just 1, Nothing ) + , ( Just AwayGame, Nothing, Just 1, Nothing ) + , ( Just HomeGame, Just 1, Nothing, Nothing ) + , ( Just AwayGame, Just 1, Nothing, Nothing ) + , ( Nothing, Nothing, Nothing, Nothing ) ] +gameTiedSpec :: Spec gameTiedSpec = describe "gameTied" $ mapM_ (\(home, away, expected) -> let desc = "home score: " ++ show home ++ @@ -283,11 +284,11 @@ gameTiedSpec = describe "gameTied" $ mapM_ in context desc $ it ("should be " ++ show expected) $ gameTied gs `shouldBe` expected) - [ ( Nothing, Nothing, False ) - , ( Nothing, Just 1, False ) - , ( Just 1, Nothing, False ) - , ( Just 1, Just 1, True ) - , ( Just 1, Just 2, False ) + [ ( Nothing, Nothing, Nothing ) + , ( Nothing, Just 1, Nothing ) + , ( Just 1, Nothing, Nothing ) + , ( Just 1, Just 1, Just True ) + , ( Just 1, Just 2, Just False ) ] pPointsSpec :: Spec