Skip to content

Commit

Permalink
Erase save files properly so that only one save can exist at a time.
Browse files Browse the repository at this point in the history
  • Loading branch information
miki151 committed Jan 23, 2017
1 parent fa76b1b commit 55ef785
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
7 changes: 7 additions & 0 deletions exit_info.h
@@ -0,0 +1,7 @@
#pragma once
#include "util.h"

struct ExitAndQuit {
};

using ExitInfo = variant<ExitAndQuit, GameSaveType>;
19 changes: 10 additions & 9 deletions game.cpp
Expand Up @@ -32,6 +32,7 @@
#include "construction_map.h"
#include "campaign_builder.h"
#include "campaign_type.h"
#include "game_save_type.h"

template <class Archive>
void Game::serialize(Archive& ar, const unsigned int version) {
Expand Down Expand Up @@ -196,7 +197,7 @@ void Game::doneRetirement() {
UniqueEntity<Item>::clearOffset();
}

optional<Game::ExitInfo> Game::update(double timeDiff) {
optional<ExitInfo> Game::update(double timeDiff) {
ScopeTimer timer("Game::update timer");
currentTime += timeDiff;
Model* currentModel = getCurrentModel();
Expand Down Expand Up @@ -229,13 +230,13 @@ void Game::considerRealTimeRender() {
}
}

optional<Game::ExitInfo> Game::updateModel(Model* model, double totalTime) {
optional<ExitInfo> Game::updateModel(Model* model, double totalTime) {
do {
if (spectator)
while (1) {
UserInput input = view->getAction();
if (input.getId() == UserInputId::EXIT)
return ExitInfo{ExitId::QUIT};
return ExitInfo(ExitAndQuit());
if (input.getId() == UserInputId::IDLE)
break;
}
Expand Down Expand Up @@ -305,21 +306,21 @@ void Game::exitAction() {
switch (Action(*ind)) {
case RETIRE:
if (view->yesOrNoPrompt("Retire your dungeon and share it online?")) {
exitInfo = ExitInfo(ExitId::SAVE, GameSaveType::RETIRED_SITE);
exitInfo = ExitInfo(GameSaveType::RETIRED_SITE);
return;
}
break;
case SAVE:
if (!playerControl) {
exitInfo = ExitInfo(ExitId::SAVE, GameSaveType::ADVENTURER);
exitInfo = ExitInfo(GameSaveType::ADVENTURER);
return;
} else {
exitInfo = ExitInfo(ExitId::SAVE, GameSaveType::KEEPER);
exitInfo = ExitInfo(GameSaveType::KEEPER);
return;
}
case ABANDON:
if (view->yesOrNoPrompt("Do you want to abandon your game? This will remove the save file.")) {
exitInfo = ExitInfo(ExitId::QUIT);
if (view->yesOrNoPrompt("Do you want to abandon your game? This is permanent and the save file will be removed!")) {
exitInfo = ExitInfo(ExitAndQuit());
return;
}
break;
Expand Down Expand Up @@ -483,7 +484,7 @@ void Game::gameOver(const Creature* creature, int numKills, const string& enemie
);
highscores->add(score);
highscores->present(view, score);
exitInfo = ExitInfo(ExitId::QUIT);
exitInfo = ExitInfo(ExitAndQuit());
}

Options* Game::getOptions() {
Expand Down
13 changes: 1 addition & 12 deletions game.h
Expand Up @@ -5,6 +5,7 @@
#include "tribe.h"
#include "enum_variant.h"
#include "position.h"
#include "exit_info.h"

class Options;
class Highscores;
Expand All @@ -20,23 +21,11 @@ class Campaign;
class SavedGameInfo;
struct CampaignSetup;

RICH_ENUM(GameSaveType,
ADVENTURER,
KEEPER,
RETIRED_SITE,
AUTOSAVE);

class Game {
public:
static PGame campaignGame(Table<PModel>&&, Vec2 basePos, const CampaignSetup&);
static PGame splashScreen(PModel&&, const CampaignSetup&);

enum class ExitId { SAVE, QUIT };

class ExitInfo : public EnumVariant<ExitId, TYPES(GameSaveType), ASSIGN(GameSaveType, ExitId::SAVE)> {
using EnumVariant::EnumVariant;
};

optional<ExitInfo> update(double timeDiff);
Options* getOptions();
void initialize(Options*, Highscores*, View*, FileSharing*);
Expand Down
7 changes: 7 additions & 0 deletions game_save_type.h
@@ -0,0 +1,7 @@
#pragma once

RICH_ENUM(GameSaveType,
ADVENTURER,
KEEPER,
RETIRED_SITE,
AUTOSAVE);
45 changes: 27 additions & 18 deletions main_loop.cpp
Expand Up @@ -27,6 +27,8 @@
#include "campaign_builder.h"
#include "player_role.h"
#include "campaign_type.h"
#include "game_save_type.h"
#include "exit_info.h"

MainLoop::MainLoop(View* v, Highscores* h, FileSharing* fSharing, const string& freePath,
const string& uPath, Options* o, Jukebox* j, SokobanInput* soko, std::atomic<bool>& fin, bool singleThread,
Expand Down Expand Up @@ -159,7 +161,7 @@ void MainLoop::uploadFile(const string& path, GameSaveType type) {
view->presentText("Error uploading file", *error);
}

string MainLoop::getSavePath(PGame& game, GameSaveType gameType) {
string MainLoop::getSavePath(const PGame& game, GameSaveType gameType) {
return userPath + "/" + stripFilename(game->getGameIdentifier()) + getSaveSuffix(gameType);
}

Expand All @@ -185,7 +187,7 @@ void MainLoop::saveUI(PGame& game, GameSaveType type, SplashType splashType) {
uploadFile(path, type);
}

void MainLoop::eraseSaveFile(PGame& game, GameSaveType type) {
void MainLoop::eraseSaveFile(const PGame& game, GameSaveType type) {
remove(getSavePath(game, type).c_str());
}

Expand Down Expand Up @@ -240,21 +242,20 @@ void MainLoop::playGame(PGame&& game, bool withMusic, bool noAutoSave) {
}
INFO << "Time step " << step;
if (auto exitInfo = game->update(step)) {
if (exitInfo->getId() == Game::ExitId::QUIT && eraseSave()) {
eraseSaveFile(game, GameSaveType::KEEPER);
eraseSaveFile(game, GameSaveType::ADVENTURER);
eraseSaveFile(game, GameSaveType::AUTOSAVE);
}
if (exitInfo->getId() == Game::ExitId::SAVE) {
bool retired = false;
if (exitInfo->get<GameSaveType>() == GameSaveType::RETIRED_SITE) {
game->prepareSiteRetirement();
retired = true;
}
saveUI(game, exitInfo->get<GameSaveType>(), SplashType::BIG);
if (retired)
game->doneRetirement();
}
apply_visitor(*exitInfo, makeVisitor<void>(
[&](GameSaveType type) {
if (type == GameSaveType::RETIRED_SITE) {
game->prepareSiteRetirement();
saveUI(game, type, SplashType::BIG);
game->doneRetirement();
} else
saveUI(game, type, SplashType::BIG);
eraseAllSavesExcept(game, type);
},
[&](ExitAndQuit) {
eraseAllSavesExcept(game, none);
}
));
return;
}
double gameTime = game->getGlobalTime();
Expand All @@ -263,15 +264,23 @@ void MainLoop::playGame(PGame&& game, bool withMusic, bool noAutoSave) {
lastMusicUpdate = gameTime;
}
if (lastAutoSave < gameTime - getAutosaveFreq() && !noAutoSave) {
if (options->getBoolValue(OptionId::AUTOSAVE))
if (options->getBoolValue(OptionId::AUTOSAVE)) {
saveUI(game, GameSaveType::AUTOSAVE, SplashType::AUTOSAVING);
eraseAllSavesExcept(game, GameSaveType::AUTOSAVE);
}
lastAutoSave = gameTime;
}
if (useSingleThread)
view->refreshView();
}
}

void MainLoop::eraseAllSavesExcept(const PGame& game, optional<GameSaveType> except) {
for (auto erasedType : ENUM_ALL(GameSaveType))
if (erasedType != except)
eraseSaveFile(game, erasedType);
}

RetiredGames MainLoop::getRetiredGames() {
RetiredGames ret;
for (auto& info : getSaveFiles(userPath, getSaveSuffix(GameSaveType::RETIRED_SITE)))
Expand Down
6 changes: 4 additions & 2 deletions main_loop.h
Expand Up @@ -2,6 +2,7 @@

#include "util.h"
#include "file_sharing.h"
#include "exit_info.h"

class View;
class Highscores;
Expand Down Expand Up @@ -59,8 +60,8 @@ class MainLoop {
PModel quickGame(RandomGen& random);
PGame loadGame(string file);
PGame loadPrevious();
string getSavePath(PGame&, GameSaveType);
void eraseSaveFile(PGame&, GameSaveType);
string getSavePath(const PGame&, GameSaveType);
void eraseSaveFile(const PGame&, GameSaveType);

bool downloadGame(const string& filename);
bool eraseSave();
Expand All @@ -80,6 +81,7 @@ class MainLoop {
PModel getBaseModel(ModelBuilder&, CampaignSetup&);
void considerGameEventsPrompt();
void considerFreeVersionText(bool tilesPresent);
void eraseAllSavesExcept(const PGame&, optional<GameSaveType>);
};


Expand Down

0 comments on commit 55ef785

Please sign in to comment.