From 52f1e34d49a9b52b76abb217c612a9751c95a9e7 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Thu, 9 Jan 2020 01:31:24 -0500 Subject: [PATCH] implemented rookie flag toggling for goalies --- src/Mtlstats/Actions.hs | 14 ++++++++++++++ src/Mtlstats/Menu/EditGoalie.hs | 23 +++++++++++++---------- test/ActionsSpec.hs | 27 +++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/Mtlstats/Actions.hs b/src/Mtlstats/Actions.hs index b068296..ef32a38 100644 --- a/src/Mtlstats/Actions.hs +++ b/src/Mtlstats/Actions.hs @@ -34,6 +34,7 @@ module Mtlstats.Actions , editPlayer , editSelectedPlayer , editGoalie + , editSelectedGoalie , addPlayer , addGoalie , resetCreatePlayerState @@ -125,6 +126,19 @@ editSelectedPlayer f s = fromMaybe s $ do editGoalie :: ProgState -> ProgState editGoalie = progMode .~ EditGoalie newEditGoalieState +-- | Edits the selected 'Goalie' +editSelectedGoalie + :: (Goalie -> Goalie) + -- ^ The modification to be made to the 'Goalie' + -> ProgState + -> ProgState +editSelectedGoalie f s = fromMaybe s $ do + n <- s^.progMode.editGoalieStateL.egsSelectedGoalie + let + goalies = s^.database.dbGoalies + goalies' = modifyNth n f goalies + Just $ s & database.dbGoalies .~ goalies' + -- | Adds the entered player to the roster addPlayer :: ProgState -> ProgState addPlayer s = fromMaybe s $ do diff --git a/src/Mtlstats/Menu/EditGoalie.hs b/src/Mtlstats/Menu/EditGoalie.hs index 812cf22..5f36606 100644 --- a/src/Mtlstats/Menu/EditGoalie.hs +++ b/src/Mtlstats/Menu/EditGoalie.hs @@ -26,7 +26,7 @@ module Mtlstats.Menu.EditGoalie ) where import Control.Monad.Trans.State (modify) -import Lens.Micro ((.~)) +import Lens.Micro ((.~), (%~)) import Mtlstats.Actions import Mtlstats.Types @@ -35,18 +35,21 @@ import Mtlstats.Types.Menu -- | The 'Goalie' edit menu editGoalieMenu :: Menu () editGoalieMenu = Menu "*** EDIT GOALTENDER ***" () $ map - (\(ch, label, mode) -> MenuItem ch label $ - modify $ case mode of - Nothing -> edit - Just m -> progMode.editGoalieStateL.egsMode .~ m) + (\(ch, label, action) -> MenuItem ch label $ modify action) + -- key, label, value - [ ( '1', "Edit number", Just EGNumber ) - , ( '2', "Edit name", Just EGName ) - , ( '3', "Edit YTD stats", Just EGYtd ) - , ( '4', "Edit Lifetime stats", Just EGLifetime ) - , ( 'R', "Return to Edit Menu", Nothing ) + [ ( '1', "Edit number", set EGNumber ) + , ( '2', "Edit name", set EGName ) + , ( '3', "Toggle rookie flag", toggle ) + , ( '4', "Edit YTD stats", set EGYtd ) + , ( '5', "Edit Lifetime stats", set EGLifetime ) + , ( 'R', "Return to Edit Menu", edit ) ] + where + set mode = progMode.editGoalieStateL.egsMode .~ mode + toggle = editSelectedGoalie (gRookie %~ not) + -- | The 'Goalie' YTD edit menu editGoalieYtdMenu :: Menu () editGoalieYtdMenu = editMenu "*** EDIT GOALTENDER YEAR-TO-DATE ***" diff --git a/test/ActionsSpec.hs b/test/ActionsSpec.hs index 7a26815..4b4f28a 100644 --- a/test/ActionsSpec.hs +++ b/test/ActionsSpec.hs @@ -55,6 +55,7 @@ spec = describe "Mtlstats.Actions" $ do editPlayerSpec editSelectedPlayerSpec editGoalieSpec + editSelectedGoalieSpec addPlayerSpec addGoalieSpec resetCreatePlayerStateSpec @@ -242,6 +243,32 @@ editGoalieSpec = describe "editGoalie" $ s = editGoalie newProgState in show (s^.progMode) `shouldBe` "EditGoalie" +editSelectedGoalieSpec :: Spec +editSelectedGoalieSpec = describe "editSelectedGoalie" $ mapM_ + (\(label, pState, expected) -> context label $ + it "should edit the goalies appropriately" $ let + pState' = editSelectedGoalie (gName .~ "foo") pState + goalies' = pState'^.database.dbGoalies + in goalies' `shouldBe` expected) + + -- label, initial state, expected + [ ( "wrong mode", baseState, goalies ) + , ( "not selected", changeGoalie Nothing, goalies ) + , ( "player 0", changeGoalie $ Just 0, changed0 ) + , ( "player 1", changeGoalie $ Just 1, changed1 ) + , ( "out of bounds", changeGoalie $ Just 2, goalies ) + ] + + where + baseState = newProgState & database.dbGoalies .~ goalies + changeGoalie n = baseState + & (progMode.editGoalieStateL.egsSelectedGoalie .~ n) + goalies = [ goalie 0, goalie 1 ] + changed0 = [ goalie' 0, goalie 1 ] + changed1 = [ goalie 0, goalie' 1 ] + goalie n = newGoalie n ("Player " ++ show n) + goalie' n = newGoalie n "foo" + addPlayerSpec :: Spec addPlayerSpec = describe "addPlayer" $ do let