Skip to content
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

Save time upon finishing scenario #1118

Merged
merged 2 commits into from
Feb 18, 2023
Merged
Changes from 1 commit
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
Next Next commit
Save time upon finishing scenario
- closes #1111
  • Loading branch information
xsebek committed Feb 18, 2023
commit f9947e07c1b4a2a640aed5279267a6e39ba7ce46
39 changes: 28 additions & 11 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -427,19 +427,25 @@ handleModalEvent = \case
where
refreshList lw = nestEventM' lw $ handleListEventWithSeparators ev shouldSkipSelection

-- | Write the @ScenarioInfo@ out to disk when exiting a game.
saveScenarioInfoOnQuit :: (MonadIO m, MonadState AppState m) => m ()
saveScenarioInfoOnQuit = do
getNormalizedCurrentScenarioPath :: (MonadIO m, MonadState AppState m) => m (Maybe FilePath)
getNormalizedCurrentScenarioPath =
-- the path should be normalized and good to search in scenario collection
use (gameState . currentScenarioPath) >>= \case
Nothing -> return Nothing
Just p' -> do
gs <- use $ gameState . scenarios
Just <$> liftIO (normalizeScenarioPath gs p')

-- | Write the @ScenarioInfo@ out to disk when finishing a game (i.e. on winning or exit).
saveScenarioInfoOnFinish :: (MonadIO m, MonadState AppState m) => m ()
saveScenarioInfoOnFinish = do
-- Don't save progress if we are in cheat mode
cheat <- use $ uiState . uiCheatMode
unless cheat $ do
-- the path should be normalized and good to search in scenario collection
mp' <- use $ gameState . currentScenarioPath
case mp' of
getNormalizedCurrentScenarioPath >>= \case
Nothing -> return ()
Just p' -> do
gs <- use $ gameState . scenarios
p <- liftIO $ normalizeScenarioPath gs p'
Just p -> do
t <- liftIO getZonedTime
wc <- use $ gameState . winCondition
let won = case wc of
Expand All @@ -461,6 +467,16 @@ saveScenarioInfoOnQuit = do
_ -> return ()
liftIO $ saveScenarioInfo p si

-- | Write the @ScenarioInfo@ out to disk when exiting a game.
saveScenarioInfoOnQuit :: (MonadIO m, MonadState AppState m) => m ()
saveScenarioInfoOnQuit = do
saveScenarioInfoOnFinish
-- Don't save progress if we are in cheat mode
cheat <- use $ uiState . uiCheatMode
unless cheat $ do
getNormalizedCurrentScenarioPath >>= \case
Nothing -> return ()
Just p -> do
-- 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.
Expand Down Expand Up @@ -777,13 +793,13 @@ doGoalUpdates = do
-- This clears the "flag" that the Lose dialog needs to pop up
gameState . winCondition .= WinConditions (Unwinnable True) x
openModal LoseModal

saveScenarioInfoOnFinish
return True
WinConditions (Won False) x -> do
-- This clears the "flag" that the Win dialog needs to pop up
gameState . winCondition .= WinConditions (Won True) x
openModal WinModal

saveScenarioInfoOnFinish
-- We do NOT advance the New Game menu to the next item here (we
-- used to!), because we do not know if the user is going to
-- select 'keep playing' or 'next challenge'. We maintain the
Expand Down Expand Up @@ -818,7 +834,8 @@ doGoalUpdates = do

-- The "uiGoal" field is necessary at least to "persist" the data that is needed
-- if the player chooses to later "recall" the goals dialog with CTRL+g.
uiState . uiGoal
uiState
. uiGoal
.= GoalDisplay
newGoalTracking
(GR.makeListWidget newGoalTracking)
Expand Down