Skip to content

Commit

Permalink
Clean Up & Organize Version Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
xsn34kzx committed Sep 27, 2024
1 parent dad81f3 commit 3cc0232
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 145 deletions.
8 changes: 4 additions & 4 deletions src/system/game-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { WeatherType } from "#app/enums/weather-type";
import { TerrainType } from "#app/data/terrain";
import { ReloadSessionPhase } from "#app/phases/reload-session-phase";
import { RUN_HISTORY_LIMIT } from "#app/ui/run-history-ui-handler";
import { applySessionDataPatches, applySettingsDataPatches, applySystemDataPatches } from "./version-converter";
import { SessionVersionConverter, SettingsVersionConverter, SystemVersionConverter } from "./version_migration/version_converter";
import { MysteryEncounterSaveData } from "../data/mystery-encounters/mystery-encounter-save-data";
import { MysteryEncounterType } from "#enums/mystery-encounter-type";

Expand Down Expand Up @@ -463,7 +463,7 @@ export class GameData {
localStorage.setItem(lsItemKey, "");
}

applySystemDataPatches(systemData);
new SystemVersionConverter(systemData, systemData.gameVersion);

this.trainerId = systemData.trainerId;
this.secretId = systemData.secretId;
Expand Down Expand Up @@ -838,7 +838,7 @@ export class GameData {

const settings = JSON.parse(localStorage.getItem("settings")!); // TODO: is this bang correct?

applySettingsDataPatches(settings);
new SettingsVersionConverter(settings);

for (const setting of Object.keys(settings)) {
setSetting(this.scene, setting, settings[setting]);
Expand Down Expand Up @@ -1270,7 +1270,7 @@ export class GameData {
return v;
}) as SessionSaveData;

applySessionDataPatches(sessionData);
new SessionVersionConverter(sessionData, sessionData.gameVersion);

return sessionData;
}
Expand Down
141 changes: 0 additions & 141 deletions src/system/version-converter.ts

This file was deleted.

61 changes: 61 additions & 0 deletions src/system/version_migration/v1_0_4/session_migrators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { SessionSaveData } from "../../game-data";

/**
* Converts old lapsing modifiers (battle items, lures, and Dire Hit) and
* other miscellaneous modifiers (vitamins, White Herb) to any new class
* names and/or change in reload arguments.
* @param data {@linkcode SessionSaveData}
*/
export function migrateModifiers(data: SessionSaveData) {
data.modifiers.forEach((m) => {
if (m.className === "PokemonBaseStatModifier") {
m.className = "BaseStatModifier";
} else if (m.className === "PokemonResetNegativeStatStageModifier") {
m.className = "ResetNegativeStatStageModifier";
} else if (m.className === "TempBattleStatBoosterModifier") {
const maxBattles = 5;
// Dire Hit no longer a part of the TempBattleStatBoosterModifierTypeGenerator
if (m.typeId !== "DIRE_HIT") {
m.className = "TempStatStageBoosterModifier";
m.typeId = "TEMP_STAT_STAGE_BOOSTER";

// Migration from TempBattleStat to Stat
const newStat = m.typePregenArgs[0] + 1;
m.typePregenArgs[0] = newStat;

// From [ stat, battlesLeft ] to [ stat, maxBattles, battleCount ]
m.args = [ newStat, maxBattles, Math.min(m.args[1], maxBattles) ];
} else {
m.className = "TempCritBoosterModifier";
m.typePregenArgs = [];

// From [ stat, battlesLeft ] to [ maxBattles, battleCount ]
m.args = [ maxBattles, Math.min(m.args[1], maxBattles) ];
}
} else if (m.className === "DoubleBattleChanceBoosterModifier" && m.args.length === 1) {
let maxBattles: number;
switch (m.typeId) {
case "MAX_LURE":
maxBattles = 30;
break;
case "SUPER_LURE":
maxBattles = 15;
break;
default:
maxBattles = 10;
break;
}

// From [ battlesLeft ] to [ maxBattles, battleCount ]
m.args = [ maxBattles, m.args[0] ];
}
});

data.enemyModifiers.forEach((m) => {
if (m.className === "PokemonBaseStatModifier") {
m.className = "BaseStatModifier";
} else if (m.className === "PokemonResetNegativeStatStageModifier") {
m.className = "ResetNegativeStatStageModifier";
}
});
}
14 changes: 14 additions & 0 deletions src/system/version_migration/v1_0_4/settings_migrators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SettingKeys } from "../../settings/settings";

