Implement client session and re-implement hybrid saving (#888)
* Implement client session and re-implement hybrid saving (WiP) * Fixes for hybrid saving * Add local data clears where applicable * Include client session ID in system update * Change save threshold from 5 waves to 10 waves or 5 minutespull/895/head
parent
835b00d457
commit
67c18a15e2
|
@ -7,13 +7,14 @@ export interface UserInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
export let loggedInUser: UserInfo = null;
|
export let loggedInUser: UserInfo = null;
|
||||||
|
export const clientSessionId = Utils.randomString(32);
|
||||||
|
|
||||||
export function updateUserInfo(): Promise<[boolean, integer]> {
|
export function updateUserInfo(): Promise<[boolean, integer]> {
|
||||||
return new Promise<[boolean, integer]>(resolve => {
|
return new Promise<[boolean, integer]>(resolve => {
|
||||||
if (bypassLogin) {
|
if (bypassLogin) {
|
||||||
let lastSessionSlot = -1;
|
let lastSessionSlot = -1;
|
||||||
for (let s = 0; s < 2; s++) {
|
for (let s = 0; s < 2; s++) {
|
||||||
if (localStorage.getItem(`sessionData${s ? s : ''}`)) {
|
if (localStorage.getItem(`sessionData${s ? s : ''}_${loggedInUser.username}`)) {
|
||||||
lastSessionSlot = s;
|
lastSessionSlot = s;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,6 +88,7 @@ export default class BattleScene extends SceneBase {
|
||||||
public uiInputs: UiInputs;
|
public uiInputs: UiInputs;
|
||||||
|
|
||||||
public sessionPlayTime: integer = null;
|
public sessionPlayTime: integer = null;
|
||||||
|
public lastSavePlayTime: integer = null;
|
||||||
public masterVolume: number = 0.5;
|
public masterVolume: number = 0.5;
|
||||||
public bgmVolume: number = 1;
|
public bgmVolume: number = 1;
|
||||||
public seVolume: number = 1;
|
public seVolume: number = 1;
|
||||||
|
@ -452,6 +453,8 @@ export default class BattleScene extends SceneBase {
|
||||||
initSession(): void {
|
initSession(): void {
|
||||||
if (this.sessionPlayTime === null)
|
if (this.sessionPlayTime === null)
|
||||||
this.sessionPlayTime = 0;
|
this.sessionPlayTime = 0;
|
||||||
|
if (this.lastSavePlayTime === null)
|
||||||
|
this.lastSavePlayTime = 0;
|
||||||
|
|
||||||
if (this.playTimeTimer)
|
if (this.playTimeTimer)
|
||||||
this.playTimeTimer.destroy();
|
this.playTimeTimer.destroy();
|
||||||
|
@ -464,6 +467,8 @@ export default class BattleScene extends SceneBase {
|
||||||
this.gameData.gameStats.playTime++;
|
this.gameData.gameStats.playTime++;
|
||||||
if (this.sessionPlayTime !== null)
|
if (this.sessionPlayTime !== null)
|
||||||
this.sessionPlayTime++;
|
this.sessionPlayTime++;
|
||||||
|
if (this.lastSavePlayTime !== null)
|
||||||
|
this.lastSavePlayTime++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -330,6 +330,7 @@ export class TitlePhase extends Phase {
|
||||||
this.scene.newBattle();
|
this.scene.newBattle();
|
||||||
this.scene.arena.init();
|
this.scene.arena.init();
|
||||||
this.scene.sessionPlayTime = 0;
|
this.scene.sessionPlayTime = 0;
|
||||||
|
this.scene.lastSavePlayTime = 0;
|
||||||
this.end();
|
this.end();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -393,8 +394,12 @@ export class UnavailablePhase extends Phase {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ReloadSessionPhase extends Phase {
|
export class ReloadSessionPhase extends Phase {
|
||||||
constructor(scene: BattleScene) {
|
private systemDataStr: string;
|
||||||
|
|
||||||
|
constructor(scene: BattleScene, systemDataStr?: string) {
|
||||||
super(scene);
|
super(scene);
|
||||||
|
|
||||||
|
this.systemDataStr = systemDataStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
start(): void {
|
start(): void {
|
||||||
|
@ -410,7 +415,9 @@ export class ReloadSessionPhase extends Phase {
|
||||||
delayElapsed = true;
|
delayElapsed = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.scene.gameData.loadSystem().then(() => {
|
this.scene.gameData.clearLocalData();
|
||||||
|
|
||||||
|
(this.systemDataStr ? this.scene.gameData.initSystem(this.systemDataStr) : this.scene.gameData.loadSystem()).then(() => {
|
||||||
if (delayElapsed)
|
if (delayElapsed)
|
||||||
this.end();
|
this.end();
|
||||||
else
|
else
|
||||||
|
@ -531,6 +538,7 @@ export class SelectStarterPhase extends Phase {
|
||||||
this.scene.newBattle();
|
this.scene.newBattle();
|
||||||
this.scene.arena.init();
|
this.scene.arena.init();
|
||||||
this.scene.sessionPlayTime = 0;
|
this.scene.sessionPlayTime = 0;
|
||||||
|
this.scene.lastSavePlayTime = 0;
|
||||||
this.end();
|
this.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -784,7 +792,7 @@ export class EncounterPhase extends BattlePhase {
|
||||||
|
|
||||||
this.scene.ui.setMode(Mode.MESSAGE).then(() => {
|
this.scene.ui.setMode(Mode.MESSAGE).then(() => {
|
||||||
if (!this.loaded) {
|
if (!this.loaded) {
|
||||||
this.scene.gameData.saveAll(this.scene, true).then(success => {
|
this.scene.gameData.saveAll(this.scene, true, battle.waveIndex % 10 === 1 || this.scene.lastSavePlayTime >= 10).then(success => {
|
||||||
this.scene.disableMenu = false;
|
this.scene.disableMenu = false;
|
||||||
if (!success)
|
if (!success)
|
||||||
return this.scene.reset(true);
|
return this.scene.reset(true);
|
||||||
|
@ -3692,7 +3700,7 @@ export class PostGameOverPhase extends Phase {
|
||||||
start() {
|
start() {
|
||||||
super.start();
|
super.start();
|
||||||
|
|
||||||
this.scene.gameData.saveSystem().then(success => {
|
this.scene.gameData.saveAll(this.scene, true, true, true).then(success => {
|
||||||
if (!success)
|
if (!success)
|
||||||
return this.scene.reset(true);
|
return this.scene.reset(true);
|
||||||
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]) => {
|
||||||
|
|
|
@ -20,7 +20,7 @@ import { Egg } from "../data/egg";
|
||||||
import { VoucherType, vouchers } from "./voucher";
|
import { VoucherType, vouchers } from "./voucher";
|
||||||
import { AES, enc } from "crypto-js";
|
import { AES, enc } from "crypto-js";
|
||||||
import { Mode } from "../ui/ui";
|
import { Mode } from "../ui/ui";
|
||||||
import { loggedInUser, updateUserInfo } from "../account";
|
import { clientSessionId, loggedInUser, updateUserInfo } from "../account";
|
||||||
import { Nature } from "../data/nature";
|
import { Nature } from "../data/nature";
|
||||||
import { GameStats } from "./game-stats";
|
import { GameStats } from "./game-stats";
|
||||||
import { Tutorial } from "../tutorial";
|
import { Tutorial } from "../tutorial";
|
||||||
|
@ -67,6 +67,18 @@ export function getDataTypeKey(dataType: GameDataType, slotId: integer = 0): str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function encrypt(data: string, bypassLogin: boolean): string {
|
||||||
|
return (bypassLogin
|
||||||
|
? (data: string) => btoa(data)
|
||||||
|
: (data: string) => AES.encrypt(data, saveKey))(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function decrypt(data: string, bypassLogin: boolean): string {
|
||||||
|
return (bypassLogin
|
||||||
|
? (data: string) => atob(data)
|
||||||
|
: (data: string) => AES.decrypt(data, saveKey).toString(enc.Utf8))(data);
|
||||||
|
}
|
||||||
|
|
||||||
interface SystemSaveData {
|
interface SystemSaveData {
|
||||||
trainerId: integer;
|
trainerId: integer;
|
||||||
secretId: integer;
|
secretId: integer;
|
||||||
|
@ -277,8 +289,10 @@ export class GameData {
|
||||||
const maxIntAttrValue = Math.pow(2, 31);
|
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);
|
const systemData = JSON.stringify(data, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v);
|
||||||
|
|
||||||
|
localStorage.setItem(`data_${loggedInUser.username}`, encrypt(systemData, bypassLogin));
|
||||||
|
|
||||||
if (!bypassLogin) {
|
if (!bypassLogin) {
|
||||||
Utils.apiPost(`savedata/update?datatype=${GameDataType.SYSTEM}`, systemData, undefined, true)
|
Utils.apiPost(`savedata/update?datatype=${GameDataType.SYSTEM}&clientSessionId=${clientSessionId}`, systemData, undefined, true)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(error => {
|
.then(error => {
|
||||||
this.scene.ui.savingIcon.hide();
|
this.scene.ui.savingIcon.hide();
|
||||||
|
@ -296,10 +310,6 @@ export class GameData {
|
||||||
resolve(true);
|
resolve(true);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
localStorage.setItem('data_bak', localStorage.getItem('data'));
|
|
||||||
|
|
||||||
localStorage.setItem('data', btoa(systemData));
|
|
||||||
|
|
||||||
this.scene.ui.savingIcon.hide();
|
this.scene.ui.savingIcon.hide();
|
||||||
|
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
@ -309,120 +319,13 @@ export class GameData {
|
||||||
|
|
||||||
public loadSystem(): Promise<boolean> {
|
public loadSystem(): Promise<boolean> {
|
||||||
return new Promise<boolean>(resolve => {
|
return new Promise<boolean>(resolve => {
|
||||||
if (bypassLogin && !localStorage.hasOwnProperty('data'))
|
console.log('Client Session:', clientSessionId);
|
||||||
|
|
||||||
|
if (bypassLogin && !localStorage.getItem(`data_${loggedInUser.username}`))
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
|
|
||||||
const handleSystemData = (systemDataStr: string) => {
|
|
||||||
try {
|
|
||||||
const systemData = this.parseSystemData(systemDataStr);
|
|
||||||
|
|
||||||
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)));
|
|
||||||
}*/
|
|
||||||
|
|
||||||
this.trainerId = systemData.trainerId;
|
|
||||||
this.secretId = systemData.secretId;
|
|
||||||
|
|
||||||
this.gender = systemData.gender;
|
|
||||||
|
|
||||||
this.saveSetting(Setting.Player_Gender, systemData.gender === PlayerGender.FEMALE ? 1 : 0);
|
|
||||||
|
|
||||||
const initStarterData = !systemData.starterData;
|
|
||||||
|
|
||||||
if (initStarterData) {
|
|
||||||
this.initStarterData();
|
|
||||||
|
|
||||||
if (systemData['starterMoveData']) {
|
|
||||||
const starterMoveData = systemData['starterMoveData'];
|
|
||||||
for (let s of Object.keys(starterMoveData))
|
|
||||||
this.starterData[s].moveset = starterMoveData[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (systemData['starterEggMoveData']) {
|
|
||||||
const starterEggMoveData = systemData['starterEggMoveData'];
|
|
||||||
for (let s of Object.keys(starterEggMoveData))
|
|
||||||
this.starterData[s].eggMoves = starterEggMoveData[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
this.migrateStarterAbilities(systemData, this.starterData);
|
|
||||||
} else {
|
|
||||||
if ([ '1.0.0', '1.0.1' ].includes(systemData.gameVersion))
|
|
||||||
this.migrateStarterAbilities(systemData);
|
|
||||||
//this.fixVariantData(systemData);
|
|
||||||
this.fixStarterData(systemData);
|
|
||||||
// Migrate ability starter data if empty for caught species
|
|
||||||
Object.keys(systemData.starterData).forEach(sd => {
|
|
||||||
if (systemData.dexData[sd].caughtAttr && !systemData.starterData[sd].abilityAttr)
|
|
||||||
systemData.starterData[sd].abilityAttr = 1;
|
|
||||||
});
|
|
||||||
this.starterData = systemData.starterData;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (systemData.gameStats) {
|
|
||||||
if (systemData.gameStats.legendaryPokemonCaught !== undefined && systemData.gameStats.subLegendaryPokemonCaught === undefined)
|
|
||||||
this.fixLegendaryStats(systemData);
|
|
||||||
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.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;
|
|
||||||
|
|
||||||
if (initStarterData) {
|
|
||||||
const starterIds = Object.keys(this.starterData).map(s => parseInt(s) as Species);
|
|
||||||
for (let s of starterIds) {
|
|
||||||
this.starterData[s].candyCount += this.dexData[s].caughtCount;
|
|
||||||
this.starterData[s].candyCount += this.dexData[s].hatchedCount * 2;
|
|
||||||
if (this.dexData[s].caughtAttr & DexAttr.SHINY)
|
|
||||||
this.starterData[s].candyCount += 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(true);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
resolve(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bypassLogin) {
|
if (!bypassLogin) {
|
||||||
Utils.apiFetch(`savedata/get?datatype=${GameDataType.SYSTEM}`, true)
|
Utils.apiFetch(`savedata/system?clientSessionId=${clientSessionId}`, true)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (!response.length || response[0] !== '{') {
|
if (!response.length || response[0] !== '{') {
|
||||||
|
@ -437,10 +340,132 @@ export class GameData {
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSystemData(response);
|
const cachedSystem = localStorage.getItem(`data_${loggedInUser.username}`);
|
||||||
|
this.initSystem(response, cachedSystem ? AES.decrypt(cachedSystem, saveKey).toString(enc.Utf8) : null).then(resolve);
|
||||||
});
|
});
|
||||||
} else
|
} else
|
||||||
handleSystemData(atob(localStorage.getItem('data')));
|
this.initSystem(decrypt(localStorage.getItem(`data_${loggedInUser.username}`), bypassLogin)).then(resolve);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public initSystem(systemDataStr: string, cachedSystemDataStr?: string): Promise<boolean> {
|
||||||
|
return new Promise<boolean>(resolve => {
|
||||||
|
try {
|
||||||
|
let systemData = this.parseSystemData(systemDataStr);
|
||||||
|
|
||||||
|
if (cachedSystemDataStr) {
|
||||||
|
let cachedSystemData = this.parseSystemData(cachedSystemDataStr);
|
||||||
|
console.log(cachedSystemData.timestamp, systemData.timestamp)
|
||||||
|
if (cachedSystemData.timestamp > systemData.timestamp) {
|
||||||
|
console.debug('Use cached system');
|
||||||
|
systemData = cachedSystemData;
|
||||||
|
} else
|
||||||
|
this.clearLocalData();
|
||||||
|
}
|
||||||
|
|
||||||
|
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)));
|
||||||
|
}*/
|
||||||
|
|
||||||
|
this.trainerId = systemData.trainerId;
|
||||||
|
this.secretId = systemData.secretId;
|
||||||
|
|
||||||
|
this.gender = systemData.gender;
|
||||||
|
|
||||||
|
this.saveSetting(Setting.Player_Gender, systemData.gender === PlayerGender.FEMALE ? 1 : 0);
|
||||||
|
|
||||||
|
const initStarterData = !systemData.starterData;
|
||||||
|
|
||||||
|
if (initStarterData) {
|
||||||
|
this.initStarterData();
|
||||||
|
|
||||||
|
if (systemData['starterMoveData']) {
|
||||||
|
const starterMoveData = systemData['starterMoveData'];
|
||||||
|
for (let s of Object.keys(starterMoveData))
|
||||||
|
this.starterData[s].moveset = starterMoveData[s];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (systemData['starterEggMoveData']) {
|
||||||
|
const starterEggMoveData = systemData['starterEggMoveData'];
|
||||||
|
for (let s of Object.keys(starterEggMoveData))
|
||||||
|
this.starterData[s].eggMoves = starterEggMoveData[s];
|
||||||
|
}
|
||||||
|
|
||||||
|
this.migrateStarterAbilities(systemData, this.starterData);
|
||||||
|
} else {
|
||||||
|
if ([ '1.0.0', '1.0.1' ].includes(systemData.gameVersion))
|
||||||
|
this.migrateStarterAbilities(systemData);
|
||||||
|
//this.fixVariantData(systemData);
|
||||||
|
this.fixStarterData(systemData);
|
||||||
|
// Migrate ability starter data if empty for caught species
|
||||||
|
Object.keys(systemData.starterData).forEach(sd => {
|
||||||
|
if (systemData.dexData[sd].caughtAttr && !systemData.starterData[sd].abilityAttr)
|
||||||
|
systemData.starterData[sd].abilityAttr = 1;
|
||||||
|
});
|
||||||
|
this.starterData = systemData.starterData;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (systemData.gameStats) {
|
||||||
|
if (systemData.gameStats.legendaryPokemonCaught !== undefined && systemData.gameStats.subLegendaryPokemonCaught === undefined)
|
||||||
|
this.fixLegendaryStats(systemData);
|
||||||
|
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.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;
|
||||||
|
|
||||||
|
if (initStarterData) {
|
||||||
|
const starterIds = Object.keys(this.starterData).map(s => parseInt(s) as Species);
|
||||||
|
for (let s of starterIds) {
|
||||||
|
this.starterData[s].candyCount += this.dexData[s].caughtCount;
|
||||||
|
this.starterData[s].candyCount += this.dexData[s].hatchedCount * 2;
|
||||||
|
if (this.dexData[s].caughtAttr & DexAttr.SHINY)
|
||||||
|
this.starterData[s].candyCount += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(true);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
resolve(false);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -474,6 +499,29 @@ export class GameData {
|
||||||
return dataStr;
|
return dataStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async verify(): Promise<boolean> {
|
||||||
|
if (bypassLogin)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
const response = await Utils.apiPost(`savedata/system/verify`, JSON.stringify({ clientSessionId: clientSessionId }), undefined, true)
|
||||||
|
.then(response => response.json());
|
||||||
|
|
||||||
|
if (!response.valid) {
|
||||||
|
this.scene.clearPhaseQueue();
|
||||||
|
this.scene.unshiftPhase(new ReloadSessionPhase(this.scene, JSON.stringify(response.systemData)));
|
||||||
|
this.clearLocalData();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public clearLocalData(): void {
|
||||||
|
localStorage.removeItem(`data_${loggedInUser.username}`);
|
||||||
|
for (let s = 0; s < 5; s++)
|
||||||
|
localStorage.removeItem(`sessionData${s ? s : ''}_${loggedInUser.username}`);
|
||||||
|
}
|
||||||
|
|
||||||
public saveSetting(setting: Setting, valueIndex: integer): boolean {
|
public saveSetting(setting: Setting, valueIndex: integer): boolean {
|
||||||
let settings: object = {};
|
let settings: object = {};
|
||||||
if (localStorage.hasOwnProperty('settings'))
|
if (localStorage.hasOwnProperty('settings'))
|
||||||
|
@ -557,40 +605,6 @@ export class GameData {
|
||||||
} as SessionSaveData;
|
} as SessionSaveData;
|
||||||
}
|
}
|
||||||
|
|
||||||
saveSession(scene: BattleScene, skipVerification?: boolean): Promise<boolean> {
|
|
||||||
return new Promise<boolean>(resolve => {
|
|
||||||
Utils.executeIf(!skipVerification, updateUserInfo).then(success => {
|
|
||||||
if (success !== null && !success)
|
|
||||||
return resolve(false);
|
|
||||||
|
|
||||||
const sessionData = this.getSessionSaveData(scene);
|
|
||||||
|
|
||||||
if (!bypassLogin) {
|
|
||||||
Utils.apiPost(`savedata/update?datatype=${GameDataType.SESSION}&slot=${scene.sessionSlotId}&trainerId=${this.trainerId}&secretId=${this.secretId}`, JSON.stringify(sessionData), undefined, true)
|
|
||||||
.then(response => 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);
|
|
||||||
return resolve(false);
|
|
||||||
}
|
|
||||||
console.debug('Session data saved');
|
|
||||||
resolve(true);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
localStorage.setItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}`, btoa(JSON.stringify(sessionData)));
|
|
||||||
|
|
||||||
console.debug('Session data saved');
|
|
||||||
|
|
||||||
resolve(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
getSession(slotId: integer): Promise<SessionSaveData> {
|
getSession(slotId: integer): Promise<SessionSaveData> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
if (slotId < 0)
|
if (slotId < 0)
|
||||||
|
@ -605,8 +619,8 @@ export class GameData {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!bypassLogin) {
|
if (!bypassLogin && !localStorage.getItem(`sessionData${slotId ? slotId : ''}_${loggedInUser.username}`)) {
|
||||||
Utils.apiFetch(`savedata/get?datatype=${GameDataType.SESSION}&slot=${slotId}`, true)
|
Utils.apiFetch(`savedata/session?slot=${slotId}&clientSessionId=${clientSessionId}`, true)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(async response => {
|
.then(async response => {
|
||||||
if (!response.length || response[0] !== '{') {
|
if (!response.length || response[0] !== '{') {
|
||||||
|
@ -614,12 +628,14 @@ export class GameData {
|
||||||
return resolve(null);
|
return resolve(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
localStorage.setItem(`sessionData${slotId ? slotId : ''}_${loggedInUser.username}`, encrypt(response, bypassLogin));
|
||||||
|
|
||||||
await handleSessionData(response);
|
await handleSessionData(response);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const sessionData = localStorage.getItem(`sessionData${slotId ? slotId : ''}`);
|
const sessionData = localStorage.getItem(`sessionData${slotId ? slotId : ''}_${loggedInUser.username}`);
|
||||||
if (sessionData)
|
if (sessionData)
|
||||||
await handleSessionData(atob(sessionData));
|
await handleSessionData(decrypt(sessionData, bypassLogin));
|
||||||
else
|
else
|
||||||
return resolve(null);
|
return resolve(null);
|
||||||
}
|
}
|
||||||
|
@ -640,6 +656,7 @@ export class GameData {
|
||||||
console.log('Seed:', scene.seed);
|
console.log('Seed:', scene.seed);
|
||||||
|
|
||||||
scene.sessionPlayTime = sessionData.playTime || 0;
|
scene.sessionPlayTime = sessionData.playTime || 0;
|
||||||
|
scene.lastSavePlayTime = 0;
|
||||||
|
|
||||||
const loadPokemonAssets: Promise<void>[] = [];
|
const loadPokemonAssets: Promise<void>[] = [];
|
||||||
|
|
||||||
|
@ -731,16 +748,17 @@ export class GameData {
|
||||||
deleteSession(slotId: integer): Promise<boolean> {
|
deleteSession(slotId: integer): Promise<boolean> {
|
||||||
return new Promise<boolean>(resolve => {
|
return new Promise<boolean>(resolve => {
|
||||||
if (bypassLogin) {
|
if (bypassLogin) {
|
||||||
localStorage.removeItem('sessionData');
|
localStorage.removeItem(`sessionData${this.scene.sessionSlotId ? this.scene.sessionSlotId : ''}_${loggedInUser.username}`);
|
||||||
return resolve(true);
|
return resolve(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUserInfo().then(success => {
|
updateUserInfo().then(success => {
|
||||||
if (success !== null && !success)
|
if (success !== null && !success)
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
Utils.apiFetch(`savedata/delete?datatype=${GameDataType.SESSION}&slot=${slotId}`, true).then(response => {
|
Utils.apiFetch(`savedata/delete?datatype=${GameDataType.SESSION}&slot=${slotId}&clientSessionId=${clientSessionId}`, true).then(response => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
loggedInUser.lastSessionSlot = -1;
|
loggedInUser.lastSessionSlot = -1;
|
||||||
|
localStorage.removeItem(`sessionData${this.scene.sessionSlotId ? this.scene.sessionSlotId : ''}_${loggedInUser.username}`);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
}
|
}
|
||||||
return response.text();
|
return response.text();
|
||||||
|
@ -792,7 +810,7 @@ export class GameData {
|
||||||
tryClearSession(scene: BattleScene, slotId: integer): Promise<[success: boolean, newClear: boolean]> {
|
tryClearSession(scene: BattleScene, slotId: integer): Promise<[success: boolean, newClear: boolean]> {
|
||||||
return new Promise<[boolean, boolean]>(resolve => {
|
return new Promise<[boolean, boolean]>(resolve => {
|
||||||
if (bypassLogin) {
|
if (bypassLogin) {
|
||||||
localStorage.removeItem(`sessionData${slotId ? slotId : ''}`);
|
localStorage.removeItem(`sessionData${slotId ? slotId : ''}_${loggedInUser.username}`);
|
||||||
return resolve([true, true]);
|
return resolve([true, true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -800,9 +818,11 @@ export class GameData {
|
||||||
if (success !== null && !success)
|
if (success !== null && !success)
|
||||||
return resolve([false, false]);
|
return resolve([false, false]);
|
||||||
const sessionData = this.getSessionSaveData(scene);
|
const sessionData = this.getSessionSaveData(scene);
|
||||||
Utils.apiPost(`savedata/clear?slot=${slotId}&trainerId=${this.trainerId}&secretId=${this.secretId}`, JSON.stringify(sessionData), undefined, true).then(response => {
|
Utils.apiPost(`savedata/clear?slot=${slotId}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`, JSON.stringify(sessionData), undefined, true).then(response => {
|
||||||
if (response.ok)
|
if (response.ok) {
|
||||||
loggedInUser.lastSessionSlot = -1;
|
loggedInUser.lastSessionSlot = -1;
|
||||||
|
localStorage.removeItem(`sessionData${this.scene.sessionSlotId ? this.scene.sessionSlotId : ''}_${loggedInUser.username}`);
|
||||||
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
}).then(jsonResponse => {
|
}).then(jsonResponse => {
|
||||||
if (!jsonResponse.error)
|
if (!jsonResponse.error)
|
||||||
|
@ -855,14 +875,14 @@ export class GameData {
|
||||||
}) as SessionSaveData;
|
}) as SessionSaveData;
|
||||||
}
|
}
|
||||||
|
|
||||||
saveAll(scene: BattleScene, skipVerification?: boolean): Promise<boolean> {
|
saveAll(scene: BattleScene, skipVerification: boolean = false, sync: boolean = false, useCachedSession: boolean = false): Promise<boolean> {
|
||||||
return new Promise<boolean>(resolve => {
|
return new Promise<boolean>(resolve => {
|
||||||
Utils.executeIf(!skipVerification, updateUserInfo).then(success => {
|
Utils.executeIf(!skipVerification, updateUserInfo).then(success => {
|
||||||
if (success !== null && !success)
|
if (success !== null && !success)
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
this.scene.ui.savingIcon.show();
|
if (sync)
|
||||||
const data = this.getSystemSaveData();
|
this.scene.ui.savingIcon.show();
|
||||||
const sessionData = this.getSessionSaveData(scene);
|
const sessionData = useCachedSession ? this.parseSessionData(decrypt(localStorage.getItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}_${loggedInUser.username}`), bypassLogin)) : this.getSessionSaveData(scene);
|
||||||
|
|
||||||
const maxIntAttrValue = Math.pow(2, 31);
|
const maxIntAttrValue = Math.pow(2, 31);
|
||||||
const systemData = this.getSystemSaveData();
|
const systemData = this.getSystemSaveData();
|
||||||
|
@ -870,14 +890,24 @@ export class GameData {
|
||||||
const request = {
|
const request = {
|
||||||
system: systemData,
|
system: systemData,
|
||||||
session: sessionData,
|
session: sessionData,
|
||||||
sessionSlotId: scene.sessionSlotId
|
sessionSlotId: scene.sessionSlotId,
|
||||||
|
clientSessionId: clientSessionId
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!bypassLogin) {
|
localStorage.setItem(`data_${loggedInUser.username}`, encrypt(JSON.stringify(systemData, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v), bypassLogin));
|
||||||
|
|
||||||
|
localStorage.setItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}_${loggedInUser.username}`, encrypt(JSON.stringify(sessionData), bypassLogin));
|
||||||
|
|
||||||
|
console.debug('Session data saved');
|
||||||
|
|
||||||
|
if (!bypassLogin && sync) {
|
||||||
Utils.apiPost('savedata/updateall', JSON.stringify(request, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v), undefined, true)
|
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(response => response.text())
|
||||||
.then(error => {
|
.then(error => {
|
||||||
this.scene.ui.savingIcon.hide();
|
if (sync) {
|
||||||
|
this.scene.lastSavePlayTime = 0;
|
||||||
|
this.scene.ui.savingIcon.hide();
|
||||||
|
}
|
||||||
if (error) {
|
if (error) {
|
||||||
if (error.startsWith('client version out of date')) {
|
if (error.startsWith('client version out of date')) {
|
||||||
this.scene.clearPhaseQueue();
|
this.scene.clearPhaseQueue();
|
||||||
|
@ -892,17 +922,10 @@ export class GameData {
|
||||||
resolve(true);
|
resolve(true);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
localStorage.setItem('data_bak', localStorage.getItem('data'));
|
this.verify().then(success => {
|
||||||
|
this.scene.ui.savingIcon.hide();
|
||||||
localStorage.setItem('data', btoa(JSON.stringify(systemData, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v)));
|
resolve(success);
|
||||||
|
});
|
||||||
localStorage.setItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}`, btoa(JSON.stringify(sessionData)));
|
|
||||||
|
|
||||||
console.debug('Session data saved');
|
|
||||||
|
|
||||||
this.scene.ui.savingIcon.hide();
|
|
||||||
|
|
||||||
resolve(true);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -910,7 +933,7 @@ export class GameData {
|
||||||
|
|
||||||
public tryExportData(dataType: GameDataType, slotId: integer = 0): Promise<boolean> {
|
public tryExportData(dataType: GameDataType, slotId: integer = 0): Promise<boolean> {
|
||||||
return new Promise<boolean>(resolve => {
|
return new Promise<boolean>(resolve => {
|
||||||
const dataKey: string = getDataTypeKey(dataType, slotId);
|
const dataKey: string = `${getDataTypeKey(dataType, slotId)}_${loggedInUser.username}`;
|
||||||
const handleData = (dataStr: string) => {
|
const handleData = (dataStr: string) => {
|
||||||
switch (dataType) {
|
switch (dataType) {
|
||||||
case GameDataType.SYSTEM:
|
case GameDataType.SYSTEM:
|
||||||
|
@ -926,7 +949,7 @@ export class GameData {
|
||||||
link.remove();
|
link.remove();
|
||||||
};
|
};
|
||||||
if (!bypassLogin && dataType < GameDataType.SETTINGS) {
|
if (!bypassLogin && dataType < GameDataType.SETTINGS) {
|
||||||
Utils.apiFetch(`savedata/get?datatype=${dataType}${dataType === GameDataType.SESSION ? `&slot=${slotId}` : ''}`, true)
|
Utils.apiFetch(`savedata/${dataType === GameDataType.SYSTEM ? 'system' : 'session'}${dataType === GameDataType.SESSION ? `&slot=${slotId}` : ''}&clientSessionId=${clientSessionId}`, true)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (!response.length || response[0] !== '{') {
|
if (!response.length || response[0] !== '{') {
|
||||||
|
@ -941,14 +964,14 @@ export class GameData {
|
||||||
} else {
|
} else {
|
||||||
const data = localStorage.getItem(dataKey);
|
const data = localStorage.getItem(dataKey);
|
||||||
if (data)
|
if (data)
|
||||||
handleData(atob(data));
|
handleData(decrypt(data, bypassLogin));
|
||||||
resolve(!!data);
|
resolve(!!data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public importData(dataType: GameDataType, slotId: integer = 0): void {
|
public importData(dataType: GameDataType, slotId: integer = 0): void {
|
||||||
const dataKey = getDataTypeKey(dataType, slotId);
|
const dataKey = `${getDataTypeKey(dataType, slotId)}_${loggedInUser.username}`;
|
||||||
|
|
||||||
let saveFile: any = document.getElementById('saveFile');
|
let saveFile: any = document.getElementById('saveFile');
|
||||||
if (saveFile)
|
if (saveFile)
|
||||||
|
@ -1009,11 +1032,13 @@ export class GameData {
|
||||||
return this.scene.ui.showText(`Your ${dataName} data could not be loaded. It may be corrupted.`, null, () => this.scene.ui.showText(null, 0), Utils.fixedInt(1500));
|
return this.scene.ui.showText(`Your ${dataName} data could not be loaded. It may be corrupted.`, null, () => this.scene.ui.showText(null, 0), Utils.fixedInt(1500));
|
||||||
this.scene.ui.showText(`Your ${dataName} data will be overridden and the page will reload. Proceed?`, null, () => {
|
this.scene.ui.showText(`Your ${dataName} data will be overridden and the page will reload. Proceed?`, null, () => {
|
||||||
this.scene.ui.setOverlayMode(Mode.CONFIRM, () => {
|
this.scene.ui.setOverlayMode(Mode.CONFIRM, () => {
|
||||||
|
localStorage.setItem(dataKey, encrypt(dataStr, bypassLogin));
|
||||||
|
|
||||||
if (!bypassLogin && dataType < GameDataType.SETTINGS) {
|
if (!bypassLogin && dataType < GameDataType.SETTINGS) {
|
||||||
updateUserInfo().then(success => {
|
updateUserInfo().then(success => {
|
||||||
if (!success)
|
if (!success)
|
||||||
return displayError(`Could not contact the server. Your ${dataName} data could not be imported.`);
|
return displayError(`Could not contact the server. Your ${dataName} data could not be imported.`);
|
||||||
Utils.apiPost(`savedata/update?datatype=${dataType}${dataType === GameDataType.SESSION ? `&slot=${slotId}` : ''}&trainerId=${this.trainerId}&secretId=${this.secretId}`, dataStr, undefined, true)
|
Utils.apiPost(`savedata/update?datatype=${dataType}${dataType === GameDataType.SESSION ? `&slot=${slotId}` : ''}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`, dataStr, undefined, true)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(error => {
|
.then(error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -1023,10 +1048,8 @@ export class GameData {
|
||||||
window.location = window.location;
|
window.location = window.location;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else
|
||||||
localStorage.setItem(dataKey, btoa(dataStr));
|
|
||||||
window.location = window.location;
|
window.location = window.location;
|
||||||
}
|
|
||||||
}, () => {
|
}, () => {
|
||||||
this.scene.ui.revertMode();
|
this.scene.ui.revertMode();
|
||||||
this.scene.ui.showText(null, 0);
|
this.scene.ui.showText(null, 0);
|
||||||
|
|
|
@ -372,7 +372,7 @@ export default class EggGachaUiHandler extends MessageUiHandler {
|
||||||
this.scene.gameData.gameStats.eggsPulled++;
|
this.scene.gameData.gameStats.eggsPulled++;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scene.gameData.saveSystem().then(success => {
|
this.scene.gameData.saveAll(this.scene, true, true, true).then(success => {
|
||||||
if (!success)
|
if (!success)
|
||||||
return this.scene.reset(true);
|
return this.scene.reset(true);
|
||||||
doPull();
|
doPull();
|
||||||
|
|
Loading…
Reference in New Issue