From 3d9f5fb157a4265ef093343b7726d036b25393c4 Mon Sep 17 00:00:00 2001 From: Flashfyre Date: Sun, 21 Apr 2024 16:19:11 -0400 Subject: [PATCH] Protect against cross-session overwrites --- src/phases.ts | 4 +++- src/system/game-data.ts | 31 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/phases.ts b/src/phases.ts index 3399838cb63..31f631bf4a6 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -3420,6 +3420,8 @@ export class GameOverPhase extends BattlePhase { handleClearSession(): void { this.scene.gameData.tryClearSession(this.scene, this.scene.sessionSlotId).then((success: boolean | [boolean, boolean]) => { + if (!success[0]) + return this.scene.reset(true); this.scene.time.delayedCall(1000, () => { let firstClear = false; if (this.victory && success[1]) { @@ -4327,7 +4329,7 @@ export class EggLapsePhase extends Phase { const eggsToHatch: Egg[] = this.scene.gameData.eggs.filter((egg: Egg) => { return --egg.hatchWaves < 1 - }) + }); if (eggsToHatch.length) { this.scene.queueMessage('Oh?'); diff --git a/src/system/game-data.ts b/src/system/game-data.ts index 491042777b7..c5b9f32cf9c 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -1,7 +1,7 @@ import BattleScene, { PokeballCounts, bypassLogin } from "../battle-scene"; import Pokemon, { EnemyPokemon, PlayerPokemon } from "../field/pokemon"; import { pokemonEvolutions, pokemonPrevolutions } from "../data/pokemon-evolutions"; -import PokemonSpecies, { SpeciesFormKey, allSpecies, getPokemonSpecies, noStarterFormKeys, speciesStarters } from "../data/pokemon-species"; +import PokemonSpecies, { allSpecies, getPokemonSpecies, noStarterFormKeys, speciesStarters } from "../data/pokemon-species"; import { Species, defaultStarterSpecies } from "../data/enums/species"; import * as Utils from "../utils"; import PokemonData from "./pokemon-data"; @@ -27,7 +27,7 @@ import { Moves } from "../data/enums/moves"; import { speciesEggMoves } from "../data/egg-moves"; import { allMoves } from "../data/move"; import { TrainerVariant } from "../field/trainer"; -import { OutdatedPhase, ReloadSessionPhase, UnavailablePhase } from "#app/phases"; +import { OutdatedPhase, ReloadSessionPhase } from "#app/phases"; import { Variant, variantData } from "#app/data/variant"; const saveKey = 'x0i2O7WRiANTqPmZ'; // Temporary; secure encryption is not yet necessary @@ -722,9 +722,19 @@ export class GameData { Utils.apiFetch(`savedata/delete?datatype=${GameDataType.SESSION}&slot=${slotId}`, true).then(response => { if (response.ok) { loggedInUser.lastSessionSlot = -1; - return resolve(true); + resolve(true); } - resolve(false); + return response.text(); + }).then(error => { + if (error) { + if (error.startsWith('session out of date')) { + this.scene.clearPhaseQueue(); + this.scene.unshiftPhase(new ReloadSessionPhase(this.scene)); + } + console.error(error); + resolve(false); + } + resolve(true); }); }); }); @@ -742,12 +752,19 @@ export class GameData { return resolve([false, false]); const sessionData = this.getSessionSaveData(scene); Utils.apiPost(`savedata/clear?slot=${slotId}`, JSON.stringify(sessionData)).then(response => { - if (response.ok) { + if (response.ok) loggedInUser.lastSessionSlot = -1; - return response.json(); + return response.json(); + }).then(jsonResponse => { + if (!jsonResponse.error) + return resolve([true, jsonResponse.success as boolean]); + if (jsonResponse && jsonResponse.error.startsWith('session out of date')) { + this.scene.clearPhaseQueue(); + this.scene.unshiftPhase(new ReloadSessionPhase(this.scene)); } + console.error(jsonResponse); resolve([false, false]); - }).then(jsonResponse => resolve([true, jsonResponse.success as boolean])); + }); }); }); }