Skip to content

Commit

Permalink
refactor: Handle missing items causing systems not to load (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
misterpotts authored Jan 14, 2023
1 parent b54715f commit e4401c8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fabricate",
"version": "0.7.3",
"version": "0.7.4",
"description": "A system-agnostic, flexible crafting module for FoundryVT",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/module.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "fabricate",
"title": "Fabricate",
"version": "0.7.3",
"version": "0.7.4",
"description": "A system-agnostic, flexible crafting module for FoundryVTT",
"authors": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class CraftingSystemDetailSubmissionHandler implements SubmissionHandler<Craftin
}

validate(formData: CraftingSystemDetailsJson): FormError[] {
const GAME = new GameProvider().globalGameObject();
const GAME = new GameProvider().globalGameObject();
const errors: Array<FormError> = [];
if (!formData.name || formData.name.length === 0) {
errors.push({
Expand Down
11 changes: 6 additions & 5 deletions src/scripts/interface/apps/core/Applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class DefaultDropHandler<V, M, S extends StateManager<V, M>> implements DropHand
const actionData: ActionData = {
action: targetData.get("dropTrigger"),
event: dropEvent,
data : null,
data : new Map<string, string>(targetData),
document: null,
checked: false,
keys: {
Expand All @@ -154,16 +154,14 @@ class DefaultDropHandler<V, M, S extends StateManager<V, M>> implements DropHand
}
}
if (rawJsonDropData) {
const data = new Map<string, string>(targetData);
try {
const dropData: any = JSON.parse(rawJsonDropData);
Object.entries(dropData).forEach(entry => {
if (data.has(entry[0])) {
if (actionData.data.has(entry[0])) {
console.warn(`The data key "${entry[0]}" exists in both the source and target. Overwriting source value with target value. `);
}
data.set(entry[0], <string>entry[1]);
actionData.data.set(entry[0], <string>entry[1]);
});
actionData.data = data;
} catch (e: any) {
console.error(`Something was dropped onto a Fabricate Drop Zone, but the event data could not be read. Caused by ${e}`);
}
Expand All @@ -174,6 +172,9 @@ class DefaultDropHandler<V, M, S extends StateManager<V, M>> implements DropHand
if (Properties.module.documents.supportedTypes.indexOf(dropData.type) >= 0) {
const document: any = await new DefaultDocumentManager().getDocumentByUuid(dropData.uuid);
actionData.document = document;
if (document) {
actionData.data.set("partId", document.uuid);
}
}
} catch (e: any) {
console.error(`Something was dropped onto a Fabricate Drop Zone, but the event data could not be read. Caused by ${e}`);
Expand Down
7 changes: 7 additions & 0 deletions src/scripts/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import {CraftingSystemJson} from "./system/CraftingSystem";
import {ApplicationWindow, ItemSheetExtension} from "./interface/apps/core/Applications";
import {FabricateItemSheetTab} from "./interface/FabricateItemSheetTab";

// `app` is an unknown type. Will need to consult foundry docs or crawl `foundry.js` to figure out what it is, but it seems JQuery related
// `id` is useless to Fabricate
Hooks.on("deleteItem", async (item: any, _app: unknown, _id: string) => {
console.log(`Item UUID ${item.uuid} deleted`);
await FabricateApplication.systemRegistry.handleItemDeleted(item.uuid);
});

Hooks.on("renderSidebarTab", async (app: any, html: any) => {
const GAME = new GameProvider().globalGameObject();
if (!(app instanceof ItemDirectory) || !GAME.user.isGM) {
Expand Down
47 changes: 47 additions & 0 deletions src/scripts/registries/SystemRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface SystemRegistry {

createCraftingSystem(systemDefinition: CraftingSystemJson): Promise<CraftingSystem>;

handleItemDeleted(uuid: string): Promise<void>;
}

enum ErrorDecisionType {
Expand Down Expand Up @@ -177,6 +178,52 @@ class DefaultSystemRegistry implements SystemRegistry {
const craftingSystem = await this._craftingSystemFactory.make(systemDefinition);
return this.saveCraftingSystem(craftingSystem);
}

public async handleItemDeleted(uuid: string): Promise<void> {
const systemsJson = await this._settingsManager.read();
let referenceCount = 0;
let recipeCount = 0;
let componentCount = 0;
Object.values(systemsJson).forEach(craftingSystemJson => {
if (craftingSystemJson.parts.components[uuid]) {
delete craftingSystemJson.parts.components[uuid];
componentCount++;
}
Object.values(craftingSystemJson.parts.components)
.forEach(componentJson => {
if (componentJson.salvage[uuid]) {
delete componentJson.salvage[uuid];
referenceCount++;
}
});
if (craftingSystemJson.parts.recipes[uuid]) {
delete craftingSystemJson.parts.recipes[uuid];
recipeCount++;
}
Object.values(craftingSystemJson.parts.recipes)
.forEach(recipeJson => {
if (recipeJson.catalysts[uuid]) {
delete recipeJson.catalysts[uuid];
referenceCount++;
}
recipeJson.ingredientGroups.forEach(ingredientGroup => {
if (ingredientGroup[uuid]) {
delete ingredientGroup[uuid];
referenceCount++;
}
});
recipeJson.resultGroups.forEach(ingredientGroup => {
if (ingredientGroup[uuid]) {
delete ingredientGroup[uuid];
referenceCount++;
}
});
});
});
await this._settingsManager.write(systemsJson);
console.info(`Deleted ${recipeCount} Recipes, ${componentCount} Components and ${referenceCount} references to the Item with UUID ${uuid} across ${Object.keys(systemsJson).length} Crafting Systems. `);
}

}

export { SystemRegistry, DefaultSystemRegistry, ErrorDecisionType }
2 changes: 1 addition & 1 deletion test/PartDictionary.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {beforeEach, describe, expect, test} from "@jest/globals";
import * as Sinon from "sinon";

import {ErrorDecisionType, PartDictionary, PartDictionaryFactory} from "../src/scripts/system/PartDictionary";
import {PartDictionary, PartDictionaryFactory} from "../src/scripts/system/PartDictionary";
import {
testComponentFive,
testComponentFour,
Expand Down

0 comments on commit e4401c8

Please sign in to comment.