mirror of
https://github.com/pagefaultgames/pokerogue.git
synced 2025-08-24 00:09:31 +02:00
Restructure Migration Initialization
This commit is contained in:
parent
13233b77e4
commit
03c36c1d84
@ -44,7 +44,7 @@ import { WeatherType } from "#app/enums/weather-type";
|
|||||||
import { TerrainType } from "#app/data/terrain";
|
import { TerrainType } from "#app/data/terrain";
|
||||||
import { ReloadSessionPhase } from "#app/phases/reload-session-phase";
|
import { ReloadSessionPhase } from "#app/phases/reload-session-phase";
|
||||||
import { RUN_HISTORY_LIMIT } from "#app/ui/run-history-ui-handler";
|
import { RUN_HISTORY_LIMIT } from "#app/ui/run-history-ui-handler";
|
||||||
import { SessionVersionConverter, SettingsVersionConverter, SystemVersionConverter } from "./version_migration/version_converter";
|
import { applySessionVersionMigration, applySystemVersionMigration, applySettingsVersionMigration } from "./version_migration/version_converter";
|
||||||
import { MysteryEncounterSaveData } from "../data/mystery-encounters/mystery-encounter-save-data";
|
import { MysteryEncounterSaveData } from "../data/mystery-encounters/mystery-encounter-save-data";
|
||||||
import { MysteryEncounterType } from "#enums/mystery-encounter-type";
|
import { MysteryEncounterType } from "#enums/mystery-encounter-type";
|
||||||
import { PokerogueApiClearSessionData } from "#app/@types/pokerogue-api";
|
import { PokerogueApiClearSessionData } from "#app/@types/pokerogue-api";
|
||||||
@ -476,7 +476,7 @@ export class GameData {
|
|||||||
localStorage.setItem(lsItemKey, "");
|
localStorage.setItem(lsItemKey, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
new SystemVersionConverter(systemData, systemData.gameVersion);
|
applySystemVersionMigration(systemData);
|
||||||
|
|
||||||
this.trainerId = systemData.trainerId;
|
this.trainerId = systemData.trainerId;
|
||||||
this.secretId = systemData.secretId;
|
this.secretId = systemData.secretId;
|
||||||
@ -851,7 +851,7 @@ export class GameData {
|
|||||||
|
|
||||||
const settings = JSON.parse(localStorage.getItem("settings")!); // TODO: is this bang correct?
|
const settings = JSON.parse(localStorage.getItem("settings")!); // TODO: is this bang correct?
|
||||||
|
|
||||||
new SettingsVersionConverter(settings);
|
applySettingsVersionMigration(settings);
|
||||||
|
|
||||||
for (const setting of Object.keys(settings)) {
|
for (const setting of Object.keys(settings)) {
|
||||||
setSetting(this.scene, setting, settings[setting]);
|
setSetting(this.scene, setting, settings[setting]);
|
||||||
@ -1289,7 +1289,7 @@ export class GameData {
|
|||||||
return v;
|
return v;
|
||||||
}) as SessionSaveData;
|
}) as SessionSaveData;
|
||||||
|
|
||||||
new SessionVersionConverter(sessionData, sessionData.gameVersion);
|
applySessionVersionMigration(sessionData);
|
||||||
|
|
||||||
return sessionData;
|
return sessionData;
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,35 @@ import * as v1_0_4 from "./versions/v1_0_4";
|
|||||||
|
|
||||||
const LATEST_VERSION = version.split(".").map(value => parseInt(value));
|
const LATEST_VERSION = version.split(".").map(value => parseInt(value));
|
||||||
|
|
||||||
export abstract class VersionConverter {
|
export function applySystemVersionMigration(data: SystemSaveData) {
|
||||||
constructor(data: any, gameVersion: string) {
|
const curVersion = data.gameVersion.split(".").map(value => parseInt(value));
|
||||||
const curVersion = gameVersion.split(".").map(value => parseInt(value));
|
|
||||||
if (!curVersion.every((value, index) => value === LATEST_VERSION[index])) {
|
|
||||||
this.applyMigration(data, curVersion);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!curVersion.every((value, index) => value === LATEST_VERSION[index])) {
|
||||||
|
const converter = new SystemVersionConverter();
|
||||||
|
converter.applyMigration(data, curVersion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applySessionVersionMigration(data: SessionSaveData) {
|
||||||
|
const curVersion = data.gameVersion.split(".").map(value => parseInt(value));
|
||||||
|
|
||||||
|
if (!curVersion.every((value, index) => value === LATEST_VERSION[index])) {
|
||||||
|
const converter = new SessionVersionConverter();
|
||||||
|
converter.applyMigration(data, curVersion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applySettingsVersionMigration(data: Object) {
|
||||||
|
const gameVersion: string = data.hasOwnProperty("gameVersion") ? data["gameVersion"] : "1.0.0";
|
||||||
|
const curVersion = gameVersion.split(".").map(value => parseInt(value));
|
||||||
|
|
||||||
|
if (!curVersion.every((value, index) => value === LATEST_VERSION[index])) {
|
||||||
|
const converter = new SettingsVersionConverter();
|
||||||
|
converter.applyMigration(data, curVersion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class VersionConverter {
|
||||||
/**
|
/**
|
||||||
* Iterates through an array of designated migration functions that are each
|
* Iterates through an array of designated migration functions that are each
|
||||||
* called one by one to transform the data.
|
* called one by one to transform the data.
|
||||||
@ -38,75 +59,53 @@ export abstract class VersionConverter {
|
|||||||
abstract applyMigration(data: any, curVersion: number[]): void;
|
abstract applyMigration(data: any, curVersion: number[]): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SessionVersionConverter extends VersionConverter {
|
class SessionVersionConverter extends VersionConverter {
|
||||||
constructor(data: SessionSaveData, gameVersion: string) {
|
|
||||||
super(data, gameVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
override applyMigration(data: SessionSaveData, curVersion: number[]): void {
|
override applyMigration(data: SessionSaveData, curVersion: number[]): void {
|
||||||
const [ curMajor, curMinor, curPatch ] = curVersion;
|
const [ curMajor, curMinor, curPatch ] = curVersion;
|
||||||
|
|
||||||
switch (curMajor) {
|
if (curMajor === 1) {
|
||||||
case 1:
|
if (curMinor === 0) {
|
||||||
switch (curMinor) {
|
|
||||||
case 0:
|
|
||||||
if (curPatch <= 4) {
|
if (curPatch <= 4) {
|
||||||
console.log("Applying v1.0.4 session data migration!");
|
console.log("Applying v1.0.4 session data migration!");
|
||||||
this.callMigrators(data, v1_0_4.sessionMigrators);
|
this.callMigrators(data, v1_0_4.sessionMigrators);
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Session data successfully migrated to v${version}!`);
|
console.log(`Session data successfully migrated to v${version}!`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SystemVersionConverter extends VersionConverter {
|
class SystemVersionConverter extends VersionConverter {
|
||||||
constructor(data: SystemSaveData, gameVersion: string) {
|
|
||||||
super(data, gameVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
override applyMigration(data: SystemSaveData, curVersion: number[]): void {
|
override applyMigration(data: SystemSaveData, curVersion: number[]): void {
|
||||||
const [ curMajor, curMinor, curPatch ] = curVersion;
|
const [ curMajor, curMinor, curPatch ] = curVersion;
|
||||||
|
|
||||||
switch (curMajor) {
|
if (curMajor === 1) {
|
||||||
case 1:
|
if (curMinor === 0) {
|
||||||
switch (curMinor) {
|
|
||||||
case 0:
|
|
||||||
if (curPatch <= 4) {
|
if (curPatch <= 4) {
|
||||||
console.log("Applying v1.0.4 system data migraton!");
|
console.log("Applying v1.0.4 system data migraton!");
|
||||||
this.callMigrators(data, v1_0_4.systemMigrators);
|
this.callMigrators(data, v1_0_4.systemMigrators);
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`System data successfully migrated to v${version}!`);
|
console.log(`System data successfully migrated to v${version}!`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SettingsVersionConverter extends VersionConverter {
|
class SettingsVersionConverter extends VersionConverter {
|
||||||
constructor(data: SystemSaveData) {
|
|
||||||
const gameVersion = data.hasOwnProperty("gameVersion") ? data["gameVersion"] : "1.0.0";
|
|
||||||
super(data, gameVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
override applyMigration(data: Object, curVersion: number[]): void {
|
override applyMigration(data: Object, curVersion: number[]): void {
|
||||||
const [ curMajor, curMinor, curPatch ] = curVersion;
|
const [ curMajor, curMinor, curPatch ] = curVersion;
|
||||||
|
|
||||||
switch (curMajor) {
|
if (curMajor === 1) {
|
||||||
case 1:
|
if (curMinor === 0) {
|
||||||
switch (curMinor) {
|
|
||||||
case 0:
|
|
||||||
if (curPatch <= 4) {
|
if (curPatch <= 4) {
|
||||||
console.log("Applying v1.0.4 settings data migraton!");
|
console.log("Applying v1.0.4 settings data migraton!");
|
||||||
this.callMigrators(data, v1_0_4.sessionMigrators);
|
this.callMigrators(data, v1_0_4.settingsMigrators);
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`System data successfully migrated to v${version}!`);
|
console.log(`System data successfully migrated to v${version}!`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user