Skip to content

Commit

Permalink
defect: #69 Item Window Will not Open (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
misterpotts authored Jan 4, 2023
1 parent 993d608 commit 2b27db5
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 18 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.2",
"version": "0.7.3",
"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.2",
"version": "0.7.3",
"description": "A system-agnostic, flexible crafting module for FoundryVTT",
"authors": [
{
Expand Down
9 changes: 8 additions & 1 deletion src/scripts/interface/apps/CraftingSystemManagerApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ class CraftingSystemManagerApp extends FormApplication {
this._selectedSystem = Array.from(craftingSystems.values())[0];
}
await this._selectedSystem?.loadPartDictionary();
const systems = Array.from(craftingSystems.values())
.sort((left,right) => Number(right.locked) - Number(left.locked));
if (!this._selectedSystem) {
return {
craftingSystems: systems
}
}
return {
craftingSystems: Array.from(craftingSystems.values()),
craftingSystems: systems,
selectedSystem: {
id: this._selectedSystem.id,
name: this._selectedSystem.details.name,
Expand Down
14 changes: 11 additions & 3 deletions src/scripts/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Hooks.on("renderItemSheet", async (app: any, html: any) => {

Hooks.once('init', async () => {
const gameProvider = new GameProvider();
const gameObject = gameProvider.globalGameObject();
const craftingSystemSettingManager = new DefaultSettingManager<Record<string, CraftingSystemJson>>({
gameProvider: gameProvider,
moduleId: Properties.module.id,
Expand All @@ -46,11 +47,12 @@ Hooks.once('init', async () => {
});
const systemRegistry = new DefaultSystemRegistry({
settingManager: craftingSystemSettingManager,
gameSystem: gameObject.system.id,
craftingSystemFactory: new CraftingSystemFactory({}),
errorDecisionProvider: async (error: Error) => {
const reset = await Dialog.confirm({
title: gameProvider.globalGameObject().i18n.localize(`${Properties.module.id}.CraftingSystemManagerApp.readErrorPrompt.title`),
content: `<p>${gameProvider.globalGameObject().i18n.format(Properties.module.id + ".CraftingSystemManagerApp.readErrorPrompt.content", {errorMessage: error.message})}</p>`,
title: gameObject.i18n.localize(`${Properties.module.id}.CraftingSystemManagerApp.readErrorPrompt.title`),
content: `<p>${gameObject.i18n.format(Properties.module.id + ".CraftingSystemManagerApp.readErrorPrompt.content", {errorMessage: error.message})}</p>`,
});
if (reset) {
return ErrorDecisionType.RESET;
Expand All @@ -61,7 +63,7 @@ Hooks.once('init', async () => {
FabricateApplication.systemRegistry = systemRegistry;
// @ts-ignore
globalThis.ui.CraftingSystemManagerApp = CraftingSystemManagerApp;
gameProvider.globalGameObject().settings.register(Properties.module.id, "craftingSystems", {
gameObject.settings.register(Properties.module.id, Properties.settings.craftingSystems.key, {
name: "",
hint: "",
scope: "world",
Expand All @@ -74,6 +76,12 @@ Hooks.once('init', async () => {
?.render(true);
}
});
try {
// todo: remove purge in later versions
await systemRegistry.purgeBundledSystemsFromStoredSettings();
} catch (e: any) {
console.warn(`Unable to purge Fabricate's bundled systems from world settings. ${{e}}`)
}
});

Hooks.once('ready', () => {
Expand Down
35 changes: 26 additions & 9 deletions src/scripts/registries/SystemRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {CraftingSystem, CraftingSystemJson} from "../system/CraftingSystem";
import {CraftingSystemFactory} from "../system/CraftingSystemFactory";
import {FabricateSetting, SettingManager} from "../interface/settings/FabricateSettings";
import {SYSTEM_DEFINITION as ALCHEMISTS_SUPPLIES} from "../system_definitions/AlchemistsSuppliesV16";
import {SYSTEM_DATA as ALCHEMISTS_SUPPLIES} from "../system_definitions/AlchemistsSuppliesV16";

interface SystemRegistry {

Expand Down Expand Up @@ -35,22 +35,26 @@ type ErrorDecisionProvider = (error: Error) => Promise<ErrorDecisionType>;

class DefaultSystemRegistry implements SystemRegistry {

private readonly _gameSystem: string;
private readonly _settingsManager: SettingManager<Record<string, CraftingSystemJson>>;
private readonly _craftingSystemFactory: CraftingSystemFactory;
private readonly _errorDecisionProvider: ErrorDecisionProvider;

constructor({
settingManager,
craftingSystemFactory,
errorDecisionProvider
errorDecisionProvider,
gameSystem
}: {
settingManager: SettingManager<Record<string, CraftingSystemJson>>;
craftingSystemFactory: CraftingSystemFactory;
errorDecisionProvider: ErrorDecisionProvider;
gameSystem: string;
}) {
this._settingsManager = settingManager;
this._craftingSystemFactory = craftingSystemFactory;
this._errorDecisionProvider = errorDecisionProvider;
this._gameSystem = gameSystem;
}

async deleteCraftingSystemById(id: string): Promise<void> {
Expand All @@ -59,27 +63,39 @@ class DefaultSystemRegistry implements SystemRegistry {
await this._settingsManager.write(allCraftingSystemSettings);
}

/**
* Removes bundled systems from stored settings.
*
* todo: deprecate and delete in later versions
* */
public async purgeBundledSystemsFromStoredSettings(): Promise<void> {
return this.deleteCraftingSystemById(ALCHEMISTS_SUPPLIES.definition.id);
}

public async getAllCraftingSystems(): Promise<Map<string, CraftingSystem>> {
let allCraftingSystemSettings: Record<string, CraftingSystemJson> = {};
try {
allCraftingSystemSettings = await this._settingsManager.read();
Object.values(this.getBundledCraftingSystemsJson())
.forEach(systemJson => allCraftingSystemSettings[systemJson.id] = systemJson);
} catch (e: any) {
allCraftingSystemSettings = await this.handleReadError(e);
}
const craftingSystems = await Promise.all(Object.values(allCraftingSystemSettings)
.map(craftingSystemJson => this._craftingSystemFactory.make(craftingSystemJson)));
.map(craftingSystemJson => this._craftingSystemFactory.make(craftingSystemJson))
);
return new Map(craftingSystems.map(craftingSystem => [craftingSystem.id, craftingSystem]));
}

private async handleReadError(e: any): Promise<Record<string, CraftingSystemJson>> {
const error: Error = e instanceof Error ? e : new Error(`An unexpected error occurred reading crafting systems. `);
console.error({error});
const decision = await this._errorDecisionProvider(error);
switch (decision) {
case ErrorDecisionType.RESET:
await this._settingsManager.delete();
const bundledCraftingSystemsJson = this.getBundledCraftingSystemsJson();
await this._settingsManager.write(bundledCraftingSystemsJson);
return bundledCraftingSystemsJson;
await this._settingsManager.write({});
return {};
default:
return {}
}
Expand Down Expand Up @@ -113,17 +129,18 @@ class DefaultSystemRegistry implements SystemRegistry {

public getBundledCraftingSystemsJson(): Record<string, CraftingSystemJson> {
const systemSpecifications: Record<string, CraftingSystemJson> = {};
systemSpecifications[ALCHEMISTS_SUPPLIES.id] = ALCHEMISTS_SUPPLIES;
const bundledSystems = [ALCHEMISTS_SUPPLIES];
bundledSystems.filter(bundledSystem => !bundledSystem.gameSystem || bundledSystem.gameSystem === this._gameSystem)
.forEach(bundledSystem => systemSpecifications[bundledSystem.definition.id] = bundledSystem.definition);
return systemSpecifications;
}

public async reset(): Promise<void> {
await this._settingsManager.delete();
await this._settingsManager.write(this.getBundledCraftingSystemsJson());
}

public getDefaultSettingValue(): FabricateSetting<Record<string, CraftingSystemJson>> {
return this._settingsManager.asVersionedSetting(this.getBundledCraftingSystemsJson(), );
return this._settingsManager.asVersionedSetting({});
}

public async createCraftingSystem(systemDefinition: CraftingSystemJson): Promise<CraftingSystem> {
Expand Down
7 changes: 6 additions & 1 deletion src/scripts/system_definitions/AlchemistsSuppliesV16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,9 @@ const SYSTEM_DEFINITION: CraftingSystemJson = {
}
}

export {SYSTEM_DEFINITION}
const SYSTEM_DATA = {
definition: SYSTEM_DEFINITION,
gameSystem: "dnd5e"
}

export {SYSTEM_DATA}
4 changes: 2 additions & 2 deletions test/AlchemistsSuppliesIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {beforeEach, describe, expect, jest, test} from '@jest/globals';
import {CraftingSystemFactory} from "../src/scripts/system/CraftingSystemFactory";
import {CraftingSystem} from "../src/scripts/system/CraftingSystem";
import * as Sinon from "sinon";
import {SYSTEM_DEFINITION as AlchemistsSupplies} from "../src/scripts/system_definitions/AlchemistsSuppliesV16"
import {SYSTEM_DATA as AlchemistsSupplies} from "../src/scripts/system_definitions/AlchemistsSuppliesV16"
import {StubDocumentManager} from "./stubs/StubDocumentManager";
import {Combination} from "../src/scripts/common/Combination";

Expand All @@ -18,7 +18,7 @@ describe('A Crafting System Factory', () => {

test('should create a new Crafting System from a valid specification', async () => {

const systemSpec = AlchemistsSupplies;
const systemSpec = AlchemistsSupplies.definition;

const craftingSystemFactory: CraftingSystemFactory = new CraftingSystemFactory({
documentManager: StubDocumentManager.forPartDefinitions({
Expand Down
1 change: 1 addition & 0 deletions test/SystemRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe("integration test", () => {
const underTest = new DefaultSystemRegistry({
settingManager: fabricateSettingsManager,
craftingSystemFactory,
gameSystem: "dnd5e",
errorDecisionProvider: () => Promise.resolve(ErrorDecisionType.RETAIN)
});

Expand Down

0 comments on commit 2b27db5

Please sign in to comment.