Protect against cross-session overwrites

This commit is contained in:
Flashfyre 2024-04-21 16:19:11 -04:00
parent c6f395d63a
commit 3d9f5fb157
2 changed files with 27 additions and 8 deletions

View File

@ -3420,6 +3420,8 @@ export class GameOverPhase extends BattlePhase {
handleClearSession(): void { handleClearSession(): void {
this.scene.gameData.tryClearSession(this.scene, this.scene.sessionSlotId).then((success: boolean | [boolean, boolean]) => { 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, () => { this.scene.time.delayedCall(1000, () => {
let firstClear = false; let firstClear = false;
if (this.victory && success[1]) { if (this.victory && success[1]) {
@ -4327,7 +4329,7 @@ export class EggLapsePhase extends Phase {
const eggsToHatch: Egg[] = this.scene.gameData.eggs.filter((egg: Egg) => { const eggsToHatch: Egg[] = this.scene.gameData.eggs.filter((egg: Egg) => {
return --egg.hatchWaves < 1 return --egg.hatchWaves < 1
}) });
if (eggsToHatch.length) { if (eggsToHatch.length) {
this.scene.queueMessage('Oh?'); this.scene.queueMessage('Oh?');

View File

@ -1,7 +1,7 @@
import BattleScene, { PokeballCounts, bypassLogin } from "../battle-scene"; import BattleScene, { PokeballCounts, bypassLogin } from "../battle-scene";
import Pokemon, { EnemyPokemon, PlayerPokemon } from "../field/pokemon"; import Pokemon, { EnemyPokemon, PlayerPokemon } from "../field/pokemon";
import { pokemonEvolutions, pokemonPrevolutions } from "../data/pokemon-evolutions"; 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 { Species, defaultStarterSpecies } from "../data/enums/species";
import * as Utils from "../utils"; import * as Utils from "../utils";
import PokemonData from "./pokemon-data"; import PokemonData from "./pokemon-data";
@ -27,7 +27,7 @@ import { Moves } from "../data/enums/moves";
import { speciesEggMoves } from "../data/egg-moves"; import { speciesEggMoves } from "../data/egg-moves";
import { allMoves } from "../data/move"; import { allMoves } from "../data/move";
import { TrainerVariant } from "../field/trainer"; 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"; import { Variant, variantData } from "#app/data/variant";
const saveKey = 'x0i2O7WRiANTqPmZ'; // Temporary; secure encryption is not yet necessary 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 => { Utils.apiFetch(`savedata/delete?datatype=${GameDataType.SESSION}&slot=${slotId}`, true).then(response => {
if (response.ok) { if (response.ok) {
loggedInUser.lastSessionSlot = -1; 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]); return resolve([false, false]);
const sessionData = this.getSessionSaveData(scene); const sessionData = this.getSessionSaveData(scene);
Utils.apiPost(`savedata/clear?slot=${slotId}`, JSON.stringify(sessionData)).then(response => { Utils.apiPost(`savedata/clear?slot=${slotId}`, JSON.stringify(sessionData)).then(response => {
if (response.ok) { if (response.ok)
loggedInUser.lastSessionSlot = -1; 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]); resolve([false, false]);
}).then(jsonResponse => resolve([true, jsonResponse.success as boolean])); });
}); });
}); });
} }