diff --git a/src/Mtlstats/Actions.hs b/src/Mtlstats/Actions.hs index 23acce7..8060e8c 100644 --- a/src/Mtlstats/Actions.hs +++ b/src/Mtlstats/Actions.hs @@ -39,6 +39,7 @@ module Mtlstats.Actions ) where import Control.Monad.Trans.State (modify) +import qualified Data.Map as M import Data.Maybe (fromMaybe) import Data.Time.Calendar (fromGregorianValid) import Lens.Micro (over, (^.), (&), (.~), (?~), (%~), (+~)) @@ -174,13 +175,16 @@ awardGoal -> ProgState -> ProgState awardGoal n ps = ps - & database.dbPlayers - %~ map - (\(i, p) -> if i == n - then p - & pYtd.psGoals %~ succ - & pLifetime.psGoals %~ succ - else p) . zip [0..] + & progMode.gameStateL.gamePlayerStats %~ + (\m -> let + stats = M.findWithDefault newPlayerStats n m + in M.insert n (stats & psGoals %~ succ) m) + & database.dbPlayers %~ map + (\(i, p) -> if i == n + then p + & pYtd.psGoals %~ succ + & pLifetime.psGoals %~ succ + else p) . zip [0..] -- | Awards an assist to a player awardAssist diff --git a/test/ActionsSpec.hs b/test/ActionsSpec.hs index 2818774..10e9460 100644 --- a/test/ActionsSpec.hs +++ b/test/ActionsSpec.hs @@ -22,6 +22,7 @@ along with this program. If not, see . module ActionsSpec (spec) where import Control.Monad (replicateM) +import qualified Data.Map as M import Lens.Micro ((^.), (&), (.~), (?~), (%~)) import System.Random (randomRIO) import Test.Hspec (Spec, context, describe, it, runIO, shouldBe, shouldNotBe) @@ -426,31 +427,34 @@ awardGoalSpec = describe "awardGoal" $ do db = newDatabase & dbPlayers .~ [joe, bob] + joeStats + = newPlayerStats + & psGoals .~ 1 ps = newProgState + & progMode.gameStateL.gamePlayerStats .~ M.singleton 0 joeStats & database .~ db - context "Joe" $ do - let - ps' = awardGoal 0 ps - player = head $ ps'^.database.dbPlayers + mapM_ + (\(pName, pid, ytd, lt, game) -> + context pName $ do + let + ps' = awardGoal pid ps + player = (ps'^.database.dbPlayers) !! pid + gStats = (ps'^.progMode.gameStateL.gamePlayerStats) M.! pid - it "should increment Joe's year-to-date goals" $ - player^.pYtd.psGoals `shouldBe` 2 + it ("should increment " ++ pName ++ "'s year-to-date goals") $ + player^.pYtd.psGoals `shouldBe` ytd - it "should increment Joe's lifetime goals" $ - player^.pLifetime.psGoals `shouldBe` 3 + it ("should increment " ++ pName ++ "'s lifetime goals") $ + player^.pLifetime.psGoals `shouldBe` lt - context "Bob" $ do - let - ps' = awardGoal 1 ps - player = last $ ps'^.database.dbPlayers - - it "should increment Bob's year-to-data goals" $ - player^.pYtd.psGoals `shouldBe` 4 - - it "should increment Bob's lifetime goals" $ - player^.pLifetime.psGoals `shouldBe` 5 + it ("should increment " ++ pName ++ "'s game goals") $ + gStats^.psGoals `shouldBe` game) + -- player name, player id, ytd goals, lifetime goals, game goals + [ ( "Joe", 0, 2, 3, 2 ) + , ( "Bob", 1, 4, 5, 1 ) + ] context "invalid index" $ let ps' = awardGoal 2 ps