Skip to content

Commit

Permalink
Merge pull request #322 from Wasteland-Ventures-Group/315-refactor-migr
Browse files Browse the repository at this point in the history
Use source values for migrations
  • Loading branch information
kmoschcau authored May 28, 2022
2 parents 1e53305 + 1795039 commit 125db0a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- changes to permanent SPECIAL also change the temporary SPECIAL before its own
modifiers

### Fixed

- migrations using derived values, they are now only using source values and are
more robust because of that

## [0.16.1] - 2022-04-24

### Fixed
Expand Down
8 changes: 4 additions & 4 deletions src/main/typescript/item/wvItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ export async function getUpdateDataFromCompendium(

const updateData = { data: document.toObject().data };
if (!item.getFlag(CONSTANTS.systemId, "overwriteNotesWithCompendium")) {
updateData.data.notes = item.data.data.notes;
updateData.data.notes = item.data._source.data.notes;
}
if (!item.getFlag(CONSTANTS.systemId, "overwriteRulesWithCompendium")) {
updateData.data.rules.sources = item.data.data.rules.sources;
updateData.data.rules.sources = item.data._source.data.rules.sources;
}
if ("amount" in updateData.data && "amount" in item.data.data) {
if ("amount" in updateData.data && "amount" in item.data._source.data) {
if (!item.getFlag(CONSTANTS.systemId, "overwriteAmountWithCompendium")) {
updateData.data.amount = item.data.data.amount;
updateData.data.amount = item.data._source.data.amount;
}
}
return updateData;
Expand Down
30 changes: 12 additions & 18 deletions src/main/typescript/migrations/actors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CONSTANTS } from "../constants.js";
import { CONSTANTS, SpecialName, SpecialNames } from "../constants.js";
import { LOG } from "../systemLogger.js";

export default function migrateActors(currentVersion: string): void {
Expand Down Expand Up @@ -76,7 +76,7 @@ function migrateVitalsToResources(
actor: foundry.documents.BaseActor,
updateData: Record<string, unknown>
): void {
const vitals = actor.data.data.vitals;
const vitals = actor.data._source.data.vitals;
if (typeof vitals?.actionPoints === "number")
updateData["data.vitals.actionPoints.value"] = vitals.actionPoints;
if (typeof vitals?.hitPoints === "number")
Expand All @@ -101,40 +101,34 @@ function removeHistory(
actor: foundry.documents.BaseActor,
updateData: Record<string, unknown>
): void {
if ("history" in actor.data.data.background)
if ("history" in actor.data._source.data.background)
updateData["data.background.-=history"] = null;
}

function migrateSpecials(
actor: foundry.documents.BaseActor,
updateData: Record<string, unknown>
): void {
const specials = actor.data.data.specials;

for (const special of [
"strength",
"perception",
"endurance",
"charisma",
"intelligence",
"agility",
"luck"
] as const) {
const sourceData = actor.data._source.data;
if (!("specials" in sourceData)) return;

const specials = (sourceData as { specials: Record<SpecialName, number> })
.specials;

for (const special of SpecialNames) {
if (typeof specials?.[special] === "number") {
updateData[`data.leveling.specialPoints.${special}`] = specials[special];
updateData[`data.specials.-=${special}`] = null;
}
}

if ("specials" in actor.data._source.data)
updateData["data.-=specials"] = null;
updateData["data.-=specials"] = null;
}

function migrateToCompositeNumbers(
actor: foundry.documents.BaseActor,
updateData: Record<string, unknown>
) {
const background = actor.data.data.background;
const background = actor.data._source.data.background;
if (typeof background.size === "number")
updateData["data.background.size.source"] = background.size;
}
21 changes: 12 additions & 9 deletions src/main/typescript/migrations/items.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Caliber, CONSTANTS, ProtoItemTypes } from "../constants.js";
import type { AmmoDataSourceData } from "../data/item/ammo/source.js";
import type { ApparelDataSourceData } from "../data/item/apparel/source.js";
import type {
DistanceSource,
RangeSource
Expand Down Expand Up @@ -105,8 +106,10 @@ function migrateRuleElementHook(
item: foundry.documents.BaseItem,
updateData: Record<string, unknown>
) {
if (item.data.data.rules.sources.some((rule) => rule.hook === undefined)) {
updateData["data.rules.sources"] = item.data.data.rules.sources.map(
if (
item.data._source.data.rules.sources.some((rule) => rule.hook === undefined)
) {
updateData["data.rules.sources"] = item.data._source.data.rules.sources.map(
(rule) => ({
// @ts-expect-error This might not be there in not migrated data
hook: "afterSpecial",
Expand Down Expand Up @@ -156,13 +159,13 @@ function migrateAmmoFix(
if (!["ammo", "weapon"].includes(item.type)) return;

if (item.type === "ammo") {
const data = item.data.data as AmmoDataSourceData;
const data = item.data._source.data as AmmoDataSourceData;
const newCaliber = transformCaliber(data.caliber);
if (!newCaliber) return;

updateData["data.caliber"] = newCaliber;
} else if (item.type === "weapon") {
const data = item.data.data as WeaponDataSourceData;
const data = item.data._source.data as WeaponDataSourceData;
if (!data.reload) return;

const newCaliber = transformCaliber(data.reload.caliber);
Expand All @@ -186,7 +189,7 @@ function migrateRanges(
) {
if (item.type !== "weapon") return;

const data = item.data.data as WeaponDataSourceData;
const data = item.data._source.data as WeaponDataSourceData;
const newShortRange = transformRange(data.ranges.short);
const newMediumRange = transformRange(data.ranges.medium);
const newLongRange = transformRange(data.ranges.long);
Expand Down Expand Up @@ -256,7 +259,7 @@ function migrateMandatoryReload(
) {
if (item.type !== "weapon") return;

const data = item.data.data as WeaponDataSourceData;
const data = item.data._source.data as WeaponDataSourceData;
if (data.reload) return;

updateData["data.reload"] = {
Expand All @@ -271,14 +274,14 @@ function migrateToCompositeNumbers(
item: foundry.documents.BaseItem,
updateData: Record<string, unknown>
) {
const data = item.data.data;
const data = item.data._source.data;
if ("value" in data && typeof data.value === "number")
updateData["data.value.source"] = data.value;
if ("weight" in data && typeof data.weight === "number")
updateData["data.weight.source"] = data.weight;

if (item.data.type === "apparel") {
const data = item.data.data;
const data = item.data._source.data as ApparelDataSourceData;
if (typeof data.damageThreshold === "number")
updateData["data.damageThreshold.source"] = data.damageThreshold;
if (typeof data.quickSlots === "number")
Expand All @@ -289,7 +292,7 @@ function migrateToCompositeNumbers(
}

if (item.data.type === "weapon") {
const data = item.data.data;
const data = item.data._source.data as WeaponDataSourceData;

if (typeof data.strengthRequirement === "number")
updateData["data.strengthRequirement.source"] = data.strengthRequirement;
Expand Down
4 changes: 2 additions & 2 deletions src/main/typescript/migrations/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export async function migrateWorld(): Promise<void> {
`Migrating this world to ${currentVersion}. Last migration was ${lastMigrVersion}`
);

await migrateActors(currentVersion);
await migrateItems(currentVersion);
migrateActors(currentVersion);
migrateItems(currentVersion);

setMigrationCurrentVersion();

Expand Down

0 comments on commit 125db0a

Please sign in to comment.