implemented awardGoal

This commit is contained in:
Jonathan Lamothe 2019-09-19 05:29:55 -04:00
parent 156c2baaba
commit b125e72034
2 changed files with 67 additions and 0 deletions

View File

@ -32,6 +32,7 @@ module Mtlstats.Actions
, validateGameDate , validateGameDate
, createPlayer , createPlayer
, addPlayer , addPlayer
, awardGoal
) where ) where
import Control.Monad.Trans.State (modify) import Control.Monad.Trans.State (modify)
@ -136,3 +137,18 @@ addPlayer s = fromMaybe s $ do
player = newPlayer num name pos player = newPlayer num name pos
Just $ s & database.dbPlayers Just $ s & database.dbPlayers
%~ (player:) %~ (player:)
-- | Awards a goal to a player
awardGoal
:: Int
-- ^ The player's index number
-> 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..]

View File

@ -41,6 +41,7 @@ spec = describe "Mtlstats.Actions" $ do
validateGameDateSpec validateGameDateSpec
createPlayerSpec createPlayerSpec
addPlayerSpec addPlayerSpec
awardGoalSpec
startNewSeasonSpec :: Spec startNewSeasonSpec :: Spec
startNewSeasonSpec = describe "startNewSeason" $ do startNewSeasonSpec = describe "startNewSeason" $ do
@ -351,6 +352,56 @@ addPlayerSpec = describe "addPlayer" $ do
s' = addPlayer $ s MainMenu s' = addPlayer $ s MainMenu
in s'^.database.dbPlayers `shouldBe` [p2] in s'^.database.dbPlayers `shouldBe` [p2]
awardGoalSpec :: Spec
awardGoalSpec = describe "awardGoal" $ do
let
joe
= newPlayer 2 "Joe" "centre"
& pYtd.psGoals .~ 1
& pLifetime.psGoals .~ 2
bob
= newPlayer 3 "Bob" "defense"
& pYtd.psGoals .~ 3
& pLifetime.psGoals .~ 4
db
= newDatabase
& dbPlayers .~ [joe, bob]
ps
= newProgState
& database .~ db
context "Joe" $ do
let
ps' = awardGoal 0 ps
player = head $ ps'^.database.dbPlayers
it "should increment Joe's year-to-date goals" $
player^.pYtd.psGoals `shouldBe` 2
it "should increment Joe's lifetime goals" $
player^.pLifetime.psGoals `shouldBe` 3
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
context "invalid index" $ let
ps' = awardGoal 2 ps
in it "should not change the database" $
ps'^.database `shouldBe` db
context "negative index" $ let
ps' = awardGoal (-1) ps
in it "should not change the database" $
ps'^.database `shouldBe` db
makePlayer :: IO Player makePlayer :: IO Player
makePlayer = Player makePlayer = Player
<$> makeNum <$> makeNum