This commit is contained in:
Johannes Loher 2021-06-30 05:00:23 +02:00
parent 650cbe7310
commit 76f42fe280
3 changed files with 29 additions and 18 deletions

View file

@ -5,10 +5,10 @@
import logger from "../logger";
export async function migrate(): Promise<void> {
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);
}
}

View file

@ -12,9 +12,9 @@ export async function migrate(): Promise<void> {
}
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<Item.Data>) {
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<Actor.Data>) {
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<Actor.Data>) {
}
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;

View file

@ -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 {