update goals for and goals against when updating game stats

This commit is contained in:
Jonathan Lamothe 2019-09-21 00:31:40 -04:00
parent 9f68d0da1d
commit 06c94260ad
2 changed files with 47 additions and 30 deletions

View File

@ -82,26 +82,37 @@ overtimeCheck s
-- | Adjusts the game stats based on the results of the current game -- | Adjusts the game stats based on the results of the current game
updateGameStats :: ProgState -> ProgState updateGameStats :: ProgState -> ProgState
updateGameStats s = fromMaybe s $ do updateGameStats s = fromMaybe s $ do
gType <- s^.progMode.gameStateL.gameType let gs = s^.progMode.gameStateL
won <- gameWon $ s^.progMode.gameStateL gType <- gs^.gameType
lost <- gameLost $ s^.progMode.gameStateL won <- gameWon gs
ot <- s^.progMode.gameStateL.overtimeFlag lost <- gameLost gs
ot <- gs^.overtimeFlag
tScore <- teamScore gs
oScore <- otherScore gs
let let
hw = if gType == HomeGame && won then 1 else 0 hw = if gType == HomeGame && won then 1 else 0
hl = if gType == HomeGame && lost then 1 else 0 hl = if gType == HomeGame && lost then 1 else 0
hot = if gType == HomeGame && ot then 1 else 0 hot = if gType == HomeGame && ot then 1 else 0
hgf = if gType == HomeGame then tScore else 0
hga = if gType == HomeGame then oScore else 0
aw = if gType == AwayGame && won then 1 else 0 aw = if gType == AwayGame && won then 1 else 0
al = if gType == AwayGame && lost then 1 else 0 al = if gType == AwayGame && lost then 1 else 0
aot = if gType == AwayGame && ot then 1 else 0 aot = if gType == AwayGame && ot then 1 else 0
agf = if gType == AwayGame then tScore else 0
aga = if gType == AwayGame then oScore else 0
Just $ s Just $ s
& database.dbHomeGameStats & database.dbHomeGameStats
%~ (gmsWins +~ hw) %~ (gmsWins +~ hw)
. (gmsLosses +~ hl) . (gmsLosses +~ hl)
. (gmsOvertime +~ hot) . (gmsOvertime +~ hot)
. (gmsGoalsFor +~ hgf)
. (gmsGoalsAgainst +~ hga)
& database.dbAwayGameStats & database.dbAwayGameStats
%~ (gmsWins +~ aw) %~ (gmsWins +~ aw)
. (gmsLosses +~ al) . (gmsLosses +~ al)
. (gmsOvertime +~ aot) . (gmsOvertime +~ aot)
. (gmsGoalsFor +~ agf)
. (gmsGoalsAgainst +~ aga)
-- | Validates the game date -- | Validates the game date
validateGameDate :: ProgState -> ProgState validateGameDate :: ProgState -> ProgState

View File

