implemented awardAssist

This commit is contained in:
Jonathan Lamothe 2019-09-26 02:07:55 -04:00
parent c7c267b2a1
commit 11fcbfcbdd
2 changed files with 73 additions and 0 deletions

View File

@ -34,6 +34,7 @@ module Mtlstats.Actions
, addPlayer
, recordGoalAssists
, awardGoal
, awardAssist
) where
import Control.Monad.Trans.State (modify)
@ -168,3 +169,18 @@ awardGoal n ps = ps
& pYtd.psGoals %~ succ
& pLifetime.psGoals %~ succ
else p) . zip [0..]
-- | Awards an assist to a player
awardAssist
:: Int
-- ^ The player's index number
-> ProgState
-> ProgState
awardAssist n ps = ps
& database.dbPlayers
%~ map
(\(i, p) -> if i == n
then p
& pYtd.psAssists %~ succ
& pLifetime.psAssists %~ succ
else p) . zip [0..]

View File

@ -42,6 +42,7 @@ spec = describe "Mtlstats.Actions" $ do
createPlayerSpec
addPlayerSpec
awardGoalSpec
awardAssistSpec
startNewSeasonSpec :: Spec
startNewSeasonSpec = describe "startNewSeason" $ do
@ -408,6 +409,62 @@ awardGoalSpec = describe "awardGoal" $ do
in it "should not change the database" $
ps'^.database `shouldBe` db
awardAssistSpec :: Spec
awardAssistSpec = describe "awardAssist" $ do
let
joe
= newPlayer 1 "Joe" "centre"
& pYtd.psAssists .~ 1
& pLifetime.psAssists .~ 2
bob
= newPlayer 2 "Bob" "defense"
& pYtd.psAssists .~ 3
& pLifetime.psAssists .~ 4
ps
= newProgState
& database.dbPlayers .~ [joe, bob]
context "Joe" $ do
let
ps' = awardAssist 0 ps
joe' = head $ ps'^.database.dbPlayers
bob' = last $ ps'^.database.dbPlayers
it "should increment Joe's year-to-date assists" $
joe'^.pYtd.psAssists `shouldBe` 2
it "should increment Joe's lifetime assists" $
joe'^.pLifetime.psAssists `shouldBe` 3
it "should leave Bob's year-to-date assists alone" $
bob'^.pYtd.psAssists `shouldBe` 3
it "should leave Bob's lifetime assists alone" $
bob^.pLifetime.psAssists `shouldBe` 4
context "Bob" $ do
let
ps' = awardAssist 1 ps
joe' = head $ ps'^.database.dbPlayers
bob' = last $ ps'^.database.dbPlayers
it "should leave Joe's year-to-date assists alone" $
joe'^.pYtd.psAssists `shouldBe` 1
it "should leave Joe's lifetime assists alone" $
joe'^.pLifetime.psAssists `shouldBe` 2
it "should increment Bob's year-to-date assists" $
bob'^.pYtd.psAssists `shouldBe` 4
it "should increment Bob's lifetime assists" $
bob'^.pLifetime.psAssists `shouldBe` 5
context "invalid index" $ let
ps' = awardAssist (-1) ps
in it "should not change anything" $
ps'^.database.dbPlayers `shouldBe` ps^.database.dbPlayers
makePlayer :: IO Player
makePlayer = Player
<$> makeNum