implemented GameStats and newGameStats

This commit is contained in:
Jonathan Lamothe 2019-08-26 10:20:10 -04:00
parent 954490fc6d
commit 3e4a9dd525
2 changed files with 61 additions and 0 deletions

View File

@ -33,6 +33,7 @@ module Mtlstats.Types (
PlayerStats (..), PlayerStats (..),
Goalie (..), Goalie (..),
GoalieStats (..), GoalieStats (..),
GameStats (..),
Prompt (..), Prompt (..),
-- * Lenses -- * Lenses
-- ** ProgState Lenses -- ** ProgState Lenses
@ -76,6 +77,10 @@ module Mtlstats.Types (
gsWins, gsWins,
gsLosses, gsLosses,
gsTies, gsTies,
-- ** GameStats Lenses
gmsWins,
gmsLosses,
gmsOvertime,
-- * Constructors -- * Constructors
newProgState, newProgState,
newGameState, newGameState,
@ -84,6 +89,7 @@ module Mtlstats.Types (
newPlayerStats, newPlayerStats,
newGoalie, newGoalie,
newGoalieStats, newGoalieStats,
newGameStats,
-- * Helper Functions -- * Helper Functions
-- ** ProgState Helpers -- ** ProgState Helpers
teamScore, teamScore,
@ -316,6 +322,33 @@ instance ToJSON GoalieStats where
"losses" .= l <> "losses" .= l <>
"ties" .= t "ties" .= t
-- | Game statistics
data GameStats = GameStats
{ _gmsWins :: Int
-- ^ Games won
, _gmsLosses :: Int
-- ^ Games lost
, _gmsOvertime :: Int
-- ^ Games lost in overtime
} deriving (Eq, Show)
instance FromJSON GameStats where
parseJSON = withObject "GameStats" $ \v -> GameStats
<$> v .: "wins"
<*> v .: "losses"
<*> v .: "overtime"
instance ToJSON GameStats where
toJSON (GameStats w l ot) = object
[ "wins" .= w
, "losses" .= l
, "overtime" .= ot
]
toEncoding (GameStats w l ot) = pairs $
"wins" .= w <>
"losses" .= l <>
"overtime" .= ot
-- | Defines a user prompt -- | Defines a user prompt
data Prompt = Prompt data Prompt = Prompt
{ promptDrawer :: ProgState -> Update () { promptDrawer :: ProgState -> Update ()
@ -335,6 +368,7 @@ makeLenses ''Player
makeLenses ''PlayerStats makeLenses ''PlayerStats
makeLenses ''Goalie makeLenses ''Goalie
makeLenses ''GoalieStats makeLenses ''GoalieStats
makeLenses ''GameStats
gameTypeL :: Lens' ProgMode (Maybe GameType) gameTypeL :: Lens' ProgMode (Maybe GameType)
gameTypeL = lens gameTypeL = lens
@ -448,6 +482,14 @@ newGoalieStats = GoalieStats
, _gsTies = 0 , _gsTies = 0
} }
-- | Constructor for a 'GameStats' value
newGameStats :: GameStats
newGameStats = GameStats
{ _gmsWins = 0
, _gmsLosses = 0
, _gmsOvertime = 0
}
-- | Determines the team's points -- | Determines the team's points
teamScore :: ProgState -> Maybe Int teamScore :: ProgState -> Maybe Int
teamScore s = case s ^. progMode . gameTypeL of teamScore s = case s ^. progMode . gameTypeL of

View File

@ -38,6 +38,7 @@ spec :: Spec
spec = describe "Mtlstats.Types" $ do spec = describe "Mtlstats.Types" $ do
playerSpec playerSpec
goalieSpec goalieSpec
gameStatsSpec
databaseSpec databaseSpec
pPointsSpec pPointsSpec
gameTypeLSpec gameTypeLSpec
@ -53,6 +54,10 @@ playerSpec = describe "Player" $ jsonSpec player playerJSON
goalieSpec :: Spec goalieSpec :: Spec
goalieSpec = describe "Goalie" $ jsonSpec goalie goalieJSON goalieSpec = describe "Goalie" $ jsonSpec goalie goalieJSON
gameStatsSpec :: Spec
gameStatsSpec = describe "GameStats" $
jsonSpec (gameStats 1) (gameStatsJSON 1)
databaseSpec :: Spec databaseSpec :: Spec
databaseSpec = describe "Database" $ jsonSpec db dbJSON databaseSpec = describe "Database" $ jsonSpec db dbJSON
@ -291,6 +296,20 @@ goalieStatsJSON n = Object $ HM.fromList
, ( "ties", toJSON $ n + 6 ) , ( "ties", toJSON $ n + 6 )
] ]
gameStats :: Int -> GameStats
gameStats n = GameStats
{ _gmsWins = n
, _gmsLosses = n + 1
, _gmsOvertime = n + 2
}
gameStatsJSON :: Int -> Value
gameStatsJSON n = Object $ HM.fromList
[ ( "wins", toJSON n )
, ( "losses", toJSON $ n + 1 )
, ( "overtime", toJSON $ n + 2 )
]
db :: Database db :: Database
db = newDatabase db = newDatabase
& dbPlayers .~ [player] & dbPlayers .~ [player]