From 009a1b5281a60a1a72d84480931d026a9301057c Mon Sep 17 00:00:00 2001 From: AngryBeaver Date: Sat, 15 Oct 2022 11:39:03 +0200 Subject: [PATCH] fix add result and updating it at once. --- module.json | 2 +- package.json | 2 +- src/Crafting.ts | 50 ++++++++++++++++++++++++----------------- src/Recipe.ts | 8 +++++-- src/RecipeCompendium.ts | 6 ++++- src/types.ts | 2 +- 6 files changed, 43 insertions(+), 27 deletions(-) diff --git a/module.json b/module.json index fe4d822..fe17015 100644 --- a/module.json +++ b/module.json @@ -2,7 +2,7 @@ "title": "Beaver's Crafting System", "description": "A Crafting Module for DnD", "id": "beavers-crafting", - "version": "0.2.0", + "version": "0.2.1", "authors": [ { "name": "angryBeaver", diff --git a/package.json b/package.json index e5cfed5..5caa43d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "beavers-crafting", "title": "Beaver's Crafting", - "version": "0.2.0", + "version": "0.2.1", "description": "Crafting", "devDir": "C:\\Users\\User\\AppData\\Local\\FoundryVTT\\Data\\modules", "main": "src/main.js", diff --git a/src/Crafting.ts b/src/Crafting.ts index 0c147f0..4340bcd 100644 --- a/src/Crafting.ts +++ b/src/Crafting.ts @@ -81,8 +81,6 @@ export class Crafting { async updateActor(result: Result) { if (!result) result = new DefaultResult(); if (result.hasException || (result.hasErrors && (!this.recipe.skill?.consume || !result.skill))) return; - await this.actor.updateEmbeddedDocuments("Item", result.changes.items.toUpdate); - await this.actor.deleteEmbeddedDocuments("Item", result.changes.items.toDelete); const createItems: any[] = []; for (const component of result.changes.items.toCreate) { if (component.uuid) { @@ -97,7 +95,9 @@ export class Crafting { } } - await this.actor.createEmbeddedDocuments("Item", createItems) + await this.actor.createEmbeddedDocuments("Item", createItems); + await this.actor.updateEmbeddedDocuments("Item", result.changes.items.toUpdate); + await this.actor.deleteEmbeddedDocuments("Item", result.changes.items.toDelete); await this.actor.update({ "system.currency": result.changes.currencies }); @@ -114,30 +114,38 @@ export class Crafting { }) } - //if you have to comment it, its not clean code ! _addComponentToResult(result: Result, component: Component) { const itemChange = RecipeCompendium.findComponentInList(this.actor.items, component); + const isAlreadyOnActor = itemChange.toUpdate["system.quantity"] > 0 + const isAlreadyUpdated = result.changes.items.toUpdate + .filter(x => x._id === itemChange.toUpdate._id).length > 0 + const isAlreadyCreated = result.changes.items.toCreate + .filter(x => x.id === itemChange.toUpdate._id).length > 0 + const isAlreadyDeleted = result.changes.items.toDelete.includes(itemChange.toUpdate._id) + if(result.results[component.uuid]){ - DefaultComponent.inc(result.results[component.uuid]) + DefaultComponent.inc(result.results[component.uuid]); }else{ result.results[component.uuid] = component; } - if (itemChange.toUpdate["system.quantity"] == 0) { // actor does not have item - result.changes.items.toCreate.push(component); //add that item - } else { // actor does have item - const updates = result.changes.items.toUpdate - .filter(x => x._id === itemChange.toUpdate._id); - if (updates.length > 0) { //crafting already updated that item - updates.forEach(x => x["system.quantity"] = x["system.quantity"] + component.quantity) //reupdate it - } else { //crafting does not update that item - if (result.changes.items.toDelete.includes(itemChange.toUpdate._id)) { //crafting deleted that item - result.changes.items.toCreate.push(component); //add that item // now i delete it then create it again. - } else { //on actor but not yet touched - itemChange.toUpdate["system.quantity"] = itemChange.toUpdate["system.quantity"] + component.quantity - result.changes.items.toUpdate.push(itemChange.toUpdate); - result.changes.items.toDelete.push(...itemChange.toDelete); - - } + if (!isAlreadyOnActor) { + if(isAlreadyCreated){ + const creates = result.changes.items.toCreate.filter(x => x.id === itemChange.toUpdate._id); + creates.forEach(x => x.quantity = x.quantity + component.quantity) + } else { + result.changes.items.toCreate.push(DefaultComponent.clone(component)); + } + } else { + if(isAlreadyDeleted){ + const deleteIndex = result.changes.items.toDelete.indexOf(itemChange.toUpdate._id); + result.changes.items.toDelete.splice(deleteIndex,1); + } + if(isAlreadyUpdated){ + const updates = result.changes.items.toUpdate.filter(x => x._id === itemChange.toUpdate._id) + updates.forEach(x => x["system.quantity"] = x["system.quantity"] + component.quantity) + }else{ + itemChange.toUpdate["system.quantity"] = itemChange.toUpdate["system.quantity"] + component.quantity + result.changes.items.toUpdate.push(itemChange.toUpdate); } } } diff --git a/src/Recipe.ts b/src/Recipe.ts index 5aa2058..bd1a79b 100644 --- a/src/Recipe.ts +++ b/src/Recipe.ts @@ -105,14 +105,18 @@ export class DefaultComponent implements Component { uuid: string; type: string; + static clone(component: Component):Component{ + return new DefaultComponent(component,component.uuid, component.type) + } + constructor(entity, uuid, type) { this.id = entity.id; this.uuid = uuid; this.type = type; this.name = entity.name; this.img = entity.img; - this.quantity = entity.system?.quantity || 1; - this.sourceId = entity.flags.core?.sourceId; + this.quantity = entity.system?.quantity || entity.quantity || 1; + this.sourceId = entity.flags?.core?.sourceId || entity.sourceId; } static inc(component){ diff --git a/src/RecipeCompendium.ts b/src/RecipeCompendium.ts index b23abb2..0844d77 100644 --- a/src/RecipeCompendium.ts +++ b/src/RecipeCompendium.ts @@ -70,7 +70,7 @@ export class RecipeCompendium { } static findComponentInList(listOfItems, component: Component): ItemChange { - const itemChange = new DefaultItemChange(); + const itemChange = new DefaultItemChange(component); listOfItems.forEach((i) => { if (this.isSame(i, component)) { if (itemChange.toUpdate["system.quantity"] == 0) { @@ -108,6 +108,10 @@ class DefaultItemChange implements ItemChange { "_id": "", "system.quantity": 0 }; + constructor(component:Component){ + this.toUpdate._id = component.id; + } + } export class DefaultResult implements Result { diff --git a/src/types.ts b/src/types.ts index 190c427..2fa0131 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,7 +4,7 @@ interface Result { items: { toUpdate:any[], toDelete:any[], - toCreate:any[] + toCreate:Component[] }, currencies: {}, }