From e2aeb5bfa49ecd46ec7a272e5cb53a9e3bf5c5c6 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Thu, 9 Jan 2020 01:01:51 -0500 Subject: [PATCH] enable toggling of rookie flag for players --- src/Mtlstats/Actions.hs | 15 +++++++++++++++ src/Mtlstats/Menu/EditPlayer.hs | 25 ++++++++++++++----------- test/ActionsSpec.hs | 27 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/Mtlstats/Actions.hs b/src/Mtlstats/Actions.hs index e5f9fdd..b068296 100644 --- a/src/Mtlstats/Actions.hs +++ b/src/Mtlstats/Actions.hs @@ -32,6 +32,7 @@ module Mtlstats.Actions , createGoalie , edit , editPlayer + , editSelectedPlayer , editGoalie , addPlayer , addGoalie @@ -47,6 +48,7 @@ import Data.Maybe (fromMaybe) import Lens.Micro ((^.), (&), (.~), (%~)) import Mtlstats.Types +import Mtlstats.Util -- | Starts a new season startNewSeason :: ProgState -> ProgState @@ -106,6 +108,19 @@ edit = progMode .~ EditMenu editPlayer :: ProgState -> ProgState editPlayer = progMode .~ EditPlayer newEditPlayerState +-- | Edits the selected 'Player' +editSelectedPlayer + :: (Player -> Player) + -- ^ The modification to be made to the 'Player' + -> ProgState + -> ProgState +editSelectedPlayer f s = fromMaybe s $ do + n <- s^.progMode.editPlayerStateL.epsSelectedPlayer + let + players = s^.database.dbPlayers + players' = modifyNth n f players + Just $ s & database.dbPlayers .~ players' + -- | Starts the 'Goalie' editing process editGoalie :: ProgState -> ProgState editGoalie = progMode .~ EditGoalie newEditGoalieState diff --git a/src/Mtlstats/Menu/EditPlayer.hs b/src/Mtlstats/Menu/EditPlayer.hs index 2b6312d..777e025 100644 --- a/src/Mtlstats/Menu/EditPlayer.hs +++ b/src/Mtlstats/Menu/EditPlayer.hs @@ -26,7 +26,7 @@ module Mtlstats.Menu.EditPlayer ) where import Control.Monad.Trans.State (modify) -import Lens.Micro ((.~)) +import Lens.Micro ((.~), (%~)) import Mtlstats.Actions import Mtlstats.Types @@ -35,19 +35,22 @@ import Mtlstats.Types.Menu -- | The 'Player' edit menu editPlayerMenu :: Menu () editPlayerMenu = Menu "*** EDIT PLAYER ***" () $ map - (\(ch, label, mode) -> MenuItem ch label $ - modify $ case mode of - Nothing -> edit - Just m -> progMode.editPlayerStateL.epsMode .~ m) + (\(ch, label, action) -> MenuItem ch label $ modify action) + -- key, label, value - [ ( '1', "Edit number", Just EPNumber ) - , ( '2', "Edit name", Just EPName ) - , ( '3', "Edit position", Just EPPosition ) - , ( '4', "Edit YTD stats", Just EPYtd ) - , ( '5', "Edit lifetime stats", Just EPLifetime ) - , ( 'R', "Return to Edit Menu", Nothing ) + [ ( '1', "Edit number", set EPNumber ) + , ( '2', "Edit name", set EPName ) + , ( '3', "Edit position", set EPPosition ) + , ( '4', "Toggle rookie flag", toggle ) + , ( '5', "Edit YTD stats", set EPYtd ) + , ( '6', "Edit lifetime stats", set EPLifetime ) + , ( 'R', "Return to Edit Menu", edit ) ] + where + set mode = progMode.editPlayerStateL.epsMode .~ mode + toggle = editSelectedPlayer $ pRookie %~ not + -- | The 'Player' YTD stats edit menu editPlayerYtdMenu :: Menu () editPlayerYtdMenu = editMenu diff --git a/test/ActionsSpec.hs b/test/ActionsSpec.hs index d29fd6a..7a26815 100644 --- a/test/ActionsSpec.hs +++ b/test/ActionsSpec.hs @@ -53,6 +53,7 @@ spec = describe "Mtlstats.Actions" $ do createGoalieSpec editSpec editPlayerSpec + editSelectedPlayerSpec editGoalieSpec addPlayerSpec addGoalieSpec @@ -209,6 +210,32 @@ editPlayerSpec = describe "editPlayer" $ s = editPlayer newProgState in show (s^.progMode) `shouldBe` "EditPlayer" +editSelectedPlayerSpec :: Spec +editSelectedPlayerSpec = describe "editSelectedPlayer" $ mapM_ + (\(label, pState, expected) -> context label $ + it "should edit the players appropriately" $ let + pState' = editSelectedPlayer (pName .~ "foo") pState + players' = pState'^.database.dbPlayers + in players' `shouldBe` expected) + + -- label, initial state, expected + [ ( "wrong mode", baseState, players ) + , ( "not selected", changePlayer Nothing, players ) + , ( "player 0", changePlayer $ Just 0, changed0 ) + , ( "player 1", changePlayer $ Just 1, changed1 ) + , ( "out of bounds", changePlayer $ Just 2, players ) + ] + + where + baseState = newProgState & database.dbPlayers .~ players + changePlayer n = baseState + & (progMode.editPlayerStateL.epsSelectedPlayer .~ n) + players = [ player 0, player 1 ] + changed0 = [ player' 0, player 1 ] + changed1 = [ player 0, player' 1 ] + player n = newPlayer n ("Player " ++ show n) "pos" + player' n = newPlayer n "foo" "pos" + editGoalieSpec :: Spec editGoalieSpec = describe "editGoalie" $ it "should change the mode appropriately" $ let