added gmsGoalsFor and gmsGoalsAgainst fields to GameStats

This commit is contained in:
Jonathan Lamothe 2019-09-21 00:03:33 -04:00
parent 5332dc0d7f
commit 9f68d0da1d
2 changed files with 63 additions and 37 deletions

View File

@ -95,6 +95,8 @@ module Mtlstats.Types (
gmsWins, gmsWins,
gmsLosses, gmsLosses,
gmsOvertime, gmsOvertime,
gmsGoalsFor,
gmsGoalsAgainst,
-- * Constructors -- * Constructors
newProgState, newProgState,
newGameState, newGameState,
@ -405,12 +407,16 @@ instance ToJSON GoalieStats where
-- | Game statistics -- | Game statistics
data GameStats = GameStats data GameStats = GameStats
{ _gmsWins :: Int { _gmsWins :: Int
-- ^ Games won -- ^ Games won
, _gmsLosses :: Int , _gmsLosses :: Int
-- ^ Games lost -- ^ Games lost
, _gmsOvertime :: Int , _gmsOvertime :: Int
-- ^ Games lost in overtime -- ^ Games lost in overtime
, _gmsGoalsFor :: Int
-- ^ Goals for the team
, _gmsGoalsAgainst :: Int
-- ^ Goals against the team
} deriving (Eq, Show) } deriving (Eq, Show)
instance FromJSON GameStats where instance FromJSON GameStats where
@ -418,17 +424,23 @@ instance FromJSON GameStats where
<$> v .: "wins" <$> v .: "wins"
<*> v .: "losses" <*> v .: "losses"
<*> v .: "overtime" <*> v .: "overtime"
<*> v .: "goals_for"
<*> v .: "goals_against"
instance ToJSON GameStats where instance ToJSON GameStats where
toJSON (GameStats w l ot) = object toJSON (GameStats w l ot gf ga) = object
[ "wins" .= w [ "wins" .= w
, "losses" .= l , "losses" .= l
, "overtime" .= ot , "overtime" .= ot
, "goals_for" .= gf
, "goals_against" .= ga
] ]
toEncoding (GameStats w l ot) = pairs $ toEncoding (GameStats w l ot gf ga) = pairs $
"wins" .= w <> "wins" .= w <>
"losses" .= l <> "losses" .= l <>
"overtime" .= ot "overtime" .= ot <>
"goals_for" .= gf <>
"goals_against" .= ga
-- | Defines a user prompt -- | Defines a user prompt
data Prompt = Prompt data Prompt = Prompt
@ -563,9 +575,11 @@ newGoalieStats = GoalieStats
-- | Constructor for a 'GameStats' value -- | Constructor for a 'GameStats' value
newGameStats :: GameStats newGameStats :: GameStats
newGameStats = GameStats newGameStats = GameStats
{ _gmsWins = 0 { _gmsWins = 0
, _gmsLosses = 0 , _gmsLosses = 0
, _gmsOvertime = 0 , _gmsOvertime = 0
, _gmsGoalsFor = 0
, _gmsGoalsAgainst = 0
} }
-- | Determines the team's score -- | Determines the team's score
@ -632,9 +646,11 @@ gmsPoints gs = 2 * gs^.gmsWins + gs^. gmsOvertime
-- | Adds two 'GameStats' values together -- | Adds two 'GameStats' values together
addGameStats :: GameStats -> GameStats -> GameStats addGameStats :: GameStats -> GameStats -> GameStats
addGameStats s1 s2 = GameStats addGameStats s1 s2 = GameStats
{ _gmsWins = s1^.gmsWins + s2^.gmsWins { _gmsWins = s1^.gmsWins + s2^.gmsWins
, _gmsLosses = s1^.gmsLosses + s2^.gmsLosses , _gmsLosses = s1^.gmsLosses + s2^.gmsLosses
, _gmsOvertime = s1^.gmsOvertime + s2^.gmsOvertime , _gmsOvertime = s1^.gmsOvertime + s2^.gmsOvertime
, _gmsGoalsFor = s1^.gmsGoalsFor + s2^.gmsGoalsFor
, _gmsGoalsAgainst = s1^.gmsGoalsAgainst + s2^.gmsGoalsAgainst
} }
-- | Calculates a player's points -- | Calculates a player's points

View File

@ -255,16 +255,20 @@ goalieStatsJSON n = Object $ HM.fromList
gameStats :: Int -> GameStats gameStats :: Int -> GameStats
gameStats n = GameStats gameStats n = GameStats
{ _gmsWins = n { _gmsWins = n
, _gmsLosses = n + 1 , _gmsLosses = n + 1
, _gmsOvertime = n + 2 , _gmsOvertime = n + 2
, _gmsGoalsFor = n + 3
, _gmsGoalsAgainst = n + 4
} }
gameStatsJSON :: Int -> Value gameStatsJSON :: Int -> Value
gameStatsJSON n = Object $ HM.fromList gameStatsJSON n = Object $ HM.fromList
[ ( "wins", toJSON n ) [ ( "wins", toJSON n )
, ( "losses", toJSON $ n + 1 ) , ( "losses", toJSON $ n + 1 )
, ( "overtime", toJSON $ n + 2 ) , ( "overtime", toJSON $ n + 2 )
, ( "goals_for", toJSON $ n + 3 )
, ( "goals_against", toJSON $ n + 4 )
] ]
db :: Database db :: Database
@ -456,11 +460,11 @@ gmsGamesSpec = describe "gmsGames" $ mapM_
gmsPointsSpec :: Spec gmsPointsSpec :: Spec
gmsPointsSpec = describe "gmsPoints" $ mapM_ gmsPointsSpec = describe "gmsPoints" $ mapM_
(\(w, l, ot, expected) -> let (\(w, l, ot, expected) -> let
gs = GameStats gs
{ _gmsWins = w = newGameStats
, _gmsLosses = l & gmsWins .~ w
, _gmsOvertime = ot & gmsLosses .~ l
} & gmsOvertime .~ ot
in context (show gs) $ in context (show gs) $
it ("should be " ++ show expected) $ it ("should be " ++ show expected) $
gmsPoints gs `shouldBe` expected) gmsPoints gs `shouldBe` expected)
@ -478,21 +482,27 @@ addGameStatsSpec = describe "addGameStats" $
it "should add the values" $ let it "should add the values" $ let
s1 = GameStats s1 = GameStats
{ _gmsWins = 1 { _gmsWins = 1
, _gmsLosses = 3 , _gmsLosses = 2
, _gmsOvertime = 2 , _gmsOvertime = 3
, _gmsGoalsFor = 4
, _gmsGoalsAgainst = 5
} }
s2 = GameStats s2 = GameStats
{ _gmsWins = 4 { _gmsWins = 6
, _gmsLosses = 6 , _gmsLosses = 7
, _gmsOvertime = 5 , _gmsOvertime = 8
, _gmsGoalsFor = 9
, _gmsGoalsAgainst = 10
} }
expected = GameStats expected = GameStats
{ _gmsWins = 5 { _gmsWins = 7
, _gmsLosses = 9 , _gmsLosses = 9
, _gmsOvertime = 7 , _gmsOvertime = 11
, _gmsGoalsFor = 13
, _gmsGoalsAgainst = 15
} }
in addGameStats s1 s2 `shouldBe` expected in addGameStats s1 s2 `shouldBe` expected