diff --git a/src/phases.ts b/src/phases.ts index d8ce55e95..271e5350c 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -775,11 +775,11 @@ export class EncounterPhase extends BattlePhase { this.scene.ui.setMode(Mode.MESSAGE).then(() => { if (!this.loaded) { - this.scene.gameData.saveSystem().then(success => { + this.scene.gameData.saveAll(this.scene, true).then(success => { this.scene.disableMenu = false; if (!success) return this.scene.reset(true); - this.scene.gameData.saveSession(this.scene, true).then(() => this.doEncounter()); + this.doEncounter(); }); } else this.doEncounter(); diff --git a/src/system/game-data.ts b/src/system/game-data.ts index a213a3549..5fc564626 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -250,24 +250,28 @@ export class GameData { this.initStarterData(); } + public getSystemSaveData(): SystemSaveData { + return { + trainerId: this.trainerId, + secretId: this.secretId, + gender: this.gender, + dexData: this.dexData, + starterData: this.starterData, + gameStats: this.gameStats, + unlocks: this.unlocks, + achvUnlocks: this.achvUnlocks, + voucherUnlocks: this.voucherUnlocks, + voucherCounts: this.voucherCounts, + eggs: this.eggs.map(e => new EggData(e)), + gameVersion: this.scene.game.config.gameVersion, + timestamp: new Date().getTime() + }; + } + public saveSystem(): Promise { return new Promise(resolve => { this.scene.ui.savingIcon.show(); - const data: SystemSaveData = { - trainerId: this.trainerId, - secretId: this.secretId, - gender: this.gender, - dexData: this.dexData, - starterData: this.starterData, - gameStats: this.gameStats, - unlocks: this.unlocks, - achvUnlocks: this.achvUnlocks, - voucherUnlocks: this.voucherUnlocks, - voucherCounts: this.voucherCounts, - eggs: this.eggs.map(e => new EggData(e)), - gameVersion: this.scene.game.config.gameVersion, - timestamp: new Date().getTime() - }; + const data = this.getSystemSaveData(); const maxIntAttrValue = Math.pow(2, 31); const systemData = JSON.stringify(data, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v); @@ -817,6 +821,59 @@ export class GameData { }) as SessionSaveData; } + saveAll(scene: BattleScene, skipVerification?: boolean): Promise { + return new Promise(resolve => { + Utils.executeIf(!skipVerification, updateUserInfo).then(success => { + if (success !== null && !success) + return resolve(false); + this.scene.ui.savingIcon.show(); + const data = this.getSystemSaveData(); + const sessionData = this.getSessionSaveData(scene); + + const maxIntAttrValue = Math.pow(2, 31); + const systemData = this.getSystemSaveData(); + + const request = { + system: systemData, + session: sessionData, + sessionSlotId: scene.sessionSlotId + }; + + if (!bypassLogin) { + Utils.apiPost('savedata/updateall', JSON.stringify(request, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v), undefined, true) + .then(response => response.text()) + .then(error => { + this.scene.ui.savingIcon.hide(); + if (error) { + if (error.startsWith('client version out of date')) { + this.scene.clearPhaseQueue(); + this.scene.unshiftPhase(new OutdatedPhase(this.scene)); + } else if (error.startsWith('session out of date')) { + this.scene.clearPhaseQueue(); + this.scene.unshiftPhase(new ReloadSessionPhase(this.scene)); + } + console.error(error); + return resolve(false); + } + resolve(true); + }); + } else { + localStorage.setItem('data_bak', localStorage.getItem('data')); + + localStorage.setItem('data', btoa(JSON.stringify(systemData, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v))); + + localStorage.setItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}`, btoa(JSON.stringify(sessionData))); + + console.debug('Session data saved'); + + this.scene.ui.savingIcon.hide(); + + resolve(true); + } + }); + }); + } + public tryExportData(dataType: GameDataType, slotId: integer = 0): Promise { return new Promise(resolve => { const dataKey: string = getDataTypeKey(dataType, slotId);