@rpg-engine/long-bow 0.7.88 → 0.7.90

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.7.88",
3
+ "version": "0.7.90",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -13,13 +13,14 @@ import { DraggableContainer } from '../DraggableContainer';
13
13
  import { InputRadio } from '../InputRadio';
14
14
  import { RPGUIContainerTypes } from '../RPGUI/RPGUIContainer';
15
15
  import { CraftingRecipe } from './CraftingRecipe';
16
+ import { calculateMaxCraftable } from './utils/calculateMaxCraftable';
16
17
 
17
18
  export interface IItemCraftSelectorProps {
18
19
  atlasJSON: any;
19
20
  atlasIMG: any;
20
21
  onClose: () => void;
21
22
  onSelect: (value: string) => void;
22
- onCraftItem: (value: string | undefined) => void;
23
+ onCraftItem: (value: string | undefined, maxCraftable: number) => void;
23
24
  craftablesItems: ICraftableItem[];
24
25
  equipmentSet?: IEquipmentSet | null;
25
26
  inventory?: IItemContainer | null;
@@ -203,7 +204,15 @@ export const CraftBook: React.FC<IItemCraftSelectorProps> = ({
203
204
  onPointerDown={() => {
204
205
  if (!craftItemKey || isCraftingDisabled) return;
205
206
 
206
- onCraftItem(craftItemKey);
207
+ const selectedItem = craftablesItems.find(
208
+ item => item.key === craftItemKey
209
+ );
210
+ const maxCraftable = calculateMaxCraftable(
211
+ selectedItem,
212
+ inventory
213
+ );
214
+
215
+ onCraftItem(craftItemKey, maxCraftable);
207
216
  setIsCraftingDisabled(true);
208
217
 
209
218
  setTimeout(() => {
@@ -0,0 +1,30 @@
1
+ import { ICraftableItem, IItem, IItemContainer } from '@rpg-engine/shared';
2
+
3
+ export function calculateMaxCraftable(
4
+ recipe: ICraftableItem | undefined,
5
+ inventory: IItemContainer | null | undefined
6
+ ): number {
7
+ if (!inventory || !recipe?.ingredients) {
8
+ return 0;
9
+ }
10
+
11
+ return recipe.ingredients.reduce((maxPossible, ingredient) => {
12
+ const inventoryItem = Object.values(inventory.slots).find(
13
+ (item): item is IItem => item?.key === ingredient.key
14
+ );
15
+
16
+ if (!inventoryItem) {
17
+ return 0;
18
+ }
19
+
20
+ const possibleWithThisIngredient = Math.floor(
21
+ inventoryItem.stackQty ?? 0 / ingredient.qty
22
+ );
23
+
24
+ if (maxPossible === -1) {
25
+ return possibleWithThisIngredient;
26
+ }
27
+
28
+ return Math.min(maxPossible, possibleWithThisIngredient);
29
+ }, -1);
30
+ }
@@ -22,7 +22,12 @@ const Template: Story = args => (
22
22
  onSelect={onSelect}
23
23
  onClose={() => console.log('closing Equipment Set Container')}
24
24
  craftablesItems={craftableItems}
25
- onCraftItem={onCraftItem}
25
+ onCraftItem={(value, maxCraftable) => {
26
+ console.log('Crafting:', {
27
+ itemKey: value,
28
+ maxCraftable: maxCraftable,
29
+ });
30
+ }}
26
31
  equipmentSet={equipmentSetMock}
27
32
  {...args}
28
33
  />
@@ -31,10 +36,6 @@ const Template: Story = args => (
31
36
 
32
37
  export const Default = Template.bind({});
33
38
 
34
- const onCraftItem = (value: string | undefined) => {
35
- console.log('Craft', value);
36
- };
37
-
38
39
  const onSelect = (value: string) => {
39
40
  console.log('Story package', value);
40
41
  };