Add combined save between waves (#777)

This commit is contained in:
Samuel H 2024-05-12 23:01:05 -04:00 committed by GitHub
parent 9a09d790f0
commit 2b9b63e3f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 17 deletions

View File

@ -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();

View File

@ -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<boolean> {
return new Promise<boolean>(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<boolean> {
return new Promise<boolean>(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<boolean> {
return new Promise<boolean>(resolve => {
const dataKey: string = getDataTypeKey(dataType, slotId);