@rpg-engine/long-bow 0.8.119 → 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/dist/long-bow.cjs.development.js +24 -21
- package/dist/long-bow.cjs.development.js.map +1 -1
- package/dist/long-bow.cjs.production.min.js +1 -1
- package/dist/long-bow.cjs.production.min.js.map +1 -1
- package/dist/long-bow.esm.js +24 -21
- package/dist/long-bow.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/CraftBook/utils/calculateMaxCraftable.ts +5 -8
- package/src/libs/itemCounter.ts +23 -15
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ICraftableItem,
|
|
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
|
|
14
|
-
(item): item is IItem => item?.key === ingredient.key
|
|
15
|
-
);
|
|
14
|
+
const totalQty = countItemFromInventory(ingredient.key, inventory);
|
|
16
15
|
|
|
17
|
-
if (
|
|
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
|
package/src/libs/itemCounter.ts
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IItemContainer } from "@rpg-engine/shared";
|
|
2
2
|
|
|
3
|
-
export const countItemFromInventory = (itemKey: string, inventory: IItemContainer) => {
|
|
4
|
-
let
|
|
3
|
+
export const countItemFromInventory = (itemKey: string, inventory: IItemContainer): number => {
|
|
4
|
+
let totalQty = 0;
|
|
5
5
|
|
|
6
|
-
if (inventory) {
|
|
7
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
};
|