refactor: convert to ECMAScript where necessary
Also drop @league-of-foundry-developers/foundry-vtt-types.
This commit is contained in:
parent
df4538f6ed
commit
6277e27056
69 changed files with 1077 additions and 1679 deletions
|
@ -10,13 +10,15 @@ import {
|
|||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/** @type {import("./migration").Migration["migrate"]} */
|
||||
async function migrate() {
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getActorUpdateData(): Record<string, unknown> {
|
||||
/** @type {import("./migrationHelpers").ActorUpdateDataGetter} */
|
||||
function getActorUpdateData() {
|
||||
const updateData = {
|
||||
data: {
|
||||
combatValues: [
|
||||
|
@ -28,7 +30,7 @@ function getActorUpdateData(): Record<string, unknown> {
|
|||
"rangedAttack",
|
||||
"spellcasting",
|
||||
"targetedSpellcasting",
|
||||
].reduce((acc: Partial<Record<string, { "-=base": null }>>, curr) => {
|
||||
].reduce((acc, curr) => {
|
||||
acc[curr] = { "-=base": null };
|
||||
return acc;
|
||||
}, {}),
|
||||
|
@ -40,6 +42,7 @@ function getActorUpdateData(): Record<string, unknown> {
|
|||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
/** @type {import("./migration").Migration} */
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
|
@ -12,18 +12,18 @@ import {
|
|||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/** @type {import("./migration").Migration["migrate"]} */
|
||||
async function migrate() {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(
|
||||
itemData: Partial<foundry.data.ItemData["_source"]>,
|
||||
): DeepPartial<foundry.data.ItemData["_source"]> | undefined {
|
||||
/** @type {import("./migrationHelpers").ItemUpdateDataGetter} */
|
||||
function getItemUpdateData(itemData) {
|
||||
if (!["equipment", "trinket"].includes(itemData.type ?? "")) return undefined;
|
||||
return { type: itemData.type === "equipment" ? ("loot" as const) : ("equipment" as const) };
|
||||
return { type: itemData.type === "equipment" ? "loot" : "equipment" };
|
||||
}
|
||||
|
||||
const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
||||
|
@ -33,6 +33,7 @@ const migrateCompendium = getCompendiumMigrator(
|
|||
{ migrateToTemplateEarly: false },
|
||||
);
|
||||
|
||||
/** @type {import("./migration").Migration} */
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
|
@ -12,14 +12,16 @@ import {
|
|||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/** @type {import("./migration").Migration["migrate"]} */
|
||||
async function migrate() {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
/** @type {import("./migrationHelpers").ItemUpdateDataGetter} */
|
||||
function getItemUpdateData(itemData) {
|
||||
if (!["loot"].includes(itemData.type ?? "")) return undefined;
|
||||
return {
|
||||
data: {
|
||||
|
@ -35,6 +37,7 @@ const migrateCompendium = getCompendiumMigrator(
|
|||
{ migrateToTemplateEarly: false },
|
||||
);
|
||||
|
||||
/** @type {import("./migration").Migration} */
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
|
@ -12,19 +12,20 @@ import {
|
|||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/** @type {import("./migration").Migration["migrate"]} */
|
||||
async function migrate() {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
/** @type {import("./migrationHelpers").ItemUpdateDataGetter} */
|
||||
function getItemUpdateData(itemData) {
|
||||
if (itemData.type !== "spell") return;
|
||||
// @ts-expect-error the type of cooldownDuration was UnitData<TemporalUnit> at the point for this migration, but it changed later on
|
||||
const cooldownDurationUnit: string | undefined = itemData.data?.cooldownDuration.unit;
|
||||
const cooldownDurationUnit = itemData.data?.cooldownDuration.unit;
|
||||
|
||||
const updateData: Record<string, unknown> = {
|
||||
const updateData = {
|
||||
data: {
|
||||
"-=scrollPrice": null,
|
||||
minimumLevels: { healer: null, wizard: null, sorcerer: null },
|
||||
|
@ -40,6 +41,7 @@ const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
|||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
/** @type {import("./migration").Migration} */
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
|
@ -21,22 +21,22 @@ const hoursPerDay = 24;
|
|||
const roundsPerDay = hoursPerDay / roundsPerHour;
|
||||
const secondsPerDay = secondsPerMinute * minutesPerHour * hoursPerDay;
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/** @type {import("./migration").Migration["migrate"]} */
|
||||
async function migrate() {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
/** @type {import("./migrationHelpers").ItemUpdateDataGetter} */
|
||||
function getItemUpdateData(itemData) {
|
||||
if (itemData.type !== "spell") return;
|
||||
// @ts-expect-error the type of cooldownDuration is changed from UnitData<TemporalUnit> to CooldownDuation with this migration
|
||||
const cooldownDurationUnit: string | undefined = itemData.data?.cooldownDuration.unit;
|
||||
// @ts-expect-error the type of cooldownDuration is changed from UnitData<TemporalUnit> to CooldownDuation with this migration
|
||||
const cooldownDurationValue: string | undefined = itemData.data?.cooldownDuration.value;
|
||||
const cooldownDurationUnit = itemData.data?.cooldownDuration.unit;
|
||||
const cooldownDurationValue = itemData.data?.cooldownDuration.value;
|
||||
const cooldownDuration = migrateCooldownDuration(cooldownDurationValue, cooldownDurationUnit);
|
||||
|
||||
const updateData: Record<string, unknown> = {
|
||||
const updateData = {
|
||||
data: {
|
||||
cooldownDuration,
|
||||
},
|
||||
|
@ -88,7 +88,13 @@ function migrateCooldownDuration(cooldownDurationValue = "", cooldownDurationUni
|
|||
}
|
||||
}
|
||||
|
||||
function getRounds(unit: string, value: number): number {
|
||||
/**
|
||||
* Given a unit and a value, return the correct number of rounds
|
||||
* @param {string} unit The unit
|
||||
* @param {number} value The value
|
||||
* @returns {number} The number of rounds
|
||||
*/
|
||||
function getRounds(unit, value) {
|
||||
switch (unit) {
|
||||
case "rounds": {
|
||||
return value;
|
||||
|
@ -112,6 +118,7 @@ const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
|||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
/** @type {import("./migration").Migration} */
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
|
@ -12,26 +12,25 @@ import {
|
|||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
import type { DS4SpellDataSourceData } from "../documents/item/spell/spell-data-source";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/** @type {import("./migration").Migration["migrate"]} */
|
||||
async function migrate() {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
/** @type {import("./migrationHelpers").ItemUpdateDataGetter} */
|
||||
function getItemUpdateData(itemData) {
|
||||
if (itemData.type !== "spell") return;
|
||||
// @ts-expect-error spellCategory is removed with this migration
|
||||
const spellCategory: string | undefined = itemData.data?.spellCategory;
|
||||
const spellCategory = itemData.data?.spellCategory;
|
||||
const spellGroups = migrateSpellCategory(spellCategory);
|
||||
|
||||
// @ts-expect-error bonus is removed with this migration
|
||||
const bonus: string | undefined = itemData.data?.bonus;
|
||||
const bonus = itemData.data?.bonus;
|
||||
const spellModifier = migrateBonus(bonus);
|
||||
|
||||
const updateData: Record<string, unknown> = {
|
||||
const updateData = {
|
||||
data: {
|
||||
spellGroups,
|
||||
"-=spellCategory": null,
|
||||
|
@ -42,7 +41,12 @@ function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>)
|
|||
return updateData;
|
||||
}
|
||||
|
||||
function migrateSpellCategory(spellCategory: string | undefined): DS4SpellDataSourceData["spellGroups"] {
|
||||
/**
|
||||
* 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,
|
||||
|
@ -95,7 +99,12 @@ function migrateSpellCategory(spellCategory: string | undefined): DS4SpellDataSo
|
|||
return spellGroups;
|
||||
}
|
||||
|
||||
function migrateBonus(bonus: string | undefined): DS4SpellDataSourceData["spellModifier"] {
|
||||
/**
|
||||
* 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)) {
|
||||
|
@ -111,6 +120,7 @@ const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
|||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
/** @type {import("./migration").Migration} */
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
|
@ -12,14 +12,16 @@ import {
|
|||
migrateScenes,
|
||||
} from "./migrationHelpers";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/** @type {import("./migration").Migration["migrate"]} */
|
||||
async function migrate() {
|
||||
await migrateItems(getItemUpdateData);
|
||||
await migrateActors(getActorUpdateData);
|
||||
await migrateScenes(getSceneUpdateData);
|
||||
await migrateCompendiums(migrateCompendium);
|
||||
}
|
||||
|
||||
function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
|
||||
/** @type {import("./migrationHelpers").ItemUpdateDataGetter} */
|
||||
function getItemUpdateData(itemData) {
|
||||
if (itemData.type !== "spell") return;
|
||||
|
||||
return {
|
||||
|
@ -33,6 +35,7 @@ const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
|
|||
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
|
||||
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });
|
||||
|
||||
/** @type {import("./migration").Migration} */
|
||||
export const migration = {
|
||||
migrate,
|
||||
migrateCompendium,
|
|
@ -13,7 +13,11 @@ import { migration as migration005 } from "./005";
|
|||
import { migration as migration006 } from "./006";
|
||||
import { migration as migration007 } from "./007";
|
||||
|
||||
async function migrate(): Promise<void> {
|
||||
/**
|
||||
* Perform migrations.
|
||||
* @returns {Promise<void>} A promise that resolves once all migrations have completed
|
||||
*/
|
||||
async function migrate() {
|
||||
if (!getGame().user?.isGM) {
|
||||
return;
|
||||
}
|
||||
|
@ -30,7 +34,13 @@ async function migrate(): Promise<void> {
|
|||
return migrateFromTo(oldMigrationVersion, targetMigrationVersion);
|
||||
}
|
||||
|
||||
async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion: number): Promise<void> {
|
||||
/**
|
||||
* Migrate from a given version to another version.
|
||||
* @param {number} oldMigrationVersion The old migration version
|
||||
* @param {number} targetMigrationVersion The migration version to migrate to
|
||||
* @returns {Promise<void>} A promise the resolves once the migration is complete
|
||||
*/
|
||||
async function migrateFromTo(oldMigrationVersion, targetMigrationVersion) {
|
||||
if (!getGame().user?.isGM) {
|
||||
return;
|
||||
}
|
||||
|
@ -76,11 +86,14 @@ async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion
|
|||
}
|
||||
}
|
||||
|
||||
async function migrateCompendiumFromTo(
|
||||
pack: CompendiumCollection<CompendiumCollection.Metadata>,
|
||||
oldMigrationVersion: number,
|
||||
targetMigrationVersion: number,
|
||||
): Promise<void> {
|
||||
/**
|
||||
* Migrate a compendium pack from a given version to another version.
|
||||
* @param {CompendiumCollection} pack The compendium pack to migrate
|
||||
* @param {number} oldMigrationVersion The old version number
|
||||
* @param {number} targetMigrationVersion The target version number
|
||||
* @returns {Promise<void>} A promise that resolves once the migration is complete
|
||||
*/
|
||||
async function migrateCompendiumFromTo(pack, oldMigrationVersion, targetMigrationVersion) {
|
||||
if (!getGame().user?.isGM) {
|
||||
return;
|
||||
}
|
||||
|
@ -128,30 +141,39 @@ async function migrateCompendiumFromTo(
|
|||
}
|
||||
}
|
||||
|
||||
function getCurrentMigrationVersion(): number {
|
||||
/**
|
||||
* Get the current migration version.
|
||||
* @returns {number} The current migration version
|
||||
*/
|
||||
function getCurrentMigrationVersion() {
|
||||
return getGame().settings.get("ds4", "systemMigrationVersion");
|
||||
}
|
||||
|
||||
function getTargetMigrationVersion(): number {
|
||||
/**
|
||||
* Get the target migration version.
|
||||
* @returns {number} The target migration version
|
||||
*/
|
||||
function getTargetMigrationVersion() {
|
||||
return migrations.length;
|
||||
}
|
||||
|
||||
interface Migration {
|
||||
migrate: () => Promise<void>;
|
||||
migrateCompendium: (pack: CompendiumCollection<CompendiumCollection.Metadata>) => Promise<void>;
|
||||
}
|
||||
/**
|
||||
* @typedef {object} Migration
|
||||
* @property {() => Promise<void>} migrate
|
||||
* @property {import("./migrationHelpers").CompendiumMigrator} migrateCompendium
|
||||
*/
|
||||
|
||||
const migrations: Migration[] = [
|
||||
migration001,
|
||||
migration002,
|
||||
migration003,
|
||||
migration004,
|
||||
migration005,
|
||||
migration006,
|
||||
migration007,
|
||||
];
|
||||
/**
|
||||
* @type {Migration[]}
|
||||
*/
|
||||
const migrations = [migration001, migration002, migration003, migration004, migration005, migration006, migration007];
|
||||
|
||||
function isFirstWorldStart(migrationVersion: number): boolean {
|
||||
/**
|
||||
* DOes the migration version indicate the world is being started for the first time?
|
||||
* @param {number} migrationVersion A migration version
|
||||
* @returns {boolean} Whether the migration version indicates it is the first start of the world
|
||||
*/
|
||||
function isFirstWorldStart(migrationVersion) {
|
||||
return migrationVersion < 0;
|
||||
}
|
||||
|
|
@ -7,11 +7,14 @@ import { DS4Item } from "../documents/item/item";
|
|||
import { logger } from "../utils/logger";
|
||||
import { getGame } from "../utils/utils";
|
||||
|
||||
type ItemUpdateDataGetter = (
|
||||
itemData: Partial<foundry.data.ItemData["_source"]>,
|
||||
) => DeepPartial<foundry.data.ItemData["_source"]> | Record<string, unknown> | undefined;
|
||||
/** @typedef {(itemData: Partial<foundry.data.ItemData["_source"]>) => DeepPartial<foundry.data.ItemData["_source"]> | Record<string, unknown> | undefined} ItemUpdateDataGetter */
|
||||
|
||||
export async function migrateItems(getItemUpdateData: ItemUpdateDataGetter): Promise<void> {
|
||||
/**
|
||||
* Migrate world items.
|
||||
* @param {ItemUpdateDataGetter} getItemUpdateData A function for getting the update data for a given item data object
|
||||
* @returns {Promise<void>} A promise that resolves once the migration is complete
|
||||
*/
|
||||
export async function migrateItems(getItemUpdateData) {
|
||||
for (const item of getGame().items ?? []) {
|
||||
try {
|
||||
const updateData = getItemUpdateData(item.toObject());
|
||||
|
@ -25,11 +28,14 @@ export async function migrateItems(getItemUpdateData: ItemUpdateDataGetter): Pro
|
|||
}
|
||||
}
|
||||
|
||||
type ActorUpdateDataGetter = (
|
||||
itemData: Partial<foundry.data.ActorData["_source"]>,
|
||||
) => DeepPartial<foundry.data.ActorData["_source"]> | undefined;
|
||||
/** @typedef {(actorData: Partial<foundry.data.ActorData["_source"]>) => DeepPartial<foundry.data.ActorData["_source"]> | undefined} ActorUpdateDataGetter */
|
||||
|
||||
export async function migrateActors(getActorUpdateData: ActorUpdateDataGetter): Promise<void> {
|
||||
/**
|
||||
* Migrate world actors.
|
||||
* @param {ActorUpdateDataGetter} getActorUpdateData A function for getting the update data for a given actor data object
|
||||
* @returns {Promise<void>} A promise that resolves once the migration is complete
|
||||
*/
|
||||
export async function migrateActors(getActorUpdateData) {
|
||||
for (const actor of getGame().actors ?? []) {
|
||||
try {
|
||||
const updateData = getActorUpdateData(actor.toObject());
|
||||
|
@ -46,17 +52,20 @@ export async function migrateActors(getActorUpdateData: ActorUpdateDataGetter):
|
|||
}
|
||||
}
|
||||
|
||||
type SceneUpdateDataGetter = (sceneData: foundry.data.SceneData) => DeepPartial<foundry.data.SceneData["_source"]>;
|
||||
/** @typedef {(aceneData: foundry.data.SceneData) => DeepPartial<foundry.data.SceneData["_source"]> | undefined} SceneUpdateDataGetter */
|
||||
|
||||
export async function migrateScenes(getSceneUpdateData: SceneUpdateDataGetter): Promise<void> {
|
||||
/**
|
||||
* Migrate world scenes.
|
||||
* @param {SceneUpdateDataGetter} getSceneUpdateData A function for getting the update data for a given scene data object
|
||||
* @returns {Promise<void>} A promise that resolves once the migration is complete
|
||||
*/
|
||||
export async function migrateScenes(getSceneUpdateData) {
|
||||
for (const scene of getGame().scenes ?? []) {
|
||||
try {
|
||||
const updateData = getSceneUpdateData(scene.data);
|
||||
if (updateData) {
|
||||
logger.info(`Migrating Scene document ${scene.name} (${scene.id})`);
|
||||
await scene.update(
|
||||
updateData as DeepPartial<Parameters<foundry.data.SceneData["_initializeSource"]>[0]>,
|
||||
);
|
||||
await scene.update(updateData);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
|
@ -67,9 +76,14 @@ export async function migrateScenes(getSceneUpdateData: SceneUpdateDataGetter):
|
|||
}
|
||||
}
|
||||
|
||||
type CompendiumMigrator = (compendium: CompendiumCollection<CompendiumCollection.Metadata>) => Promise<void>;
|
||||
/** @typedef {(pack: CompendiumCollection) => Promise<void>} CompendiumMigrator*/
|
||||
|
||||
export async function migrateCompendiums(migrateCompendium: CompendiumMigrator): Promise<void> {
|
||||
/**
|
||||
* Migrate world compendium packs.
|
||||
* @param {CompendiumMigrator} migrateCompendium A function for migrating a single compendium pack
|
||||
* @returns {Promise<void>} A promise that resolves once the migration is complete
|
||||
*/
|
||||
export async function migrateCompendiums(migrateCompendium) {
|
||||
for (const compendium of getGame().packs ?? []) {
|
||||
if (compendium.metadata.package !== "world") continue;
|
||||
if (!["Actor", "Item", "Scene"].includes(compendium.metadata.type)) continue;
|
||||
|
@ -77,10 +91,13 @@ export async function migrateCompendiums(migrateCompendium: CompendiumMigrator):
|
|||
}
|
||||
}
|
||||
|
||||
export function getActorUpdateDataGetter(getItemUpdateData: ItemUpdateDataGetter): ActorUpdateDataGetter {
|
||||
return (
|
||||
actorData: Partial<foundry.data.ActorData["_source"]>,
|
||||
): DeepPartial<foundry.data.ActorData["_source"]> | undefined => {
|
||||
/**
|
||||
* Get a function to create actor update data that adjusts the owned items of the actor according to the given function.
|
||||
* @param {ItemUpdateDataGetter} getItemUpdateData The function to generate item update data
|
||||
* @returns {ActorUpdateDataGetter} A function to get actor update data
|
||||
*/
|
||||
export function getActorUpdateDataGetter(getItemUpdateData) {
|
||||
return (actorData) => {
|
||||
let hasItemUpdates = false;
|
||||
const items = actorData.items?.map((itemData) => {
|
||||
const update = getItemUpdateData(itemData);
|
||||
|
@ -95,9 +112,14 @@ export function getActorUpdateDataGetter(getItemUpdateData: ItemUpdateDataGetter
|
|||
};
|
||||
}
|
||||
|
||||
export function getSceneUpdateDataGetter(getActorUpdateData: ActorUpdateDataGetter): SceneUpdateDataGetter {
|
||||
return (sceneData: foundry.data.SceneData) => {
|
||||
const tokens = sceneData.tokens.map((token: TokenDocument) => {
|
||||
/**
|
||||
* Get a function to create scene update data that adjusts the actors of the tokens of the scene according to the given function.
|
||||
* @param {ActorUpdateDataGetter} getItemUpdateData The function to generate actor update data
|
||||
* @returns {SceneUpdateDataGetter} A function to get scene update data
|
||||
*/
|
||||
export function getSceneUpdateDataGetter(getActorUpdateData) {
|
||||
return (sceneData) => {
|
||||
const tokens = sceneData.tokens.map((token) => {
|
||||
const t = token.toObject();
|
||||
if (!t.actorId || t.actorLink) {
|
||||
t.actorData = {};
|
||||
|
@ -109,7 +131,7 @@ export function getSceneUpdateDataGetter(getActorUpdateData: ActorUpdateDataGett
|
|||
actorData.type = token.actor?.type;
|
||||
const update = getActorUpdateData(actorData);
|
||||
if (update !== undefined) {
|
||||
["items" as const, "effects" as const].forEach((embeddedName) => {
|
||||
["items", "effects"].forEach((embeddedName) => {
|
||||
const embeddedUpdates = update[embeddedName];
|
||||
if (embeddedUpdates === undefined || !embeddedUpdates.length) return;
|
||||
const updates = new Map(embeddedUpdates.flatMap((u) => (u && u._id ? [[u._id, u]] : [])));
|
||||
|
@ -131,32 +153,37 @@ export function getSceneUpdateDataGetter(getActorUpdateData: ActorUpdateDataGett
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {object} UpdateDataGetters
|
||||
* @property {ItemUpdateDataGetter} [getItemUpdateData]
|
||||
* @property {ActorUpdateDataGetter} [getActorUpdateData]
|
||||
* @property {SceneUpdateDataGetter} [getSceneUpdateData]
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get a compendium migrator for the given update data getters.
|
||||
* @param {UpdateDataGetters} [updateDataGetters={}] The functions to use for getting update data
|
||||
* @param {{migrateToTemplateEarly?: boolean}} [options={}] Additional options for the compendium migrator
|
||||
* @returns {CompendiumMigrator} The resulting compendium migrator
|
||||
*/
|
||||
export function getCompendiumMigrator(
|
||||
{
|
||||
getItemUpdateData,
|
||||
getActorUpdateData,
|
||||
getSceneUpdateData,
|
||||
}: {
|
||||
getItemUpdateData?: ItemUpdateDataGetter;
|
||||
getActorUpdateData?: ActorUpdateDataGetter;
|
||||
getSceneUpdateData?: SceneUpdateDataGetter;
|
||||
} = {},
|
||||
{ getItemUpdateData, getActorUpdateData, getSceneUpdateData } = {},
|
||||
{ migrateToTemplateEarly = true } = {},
|
||||
) {
|
||||
return async (compendium: CompendiumCollection<CompendiumCollection.Metadata>): Promise<void> => {
|
||||
const type = compendium.metadata.type;
|
||||
return async (pack) => {
|
||||
const type = pack.metadata.type;
|
||||
if (!["Actor", "Item", "Scene"].includes(type)) return;
|
||||
const wasLocked = compendium.locked;
|
||||
await compendium.configure({ locked: false });
|
||||
const wasLocked = pack.locked;
|
||||
await pack.configure({ locked: false });
|
||||
if (migrateToTemplateEarly) {
|
||||
await compendium.migrate();
|
||||
await pack.migrate();
|
||||
}
|
||||
|
||||
const documents = await compendium.getDocuments();
|
||||
const documents = await pack.getDocuments();
|
||||
|
||||
for (const doc of documents) {
|
||||
try {
|
||||
logger.info(`Migrating document ${doc.name} (${doc.id}) in compendium ${compendium.collection}`);
|
||||
logger.info(`Migrating document ${doc.name} (${doc.id}) in compendium ${pack.collection}`);
|
||||
if (doc instanceof DS4Item && getItemUpdateData) {
|
||||
const updateData = getItemUpdateData(doc.toObject());
|
||||
updateData && (await doc.update(updateData));
|
||||
|
@ -164,23 +191,20 @@ export function getCompendiumMigrator(
|
|||
const updateData = getActorUpdateData(doc.toObject());
|
||||
updateData && (await doc.update(updateData));
|
||||
} else if (doc instanceof Scene && getSceneUpdateData) {
|
||||
const updateData = getSceneUpdateData(doc.data as foundry.data.SceneData);
|
||||
updateData &&
|
||||
(await doc.update(
|
||||
updateData as DeepPartial<Parameters<foundry.data.SceneData["_initializeSource"]>[0]>,
|
||||
));
|
||||
const updateData = getSceneUpdateData(doc.data);
|
||||
updateData && (await doc.update(updateData));
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`Error during migration of document ${doc.name} (${doc.id}) in compendium ${compendium.collection}, continuing anyways.`,
|
||||
`Error during migration of document ${doc.name} (${doc.id}) in compendium ${pack.collection}, continuing anyways.`,
|
||||
err,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!migrateToTemplateEarly) {
|
||||
await compendium.migrate();
|
||||
await pack.migrate();
|
||||
}
|
||||
await compendium.configure({ locked: wasLocked });
|
||||
await pack.configure({ locked: wasLocked });
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue