Listening new state for respects and replace treat

This commit is contained in:
object 2022-08-10 16:29:43 +00:00
parent 058088b789
commit 6afbba964d

View File

@ -1,7 +1,7 @@
import { PetRespectComposer, PetType, RoomControllerLevel, RoomObjectCategory, RoomObjectType, RoomObjectVariable, RoomUnitGiveHandItemPetComposer } from '@nitrots/nitro-renderer'; import { PetRespectComposer, PetType, RoomControllerLevel, RoomObjectCategory, RoomObjectType, RoomObjectVariable, RoomUnitGiveHandItemPetComposer } from '@nitrots/nitro-renderer';
import { FC, useEffect, useMemo, useState } from 'react'; import { FC, useEffect, useMemo, useState } from 'react';
import { AvatarInfoPet, GetOwnRoomObject, GetSessionDataManager, LocalizeText, SendMessageComposer } from '../../../../../api'; import { AvatarInfoPet, GetOwnRoomObject, GetSessionDataManager, LocalizeText, SendMessageComposer } from '../../../../../api';
import { useRoom } from '../../../../../hooks'; import { usePets, useRoom } from '../../../../../hooks';
import { ContextMenuHeaderView } from '../../context-menu/ContextMenuHeaderView'; import { ContextMenuHeaderView } from '../../context-menu/ContextMenuHeaderView';
import { ContextMenuListItemView } from '../../context-menu/ContextMenuListItemView'; import { ContextMenuListItemView } from '../../context-menu/ContextMenuListItemView';
import { ContextMenuView } from '../../context-menu/ContextMenuView'; import { ContextMenuView } from '../../context-menu/ContextMenuView';
@ -21,8 +21,14 @@ export const AvatarInfoWidgetPetView: FC<AvatarInfoWidgetPetViewProps> = props =
{ {
const { avatarInfo = null, onClose = null } = props; const { avatarInfo = null, onClose = null } = props;
const [ mode, setMode ] = useState(MODE_NORMAL); const [ mode, setMode ] = useState(MODE_NORMAL);
const [ respectsLeft, setRespectsLeft ] = useState(0);
const { roomSession = null } = useRoom(); const { roomSession = null } = useRoom();
const { petRespect, changePetRespect } = usePets();
useEffect(() =>
{
changePetRespect(avatarInfo.respectsPetLeft);
}, [ avatarInfo ]);
const canPickUp = useMemo(() => const canPickUp = useMemo(() =>
{ {
@ -54,18 +60,18 @@ export const AvatarInfoWidgetPetView: FC<AvatarInfoWidgetPetViewProps> = props =
switch(name) switch(name)
{ {
case 'respect': case 'respect':
let newRespectsLeft = 0; let newRespectsLeftChange = 0;
setRespectsLeft(prevValue => changePetRespect(prevValue =>
{ {
newRespectsLeft = (prevValue - 1); newRespectsLeftChange = (prevValue - 1);
return newRespectsLeft; return newRespectsLeftChange;
}); });
GetSessionDataManager().givePetRespect(avatarInfo.id); GetSessionDataManager().givePetRespect(avatarInfo.id);
if(newRespectsLeft > 0) hideMenu = false; if(newRespectsLeftChange > 0) hideMenu = false;
break; break;
case 'treat': case 'treat':
SendMessageComposer(new PetRespectComposer(avatarInfo.id)); SendMessageComposer(new PetRespectComposer(avatarInfo.id));
@ -99,7 +105,8 @@ export const AvatarInfoWidgetPetView: FC<AvatarInfoWidgetPetViewProps> = props =
return MODE_NORMAL; return MODE_NORMAL;
}); });
setRespectsLeft(avatarInfo.respectsPetLeft); changePetRespect(avatarInfo.respectsPetLeft);
}, [ avatarInfo ]); }, [ avatarInfo ]);
return ( return (
@ -107,9 +114,9 @@ export const AvatarInfoWidgetPetView: FC<AvatarInfoWidgetPetViewProps> = props =
<ContextMenuHeaderView> <ContextMenuHeaderView>
{ avatarInfo.name } { avatarInfo.name }
</ContextMenuHeaderView> </ContextMenuHeaderView>
{ (mode === MODE_NORMAL) && (respectsLeft > 0) && { (mode === MODE_NORMAL) && (petRespect > 0) &&
<ContextMenuListItemView onClick={ event => processAction('respect') }> <ContextMenuListItemView onClick={ event => processAction('respect') }>
{ LocalizeText('infostand.button.petrespect', [ 'count' ], [ respectsLeft.toString() ]) } { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) }
</ContextMenuListItemView> } </ContextMenuListItemView> }
{ (mode === MODE_SADDLED_UP) && { (mode === MODE_SADDLED_UP) &&
<> <>
@ -117,9 +124,9 @@ export const AvatarInfoWidgetPetView: FC<AvatarInfoWidgetPetViewProps> = props =
<ContextMenuListItemView onClick={ event => processAction('mount') }> <ContextMenuListItemView onClick={ event => processAction('mount') }>
{ LocalizeText('infostand.button.mount') } { LocalizeText('infostand.button.mount') }
</ContextMenuListItemView> } </ContextMenuListItemView> }
{ (respectsLeft > 0) && { (petRespect > 0) &&
<ContextMenuListItemView onClick={ event => processAction('respect') }> <ContextMenuListItemView onClick={ event => processAction('respect') }>
{ LocalizeText('infostand.button.petrespect', [ 'count' ], [ respectsLeft.toString() ]) } { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) }
</ContextMenuListItemView> } </ContextMenuListItemView> }
</> } </> }
{ (mode === MODE_RIDING) && { (mode === MODE_RIDING) &&
@ -127,14 +134,14 @@ export const AvatarInfoWidgetPetView: FC<AvatarInfoWidgetPetViewProps> = props =
<ContextMenuListItemView onClick={ event => processAction('dismount') }> <ContextMenuListItemView onClick={ event => processAction('dismount') }>
{ LocalizeText('infostand.button.dismount') } { LocalizeText('infostand.button.dismount') }
</ContextMenuListItemView> </ContextMenuListItemView>
{ (respectsLeft > 0) && { (petRespect > 0) &&
<ContextMenuListItemView onClick={ event => processAction('respect') }> <ContextMenuListItemView onClick={ event => processAction('respect') }>
{ LocalizeText('infostand.button.petrespect', [ 'count' ], [ respectsLeft.toString() ]) } { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) }
</ContextMenuListItemView> } </ContextMenuListItemView> }
</> } </> }
{ (mode === MODE_MONSTER_PLANT) && !avatarInfo.dead && ((avatarInfo.energy / avatarInfo.maximumEnergy) < 0.98) && { (mode === MODE_MONSTER_PLANT) && !avatarInfo.dead && ((avatarInfo.energy / avatarInfo.maximumEnergy) < 0.98) &&
<ContextMenuListItemView onClick={ event => processAction('treat') }> <ContextMenuListItemView onClick={ event => processAction('treat') }>
{ LocalizeText('infostand.button.treat') } { LocalizeText('infostand.button.pettreat') }
</ContextMenuListItemView> } </ContextMenuListItemView> }
{ canPickUp && { canPickUp &&
<ContextMenuListItemView onClick={ event => processAction('pick_up') }> <ContextMenuListItemView onClick={ event => processAction('pick_up') }>