/**
* Migrate from "REROLL_TARGET" property to {@linkcode
* SettingKeys.Shop_Cursor_Target}.
* @param data the `settings` object
*/
export function fixRerollTarget(data: Object) {
if (data.hasOwnProperty("REROLL_TARGET") && !data.hasOwnProperty(SettingKeys.Shop_Cursor_Target)) {
data[SettingKeys.Shop_Cursor_Target] = data["REROLL_TARGET"];
delete data["REROLL_TARGET"];
localStorage.setItem("settings", JSON.stringify(data));
}
}
56 changes: 56 additions & 0 deletions src/system/version_migration/v1_0_4/system_migrators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { AbilityAttr, defaultStarterSpecies, DexAttr, SystemSaveData } from "../../game-data";
import { allSpecies } from "../../../data/pokemon-species";

/**
* Migrate ability starter data if empty for caught species
* @param data {@linkcode SystemSaveData}
*/
export function migrateAbilityData(data: SystemSaveData) {
if (data.starterData && data.dexData) {
// Migrate ability starter data if empty for caught species
Object.keys(data.starterData).forEach(sd => {
if (data.dexData[sd]?.caughtAttr && (data.starterData[sd] && !data.starterData[sd].abilityAttr)) {
data.starterData[sd].abilityAttr = 1;
}
});
}
}

/**
* Populate legendary Pokémon statistics if they are missing
* @param data {@linkcode SystemSaveData}
*/
export function fixLegendaryStats(data: SystemSaveData) {
if (data.gameStats && (data.gameStats.legendaryPokemonCaught !== undefined && data.gameStats.subLegendaryPokemonCaught === undefined)) {
data.gameStats.subLegendaryPokemonSeen = 0;
data.gameStats.subLegendaryPokemonCaught = 0;
data.gameStats.subLegendaryPokemonHatched = 0;
allSpecies.filter(s => s.subLegendary).forEach(s => {
const dexEntry = data.dexData[s.speciesId];
data.gameStats.subLegendaryPokemonSeen += dexEntry.seenCount;
data.gameStats.legendaryPokemonSeen = Math.max(data.gameStats.legendaryPokemonSeen - dexEntry.seenCount, 0);
data.gameStats.subLegendaryPokemonCaught += dexEntry.caughtCount;
data.gameStats.legendaryPokemonCaught = Math.max(data.gameStats.legendaryPokemonCaught - dexEntry.caughtCount, 0);
data.gameStats.subLegendaryPokemonHatched += dexEntry.hatchedCount;
data.gameStats.legendaryPokemonHatched = Math.max(data.gameStats.legendaryPokemonHatched - dexEntry.hatchedCount, 0);
});
data.gameStats.subLegendaryPokemonSeen = Math.max(data.gameStats.subLegendaryPokemonSeen, data.gameStats.subLegendaryPokemonCaught);
data.gameStats.legendaryPokemonSeen = Math.max(data.gameStats.legendaryPokemonSeen, data.gameStats.legendaryPokemonCaught);
data.gameStats.mythicalPokemonSeen = Math.max(data.gameStats.mythicalPokemonSeen, data.gameStats.mythicalPokemonCaught);
}
}

/**
* Unlock all starters' first ability and female gender option
* @param data {@linkcode SystemSaveData}
*/
export function fixStarterData(data: SystemSaveData) {
for (const starterId of defaultStarterSpecies) {
if (data.starterData[starterId]?.abilityAttr) {
data.starterData[starterId].abilityAttr |= AbilityAttr.ABILITY_1;
}
if (data.dexData[starterId]?.caughtAttr) {
data.dexData[starterId].caughtAttr |= DexAttr.FEMALE;
}
}
}
Loading

0 comments on commit 3cc0232

Please sign in to comment.