preserve the focused scenario when rebuilding the NewGameMenu (#688)

Fixes #682.

I still don't understand why it was doing the thing before where it repeated each challenge twice, instead of just repeating the same challenge over and over again.  So that has me a little worried.  But I do understand why there was a problem --- because when we win a scenario, the menu automatically advances to the next one, but the `currentScenarioPath` is still the one we just won, as it should be so we can save the updated `ScenarioInfo` associated to the correct scenario.  But then the menu is rebuilt in order to get all the updated `ScenarioInfo` --- so we must be sure to carefully preserve whatever scenario the menu is focused on, which may or may not be the same as the one we just exited, depending on whether we won or just quit.
This commit is contained in:
Brent Yorgey 2022-09-17 06:32:11 -05:00 committed by GitHub
parent 1b3e622655
commit effef86d22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -175,7 +175,7 @@ getTutorials sc = case M.lookup "Tutorials" (scMap sc) of
-- | If we are in a New Game menu, advance the menu to the next item in order.
advanceMenu :: Menu -> Menu
advanceMenu = _NewGameMenu . lens NE.head (\(_ :| t) a -> a :| t) %~ BL.listMoveDown
advanceMenu = _NewGameMenu . ix 0 %~ BL.listMoveDown
handleMainMessagesEvent :: BrickEvent Name AppEvent -> EventM Name AppState ()
handleMainMessagesEvent = \case
@ -399,9 +399,14 @@ saveScenarioInfoOnQuit = do
Nothing -> return ()
Just si -> liftIO $ saveScenarioInfo p si
-- rebuild the NewGameMenu so it gets the updated ScenarioInfo
-- See what scenario is currently focused in the menu. Depending on how the
-- previous scenario ended (via quit vs. via win), it might be the same as
-- currentScenarioPath or it might be different.
curPath <- preuse $ uiState . uiMenu . _NewGameMenu . ix 0 . BL.listSelectedElementL . _SISingle . _2 . scenarioPath
-- Now rebuild the NewGameMenu so it gets the updated ScenarioInfo,
-- being sure to preserve the same focused scenario.
sc <- use $ gameState . scenarios
forM_ (mkNewGameMenu cheat sc p) (uiState . uiMenu .=)
forM_ (mkNewGameMenu cheat sc (fromMaybe p curPath)) (uiState . uiMenu .=)
-- | Quit a game.
--