bool SQLitePersister::updateCampaign (Campaign* campaign) { Log::info(LOG_CAMPAIGN, "update campaign progress in database for %s", campaign->getId().c_str()); Transaction t(*this); ExecutionTime e("save progress"); activateCampaign(campaign); SQLiteStatement stmt; prepare(stmt, "INSERT OR REPLACE INTO " TABLE_GAMEMAPS " (campaignid, mapid, locked, time, finishPoints, stars, version) VALUES (?, ?, ?, ?, ?, ?, ?);"); if (!stmt) return false; stmt.bindText(1, campaign->getId()); stmt.bindText(7, Singleton<Application>::getInstance().getName()); const Campaign::MapList& maps = campaign->getMaps(); for (Campaign::MapListConstIter i = maps.begin(); i != maps.end(); ++i) { const CampaignMapPtr& map = *i; saveCampaignMapParameters(map.get(), stmt); stmt.step(true); } const bool savedLives = saveLives(campaign->getLives(), campaign->getId()); if (savedLives) Log::info(LOG_CAMPAIGN, "updated campaign progress in database for %s", campaign->getId().c_str()); getSystem().syncFiles(); return savedLives; }
bool SQLitePersister::deleteMaps (Campaign* campaign) { SQLiteStatement stmt; prepare(stmt, "DELETE FROM " TABLE_GAMEMAPS " WHERE campaignid = ?;"); if (!stmt) return false; stmt.bindText(1, campaign->getId()); stmt.step(); return true; }
bool SQLitePersister::activateCampaign (const std::string& id) { SQLiteStatement stmt; prepare(stmt, "UPDATE " TABLE_GAMESTATE " SET activecampaign = ?, version = ?;"); if (!stmt) return false; stmt.bindText(1, id); stmt.bindText(2, Singleton<Application>::getInstance().getVersion()); stmt.step(); return true; }
bool SQLitePersister::saveLives (uint8_t lives, const std::string& campaignId) { SQLiteStatement stmt; prepare(stmt, "INSERT OR REPLACE INTO " TABLE_LIVES " (campaignid, lives, version) VALUES (?, ?, ?);"); if (!stmt) return false; stmt.bindText(1, campaignId); stmt.bindInt(2, lives); stmt.bindText(3, Singleton<Application>::getInstance().getVersion()); stmt.step(); Log::info(LOG_CAMPAIGN, "update lives in database %i", static_cast<int>(lives)); return true; }
std::string SQLitePersister::loadActiveCampaign () { SQLiteStatement stmt; prepare(stmt, "SELECT activecampaign FROM " TABLE_GAMESTATE); if (!stmt) { return DEFAULT_CAMPAIGN; } const int s = stmt.step(); if (s == SQLITE_ROW) { return stmt.getText(0); } else if (s != SQLITE_DONE) { Log::error(LOG_CAMPAIGN, "error loading activecampaign"); } return DEFAULT_CAMPAIGN; }
uint8_t SQLitePersister::loadLives (const std::string& campaignId) { SQLiteStatement stmt; prepare(stmt, "SELECT lives FROM " TABLE_LIVES " WHERE campaignid = ?;"); if (!stmt) { Log::info(LOG_CAMPAIGN, "no lives entry for %s", campaignId.c_str()); return 0; } stmt.bindText(1, campaignId); const int s = stmt.step(); if (s == SQLITE_ROW) { const int lives = stmt.getInt(0); Log::info(LOG_CAMPAIGN, "got %i lives for campaign %s", lives, campaignId.c_str()); return lives; } else if (s != SQLITE_DONE) { Log::error(LOG_CAMPAIGN, "error loading lives"); } return 0; }
uint8_t SQLitePersister::loadLives (const std::string& campaignId) { SQLiteStatement stmt; prepare(stmt, "SELECT lives FROM " TABLE_LIVES " WHERE campaignid = ?;"); if (!stmt) { info(LOG_STORAGE, "no lives entry for " + campaignId); return 0; } stmt.bindText(1, campaignId); const int s = stmt.step(); if (s == SQLITE_ROW) { const int lives = stmt.getInt(0); info(LOG_STORAGE, "got " + string::toString(lives) + " lives for campaign " + campaignId); return lives; } else if (s != SQLITE_DONE) { error(LOG_STORAGE, "error loading lives"); } return 0; }
bool SQLitePersister::loadCampaign (Campaign* campaign) { _activeCampaign = campaign->getId(); const uint8_t lives = loadLives(campaign->getId()); if (lives == 0) { Log::error(LOG_CAMPAIGN, "no live entry for %s", campaign->getId().c_str()); return false; } campaign->resetMaps(); campaign->setLives(lives); SQLiteStatement stmt; prepare(stmt, "SELECT * FROM " TABLE_GAMEMAPS " WHERE campaignid = ?;"); if (!stmt) return false; stmt.bindText(1, campaign->getId()); for (;;) { const int s = stmt.step(); if (s == SQLITE_ROW) { const std::string mapid = stmt.getText(1); CampaignMap* map = campaign->getMapById(mapid); if (map) { loadCampaignMapParameters(map, stmt); } } else if (s == SQLITE_DONE) { break; } else { Log::error(LOG_CAMPAIGN, "SQL step error in loadCampaign"); return false; } } return campaign->getLives() > 0; }