prompt for confirmation of game input

This commit is contained in:
Jonathan Lamothe 2019-09-07 00:27:18 -04:00
parent 27867ba69d
commit dc2f632563

View File

@ -24,11 +24,13 @@ module Mtlstats.Control (dispatch) where
import Control.Monad (when) import Control.Monad (when)
import Control.Monad.Trans.State (modify) import Control.Monad.Trans.State (modify)
import Data.Char (toUpper) import Data.Char (toUpper)
import Data.Maybe (fromJust)
import Lens.Micro ((^.), (.~)) import Lens.Micro ((^.), (.~))
import qualified UI.NCurses as C import qualified UI.NCurses as C
import Mtlstats.Actions import Mtlstats.Actions
import Mtlstats.Format import Mtlstats.Format
import Mtlstats.Handlers
import Mtlstats.Menu import Mtlstats.Menu
import Mtlstats.Prompt import Mtlstats.Prompt
import Mtlstats.Report import Mtlstats.Report
@ -38,22 +40,36 @@ import Mtlstats.Types
-- run -- run
dispatch :: ProgState -> Controller dispatch :: ProgState -> Controller
dispatch s = case s^.progMode of dispatch s = case s^.progMode of
MainMenu -> mainMenuC
NewSeason -> newSeasonC
NewGame 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
| otherwise -> reportC
MainMenu -> Controller mainMenuC :: Controller
mainMenuC = Controller
{ drawController = const $ drawMenu mainMenu { drawController = const $ drawMenu mainMenu
, handleController = menuHandler mainMenu , handleController = menuHandler mainMenu
} }
NewSeason -> Controller newSeasonC :: Controller
newSeasonC = Controller
{ drawController = const $ drawMenu newSeasonMenu { drawController = const $ drawMenu newSeasonMenu
, handleController = \e -> do , handleController = \e -> do
menuHandler newSeasonMenu e menuHandler newSeasonMenu e
return True return True
} }
NewGame gs gameYearC :: Controller
gameYearC = Controller
| null $ gs^.gameYear -> Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
drawPrompt gameYearPrompt s drawPrompt gameYearPrompt s
@ -62,7 +78,8 @@ dispatch s = case s^.progMode of
return True return True
} }
| null $ gs^.gameMonth -> Controller gameMonthC :: Controller
gameMonthC = Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
drawMenu gameMonthMenu drawMenu gameMonthMenu
@ -71,7 +88,8 @@ dispatch s = case s^.progMode of
return True return True
} }
| null $ gs^.gameDay -> Controller gameDayC :: Controller
gameDayC = Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
drawPrompt gameDayPrompt s drawPrompt gameDayPrompt s
@ -81,7 +99,8 @@ dispatch s = case s^.progMode of
return True return True
} }
| null $ gs^.gameType -> Controller gameTypeC :: Controller
gameTypeC = Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
drawMenu gameTypeMenu drawMenu gameTypeMenu
@ -90,7 +109,8 @@ dispatch s = case s^.progMode of
return True return True
} }
| null $ gs^.otherTeam -> Controller otherTeamC :: Controller
otherTeamC = Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
drawPrompt otherTeamPrompt s drawPrompt otherTeamPrompt s
@ -99,7 +119,8 @@ dispatch s = case s^.progMode of
return True return True
} }
| null $ gs^.homeScore -> Controller homeScoreC :: Controller
homeScoreC = Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
drawPrompt homeScorePrompt s drawPrompt homeScorePrompt s
@ -108,29 +129,54 @@ dispatch s = case s^.progMode of
return True return True
} }
| null $ gs^.awayScore -> Controller awayScoreC :: Controller
awayScoreC = Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
drawPrompt awayScorePrompt s drawPrompt awayScorePrompt s
, handleController = \e -> do , handleController = \e -> do
promptHandler awayScorePrompt e promptHandler awayScorePrompt e
modify overtimeCheck modify overtimeCheck
modify updateGameStats
return True return True
} }
| null $ gs^.overtimeFlag -> Controller overtimeFlagC :: Controller
overtimeFlagC = Controller
{ drawController = \s -> do { drawController = \s -> do
header s header s
C.drawString "Did the game go into overtime? (Y/N)" C.drawString "Did the game go into overtime? (Y/N)"
return C.CursorInvisible return C.CursorInvisible
, handleController = \e -> do , handleController = \e -> do
overtimePrompt e modify $ progMode.gameStateL.overtimeFlag .~ ynHandler e
modify updateGameStats
return True return True
} }
| otherwise -> Controller verifyDataC :: Controller
verifyDataC = Controller
{ drawController = \s -> do
let gs = s^.progMode.gameStateL
header s
C.drawString "\n"
C.drawString $ " Date: " ++ date 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)"
return C.CursorInvisible
, handleController = \e -> do
case ynHandler e of
Just True -> do
modify $ progMode.gameStateL.dataVerified .~ True
modify updateGameStats
Just False -> modify $ progMode.gameStateL .~ newGameState
Nothing -> return ()
return True
}
reportC :: Controller
reportC = Controller
{ drawController = \s -> do { drawController = \s -> do
(_, cols) <- C.windowSize (_, cols) <- C.windowSize
C.drawString $ report (fromInteger $ pred cols) s C.drawString $ report (fromInteger $ pred cols) s
@ -148,11 +194,3 @@ dispatch s = case s^.progMode of
header :: ProgState -> C.Update () header :: ProgState -> C.Update ()
header s = C.drawString $ header s = C.drawString $
"*** GAME " ++ padNum 2 (s^.database.dbGames) ++ " ***\n" "*** GAME " ++ padNum 2 (s^.database.dbGames) ++ " ***\n"
overtimePrompt :: C.Event -> Action ()
overtimePrompt (C.EventCharacter c) = modify $
progMode.gameStateL.overtimeFlag .~ case toUpper c of
'Y' -> Just True
'N' -> Just False
_ -> Nothing
overtimePrompt _ = return ()