From b47be5dd52264ed0882541321b754007074199ca Mon Sep 17 00:00:00 2001 From: AngryBeaver Date: Tue, 18 Oct 2022 23:03:59 +0200 Subject: [PATCH] filter for anyOf items --- module.json | 2 +- package.json | 2 +- src/apps/CraftingApp.ts | 2 +- src/apps/RecipeCompendium.ts | 54 +++++++++++++++++++++++------------- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/module.json b/module.json index f1fb214..03d458d 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.3.1", + "version": "0.3.2", "authors": [ { "name": "angryBeaver", diff --git a/package.json b/package.json index 42fae09..dc35c9d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "beavers-crafting", "title": "Beaver's Crafting", - "version": "0.3.1", + "version": "0.3.2", "description": "Crafting", "devDir": "C:\\Users\\User\\AppData\\Local\\FoundryVTT\\Data\\modules", "main": "src/main.js", diff --git a/src/apps/CraftingApp.ts b/src/apps/CraftingApp.ts index 9b85c11..cac4f4b 100644 --- a/src/apps/CraftingApp.ts +++ b/src/apps/CraftingApp.ts @@ -48,7 +48,7 @@ export class CraftingApp extends Application { const data: any = mergeObject(this.data, await super.getData(options)); let recipes = await RecipeCompendium.filterForActor(data.actor, data.filter); if(Object.values(data.filterItems).length != 0){ - recipes = RecipeCompendium.filterForItems(recipes,Object.values(data.filterItems)); + recipes = await RecipeCompendium.filterForItems(recipes,Object.values(data.filterItems)); } data.recipes = recipes; await this.renderRecipeSheet(data); diff --git a/src/apps/RecipeCompendium.ts b/src/apps/RecipeCompendium.ts index ac0453b..3539326 100644 --- a/src/apps/RecipeCompendium.ts +++ b/src/apps/RecipeCompendium.ts @@ -12,35 +12,51 @@ export class RecipeCompendium { .filter(item => RecipeCompendium.isRecipe(item)) .map(item => Recipe.fromItem(item)); } - //todo ANYOF - static filterForItems(recipes: Recipe[], items) { - return recipes.filter(recipe => { - const recipeItemsInItemList = items.filter( - item => { - for (const [k, component] of Object.entries(recipe.ingredients)) { - if (this.isSame(item, component)) { - return true; - } + + static async filterForItems(recipes: Recipe[], items) { + const returnList: Recipe[] = []; + for (const recipe of recipes) { + const listOfAnyOfIngredients = Object.values(recipe.ingredients).filter(component => component.type === Settings.ANYOF_SUBTYPE); + const listOfIngredientsWithoutAnyOf = Object.values(recipe.ingredients).filter(component => component.type !== Settings.ANYOF_SUBTYPE); + let countItems = 0; + itemLoop: for(const item of items) { + for (const component of listOfIngredientsWithoutAnyOf) { + if (this.isSame(item, component)) { + countItems++; + continue itemLoop; } - return false; - }) - return recipeItemsInItemList.length === items.length; - }); + } + for (const component of listOfAnyOfIngredients) { + const entity = await fromUuid(component.uuid); + const anyOf = new AnyOf(entity); + const isOf = await anyOf.executeMacro(item); + if (isOf.value) { + countItems++ + continue itemLoop; + } + } + break itemLoop; + } + if (countItems === items.length) { + returnList.push(recipe); + } + } + return returnList; } static async filterForActor(actor, filter) { const list = RecipeCompendium.getAll(); - const returnList:Recipe[] = []; - for(const recipe of list){ + const returnList: Recipe[] = []; + for (const recipe of list) { if (filter == FilterType.all) { returnList.push(recipe); - }else{ + } else { const listOfAnyOfIngredients = Object.values(recipe.ingredients).filter(component => component.type === Settings.ANYOF_SUBTYPE); if (await this.isAnyAnyOfInList(listOfAnyOfIngredients, actor.items)) { //isAvailable or usable ! when any item matches anyOf has the given quantity const listOfIngredientsWithoutAnyOf = Object.values(recipe.ingredients).filter(component => component.type !== Settings.ANYOF_SUBTYPE); const result = RecipeCompendium.validateRecipeToItemList(listOfIngredientsWithoutAnyOf, actor.items); - if((filter == FilterType.usable && !result.hasErrors) - || (filter == FilterType.available && result.isAvailable)){ + if ((filter == FilterType.usable && !result.hasErrors) + || (filter == FilterType.available && result.isAvailable)) { returnList.push(recipe); } } @@ -56,7 +72,7 @@ export class RecipeCompendium { const item = await fromUuid(component.uuid); const anyOf = new AnyOf(item); const results = await anyOf.filter(listOfItems); - if (results.filter(c=>c.quantity >= component.quantity).length == 0) { + if (results.filter(c => c.quantity >= component.quantity).length == 0) { return false; } }