From ac92182b2036f2b0320859c8dcc1c277164fa7ac Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 28 Sep 2019 01:46:28 -0400 Subject: [PATCH 1/2] insert players at end of list (preserve index numbers) --- src/Mtlstats/Actions.hs | 2 +- src/Mtlstats/Prompt.hs | 3 ++- test/ActionsSpec.hs | 10 +++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Mtlstats/Actions.hs b/src/Mtlstats/Actions.hs index b12d587..89d75a3 100644 --- a/src/Mtlstats/Actions.hs +++ b/src/Mtlstats/Actions.hs @@ -149,7 +149,7 @@ addPlayer s = fromMaybe s $ do pos = cps^.cpsPosition player = newPlayer num name pos Just $ s & database.dbPlayers - %~ (player:) + %~ (++[player]) -- | Awards the goal and assists to the players recordGoalAssists :: ProgState -> ProgState diff --git a/src/Mtlstats/Prompt.hs b/src/Mtlstats/Prompt.hs index 9d7f0d8..b4d6ee7 100644 --- a/src/Mtlstats/Prompt.hs +++ b/src/Mtlstats/Prompt.hs @@ -182,7 +182,8 @@ selectPlayerPrompt pStr callback = Prompt & cpsName .~ sStr & cpsSuccessCallback .~ do modify $ progMode .~ mode - callback (Just 0) + pIndex <- pred . length <$> gets (view $ database.dbPlayers) + callback $ Just pIndex & cpsFailureCallback .~ do modify $ progMode .~ mode callback Nothing diff --git a/test/ActionsSpec.hs b/test/ActionsSpec.hs index 9cc64ad..9a5ef62 100644 --- a/test/ActionsSpec.hs +++ b/test/ActionsSpec.hs @@ -342,7 +342,7 @@ addPlayerSpec = describe "addPlayer" $ do p1 = newPlayer 1 "Joe" "centre" p2 = newPlayer 2 "Bob" "defense" db = newDatabase - & dbPlayers .~ [p2] + & dbPlayers .~ [p1] s pm = newProgState & progMode .~ pm & database .~ db @@ -350,15 +350,15 @@ addPlayerSpec = describe "addPlayer" $ do context "data available" $ it "should create the player" $ let s' = addPlayer $ s $ CreatePlayer $ newCreatePlayerState - & cpsNumber ?~ 1 - & cpsName .~ "Joe" - & cpsPosition .~ "centre" + & cpsNumber ?~ 2 + & cpsName .~ "Bob" + & cpsPosition .~ "defense" in s'^.database.dbPlayers `shouldBe` [p1, p2] context "data unavailable" $ it "should not create the player" $ let s' = addPlayer $ s MainMenu - in s'^.database.dbPlayers `shouldBe` [p2] + in s'^.database.dbPlayers `shouldBe` [p1] recordGoalAssistsSpec :: Spec recordGoalAssistsSpec = describe "recordGoalAssists" $ do From b8a3af11a1d8322d90fabff9fc0beacffd7c7d93 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 28 Sep 2019 02:09:11 -0400 Subject: [PATCH 2/2] reference goals and assists by index number, not names --- src/Mtlstats/Actions.hs | 12 ++++-------- src/Mtlstats/Prompt.hs | 20 ++++++-------------- src/Mtlstats/Types.hs | 12 +++++++----- test/ActionsSpec.hs | 6 +++--- 4 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/Mtlstats/Actions.hs b/src/Mtlstats/Actions.hs index 89d75a3..513c7f5 100644 --- a/src/Mtlstats/Actions.hs +++ b/src/Mtlstats/Actions.hs @@ -154,18 +154,14 @@ addPlayer s = fromMaybe s $ do -- | Awards the goal and assists to the players recordGoalAssists :: ProgState -> ProgState recordGoalAssists ps = fromMaybe ps $ do - let - gs = ps^.progMode.gameStateL - players = ps^.database.dbPlayers - (goalId, _) <- playerSearchExact (gs^.goalBy) players - assistIds <- mapM - (\name -> fst <$> playerSearchExact name players) - (gs^.assistsBy) + let gs = ps^.progMode.gameStateL + goalId <- gs^.goalBy + let assistIds = gs^.assistsBy Just $ ps & awardGoal goalId & (\s -> foldr awardAssist s assistIds) & progMode.gameStateL - %~ (goalBy .~ "") + %~ (goalBy .~ Nothing) . (assistsBy .~ []) . (pointsAccounted %~ succ) diff --git a/src/Mtlstats/Prompt.hs b/src/Mtlstats/Prompt.hs index b4d6ee7..977f17b 100644 --- a/src/Mtlstats/Prompt.hs +++ b/src/Mtlstats/Prompt.hs @@ -211,12 +211,7 @@ recordGoalPrompt recordGoalPrompt game goal = selectPlayerPrompt ( "*** GAME " ++ padNum 2 game ++ " ***\n" ++ "Who scored goal number " ++ show goal ++ "? " - ) $ \case - Nothing -> return () - Just n -> nth n <$> gets (view $ database.dbPlayers) - >>= maybe - (return ()) - (\p -> modify $ progMode.gameStateL.goalBy .~ p^.pName) + ) $ modify . (progMode.gameStateL.goalBy .~) -- | Prompts for a player who assisted the goal recordAssistPrompt @@ -233,14 +228,11 @@ recordAssistPrompt game goal assist = selectPlayerPrompt ++ "Assist #" ++ show assist ++ ": " ) $ \case Nothing -> modify recordGoalAssists - Just n -> nth n <$> gets (view $ database.dbPlayers) - >>= maybe - (return ()) - (\p -> do - modify $ progMode.gameStateL.assistsBy %~ (++[p^.pName]) - nAssists <- length <$> gets (view $ progMode.gameStateL.assistsBy) - when (nAssists >= maxAssists) $ - modify recordGoalAssists) + Just n -> do + modify $ progMode.gameStateL.assistsBy %~ (++[n]) + nAssists <- length <$> gets (view $ progMode.gameStateL.assistsBy) + when (nAssists >= maxAssists) $ + modify recordGoalAssists drawSimplePrompt :: String -> ProgState -> C.Update () drawSimplePrompt pStr s = C.drawString $ pStr ++ s^.inputBuffer diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index c385b64..1c478b1 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -207,10 +207,12 @@ data GameState = GameState -- ^ Set to 'True' when the user confirms the entered data , _pointsAccounted :: Int -- ^ The number of game points accounted for - , _goalBy :: String - -- ^ The player who scored the most recently entered goal - , _assistsBy :: [String] - -- ^ The players who have assisted the most recently entered goal + , _goalBy :: Maybe Int + -- ^ The index number of the player who scored the most recently + -- entered goal + , _assistsBy :: [Int] + -- ^ The index numbers of the players who have assisted the most + -- recently entered goal } deriving (Eq, Show) -- | The type of game @@ -507,7 +509,7 @@ newGameState = GameState , _overtimeFlag = Nothing , _dataVerified = False , _pointsAccounted = 0 - , _goalBy = "" + , _goalBy = Nothing , _assistsBy = [] } diff --git a/test/ActionsSpec.hs b/test/ActionsSpec.hs index 9a5ef62..eae38b9 100644 --- a/test/ActionsSpec.hs +++ b/test/ActionsSpec.hs @@ -371,8 +371,8 @@ recordGoalAssistsSpec = describe "recordGoalAssists" $ do = newProgState & database.dbPlayers .~ [joe, bob, steve, dave] & progMode.gameStateL - %~ (goalBy .~ "Joe") - . (assistsBy .~ ["Bob", "Steve"]) + %~ (goalBy ?~ 0) + . (assistsBy .~ [1, 2]) & recordGoalAssists mapM_ @@ -399,7 +399,7 @@ recordGoalAssistsSpec = describe "recordGoalAssists" $ do ] it "should clear the goalBy value" $ - ps^.progMode.gameStateL.goalBy `shouldBe` "" + ps^.progMode.gameStateL.goalBy `shouldBe` Nothing it "should clear the assistsBy list" $ ps^.progMode.gameStateL.assistsBy `shouldBe` []