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` []