diff --git a/ChangeLog.md b/ChangeLog.md index efb1001..6f2c900 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -5,6 +5,7 @@ - Allow lower case player names - Don't show players without points in game report - Removed unnecessary goalie statistics from game report +- Fixed goalie average calculation ## 0.7.0 - Shortened views to fit within 25 lines diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 6997628..c0162e6 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -1010,4 +1010,10 @@ addGoalieStats g1 g2 = GoalieStats -- | Determines a goalie's average goals allowed per game. gsAverage :: GoalieStats -> Rational -gsAverage gs = fromIntegral (gs^.gsGoalsAllowed) / fromIntegral (gs^.gsGames) +gsAverage gs = let + allowed = fromIntegral $ gs^.gsGoalsAllowed + mins = fromIntegral $ gs^.gsMinsPlayed + gLen = fromIntegral gameLength + in if mins == 0 + then 0 + else allowed / mins * gLen diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index 1744669..4237fd8 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -813,15 +813,20 @@ addGoalieStatsSpec = describe "addGoalieStats" $ let actual `shouldBe` expected gsAverageSpec :: Spec -gsAverageSpec = describe "gsAverage" $ let - gs = newGoalieStats - & gsGames .~ 2 - & gsGoalsAllowed .~ 3 +gsAverageSpec = describe "gsAverage" $ mapM_ + (\(label, stats, expected) -> context label $ + it ("should be " ++ show expected) $ + gsAverage stats `shouldBe` expected) - expected = 3 % 2 + -- label, stats, expected + [ ( "with minutes", gs, 3 % 2 ) + , ( "no minutes", newGoalieStats , 0 ) + ] - in it ("should be " ++ show expected) $ - gsAverage gs `shouldBe` expected + where + gs = newGoalieStats + & gsMinsPlayed .~ 2 * gameLength + & gsGoalsAllowed .~ 3 joe :: Player joe = newPlayer 2 "Joe" "center"