diff --git a/src/battle-phases.ts b/src/battle-phases.ts index 6fc91924c..8b95decf5 100644 --- a/src/battle-phases.ts +++ b/src/battle-phases.ts @@ -73,7 +73,7 @@ export class LoginPhase extends BattlePhase { this.scene.playSound('menu_open'); const loadData = () => { - this.scene.gameData.loadSystem().then(() => this.end()); + updateUserInfo().then(() => this.scene.gameData.loadSystem().then(() => this.end())); }; this.scene.ui.setMode(Mode.LOGIN_FORM, { @@ -98,14 +98,25 @@ export class LoginPhase extends BattlePhase { ] }); return null; - } else - this.end(); + } else { + this.scene.gameData.loadSystem().then(success => { + if (success) + this.end(); + else { + this.scene.ui.setMode(Mode.MESSAGE); + this.scene.ui.showText('Failed to load save data. Please reload the page.\nIf this continues, please contact the administrator.'); + } + }); + } }); } end(): void { this.scene.ui.setMode(Mode.MESSAGE); + if (!this.scene.gameData.gender) + this.scene.unshiftPhase(new SelectGenderPhase(this.scene)); + handleTutorial(this.scene, Tutorial.Intro).then(() => super.end()); } } diff --git a/src/battle-scene.ts b/src/battle-scene.ts index db3ded91f..4b8751ab4 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -591,8 +591,6 @@ export default class BattleScene extends Phaser.Scene { this.pushPhase(new LoginPhase(this)); if (!bypassLogin) this.pushPhase(new ConsolidateDataPhase(this)); // TODO: Remove - if (!this.gameData.gender) - this.pushPhase(new SelectGenderPhase(this)); this.pushPhase(new CheckLoadPhase(this)); } else this.pushPhase(new EncounterPhase(this)); diff --git a/src/system/game-data.ts b/src/system/game-data.ts index c65ef9831..5bf2d8e8d 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -189,7 +189,6 @@ export class GameData { }; this.eggs = []; this.initDexData(); - this.loadSystem(); } public saveSystem(): Promise { @@ -242,66 +241,71 @@ export class GameData { public loadSystem(): Promise { return new Promise(resolve => { if (bypassLogin && !localStorage.hasOwnProperty('data')) - return false; + return resolve(false); const handleSystemData = (systemDataStr: string) => { - const systemData = this.parseSystemData(systemDataStr); + try { + const systemData = this.parseSystemData(systemDataStr); - console.debug(systemData); + console.debug(systemData); - /*const versions = [ this.scene.game.config.gameVersion, data.gameVersion || '0.0.0' ]; - - if (versions[0] !== versions[1]) { - const [ versionNumbers, oldVersionNumbers ] = versions.map(ver => ver.split('.').map(v => parseInt(v))); - }*/ + /*const versions = [ this.scene.game.config.gameVersion, data.gameVersion || '0.0.0' ]; + + if (versions[0] !== versions[1]) { + const [ versionNumbers, oldVersionNumbers ] = versions.map(ver => ver.split('.').map(v => parseInt(v))); + }*/ - this.trainerId = systemData.trainerId; - this.secretId = systemData.secretId; + this.trainerId = systemData.trainerId; + this.secretId = systemData.secretId; - this.gender = systemData.gender; + this.gender = systemData.gender; - this.saveSetting(Setting.Player_Gender, systemData.gender === PlayerGender.FEMALE ? 1 : 0); + this.saveSetting(Setting.Player_Gender, systemData.gender === PlayerGender.FEMALE ? 1 : 0); - if (systemData.gameStats) - this.gameStats = systemData.gameStats; + if (systemData.gameStats) + this.gameStats = systemData.gameStats; - if (systemData.unlocks) { - for (let key of Object.keys(systemData.unlocks)) { - if (this.unlocks.hasOwnProperty(key)) - this.unlocks[key] = systemData.unlocks[key]; + if (systemData.unlocks) { + for (let key of Object.keys(systemData.unlocks)) { + if (this.unlocks.hasOwnProperty(key)) + this.unlocks[key] = systemData.unlocks[key]; + } } - } - if (systemData.achvUnlocks) { - for (let a of Object.keys(systemData.achvUnlocks)) { - if (achvs.hasOwnProperty(a)) - this.achvUnlocks[a] = systemData.achvUnlocks[a]; - } - } - - if (systemData.voucherUnlocks) { - for (let v of Object.keys(systemData.voucherUnlocks)) { - if (vouchers.hasOwnProperty(v)) - this.voucherUnlocks[v] = systemData.voucherUnlocks[v]; + if (systemData.achvUnlocks) { + for (let a of Object.keys(systemData.achvUnlocks)) { + if (achvs.hasOwnProperty(a)) + this.achvUnlocks[a] = systemData.achvUnlocks[a]; + } } + + if (systemData.voucherUnlocks) { + for (let v of Object.keys(systemData.voucherUnlocks)) { + if (vouchers.hasOwnProperty(v)) + this.voucherUnlocks[v] = systemData.voucherUnlocks[v]; + } + } + + if (systemData.voucherCounts) { + Utils.getEnumKeys(VoucherType).forEach(key => { + const index = VoucherType[key]; + this.voucherCounts[index] = systemData.voucherCounts[index] || 0; + }); + } + + this.eggs = systemData.eggs + ? systemData.eggs.map(e => e.toEgg()) + : []; + + this.dexData = Object.assign(this.dexData, systemData.dexData); + this.consolidateDexData(this.dexData); + this.defaultDexData = null; + + resolve(true); + } catch (err) { + console.error(err); + resolve(false); } - - if (systemData.voucherCounts) { - Utils.getEnumKeys(VoucherType).forEach(key => { - const index = VoucherType[key]; - this.voucherCounts[index] = systemData.voucherCounts[index] || 0; - }); - } - - this.eggs = systemData.eggs - ? systemData.eggs.map(e => e.toEgg()) - : []; - - this.dexData = Object.assign(this.dexData, systemData.dexData); - this.consolidateDexData(this.dexData); - this.defaultDexData = null; - - resolve(true); } if (!bypassLogin) { diff --git a/src/ui/menu-ui-handler.ts b/src/ui/menu-ui-handler.ts index 3d7e22c38..4b35a81dc 100644 --- a/src/ui/menu-ui-handler.ts +++ b/src/ui/menu-ui-handler.ts @@ -7,6 +7,7 @@ import MessageUiHandler from "./message-ui-handler"; import { GameDataType } from "../system/game-data"; import { OptionSelectConfig } from "./abstact-option-select-ui-handler"; import { Tutorial, handleTutorial } from "../tutorial"; +import { updateUserInfo } from "../account"; export enum MenuOptions { GAME_SETTINGS, @@ -220,7 +221,7 @@ export default class MenuUiHandler extends MessageUiHandler { if (!res.ok) console.error(`Log out failed (${res.status}: ${res.statusText})`); Utils.setCookie(Utils.sessionIdKey, ''); - this.scene.reset(true); + updateUserInfo().then(() => this.scene.reset(true)); }); }; if (this.scene.currentBattle) {