Skip to content

Commit

Permalink
fix add result and updating it at once.
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryBeaver committed Oct 15, 2022
1 parent a05e6de commit 009a1b5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
50 changes: 29 additions & 21 deletions src/Crafting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
});
Expand All @@ -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);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
6 changes: 5 additions & 1 deletion src/RecipeCompendium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface Result {
items: {
toUpdate:any[],
toDelete:any[],
toCreate:any[]
toCreate:Component[]
},
currencies: {},
}
Expand Down

0 comments on commit 009a1b5

Please sign in to comment.