From 76f42fe280593102e7c5095d1719628f78429ae4 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Wed, 30 Jun 2021 05:00:23 +0200 Subject: [PATCH] More WIP --- src/module/migrations/001.ts | 4 +-- src/module/migrations/002.ts | 41 ++++++++++++++--------- src/module/rolls/slaying-dice-modifier.ts | 2 +- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/module/migrations/001.ts b/src/module/migrations/001.ts index f210ec76..0bfb3a0f 100644 --- a/src/module/migrations/001.ts +++ b/src/module/migrations/001.ts @@ -5,10 +5,10 @@ import logger from "../logger"; export async function migrate(): Promise { - for (const a of game.actors ?? []) { + for (const a of game.actors?.contents ?? []) { const updateData = getActorUpdateData(); logger.info(`Migrating actor ${a.name}`); - await a.update(updateData, { enforceTypes: false }); + await a.update(updateData); } } diff --git a/src/module/migrations/002.ts b/src/module/migrations/002.ts index 741996e8..edd404f4 100644 --- a/src/module/migrations/002.ts +++ b/src/module/migrations/002.ts @@ -12,9 +12,9 @@ export async function migrate(): Promise { } async function migrateItems() { - for (const item of game.items?.entities ?? []) { + for (const item of game.items?.contents ?? []) { try { - const updateData = getItemUpdateData(item._data); + const updateData = getItemUpdateData(item.toObject()); if (updateData) { logger.info(`Migrating Item entity ${item.name} (${item.id})`); await item.update(updateData), { enforceTypes: false }; @@ -26,18 +26,18 @@ async function migrateItems() { } } -function getItemUpdateData(itemData: DeepPartial) { +function getItemUpdateData(itemData: foundry.data.ItemData["_source"]) { if (!["equipment", "trinket"].includes(itemData.type ?? "")) return undefined; - return { type: itemData.type === "equipment" ? "loot" : "equipment" }; + return { type: itemData.type === "equipment" ? ("loot" as const) : ("equipment" as const) }; } async function migrateActors() { - for (const actor of game.actors?.entities ?? []) { + for (const actor of game.actors?.contents ?? []) { try { - const updateData = getActorUpdateData(actor._data); + const updateData = getActorUpdateData(actor.toObject()); if (updateData) { logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`); - await actor.update(updateData, { enforceTypes: false }); + await actor.update(updateData); } } catch (err) { err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`; @@ -46,9 +46,9 @@ async function migrateActors() { } } -function getActorUpdateData(actorData: DeepPartial) { +function getActorUpdateData(actorData: foundry.data.ActorData["_source"]) { let hasItemUpdates = false; - const items = actorData.items?.map((itemData) => { + const items = actorData.items.map((itemData: foundry.data.ItemData["_source"]) => { const update = itemData ? getItemUpdateData(itemData) : undefined; if (update) { hasItemUpdates = true; @@ -61,23 +61,34 @@ function getActorUpdateData(actorData: DeepPartial) { } async function migrateScenes() { - for (const scene of game.scenes?.entities ?? []) { + for (const scene of game.scenes?.contents ?? []) { try { - const updateData = getSceneUpdateData(scene._data); + const updateData = getSceneUpdateData(scene.toObject()); if (updateData) { logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`); - await scene.update(updateData, { enforceTypes: false }); + await scene.update(updateData); } } catch (err) { - err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`; + err.message = `Error during migration of Scene document ${scene.name} (${scene.id}), continuing anyways.`; logger.error(err); } } } -function getSceneUpdateData(sceneData: Scene.Data) { +function getSceneUpdateData(scene: Scene) { let hasTokenUpdates = false; - const tokens = sceneData.tokens.map((tokenData) => { + // TODO: Continue from here + const tokens = scene.tokens.map((tokenData: foundry.data.TokenData["_source"]) => { + if (!tokenData.actorId || tokenData.actorLink) { + tokenData.actorData = {}; + } else if (!game.actors?.has(tokenData.actorId)) { + tokenData.actorId = null; + tokenData.actorData = {}; + } else if (!tokenData.actorLink) { + const actorData = duplicate(tokenData.actorData); + actorData.type = token.actor?.type; + } + if (!tokenData.actorId || tokenData.actorLink || tokenData.actorData.data) { tokenData.actorData = {}; hasTokenUpdates = true; diff --git a/src/module/rolls/slaying-dice-modifier.ts b/src/module/rolls/slaying-dice-modifier.ts index bdd849ee..9e2757e0 100644 --- a/src/module/rolls/slaying-dice-modifier.ts +++ b/src/module/rolls/slaying-dice-modifier.ts @@ -7,7 +7,7 @@ import { DS4Check } from "./check"; export default function registerSlayingDiceModifier(): void { PoolTerm.MODIFIERS.x = slay; - PoolTerm.POOL_REGEX = /^{([^}]+)}([A-z]([A-z0-9<=>]+)?)?$/; + // PoolTerm.POOL_REGEX = /^{([^}]+)}([A-z]([A-z0-9<=>]+)?)?$/; // TODO: Maybe we don't need this anymore? } function slay(this: PoolTerm, modifier: string): void {