From 8795cb46a927f8d2db8b982dfb8f36d75b81aa83 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 23 Nov 2019 01:13:17 -0500 Subject: [PATCH] refactored game entry control flow --- src/Mtlstats/Control.hs | 2 +- src/Mtlstats/Control/NewGame.hs | 114 ++++++-------------- src/Mtlstats/Control/NewGame/GoalieInput.hs | 20 ++-- src/Mtlstats/Menu.hs | 16 +++ src/Mtlstats/Prompt/NewGame.hs | 5 +- 5 files changed, 60 insertions(+), 97 deletions(-) diff --git a/src/Mtlstats/Control.hs b/src/Mtlstats/Control.hs index e57b4b4..b8521d7 100644 --- a/src/Mtlstats/Control.hs +++ b/src/Mtlstats/Control.hs @@ -43,7 +43,7 @@ dispatch :: ProgState -> Controller dispatch s = case s^.progMode of MainMenu -> mainMenuC NewSeason -> newSeasonC - NewGame _ -> newGameC s + NewGame gs -> newGameC gs CreatePlayer cps | null $ cps^.cpsNumber -> getPlayerNumC | null $ cps^.cpsName -> getPlayerNameC diff --git a/src/Mtlstats/Control/NewGame.hs b/src/Mtlstats/Control/NewGame.hs index 6c26bca..79f43c2 100644 --- a/src/Mtlstats/Control/NewGame.hs +++ b/src/Mtlstats/Control/NewGame.hs @@ -39,95 +39,43 @@ import Mtlstats.Types import Mtlstats.Util -- | Dispatcher for a new game -newGameC :: ProgState -> Controller -newGameC s = let - gs = s^.progMode.gameStateL - in if null $ gs^.gameYear then gameYearC - else if null $ gs^.gameMonth then gameMonthC - else if null $ gs^.gameDay then gameDayC - else if null $ gs^.gameType then gameTypeC - else if null $ gs^.otherTeam then otherTeamC - else if null $ gs^.homeScore then homeScoreC - else if null $ gs^.awayScore then awayScoreC - else if null $ gs^.overtimeFlag then overtimeFlagC - else if not $ gs^.dataVerified then verifyDataC - else if fromJust (unaccountedPoints gs) then goalInput gs - else if isJust $ gs^.gameSelectedPlayer then getPMinsC - else if not $ gs^.gamePMinsRecorded then pMinPlayerC - else if not $ gs^.gameGoalieAssigned then goalieInputC s - else reportC +newGameC :: GameState -> Controller +newGameC gs + | null $ gs^.gameYear = gameYearC + | null $ gs^.gameMonth = gameMonthC + | null $ gs^.gameDay = gameDayC + | null $ gs^.gameType = gameTypeC + | null $ gs^.otherTeam = otherTeamC + | null $ gs^.homeScore = homeScoreC + | null $ gs^.awayScore = awayScoreC + | null $ gs^.overtimeFlag = overtimeFlagC + | not $ gs^.dataVerified = verifyDataC + | fromJust (unaccountedPoints gs) = goalInput gs + | isJust $ gs^.gameSelectedPlayer = getPMinsC + | not $ gs^.gamePMinsRecorded = pMinPlayerC + | not $ gs^.gameGoalieAssigned = goalieInputC gs + | otherwise = reportC gameYearC :: Controller -gameYearC = Controller - { drawController = \s -> do - header s - drawPrompt gameYearPrompt s - , handleController = \e -> do - promptHandler gameYearPrompt e - return True - } +gameYearC = promptControllerWith header gameYearPrompt gameMonthC :: Controller -gameMonthC = Controller - { drawController = \s -> do - header s - drawMenu gameMonthMenu - , handleController = \e -> do - menuHandler gameMonthMenu e - return True - } +gameMonthC = menuControllerWith header gameMonthMenu gameDayC :: Controller -gameDayC = Controller - { drawController = \s -> do - header s - drawPrompt gameDayPrompt s - , handleController = \e -> do - promptHandler gameDayPrompt e - modify validateGameDate - return True - } +gameDayC = promptControllerWith header gameDayPrompt gameTypeC :: Controller -gameTypeC = Controller - { drawController = \s -> do - header s - drawMenu gameTypeMenu - , handleController = \e -> do - menuHandler gameTypeMenu e - return True - } +gameTypeC = menuControllerWith header gameTypeMenu otherTeamC :: Controller -otherTeamC = Controller - { drawController = \s -> do - header s - drawPrompt otherTeamPrompt s - , handleController = \e -> do - promptHandler otherTeamPrompt e - return True - } +otherTeamC = promptControllerWith header otherTeamPrompt homeScoreC :: Controller -homeScoreC = Controller - { drawController = \s -> do - header s - drawPrompt homeScorePrompt s - , handleController = \e -> do - promptHandler homeScorePrompt e - return True - } +homeScoreC = promptControllerWith header homeScorePrompt awayScoreC :: Controller -awayScoreC = Controller - { drawController = \s -> do - header s - drawPrompt awayScorePrompt s - , handleController = \e -> do - promptHandler awayScorePrompt e - modify overtimeCheck - return True - } +awayScoreC = promptControllerWith header awayScorePrompt overtimeFlagC :: Controller overtimeFlagC = Controller @@ -146,13 +94,15 @@ verifyDataC = Controller let gs = s^.progMode.gameStateL header s C.drawString "\n" - C.drawString $ " Date: " ++ gameDate gs ++ "\n" - C.drawString $ " Game type: " ++ show (fromJust $ gs^.gameType) ++ "\n" - C.drawString $ "Other team: " ++ gs^.otherTeam ++ "\n" - C.drawString $ "Home score: " ++ show (fromJust $ gs^.homeScore) ++ "\n" - C.drawString $ "Away score: " ++ show (fromJust $ gs^.awayScore) ++ "\n" - C.drawString $ " Overtime: " ++ show (fromJust $ gs^.overtimeFlag) ++ "\n\n" - C.drawString "Is the above information correct? (Y/N)" + C.drawString $ unlines $ labelTable + [ ( "Date", gameDate gs ) + , ( "Game type", show $ fromJust $ gs^.gameType ) + , ( "Other team", gs^.otherTeam ) + , ( "Home score", show $ fromJust $ gs^.homeScore ) + , ( "Away score", show $ fromJust $ gs^.awayScore ) + , ( "Overtime", show $ fromJust $ gs^.overtimeFlag ) + ] + C.drawString "\nIs the above information correct? (Y/N)" return C.CursorInvisible , handleController = \e -> do case ynHandler e of diff --git a/src/Mtlstats/Control/NewGame/GoalieInput.hs b/src/Mtlstats/Control/NewGame/GoalieInput.hs index 9cf5d6a..17eb278 100644 --- a/src/Mtlstats/Control/NewGame/GoalieInput.hs +++ b/src/Mtlstats/Control/NewGame/GoalieInput.hs @@ -33,16 +33,12 @@ import Mtlstats.Types import Mtlstats.Util -- | The dispatcher for handling goalie input -goalieInputC :: ProgState -> Controller -goalieInputC s = let - gs = s^.progMode.gameStateL - in if gs^.gameGoaliesRecorded - then selectGameGoalieC s - else if null $ gs^.gameSelectedGoalie - then selectGoalieC - else if null $ gs^.gameGoalieMinsPlayed - then minsPlayedC - else goalsAllowedC +goalieInputC :: GameState -> Controller +goalieInputC gs + | gs^.gameGoaliesRecorded = selectGameGoalieC + | null $ gs^.gameSelectedGoalie = selectGoalieC + | null $ gs^.gameGoalieMinsPlayed = minsPlayedC + | otherwise = goalsAllowedC selectGoalieC :: Controller selectGoalieC = promptController selectGameGoaliePrompt @@ -53,8 +49,8 @@ minsPlayedC = promptControllerWith header goalieMinsPlayedPrompt goalsAllowedC :: Controller goalsAllowedC = promptControllerWith header goalsAllowedPrompt -selectGameGoalieC :: ProgState -> Controller -selectGameGoalieC = menuController . gameGoalieMenu +selectGameGoalieC :: Controller +selectGameGoalieC = menuStateController gameGoalieMenu header :: ProgState -> C.Update () header s = C.drawString $ unlines diff --git a/src/Mtlstats/Menu.hs b/src/Mtlstats/Menu.hs index db404ae..77c6f63 100644 --- a/src/Mtlstats/Menu.hs +++ b/src/Mtlstats/Menu.hs @@ -23,6 +23,7 @@ module Mtlstats.Menu ( -- * Menu Functions menuController, menuControllerWith, + menuStateController, drawMenu, menuHandler, -- * Menus @@ -76,6 +77,21 @@ menuControllerWith header menu = Controller return True } +-- | Generate and create a controller for a menu based on the current +-- 'ProgState' +menuStateController + :: (ProgState -> Menu ()) + -- ^ The function to generate the menu + -> Controller + -- ^ The resulting controller +menuStateController menuFunc = Controller + { drawController = drawMenu . menuFunc + , handleController = \e -> do + menu <- gets menuFunc + menuHandler menu e + return True + } + -- | The draw function for a 'Menu' drawMenu :: Menu a -> C.Update C.CursorMode drawMenu m = do diff --git a/src/Mtlstats/Prompt/NewGame.hs b/src/Mtlstats/Prompt/NewGame.hs index 3812b97..a6cdfc7 100644 --- a/src/Mtlstats/Prompt/NewGame.hs +++ b/src/Mtlstats/Prompt/NewGame.hs @@ -65,8 +65,9 @@ homeScorePrompt = numPrompt "Home score: " $ -- | Prompts for the away score awayScorePrompt :: Prompt -awayScorePrompt = numPrompt "Away score: " $ - modify . (progMode.gameStateL.awayScore ?~) +awayScorePrompt = numPrompt "Away score: " $ \score -> modify + $ overtimeCheck + . (progMode.gameStateL.awayScore ?~ score) -- | Prompts for the player who scored the goal recordGoalPrompt