Crafting updates (still missing some details)

This commit is contained in:
MyNameIsBatman 2022-09-20 17:50:45 -03:00
parent 818c6b6070
commit c6e44bff70
2 changed files with 25 additions and 44 deletions

View File

@ -1,18 +1,15 @@
import { CraftComposer, CraftingRecipeIngredientParser, RoomObjectCategory } from '@nitrots/nitro-renderer';
import { FC, useCallback, useEffect, useMemo, useState } from 'react';
import { GetRoomEngine, IsOwnerOfFurniture, LocalizeText, SendMessageComposer } from '../../../../api';
import { AutoGrid, Button, Column, Flex, LayoutGridItem, NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../../../common';
import { useFurnitureCraftingWidget, useInventoryFurni, useRoom } from '../../../../hooks';
import { CraftingRecipeIngredientParser, RoomObjectCategory } from '@nitrots/nitro-renderer';
import { FC, useMemo, useState } from 'react';
import { GetRoomEngine, IsOwnerOfFurniture, LocalizeText } from '../../../../api';
import { AutoGrid, Button, Column, Flex, LayoutGridItem, LayoutLoadingSpinnerView, NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../../../common';
import { useFurnitureCraftingWidget, useRoom } from '../../../../hooks';
export const FurnitureCraftingView: FC<{}> = props =>
{
const { objectId = -1, recipes = [], ingredients = [], selectedRecipe = null, selectedRecipeIngredients = null, selectRecipe = null, onClose = null } = useFurnitureCraftingWidget();
const { activate, deactivate } = useInventoryFurni();
const { objectId = -1, recipes = [], ingredients = [], selectedRecipe = null, selectedRecipeIngredients = null, isCrafting = false, craft = null, selectRecipe = null, onClose = null } = useFurnitureCraftingWidget();
const { roomSession = null } = useRoom();
const [ craftCounter, setCraftCounter ] = useState(0);
const [ tryingToCraft, setTryingToCraft ] = useState(false);
const [ craftInterval, setCraftInterval ] = useState(null);
const [ waitingToConfirm, setWaitingToConfirm ] = useState(false);
const isOwner = useMemo(() =>
{
@ -32,44 +29,18 @@ export const FurnitureCraftingView: FC<{}> = props =>
return true;
}, [ ingredients, selectedRecipeIngredients ]);
const cancelCraft = () =>
{
setTryingToCraft(false);
setCraftCounter(0);
clearInterval(craftInterval);
};
const tryCraft = () =>
{
if (tryingToCraft)
if (!waitingToConfirm)
{
cancelCraft();
setWaitingToConfirm(true);
return;
}
setCraftCounter(5);
setTryingToCraft(true);
setCraftInterval(setInterval(() => setCraftCounter(v => v - 1), 1000));
craft();
setWaitingToConfirm(false);
};
useEffect(() =>
{
if (craftCounter <= 0 && tryingToCraft)
{
clearInterval(craftInterval);
setCraftInterval(null);
setTryingToCraft(false);
craft();
}
}, [ craftCounter ]);
const craft = useCallback(() =>
{
if (!selectedRecipe) return;
SendMessageComposer(new CraftComposer(objectId, selectedRecipe.name));
}, [ objectId, selectedRecipe ]);
const renderSelectedRecipeIngredient = (ingredient: CraftingRecipeIngredientParser) =>
{
const ingredientData = ingredients.find((i) => i.name === ingredient.itemName);
@ -124,8 +95,9 @@ export const FurnitureCraftingView: FC<{}> = props =>
<div className="text-black">{ selectedRecipe.localizedName }</div>
</Flex>
</Flex>
<Button variant={ !isOwner || !canCraft ? 'danger' : tryingToCraft ? 'warning' : 'success' } disabled={ !isOwner || !canCraft } onClick={ tryCraft }>
{ LocalizeText(!isOwner ? 'crafting.btn.notowner' : !canCraft ? 'crafting.status.recipe.incomplete' : tryingToCraft ? 'generic.cancel' : 'crafting.btn.craft') }
<Button variant={ !isOwner || !canCraft ? 'danger' : waitingToConfirm ? 'warning' : isCrafting ? 'primary' : 'success' } disabled={ !isOwner || !canCraft || isCrafting } onClick={ tryCraft }>
{ !isCrafting && LocalizeText(!isOwner ? 'crafting.btn.notowner' : !canCraft ? 'crafting.status.recipe.incomplete' : waitingToConfirm ? 'generic.confirm' : 'crafting.btn.craft') }
{ isCrafting && <LayoutLoadingSpinnerView /> }
</Button>
</Flex>
</> }

View File

@ -1,4 +1,4 @@
import { CraftableProductsEvent, CraftingRecipeEvent, CraftingRecipeIngredientParser, CraftingRecipesAvailableEvent, CraftingResultEvent, GetCraftableProductsComposer, GetCraftingRecipeComposer, RoomEngineTriggerWidgetEvent, RoomWidgetEnum } from '@nitrots/nitro-renderer';
import { CraftableProductsEvent, CraftComposer, CraftingRecipeEvent, CraftingRecipeIngredientParser, CraftingRecipesAvailableEvent, CraftingResultEvent, GetCraftableProductsComposer, GetCraftingRecipeComposer, RoomEngineTriggerWidgetEvent, RoomWidgetEnum } from '@nitrots/nitro-renderer';
import { useCallback, useState } from 'react';
import { GetRoomEngine, LocalizeText, SendMessageComposer } from '../../../../api';
import { useMessageEvent, useRoomEngineEvent } from '../../../events';
@ -16,6 +16,7 @@ const useFurnitureCraftingWidgetState = () =>
const [ selectedRecipe, setSelectedRecipe ] = useState<{ name: string, localizedName: string, iconUrl: string }>(null);
const [ selectedRecipeIngredients, setSelectedRecipeIngredients ] = useState<CraftingRecipeIngredientParser[]>([]);
const [ cacheRecipeIngredients, setCacheRecipeIngredients ] = useState<Map<string, CraftingRecipeIngredientParser[]>>(new Map());
const [ isCrafting, setIsCrafting ] = useState(false);
const resetData = () =>
{
@ -32,6 +33,12 @@ const useFurnitureCraftingWidgetState = () =>
resetData();
};
const craft = () =>
{
setIsCrafting(true);
SendMessageComposer(new CraftComposer(objectId, selectedRecipe.name));
};
const selectRecipe = useCallback((recipe: { name: string, localizedName: string, iconUrl: string }) =>
{
setSelectedRecipeIngredients([]);
@ -124,13 +131,15 @@ const useFurnitureCraftingWidgetState = () =>
{
simpleAlert(LocalizeText('crafting.info.result.ok'));
}
setIsCrafting(false);
});
useMessageEvent<CraftingRecipesAvailableEvent>(CraftingRecipesAvailableEvent, event =>
{
});
return { objectId, recipes, ingredients, selectedRecipe, selectedRecipeIngredients, selectRecipe, onClose };
return { objectId, recipes, ingredients, selectedRecipe, selectedRecipeIngredients, isCrafting, selectRecipe, craft, onClose };
}
export const useFurnitureCraftingWidget = useFurnitureCraftingWidgetState;