From 669c854f4ff9ac3f8d46dcfaba35ab3b2f84c6bc Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Fri, 27 Sep 2019 01:39:50 -0400 Subject: [PATCH] implemented awardGoalAssists --- src/Mtlstats/Actions.hs | 16 +++++++++++++- test/ActionsSpec.hs | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Mtlstats/Actions.hs b/src/Mtlstats/Actions.hs index a186dbf..b12d587 100644 --- a/src/Mtlstats/Actions.hs +++ b/src/Mtlstats/Actions.hs @@ -153,7 +153,21 @@ addPlayer s = fromMaybe s $ do -- | Awards the goal and assists to the players recordGoalAssists :: ProgState -> ProgState -recordGoalAssists = undefined +recordGoalAssists ps = fromMaybe ps $ do + let + gs = ps^.progMode.gameStateL + players = ps^.database.dbPlayers + (goalId, _) <- playerSearchExact (gs^.goalBy) players + assistIds <- mapM + (\name -> fst <$> playerSearchExact name players) + (gs^.assistsBy) + Just $ ps + & awardGoal goalId + & (\s -> foldr awardAssist s assistIds) + & progMode.gameStateL + %~ (goalBy .~ "") + . (assistsBy .~ []) + . (pointsAccounted %~ succ) -- | Awards a goal to a player awardGoal diff --git a/test/ActionsSpec.hs b/test/ActionsSpec.hs index 88707ee..9cc64ad 100644 --- a/test/ActionsSpec.hs +++ b/test/ActionsSpec.hs @@ -41,6 +41,7 @@ spec = describe "Mtlstats.Actions" $ do validateGameDateSpec createPlayerSpec addPlayerSpec + recordGoalAssistsSpec awardGoalSpec awardAssistSpec @@ -359,6 +360,53 @@ addPlayerSpec = describe "addPlayer" $ do s' = addPlayer $ s MainMenu in s'^.database.dbPlayers `shouldBe` [p2] +recordGoalAssistsSpec :: Spec +recordGoalAssistsSpec = describe "recordGoalAssists" $ do + let + joe = newPlayer 1 "Joe" "centre" + bob = newPlayer 2 "Bob" "defense" + steve = newPlayer 3 "Steve" "forward" + dave = newPlayer 4 "Dave" "somewhere" + ps + = newProgState + & database.dbPlayers .~ [joe, bob, steve, dave] + & progMode.gameStateL + %~ (goalBy .~ "Joe") + . (assistsBy .~ ["Bob", "Steve"]) + & recordGoalAssists + + mapM_ + (\(name, n, ytdg, ltg, ytda, lta) -> context name $ do + let player = (ps^.database.dbPlayers) !! n + + it ("should set the year-to-date goals to " ++ show ytdg) $ + player^.pYtd.psGoals `shouldBe` ytdg + + it ("should set the lifetime goals to " ++ show ltg) $ + player^.pLifetime.psGoals `shouldBe` ltg + + it ("should set the year-to-date assists to " ++ show ytda) $ + player^.pYtd.psAssists `shouldBe` ytda + + it ("should set the lifetime assists to " ++ show lta) $ + player^.pLifetime.psAssists `shouldBe` lta) + + -- name, index, ytd goals, lt goals, ytd assists, lt assists + [ ( "Joe", 0, 1, 1, 0, 0 ) + , ( "Bob", 1, 0, 0, 1, 1 ) + , ( "Steve", 2, 0, 0, 1, 1 ) + , ( "Dave", 3, 0, 0, 0, 0 ) + ] + + it "should clear the goalBy value" $ + ps^.progMode.gameStateL.goalBy `shouldBe` "" + + it "should clear the assistsBy list" $ + ps^.progMode.gameStateL.assistsBy `shouldBe` [] + + it "should increment the pointsAccounted counter" $ + ps^.progMode.gameStateL.pointsAccounted `shouldBe` 1 + awardGoalSpec :: Spec awardGoalSpec = describe "awardGoal" $ do let