ds4/src/module/migrations/004.ts

159 lines
6.1 KiB
TypeScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
2021-06-30 03:53:52 +02:00
import { DS4SpellDataSourceData } from "../item/item-data-source";
import logger from "../logger";
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// export async function migrate(): Promise<void> {
// await migrateItems();
// await migrateActors();
// await migrateScenes();
// await migrateCompendiums();
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// async function migrateItems() {
2021-07-07 19:22:35 +02:00
// for (const item of getGame().items?.entities ?? []) {
2021-06-30 05:04:58 +02:00
// try {
// const updateData = getItemUpdateData(item._data);
// if (updateData) {
// logger.info(`Migrating Item entity ${item.name} (${item.id})`);
// await item.update(updateData), { enforceTypes: false };
// }
// } catch (err) {
// err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`;
// logger.error(err);
// }
// }
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// function getItemUpdateData(itemData: DeepPartial<Item.Data>) {
// if (!["spell"].includes(itemData.type ?? "")) return undefined;
// const updateData: Record<string, unknown> = {
// "-=data.scrollPrice": null,
// "data.minimumLevels": { healer: null, wizard: null, sorcerer: null },
// };
// if (((itemData.data as DS4SpellDataSourceData).cooldownDuration.unit as string) === "custom") {
// updateData["data.cooldownDuration.unit"] = "rounds";
// }
// return updateData;
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// async function migrateActors() {
2021-07-07 19:22:35 +02:00
// for (const actor of getGame().actors?.entities ?? []) {
2021-06-30 05:04:58 +02:00
// try {
// const updateData = getActorUpdateData(actor._data);
// if (updateData) {
// logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`);
// await actor.update(updateData, { enforceTypes: false });
// }
// } catch (err) {
// err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`;
// logger.error(err);
// }
// }
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// function getActorUpdateData(actorData: DeepPartial<Actor.Data>) {
// let hasItemUpdates = false;
// const items = actorData.items?.map((itemData) => {
// const update = itemData ? getItemUpdateData(itemData) : undefined;
// if (update) {
// hasItemUpdates = true;
// return mergeObject(itemData, update, { enforceTypes: false, inplace: false });
// } else {
// return itemData;
// }
// });
// const updateData: Record<string, unknown> = {};
// if (actorData.type === "character") {
// updateData["data.slayerPoints"] = { value: 0 };
// }
// if (hasItemUpdates) {
// updateData["items"] = items;
// }
// return updateData;
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// async function migrateScenes() {
2021-07-07 19:22:35 +02:00
// for (const scene of getGame().scenes?.entities ?? []) {
2021-06-30 05:04:58 +02:00
// try {
// const updateData = getSceneUpdateData(scene._data);
// if (updateData) {
// logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`);
// await scene.update(updateData, { enforceTypes: false });
// }
// } catch (err) {
// err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`;
// logger.error(err);
// }
// }
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// function getSceneUpdateData(sceneData: Scene.Data) {
// let hasTokenUpdates = false;
// const tokens = sceneData.tokens.map((tokenData) => {
// if (!tokenData.actorId || tokenData.actorLink || tokenData.actorData.data) {
// tokenData.actorData = {};
// hasTokenUpdates = true;
// return tokenData;
// }
// const token = new Token(tokenData);
// if (!token.actor) {
// tokenData.actorId = null as unknown as string;
// tokenData.actorData = {};
// hasTokenUpdates = true;
// } else if (!tokenData.actorLink) {
// const actorUpdateData = getActorUpdateData(token.data.actorData);
// tokenData.actorData = mergeObject(token.data.actorData, actorUpdateData);
// hasTokenUpdates = true;
// }
// return tokenData;
// });
// if (!hasTokenUpdates) return undefined;
// return hasTokenUpdates ? { tokens } : undefined;
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// async function migrateCompendiums() {
2021-07-07 19:22:35 +02:00
// for (const compendium of getGame().packs ?? []) {
2021-06-30 05:04:58 +02:00
// if (compendium.metadata.package !== "world") continue;
// if (!["Actor", "Item", "Scene"].includes(compendium.metadata.entity)) continue;
// await migrateCompendium(compendium);
// }
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// async function migrateCompendium(compendium: Compendium) {
// const entityName = compendium.metadata.entity;
// if (!["Actor", "Item", "Scene"].includes(entityName)) return;
// const wasLocked = compendium.locked;
// await compendium.configure({ locked: false });
// await compendium.migrate({});
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// const content = await compendium.getContent();
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// for (const entity of content) {
// try {
// const getUpdateData = (entity: Entity) => {
// switch (entityName) {
// case "Item":
// return getItemUpdateData(entity._data);
// case "Actor":
// return getActorUpdateData(entity._data);
// case "Scene":
// return getSceneUpdateData(entity._data as Scene.Data);
// }
// };
// const updateData = getUpdateData(entity);
// if (updateData) {
// logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`);
// await compendium.updateEntity({ ...updateData, _id: entity._id });
// }
// } catch (err) {
// err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`;
// logger.error(err);
// }
// }
2021-05-13 19:59:44 +02:00
2021-06-30 05:04:58 +02:00
// await compendium.configure({ locked: wasLocked });
// }