Skip to content

Returns to the scenario menu after winning a game #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,22 @@ handleNewGameMenuEvent scenarioStack@(curMenu :| rest) s = \case
let gs = s ^. gameState
gs' <- liftIO $ playScenario (gs ^. entityMap) scene Nothing Nothing gs

let nextMenu
-- Go back to the scenario list
| null rest = NewGameMenu scenarioStack
-- Advance to the next tutorial or challenge
| otherwise = NewGameMenu (BL.listMoveDown curMenu :| rest)

continue $
s & uiState . uiMenu .~ NoMenu
& uiState . uiPrevMenu .~ nextMenu
& gameState .~ gs'
Just (SICollection _ c) ->
continue $
s & uiState . uiMenu .~ NewGameMenu (NE.cons (mkScenarioList (s ^. uiState . uiCheatMode) c) scenarioStack)
Key V.KEsc -> exitNewGameMenu s scenarioStack
CharKey 'q' -> exitNewGameMenu s scenarioStack
ControlKey 'q' -> exitNewGameMenu s scenarioStack
ControlKey 'q' -> halt s
VtyEvent ev -> do
menu' <- handleListEvent ev curMenu
continue $ s & uiState . uiMenu .~ NewGameMenu (menu' :| rest)
Expand Down Expand Up @@ -243,14 +250,14 @@ handleModalEvent s = \case
continue s'

-- | Quit a game. Currently all it does is write out the updated REPL
-- history to a @.swarm_history@ file, and return to the main menu.
-- history to a @.swarm_history@ file, and return to the previous menu.
quitGame :: AppState -> EventM Name (Next AppState)
quitGame s = do
let hist = mapMaybe getREPLEntry $ getLatestREPLHistoryItems maxBound history
liftIO $ (`T.appendFile` T.unlines hist) =<< getSwarmHistoryPath True
let s' =
s & uiState . uiReplHistory %~ restartREPLHistory
& uiState . uiMenu .~ MainMenu (mainMenu NewGame)
& uiState . uiMenu .~ (s ^. uiState . uiPrevMenu)
continue s'
where
history = s ^. uiState . uiReplHistory
Expand Down
6 changes: 6 additions & 0 deletions src/Swarm/TUI/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module Swarm.TUI.Model (
-- ** UI Model
UIState,
uiMenu,
uiPrevMenu,
uiCheatMode,
uiFocusRing,
uiReplForm,
Expand Down Expand Up @@ -359,6 +360,7 @@ makePrisms ''InventoryListEntry
-- see the lenses below.
data UIState = UIState
{ _uiMenu :: Menu
, _uiPrevMenu :: Menu
, _uiCheatMode :: Bool
, _uiFocusRing :: FocusRing Name
, _uiReplForm :: Form Text AppEvent Name
Expand Down Expand Up @@ -407,6 +409,9 @@ let exclude = ['_lgTicksPerSecond]
-- | The current menu state.
uiMenu :: Lens' UIState Menu

-- | The previous menu state, to go back after playing a game
uiPrevMenu :: Lens' UIState Menu

-- | Cheat mode, i.e. are we allowed to turn creative mode on and off?
uiCheatMode :: Lens' UIState Bool

Expand Down Expand Up @@ -579,6 +584,7 @@ initUIState showMainMenu cheatMode = liftIO $ do
return $
UIState
{ _uiMenu = if showMainMenu then MainMenu (mainMenu NewGame) else NoMenu
, _uiPrevMenu = NoMenu
, _uiCheatMode = cheatMode
, _uiFocusRing = initFocusRing
, _uiReplForm = initReplForm
Expand Down
10 changes: 9 additions & 1 deletion src/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,15 @@ generateModal s mt = Modal mt (dialog (Just title) buttons (maxModalWindowWidth
(title, widget, buttons, requiredWidth) =
case mt of
HelpModal -> (" Help ", helpWidget, Nothing, maxModalWindowWidth)
WinModal -> ("", txt "Congratulations!", Nothing, maxModalWindowWidth)
WinModal ->
let winMsg = "Congratulations!"
continueMsg = "Keep playing"
stopMsg = "Pick the next game"
in ( ""
, padBottom (Pad 1) $ hCenter $ txt winMsg
, Just (0, [(stopMsg, Confirm), (continueMsg, Cancel)])
, length continueMsg + length stopMsg + 32
)
DescriptionModal e -> (descriptionTitle e, descriptionWidget s e, Nothing, 100)
QuitModal ->
let quitMsg = "Are you sure you want to quit this game and return to the menu?"
Expand Down