@ -196,6 +196,8 @@ updateGameStatsSpec = describe "updateGameStats" $ do
& gmsWins .~ 1 & gmsWins .~ 1
& gmsLosses .~ 1 & gmsLosses .~ 1
& gmsOvertime .~ 1 & gmsOvertime .~ 1
& gmsGoalsFor .~ 1
& gmsGoalsAgainst .~ 1
s t h a o = newProgState s t h a o = newProgState
& progMode.gameStateL & progMode.gameStateL
@ -207,75 +209,79 @@ updateGameStatsSpec = describe "updateGameStats" $ do
%~ (dbHomeGameStats .~ baseStats) %~ (dbHomeGameStats .~ baseStats)
. (dbAwayGameStats .~ baseStats) . (dbAwayGameStats .~ baseStats)
db hw hl ho aw al ao = newDatabase db hw hl ho hf ha aw al ao af aa = newDatabase
& dbHomeGameStats & dbHomeGameStats
%~ (gmsWins .~ hw) %~ (gmsWins .~ hw)
. (gmsLosses .~ hl) . (gmsLosses .~ hl)
. (gmsOvertime .~ ho) . (gmsOvertime .~ ho)
. (gmsGoalsFor .~ hf)
. (gmsGoalsAgainst .~ ha)
& dbAwayGameStats & dbAwayGameStats
%~ (gmsWins .~ aw) %~ (gmsWins .~ aw)
. (gmsLosses .~ al) . (gmsLosses .~ al)
. (gmsOvertime .~ ao) . (gmsOvertime .~ ao)
. (gmsGoalsFor .~ af)
. (gmsGoalsAgainst .~ aa)
context "home win" $ context "home win" $
it "should record a home win" $ let it "should record a home win" $ let
s' = s (Just HomeGame) (Just 2) (Just 1) (Just False) s' = s (Just HomeGame) (Just 2) (Just 1) (Just False)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 2 1 1 1 1 1 in db' `shouldBe` db 2 1 1 3 2 1 1 1 1 1
context "home loss" $ context "home loss" $
it "should record a home loss" $ let it "should record a home loss" $ let
s' = s (Just HomeGame) (Just 1) (Just 2) (Just False) s' = s (Just HomeGame) (Just 1) (Just 2) (Just False)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 2 1 1 1 1 in db' `shouldBe` db 1 2 1 2 3 1 1 1 1 1
context "home overtime loss" $ context "home overtime loss" $
it "should record a home overtime" $ let it "should record a home overtime" $ let
s' = s (Just HomeGame) (Just 1) (Just 2) (Just True) s' = s (Just HomeGame) (Just 1) (Just 2) (Just True)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 2 1 1 1 in db' `shouldBe` db 1 1 2 2 3 1 1 1 1 1
context "away win" $ context "away win" $
it "should record an away win" $ let it "should record an away win" $ let
s' = s (Just AwayGame) (Just 1) (Just 2) (Just False) s' = s (Just AwayGame) (Just 1) (Just 2) (Just False)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 1 2 1 1 in db' `shouldBe` db 1 1 1 1 1 2 1 1 3 2
context "away loss" $ context "away loss" $
it "should record an away loss" $ let it "should record an away loss" $ let
s' = s (Just AwayGame) (Just 2) (Just 1) (Just False) s' = s (Just AwayGame) (Just 2) (Just 1) (Just False)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 1 1 2 1 in db' `shouldBe` db 1 1 1 1 1 1 2 1 2 3
context "away overtime loss" $ context "away overtime loss" $
it "should record an away overtime" $ let it "should record an away overtime" $ let
s' = s (Just AwayGame) (Just 2) (Just 1) (Just True) s' = s (Just AwayGame) (Just 2) (Just 1) (Just True)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 1 1 1 2 in db' `shouldBe` db 1 1 1 1 1 1 1 2 2 3
context "missing game type" $ context "missing game type" $
it "should not change anything" $ let it "should not change anything" $ let
s' = s Nothing (Just 1) (Just 2) (Just True) s' = s Nothing (Just 1) (Just 2) (Just True)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 1 1 1 1 in db' `shouldBe` db 1 1 1 1 1 1 1 1 1 1
context "missing home score" $ context "missing home score" $
it "should not change anything" $ let it "should not change anything" $ let
s' = s (Just HomeGame) Nothing (Just 1) (Just True) s' = s (Just HomeGame) Nothing (Just 1) (Just True)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 1 1 1 1 in db' `shouldBe` db 1 1 1 1 1 1 1 1 1 1
context "missing away score" $ context "missing away score" $
it "should not change anything" $ let it "should not change anything" $ let
s' = s (Just HomeGame) (Just 1) Nothing (Just True) s' = s (Just HomeGame) (Just 1) Nothing (Just True)
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 1 1 1 1 in db' `shouldBe` db 1 1 1 1 1 1 1 1 1 1
context "missing overtime flag" $ context "missing overtime flag" $
it "should not change anything" $ let it "should not change anything" $ let
s' = s (Just HomeGame) (Just 1) (Just 2) Nothing s' = s (Just HomeGame) (Just 1) (Just 2) Nothing
db' = updateGameStats s' ^. database db' = updateGameStats s' ^. database
in db' `shouldBe` db 1 1 1 1 1 1 in db' `shouldBe` db 1 1 1 1 1 1 1 1 1 1
validateGameDateSpec :: Spec validateGameDateSpec :: Spec
validateGameDateSpec = describe "validateGameDate" $ do validateGameDateSpec = describe "validateGameDate" $ do