diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 4561cfc..6a1f97e 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -93,6 +93,7 @@ module Mtlstats.Types ( -- ** GameState Helpers teamScore, otherScore, + gameWon, gameTied, -- ** Player Helpers pPoints @@ -488,6 +489,12 @@ 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 has tied (retuns 'False' if unknown) gameTied :: GameState -> Bool gameTied gs = case (,) <$> gs^.homeScore <*> gs^.awayScore of diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index 9ef0a8f..b37d4f4 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -43,6 +43,7 @@ spec = describe "Mtlstats.Types" $ do gameStateLSpec teamScoreSpec otherScoreSpec + gameWonSpec gameTiedSpec pPointsSpec Menu.spec @@ -244,6 +245,34 @@ dbJSON = Object $ HM.fromList , ( "away_game_stats", gameStatsJSON 2 ) ] +gameWonSpec :: Spec +gameWonSpec = describe "gameWon" $ mapM_ + (\(t, h, a, expected) -> let + desc = "game type: " ++ show t ++ + ", home score: " ++ show h ++ + ", away score: " ++ show a + gs = newGameState + & gameType .~ t + & homeScore .~ h + & awayScore .~ a + in context desc $ + 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 ) + ] + gameTiedSpec = describe "gameTied" $ mapM_ (\(home, away, expected) -> let desc = "home score: " ++ show home ++