ds4/src/migration/006.js
Johannes Loher 6277e27056 refactor: convert to ECMAScript where necessary
Also drop @league-of-foundry-developers/foundry-vtt-types.
2022-11-28 02:38:17 +01:00

127 lines
3.6 KiB
JavaScript

// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT
import {
getActorUpdateDataGetter,
getCompendiumMigrator,
getSceneUpdateDataGetter,
migrateActors,
migrateCompendiums,
migrateItems,
migrateScenes,
} from "./migrationHelpers";
/** @type {import("./migration").Migration["migrate"]} */
async function migrate() {
await migrateItems(getItemUpdateData);
await migrateActors(getActorUpdateData);
await migrateScenes(getSceneUpdateData);
await migrateCompendiums(migrateCompendium);
}
/** @type {import("./migrationHelpers").ItemUpdateDataGetter} */
function getItemUpdateData(itemData) {
if (itemData.type !== "spell") return;
const spellCategory = itemData.data?.spellCategory;
const spellGroups = migrateSpellCategory(spellCategory);
// @ts-expect-error bonus is removed with this migration
const bonus = itemData.data?.bonus;
const spellModifier = migrateBonus(bonus);
const updateData = {
data: {
spellGroups,
"-=spellCategory": null,
spellModifier,
"-=bonus": null,
},
};
return updateData;
}
/**
* Migrate a spell category to spell groups.
* @param {string | undefined} spellCategory The spell category
* @returns {import("../documents/item/spell/spell-data-source").DS4SpellDataSourceData["spellGroups"]} The spell groups for the given category
*/
function migrateSpellCategory(spellCategory) {
const spellGroups = {
lightning: false,
earth: false,
water: false,
ice: false,
fire: false,
healing: false,
light: false,
air: false,
transport: false,
damage: false,
shadow: false,
protection: false,
mindAffecting: false,
demonology: false,
necromancy: false,
transmutation: false,
area: false,
};
switch (spellCategory) {
case "healing": {
spellGroups.healing = true;
break;
}
case "fire": {
spellGroups.fire = true;
break;
}
case "ice": {
spellGroups.ice = true;
break;
}
case "light": {
spellGroups.light = true;
break;
}
case "darkness": {
spellGroups.shadow = true;
break;
}
case "mindAffecting": {
spellGroups.mindAffecting = true;
break;
}
case "electricity": {
spellGroups.lightning = true;
break;
}
}
return spellGroups;
}
/**
* Migrate a spell bonus to a spell modifier.
* @param {string | undefined} bonus The spell bonus
* @returns {import("../documents/item/spell/spell-data-source").DS4SpellDataSourceData["spellModifier"]} The spell modifier
*/
function migrateBonus(bonus) {
const spellModifier = { numerical: 0, complex: "" };
if (bonus) {
if (Number.isNumeric(bonus)) {
spellModifier.numerical = +bonus;
} else {
spellModifier.complex = bonus;
}
}
return spellModifier;
}
const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
/** @type {import("./migration").Migration} */
export const migration = {
migrate,
migrateCompendium,
};