@rpg-engine/long-bow 0.8.118 → 0.8.120

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpg-engine/long-bow",
3
- "version": "0.8.118",
3
+ "version": "0.8.120",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -84,7 +84,7 @@
84
84
  "dependencies": {
85
85
  "@capacitor/core": "^6.1.0",
86
86
  "@rollup/plugin-image": "^2.1.1",
87
- "@rpg-engine/shared": "^0.10.55",
87
+ "@rpg-engine/shared": "^0.10.59",
88
88
  "dayjs": "^1.11.2",
89
89
  "font-awesome": "^4.7.0",
90
90
  "fs-extra": "^10.1.0",
@@ -1,4 +1,5 @@
1
- import { ICraftableItem, IItem, IItemContainer } from '@rpg-engine/shared';
1
+ import { ICraftableItem, IItemContainer } from '@rpg-engine/shared';
2
+ import { countItemFromInventory } from '../../../libs/itemCounter';
2
3
 
3
4
  export function calculateMaxCraftable(
4
5
  recipe: ICraftableItem | undefined,
@@ -10,17 +11,13 @@ export function calculateMaxCraftable(
10
11
 
11
12
  return (
12
13
  recipe.ingredients.reduce((maxPossible, ingredient) => {
13
- const inventoryItem = Object.values(inventory.slots).find(
14
- (item): item is IItem => item?.key === ingredient.key
15
- );
14
+ const totalQty = countItemFromInventory(ingredient.key, inventory);
16
15
 
17
- if (!inventoryItem) {
16
+ if (totalQty === 0) {
18
17
  return 0;
19
18
  }
20
19
 
21
- const possibleWithThisIngredient = Math.floor(
22
- (inventoryItem.stackQty ?? 0) / ingredient.qty
23
- );
20
+ const possibleWithThisIngredient = Math.floor(totalQty / ingredient.qty);
24
21
 
25
22
  return maxPossible === undefined
26
23
  ? possibleWithThisIngredient
@@ -1,22 +1,30 @@
1
- import { IItem, IItemContainer } from "@rpg-engine/shared";
1
+ import { IItemContainer } from "@rpg-engine/shared";
2
2
 
3
- export const countItemFromInventory = (itemKey: string, inventory: IItemContainer) => {
4
- let itemsFromInventory: (IItem | undefined | null)[] = [];
3
+ export const countItemFromInventory = (itemKey: string, inventory: IItemContainer): number => {
4
+ let totalQty = 0;
5
5
 
6
- if (inventory) {
7
- Object.keys(inventory.slots).forEach(i => {
8
- const index = parseInt(i);
9
-
10
- if (inventory.slots[index]?.key === itemKey) {
11
- itemsFromInventory.push(inventory.slots[index]);
12
- }
13
- });
6
+ if (!inventory) {
7
+ return totalQty;
14
8
  }
15
9
 
16
- const totalQty = itemsFromInventory.reduce(
17
- (acc, item) => acc + (item?.stackQty || 1),
18
- 0
19
- );
10
+ Object.keys(inventory.slots).forEach(i => {
11
+ const index = parseInt(i);
12
+ const item = inventory.slots[index];
13
+
14
+ if (!item) {
15
+ return;
16
+ }
17
+
18
+ // Count matching items
19
+ if (item.key === itemKey) {
20
+ totalQty += item.stackQty || 1;
21
+ }
22
+
23
+ // Recursively check nested containers
24
+ if (item.isItemContainer && item.itemContainer && typeof item.itemContainer !== "string") {
25
+ totalQty += countItemFromInventory(itemKey, item.itemContainer);
26
+ }
27
+ });
20
28
 
21
29
  return totalQty;
22
30
  };