Manual merging

This commit is contained in:
Frutescens 2024-07-30 08:22:16 -07:00
parent bc125f145b
commit 66760d8f0d
2 changed files with 1102 additions and 551 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,19 @@
import i18next from "i18next";
import BattleScene, { PokeballCounts, bypassLogin } from "../battle-scene";
import Pokemon, { EnemyPokemon, PlayerPokemon } from "../field/pokemon";
import { pokemonEvolutions, pokemonPrevolutions } from "../data/pokemon-evolutions";
import PokemonSpecies, { allSpecies, getPokemonSpecies, noStarterFormKeys, speciesStarters } from "../data/pokemon-species";
import { Species, defaultStarterSpecies } from "../data/enums/species";
import * as Utils from "../utils";
import * as Overrides from "../overrides";
import Overrides from "#app/overrides";
import PokemonData from "./pokemon-data";
import PersistentModifierData from "./modifier-data";
import ArenaData from "./arena-data";
import { Unlockables } from "./unlockables";
import { GameModes, gameModes } from "../game-mode";
import { GameModes, getGameMode } from "../game-mode";
import { BattleType } from "../battle";
import TrainerData from "./trainer-data";
import { trainerConfigs } from "../data/trainer-config";
import { Setting, setSetting, settingDefaults } from "./settings";
import { SettingKeys, resetSettings, setSetting } from "./settings/settings";
import { achvs } from "./achv";
import EggData from "./egg-data";
import { Egg } from "../data/egg";
@ -24,34 +24,39 @@ import { clientSessionId, loggedInUser, updateUserInfo } from "../account";
import { Nature } from "../data/nature";
import { GameStats } from "./game-stats";
import { Tutorial } from "../tutorial";
import { Moves } from "../data/enums/moves";
import { speciesEggMoves } from "../data/egg-moves";
import { allMoves } from "../data/move";
import { TrainerVariant } from "../field/trainer";
import { OutdatedPhase, ReloadSessionPhase } from "#app/phases";
import { Variant, variantData } from "#app/data/variant";
import {setSettingGamepad, SettingGamepad, settingGamepadDefaults} from "./settings/settings-gamepad";
import {setSettingKeyboard, SettingKeyboard} from "#app/system/settings/settings-keyboard";
import { TerrainChangedEvent, WeatherChangedEvent } from "#app/events/arena.js";
import { EnemyAttackStatusEffectChanceModifier } from "../modifier/modifier";
import { StatusEffect } from "#app/data/status-effect.js";
import ChallengeData from "./challenge-data";
import { Device } from "#enums/devices";
import { GameDataType } from "#enums/game-data-type";
import { Moves } from "#enums/moves";
import { PlayerGender } from "#enums/player-gender";
import { Species } from "#enums/species";
import { applyChallenges, ChallengeType } from "#app/data/challenge.js";
import { Abilities } from "#app/enums/abilities.js";
export const defaultStarterSpecies: Species[] = [
Species.BULBASAUR, Species.CHARMANDER, Species.SQUIRTLE,
Species.CHIKORITA, Species.CYNDAQUIL, Species.TOTODILE,
Species.TREECKO, Species.TORCHIC, Species.MUDKIP,
Species.TURTWIG, Species.CHIMCHAR, Species.PIPLUP,
Species.SNIVY, Species.TEPIG, Species.OSHAWOTT,
Species.CHESPIN, Species.FENNEKIN, Species.FROAKIE,
Species.ROWLET, Species.LITTEN, Species.POPPLIO,
Species.GROOKEY, Species.SCORBUNNY, Species.SOBBLE,
Species.SPRIGATITO, Species.FUECOCO, Species.QUAXLY
];
const saveKey = "x0i2O7WRiANTqPmZ"; // Temporary; secure encryption is not yet necessary
export enum GameDataType {
SYSTEM,
SESSION,
SETTINGS,
TUTORIALS,
RUN_HISTORY
}
export enum PlayerGender {
UNSET,
MALE,
FEMALE
}
export enum Passive {
UNLOCKED = 1,
ENABLED = 2
}
export function getDataTypeKey(dataType: GameDataType, slotId: integer = 0): string {
switch (dataType) {
case GameDataType.SYSTEM:
@ -66,18 +71,20 @@ export function getDataTypeKey(dataType: GameDataType, slotId: integer = 0): str
return "settings";
case GameDataType.TUTORIALS:
return "tutorials";
case GameDataType.SEEN_DIALOGUES:
return "seenDialogues";
case GameDataType.RUN_HISTORY:
return "runHistory";
}
}
function encrypt(data: string, bypassLogin: boolean): string {
export 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 {
export function decrypt(data: string, bypassLogin: boolean): string {
return (bypassLogin
? (data: string) => atob(data)
: (data: string) => AES.decrypt(data, saveKey).toString(enc.Utf8))(data);
@ -90,7 +97,6 @@ interface SystemSaveData {
dexData: DexData;
starterData: StarterData;
gameStats: GameStats;
runHistory: RunHistoryData;
unlocks: Unlocks;
achvUnlocks: AchvUnlocks;
voucherUnlocks: VoucherUnlocks;
@ -98,6 +104,8 @@ interface SystemSaveData {
eggs: EggData[];
gameVersion: string;
timestamp: integer;
eggPity: integer[];
unlockPity: integer[];
}
export interface SessionSaveData {
@ -117,15 +125,7 @@ export interface SessionSaveData {
trainer: TrainerData;
gameVersion: string;
timestamp: integer;
}
export interface RunHistoryData {
[key: integer]: RunEntries;
}
export interface RunEntries {
entry: SessionSaveData;
victory: boolean;
challenges: ChallengeData[];
}
interface Unlocks {
@ -141,7 +141,7 @@ interface VoucherUnlocks {
}
export interface VoucherCounts {
[type: string]: integer;
[type: string]: integer;
}
export interface DexData {
@ -182,6 +182,15 @@ export const AbilityAttr = {
ABILITY_HIDDEN: 4
};
export interface RunHistoryData {
[key: integer]: RunEntries;
}
export interface RunEntries {
entry: SessionSaveData;
victory: boolean;
}
export type StarterMoveset = [ Moves ] | [ Moves, Moves ] | [ Moves, Moves, Moves ] | [ Moves, Moves, Moves, Moves ];
export interface StarterFormMoveData {
@ -192,6 +201,46 @@ export interface StarterMoveData {
[key: integer]: StarterMoveset | StarterFormMoveData
}
export interface StarterAttributes {
nature?: integer;
ability?: integer;
variant?: integer;
form?: integer;
female?: boolean;
}
export interface StarterPreferences {
[key: integer]: StarterAttributes;
}
// the latest data saved/loaded for the Starter Preferences. Required to reduce read/writes. Initialize as "{}", since this is the default value and no data needs to be stored if present.
// if they ever add private static variables, move this into StarterPrefs
const StarterPrefers_DEFAULT : string = "{}";
let StarterPrefers_private_latest : string = StarterPrefers_DEFAULT;
// This is its own class as StarterPreferences...
// - don't need to be loaded on startup
// - isn't stored with other data
// - don't require to be encrypted
// - shouldn't require calls outside of the starter selection
export class StarterPrefs {
// called on starter selection show once
static load(): StarterPreferences {
return JSON.parse(
StarterPrefers_private_latest = (localStorage.getItem(`starterPrefs_${loggedInUser?.username}`) || StarterPrefers_DEFAULT)
);
}
// called on starter selection clear, always
static save(prefs: StarterPreferences): void {
const pStr : string = JSON.stringify(prefs);
if (pStr !== StarterPrefers_private_latest) {
// something changed, store the update
localStorage.setItem(`starterPrefs_${loggedInUser?.username}`, pStr);
}
}
}
export interface StarterDataEntry {
moveset: StarterMoveset | StarterFormMoveData;
eggMoves: integer;
@ -211,6 +260,10 @@ export interface TutorialFlags {
[key: string]: boolean
}
export interface SeenDialogues {
[key: string]: boolean;
}
const systemShortKeys = {
seenAttr: "$sa",
caughtAttr: "$ca",
@ -243,7 +296,7 @@ export class GameData {
public starterData: StarterData;
public gameStats: GameStats;
public runHistory: RunHistoryData;
public unlocks: Unlocks;
public achvUnlocks: AchvUnlocks;
@ -251,14 +304,17 @@ export class GameData {
public voucherUnlocks: VoucherUnlocks;
public voucherCounts: VoucherCounts;
public eggs: Egg[];
public eggPity: integer[];
public unlockPity: integer[];
constructor(scene: BattleScene) {
this.scene = scene;
this.loadSettings();
this.loadGamepadSettings();
this.loadMappingConfigs();
this.trainerId = Utils.randInt(65536);
this.secretId = Utils.randInt(65536);
this.starterData = {};
this.runHistory = {};
this.gameStats = new GameStats();
this.unlocks = {
[Unlockables.ENDLESS_MODE]: false,
@ -267,6 +323,7 @@ export class GameData {
};
this.achvUnlocks = {};
this.voucherUnlocks = {};
this.runHistory = {};
this.voucherCounts = {
[VoucherType.REGULAR]: 0,
[VoucherType.PLUS]: 0,
@ -274,6 +331,8 @@ export class GameData {
[VoucherType.GOLDEN]: 0
};
this.eggs = [];
this.eggPity = [0, 0, 0, 0];
this.unlockPity = [0, 0, 0, 0];
this.initDexData();
this.initStarterData();
}
@ -283,7 +342,6 @@ export class GameData {
trainerId: this.trainerId,
secretId: this.secretId,
gender: this.gender,
runHistory: this.runHistory,
dexData: this.dexData,
starterData: this.starterData,
gameStats: this.gameStats,
@ -293,7 +351,9 @@ export class GameData {
voucherCounts: this.voucherCounts,
eggs: this.eggs.map(e => new EggData(e)),
gameVersion: this.scene.game.config.gameVersion,
timestamp: new Date().getTime()
timestamp: new Date().getTime(),
eggPity: this.eggPity.slice(0),
unlockPity: this.unlockPity.slice(0)
};
}
@ -308,7 +368,7 @@ export class GameData {
localStorage.setItem(`data_${loggedInUser.username}`, encrypt(systemData, bypassLogin));
if (!bypassLogin) {
Utils.apiPost(`savedata/update?datatype=${GameDataType.SYSTEM}&clientSessionId=${clientSessionId}`, systemData, undefined, true)
Utils.apiPost(`savedata/system/update?clientSessionId=${clientSessionId}`, systemData, undefined, true)
.then(response => response.text())
.then(error => {
this.scene.ui.savingIcon.hide();
@ -342,7 +402,7 @@ export class GameData {
}
if (!bypassLogin) {
Utils.apiFetch(`savedata/system?clientSessionId=${clientSessionId}`, true)
Utils.apiFetch(`savedata/system/get?clientSessionId=${clientSessionId}`, true)
.then(response => response.text())
.then(response => {
if (!response.length || response[0] !== "{") {
@ -386,23 +446,22 @@ export class GameData {
localStorage.setItem(`data_${loggedInUser.username}`, encrypt(systemDataStr, bypassLogin));
if (!localStorage.hasOwnProperty(`runHistoryData_${loggedInUser.username}`)) {
localStorage.setItem(`runHistoryData_${loggedInUser.username}`, encrypt("", true));
}
/*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)));
}*/
if (!localStorage.hasOwnProperty(`runHistoryData_${loggedInUser.username}`)) {
localStorage.setItem(`runHistoryData_${loggedInUser.username}`, encrypt("", true));
}
this.trainerId = systemData.trainerId;
this.secretId = systemData.secretId;
this.gender = systemData.gender;
this.saveSetting(Setting.Player_Gender, systemData.gender === PlayerGender.FEMALE ? 1 : 0);
this.saveSetting(SettingKeys.Player_Gender, systemData.gender === PlayerGender.FEMALE ? 1 : 0);
const initStarterData = !systemData.starterData;
@ -481,6 +540,9 @@ export class GameData {
? systemData.eggs.map(e => e.toEgg())
: [];
this.eggPity = systemData.eggPity ? systemData.eggPity.slice(0) : [0, 0, 0, 0];
this.unlockPity = systemData.unlockPity ? systemData.unlockPity.slice(0) : [0, 0, 0, 0];
this.dexData = Object.assign(this.dexData, systemData.dexData);
this.consolidateDexData(this.dexData);
this.defaultDexData = null;
@ -504,7 +566,67 @@ export class GameData {
});
}
private parseSystemData(dataStr: string): SystemSaveData {
public async getRunHistoryData(scene: BattleScene): Promise<Object> {
if (!Utils.isLocal) {
const data = await Utils.apiFetch("savedata/runHistory", true).json();
//const data = await response.json();
if (localStorage.hasOwnProperty(`runHistoryData_${loggedInUser.username}`)) {
let cachedResponse = localStorage.getItem(`runHistoryData_${loggedInUser.username}`, true);
if (cachedResponse) {
cachedResponse = JSON.parse(decrypt(cachedResponse, true));
}
const cachedRHData = cachedResponse ?? {};
//check to see whether cachedData or serverData is more up-to-date
if ( Object.keys(cachedRHData).length >= Object.keys(data).length ) {
return cachedRHData;
}
} else {
localStorage.setItem(`runHistoryData_${loggedInUser.username}`, JSON.parse(encrypt({}, true)));
return {};
}
return data;
} else {
let cachedResponse = localStorage.getItem(`runHistoryData_${loggedInUser.username}`, true);
if (cachedResponse) {
cachedResponse = JSON.parse(decrypt(cachedResponse, true));
}
const cachedRHData = cachedResponse ?? {};
return cachedRHData;
}
}
async saveRunHistory(scene: BattleScene, runEntry : SessionSaveData, victory: boolean): Promise<boolean> {
let runHistoryData = await this.getRunHistoryData(scene);
if (!runHistoryData) {
runHistoryData = {};
}
const timestamps = Object.keys(runHistoryData);
//Arbitrary limit of 25 entries per User --> Can increase or decrease
if (timestamps.length >= 25) {
delete this.scene.gameData.runHistory[Math.min(timestamps)];
}
const timestamp = (runEntry.timestamp).toString();
runHistoryData[timestamp] = {};
runHistoryData[timestamp]["entry"] = runEntry;
runHistoryData[timestamp]["victory"] = victory;
localStorage.setItem(`runHistoryData_${loggedInUser.username}`, encrypt(JSON.stringify(runHistoryData), true));
if (!Utils.isLocal) {
try {
Utils.apiPost("savedata/runHistory", JSON.stringify(runHistoryData), undefined, true);
return true;
} catch (err) {
console.log("savedata/runHistory POST failed : ", err);
return false;
}
}
}
parseSystemData(dataStr: string): SystemSaveData {
return JSON.parse(dataStr, (k: string, v: any) => {
if (k === "gameStats") {
return new GameStats(v);
@ -523,11 +645,13 @@ export class GameData {
}) as SystemSaveData;
}
private convertSystemDataStr(dataStr: string, shorten: boolean = false): string {
convertSystemDataStr(dataStr: string, shorten: boolean = false): string {
if (!shorten) {
// Account for past key oversight
dataStr = dataStr.replace(/\$pAttr/g, "$pa");
}
dataStr = dataStr.replace(/"trainerId":\d+/g, `"trainerId":${this.trainerId}`);
dataStr = dataStr.replace(/"secretId":\d+/g, `"secretId":${this.secretId}`);
const fromKeys = shorten ? Object.keys(systemShortKeys) : Object.values(systemShortKeys);
const toKeys = shorten ? Object.values(systemShortKeys) : Object.keys(systemShortKeys);
for (const k in fromKeys) {
@ -542,7 +666,7 @@ export class GameData {
return true;
}
const response = await Utils.apiPost("savedata/system/verify", JSON.stringify({ clientSessionId: clientSessionId }), undefined, true)
const response = await Utils.apiFetch(`savedata/system/verify?clientSessionId=${clientSessionId}`, true)
.then(response => response.json());
if (!response.valid) {
@ -565,27 +689,123 @@ export class GameData {
}
}
public saveSetting(setting: Setting, valueIndex: integer): boolean {
/**
* Saves a setting to localStorage
* @param setting string ideally of SettingKeys
* @param valueIndex index of the setting's option
* @returns true
*/
public saveSetting(setting: string, valueIndex: integer): boolean {
let settings: object = {};
if (localStorage.hasOwnProperty("settings")) {
settings = JSON.parse(localStorage.getItem("settings"));
}
setSetting(this.scene, setting as Setting, valueIndex);
setSetting(this.scene, setting, valueIndex);
Object.keys(settingDefaults).forEach(s => {
if (s === setting) {
settings[s] = valueIndex;
}
});
settings[setting] = valueIndex;
localStorage.setItem("settings", JSON.stringify(settings));
return true;
}
/**
* Saves the mapping configurations for a specified device.
*
* @param deviceName - The name of the device for which the configurations are being saved.
* @param config - The configuration object containing custom mapping details.
* @returns `true` if the configurations are successfully saved.
*/
public saveMappingConfigs(deviceName: string, config): boolean {
const key = deviceName.toLowerCase(); // Convert the gamepad name to lowercase to use as a key
let mappingConfigs: object = {}; // Initialize an empty object to hold the mapping configurations
if (localStorage.hasOwnProperty("mappingConfigs")) {// Check if 'mappingConfigs' exists in localStorage
mappingConfigs = JSON.parse(localStorage.getItem("mappingConfigs"));
} // Parse the existing 'mappingConfigs' from localStorage
if (!mappingConfigs[key]) {
mappingConfigs[key] = {};
} // If there is no configuration for the given key, create an empty object for it
mappingConfigs[key].custom = config.custom; // Assign the custom configuration to the mapping configuration for the given key
localStorage.setItem("mappingConfigs", JSON.stringify(mappingConfigs)); // Save the updated mapping configurations back to localStorage
return true; // Return true to indicate the operation was successful
}
/**
* Loads the mapping configurations from localStorage and injects them into the input controller.
*
* @returns `true` if the configurations are successfully loaded and injected; `false` if no configurations are found in localStorage.
*
* @remarks
* This method checks if the 'mappingConfigs' entry exists in localStorage. If it does not exist, the method returns `false`.
* If 'mappingConfigs' exists, it parses the configurations and injects each configuration into the input controller
* for the corresponding gamepad or device key. The method then returns `true` to indicate success.
*/
public loadMappingConfigs(): boolean {
if (!localStorage.hasOwnProperty("mappingConfigs")) {// Check if 'mappingConfigs' exists in localStorage
return false;
} // If 'mappingConfigs' does not exist, return false
const mappingConfigs = JSON.parse(localStorage.getItem("mappingConfigs")); // Parse the existing 'mappingConfigs' from localStorage
for (const key of Object.keys(mappingConfigs)) {// Iterate over the keys of the mapping configurations
this.scene.inputController.injectConfig(key, mappingConfigs[key]);
} // Inject each configuration into the input controller for the corresponding key
return true; // Return true to indicate the operation was successful
}
public resetMappingToFactory(): boolean {
if (!localStorage.hasOwnProperty("mappingConfigs")) {// Check if 'mappingConfigs' exists in localStorage
return false;
} // If 'mappingConfigs' does not exist, return false
localStorage.removeItem("mappingConfigs");
this.scene.inputController.resetConfigs();
}
/**
* Saves a gamepad setting to localStorage.
*
* @param setting - The gamepad setting to save.
* @param valueIndex - The index of the value to set for the gamepad setting.
* @returns `true` if the setting is successfully saved.
*
* @remarks
* This method initializes an empty object for gamepad settings if none exist in localStorage.
* It then updates the setting in the current scene and iterates over the default gamepad settings
* to update the specified setting with the new value. Finally, it saves the updated settings back
* to localStorage and returns `true` to indicate success.
*/
public saveControlSetting(device: Device, localStoragePropertyName: string, setting: SettingGamepad|SettingKeyboard, settingDefaults, valueIndex: integer): boolean {
let settingsControls: object = {}; // Initialize an empty object to hold the gamepad settings
if (localStorage.hasOwnProperty(localStoragePropertyName)) { // Check if 'settingsControls' exists in localStorage
settingsControls = JSON.parse(localStorage.getItem(localStoragePropertyName)); // Parse the existing 'settingsControls' from localStorage
}
if (device === Device.GAMEPAD) {
setSettingGamepad(this.scene, setting as SettingGamepad, valueIndex); // Set the gamepad setting in the current scene
} else if (device === Device.KEYBOARD) {
setSettingKeyboard(this.scene, setting as SettingKeyboard, valueIndex); // Set the keyboard setting in the current scene
}
Object.keys(settingDefaults).forEach(s => { // Iterate over the default gamepad settings
if (s === setting) {// If the current setting matches, update its value
settingsControls[s] = valueIndex;
}
});
localStorage.setItem(localStoragePropertyName, JSON.stringify(settingsControls)); // Save the updated gamepad settings back to localStorage
return true; // Return true to indicate the operation was successful
}
/**
* Loads Settings from local storage if available
* @returns true if succesful, false if not
*/
private loadSettings(): boolean {
Object.values(Setting).map(setting => setting as Setting).forEach(setting => setSetting(this.scene, setting, settingDefaults[setting]));
resetSettings(this.scene);
if (!localStorage.hasOwnProperty("settings")) {
return false;
@ -594,14 +814,28 @@ export class GameData {
const settings = JSON.parse(localStorage.getItem("settings"));
for (const setting of Object.keys(settings)) {
setSetting(this.scene, setting as Setting, settings[setting]);
setSetting(this.scene, setting, settings[setting]);
}
}
private loadGamepadSettings(): boolean {
Object.values(SettingGamepad).map(setting => setting as SettingGamepad).forEach(setting => setSettingGamepad(this.scene, setting, settingGamepadDefaults[setting]));
if (!localStorage.hasOwnProperty("settingsGamepad")) {
return false;
}
const settingsGamepad = JSON.parse(localStorage.getItem("settingsGamepad"));
for (const setting of Object.keys(settingsGamepad)) {
setSettingGamepad(this.scene, setting as SettingGamepad, settingsGamepad[setting]);
}
}
public saveTutorialFlag(tutorial: Tutorial, flag: boolean): boolean {
const key = getDataTypeKey(GameDataType.TUTORIALS);
let tutorials: object = {};
if (localStorage.hasOwnProperty("tutorials")) {
tutorials = JSON.parse(localStorage.getItem("tutorials"));
if (localStorage.hasOwnProperty(key)) {
tutorials = JSON.parse(localStorage.getItem(key));
}
Object.keys(Tutorial).map(t => t as Tutorial).forEach(t => {
@ -613,20 +847,21 @@ export class GameData {
}
});
localStorage.setItem("tutorials", JSON.stringify(tutorials));
localStorage.setItem(key, JSON.stringify(tutorials));
return true;
}
public getTutorialFlags(): TutorialFlags {
const key = getDataTypeKey(GameDataType.TUTORIALS);
const ret: TutorialFlags = {};
Object.values(Tutorial).map(tutorial => tutorial as Tutorial).forEach(tutorial => ret[Tutorial[tutorial]] = false);
if (!localStorage.hasOwnProperty("tutorials")) {
if (!localStorage.hasOwnProperty(key)) {
return ret;
}
const tutorials = JSON.parse(localStorage.getItem("tutorials"));
const tutorials = JSON.parse(localStorage.getItem(key));
for (const tutorial of Object.keys(tutorials)) {
ret[tutorial] = tutorials[tutorial];
@ -635,6 +870,34 @@ export class GameData {
return ret;
}
public saveSeenDialogue(dialogue: string): boolean {
const key = getDataTypeKey(GameDataType.SEEN_DIALOGUES);
const dialogues: object = this.getSeenDialogues();
dialogues[dialogue] = true;
localStorage.setItem(key, JSON.stringify(dialogues));
console.log("Dialogue saved as seen:", dialogue);
return true;
}
public getSeenDialogues(): SeenDialogues {
const key = getDataTypeKey(GameDataType.SEEN_DIALOGUES);
const ret: SeenDialogues = {};
if (!localStorage.hasOwnProperty(key)) {
return ret;
}
const dialogues = JSON.parse(localStorage.getItem(key));
for (const dialogue of Object.keys(dialogues)) {
ret[dialogue] = dialogues[dialogue];
}
return ret;
}
private getSessionSaveData(scene: BattleScene): SessionSaveData {
return {
seed: scene.seed,
@ -652,7 +915,8 @@ export class GameData {
battleType: scene.currentBattle.battleType,
trainer: scene.currentBattle.battleType === BattleType.TRAINER ? new TrainerData(scene.currentBattle.trainer) : null,
gameVersion: scene.game.config.gameVersion,
timestamp: new Date().getTime()
timestamp: new Date().getTime(),
challenges: scene.gameMode.challenges.map(c => new ChallengeData(c))
} as SessionSaveData;
}
@ -664,6 +928,14 @@ export class GameData {
const handleSessionData = async (sessionDataStr: string) => {
try {
const sessionData = this.parseSessionData(sessionDataStr);
for (let i = 0; i <= 5; i++) {
const speciesToCheck = getPokemonSpecies(sessionData.party[i]?.species);
if (sessionData.party[i]?.abilityIndex === 1) {
if (speciesToCheck.ability1 === speciesToCheck.ability2 && speciesToCheck.abilityHidden !== Abilities.NONE && speciesToCheck.abilityHidden !== speciesToCheck.ability1) {
sessionData.party[i].abilityIndex = 2;
}
}
}
resolve(sessionData);
} catch (err) {
reject(err);
@ -672,7 +944,7 @@ export class GameData {
};
if (!bypassLogin && !localStorage.getItem(`sessionData${slotId ? slotId : ""}_${loggedInUser.username}`)) {
Utils.apiFetch(`savedata/session?slot=${slotId}&clientSessionId=${clientSessionId}`, true)
Utils.apiFetch(`savedata/session/get?slot=${slotId}&clientSessionId=${clientSessionId}`, true)
.then(response => response.text())
.then(async response => {
if (!response.length || response[0] !== "{") {
@ -698,10 +970,13 @@ export class GameData {
loadSession(scene: BattleScene, slotId: integer, sessionData?: SessionSaveData): Promise<boolean> {
return new Promise(async (resolve, reject) => {
try {
const initSessionFromData = async sessionData => {
const initSessionFromData = async (sessionData: SessionSaveData) => {
console.debug(sessionData);
scene.gameMode = gameModes[sessionData.gameMode || GameModes.CLASSIC];
scene.gameMode = getGameMode(sessionData.gameMode || GameModes.CLASSIC);
if (sessionData.challenges) {
scene.gameMode.challenges = sessionData.challenges.map(c => c.toChallenge());
}
scene.setSeed(sessionData.seed || scene.game.config.seed[0]);
scene.resetSeed();
@ -760,6 +1035,10 @@ export class GameData {
});
scene.arena.weather = sessionData.arena.weather;
scene.arena.eventTarget.dispatchEvent(new WeatherChangedEvent(null, scene.arena.weather?.weatherType, scene.arena.weather?.turnsLeft));
scene.arena.terrain = sessionData.arena.terrain;
scene.arena.eventTarget.dispatchEvent(new TerrainChangedEvent(null, scene.arena.terrain?.terrainType, scene.arena.terrain?.turnsLeft));
// TODO
//scene.arena.tags = sessionData.arena.tags;
@ -813,7 +1092,7 @@ export class GameData {
if (success !== null && !success) {
return resolve(false);
}
Utils.apiFetch(`savedata/delete?datatype=${GameDataType.SESSION}&slot=${slotId}&clientSessionId=${clientSessionId}`, true).then(response => {
Utils.apiFetch(`savedata/session/delete?slot=${slotId}&clientSessionId=${clientSessionId}`, true).then(response => {
if (response.ok) {
loggedInUser.lastSessionSlot = -1;
localStorage.removeItem(`sessionData${this.scene.sessionSlotId ? this.scene.sessionSlotId : ""}_${loggedInUser.username}`);
@ -877,7 +1156,7 @@ export class GameData {
return resolve([false, false]);
}
const sessionData = this.getSessionSaveData(scene);
Utils.apiPost(`savedata/clear?slot=${slotId}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`, JSON.stringify(sessionData), undefined, true).then(response => {
Utils.apiPost(`savedata/session/clear?slot=${slotId}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`, JSON.stringify(sessionData), undefined, true).then(response => {
if (response.ok) {
loggedInUser.lastSessionSlot = -1;
localStorage.removeItem(`sessionData${this.scene.sessionSlotId ? this.scene.sessionSlotId : ""}_${loggedInUser.username}`);
@ -931,6 +1210,9 @@ export class GameData {
if (md?.className === "ExpBalanceModifier") { // Temporarily limit EXP Balance until it gets reworked
md.stackCount = Math.min(md.stackCount, 4);
}
if (md instanceof EnemyAttackStatusEffectChanceModifier && md.effect === StatusEffect.FREEZE || md.effect === StatusEffect.SLEEP) {
continue;
}
ret.push(new PersistentModifierData(md, player));
}
return ret;
@ -940,70 +1222,21 @@ export class GameData {
return new ArenaData(v);
}
if (k === "challenges") {
const ret: ChallengeData[] = [];
if (v === null) {
v = [];
}
for (const c of v) {
ret.push(new ChallengeData(c));
}
return ret;
}
return v;
}) as SessionSaveData;
}
public async getRunHistoryData(scene: BattleScene): Promise<Object> {
if (!Utils.isLocal) {
const data = await Utils.apiFetch("savedata/runHistory", true).json();
//const data = await response.json();
if (localStorage.hasOwnProperty(`runHistoryData_${loggedInUser.username}`)) {
let cachedResponse = localStorage.getItem(`runHistoryData_${loggedInUser.username}`, true);
if (cachedResponse) {
cachedResponse = JSON.parse(decrypt(cachedResponse, true));
}
const cachedRHData = cachedResponse ?? {};
//check to see whether cachedData or serverData is more up-to-date
if ( Object.keys(cachedRHData).length >= Object.keys(data).length ) {
return cachedRHData;
}
} else {
localStorage.setItem(`runHistoryData_${loggedInUser.username}`, JSON.parse(encrypt({}, true)));
return {};
}
return data;
} else {
let cachedResponse = localStorage.getItem(`runHistoryData_${loggedInUser.username}`, true);
if (cachedResponse) {
cachedResponse = JSON.parse(decrypt(cachedResponse, true));
}
const cachedRHData = cachedResponse ?? {};
return cachedRHData;
}
}
async saveRunHistory(scene: BattleScene, runEntry : SessionSaveData, victory: boolean): Promise<boolean> {
let runHistoryData = await this.getRunHistoryData(scene);
if (!runHistoryData) {
runHistoryData = {};
}
const timestamps = Object.keys(runHistoryData);
//Arbitrary limit of 25 entries per User --> Can increase or decrease
if (timestamps.length >= 25) {
delete this.scene.gameData.runHistory[Math.min(timestamps)];
}
const timestamp = (runEntry.timestamp).toString();
runHistoryData[timestamp] = {};
runHistoryData[timestamp]["entry"] = runEntry;
runHistoryData[timestamp]["victory"] = victory;
localStorage.setItem(`runHistoryData_${loggedInUser.username}`, encrypt(JSON.stringify(runHistoryData), true));
if (!Utils.isLocal) {
try {
Utils.apiPost("savedata/runHistory", JSON.stringify(runHistoryData), undefined, true);
return true;
} catch (err) {
console.log("savedata/runHistory POST failed : ", err);
return false;
}
}
}
saveAll(scene: BattleScene, skipVerification: boolean = false, sync: boolean = false, useCachedSession: boolean = false, useCachedSystem: boolean = false): Promise<boolean> {
return new Promise<boolean>(resolve => {
Utils.executeIf(!skipVerification, updateUserInfo).then(success => {
@ -1080,7 +1313,7 @@ export class GameData {
link.remove();
};
if (!bypassLogin && dataType < GameDataType.SETTINGS) {
Utils.apiFetch(`savedata/${dataType === GameDataType.SYSTEM ? "system" : "session"}?clientSessionId=${clientSessionId}${dataType === GameDataType.SESSION ? `&slot=${slotId}` : ""}`, true)
Utils.apiFetch(`savedata/${dataType === GameDataType.SYSTEM ? "system" : "session"}/get?clientSessionId=${clientSessionId}${dataType === GameDataType.SESSION ? `&slot=${slotId}` : ""}`, true)
.then(response => response.text())
.then(response => {
if (!response.length || response[0] !== "{") {
@ -1121,9 +1354,11 @@ export class GameData {
reader.onload = (_ => {
return e => {
let dataName: string;
let dataStr = AES.decrypt(e.target.result.toString(), saveKey).toString(enc.Utf8);
let valid = false;
try {
dataName = GameDataType[dataType].toLowerCase();
switch (dataType) {
case GameDataType.SYSTEM:
dataStr = this.convertSystemDataStr(dataStr);
@ -1143,38 +1378,28 @@ export class GameData {
console.error(ex);
}
let dataName: string;
switch (dataType) {
case GameDataType.SYSTEM:
dataName = "save";
break;
case GameDataType.SESSION:
dataName = "session";
break;
case GameDataType.SETTINGS:
dataName = "settings";
break;
case GameDataType.TUTORIALS:
dataName = "tutorials";
break;
}
const displayError = (error: string) => this.scene.ui.showText(error, null, () => this.scene.ui.showText(null, 0), Utils.fixedInt(1500));
if (!valid) {
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.revertMode();
this.scene.ui.showText(`Your ${dataName} data will be overridden and the page will reload. Proceed?`, null, () => {
this.scene.ui.setOverlayMode(Mode.CONFIRM, () => {
localStorage.setItem(dataKey, encrypt(dataStr, bypassLogin));
if (!bypassLogin && dataType < GameDataType.SETTINGS) {
updateUserInfo().then(success => {
if (!success) {
if (!success[0]) {
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}&clientSessionId=${clientSessionId}`, dataStr, undefined, true)
let url: string;
if (dataType === GameDataType.SESSION) {
url = `savedata/session/update?slot=${slotId}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`;
} else {
url = `savedata/system/update?trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`;
}
Utils.apiPost(url, dataStr, undefined, true)
.then(response => response.text())
.then(error => {
if (error) {
@ -1349,7 +1574,7 @@ export class GameData {
if (newCatch && speciesStarters.hasOwnProperty(species.speciesId)) {
this.scene.playSound("level_up_fanfare");
this.scene.ui.showText(`${species.name} has been\nadded as a starter!`, null, () => checkPrevolution(), null, true);
this.scene.ui.showText(i18next.t("battle:addedAsAStarter", { pokemonName: species.name }), null, () => checkPrevolution(), null, true);
} else {
checkPrevolution();
}
@ -1415,7 +1640,9 @@ export class GameData {
this.starterData[speciesId].eggMoves |= value;
this.scene.playSound("level_up_fanfare");
this.scene.ui.showText(`${eggMoveIndex === 3 ? "Rare " : ""}Egg Move unlocked: ${allMoves[speciesEggMoves[speciesId][eggMoveIndex]].name}`, null, () => resolve(true), null, true);
const moveName = allMoves[speciesEggMoves[speciesId][eggMoveIndex]].name;
this.scene.ui.showText(eggMoveIndex === 3 ? i18next.t("egg:rareEggMoveUnlock", { moveName: moveName }) : i18next.t("egg:eggMoveUnlock", { moveName: moveName }), null, () => resolve(true), null, true);
});
}
@ -1537,7 +1764,10 @@ export class GameData {
value = decrementValue(value);
}
return value;
const cost = new Utils.NumberHolder(value);
applyChallenges(this.scene.gameMode, ChallengeType.STARTER_COST, speciesId, cost);
return cost.value;
}
getFormIndex(attr: bigint): integer {