Skip to content

Commit

Permalink
filter for anyOf items
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryBeaver committed Oct 18, 2022
1 parent f8ebc04 commit b47be5d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 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.3.1",
"version": "0.3.2",
"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.3.1",
"version": "0.3.2",
"description": "Crafting",
"devDir": "C:\\Users\\User\\AppData\\Local\\FoundryVTT\\Data\\modules",
"main": "src/main.js",
Expand Down
2 changes: 1 addition & 1 deletion src/apps/CraftingApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 35 additions & 19 deletions src/apps/RecipeCompendium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit b47be5d

Please sign in to comment.