nitro-react/src/views/room/widgets/avatar-info/AvatarInfoWidgetView.tsx

362 lines
15 KiB
TypeScript
Raw Normal View History

import { RoomEnterEffect, RoomObjectCategory } from '@nitrots/nitro-renderer';
2021-06-24 09:58:43 +02:00
import { FC, useCallback, useMemo, useState } from 'react';
2021-08-17 05:38:07 +02:00
import { GetRoomSession, GetSessionDataManager, RoomWidgetObjectNameEvent, RoomWidgetRoomEngineUpdateEvent, RoomWidgetRoomObjectMessage, RoomWidgetRoomObjectUpdateEvent, RoomWidgetUpdateDanceStatusEvent, RoomWidgetUpdateInfostandEvent, RoomWidgetUpdateInfostandFurniEvent, RoomWidgetUpdateInfostandPetEvent, RoomWidgetUpdateInfostandRentableBotEvent, RoomWidgetUpdateInfostandUserEvent, RoomWidgetUpdateRentableBotChatEvent, RoomWidgetUseProductBubbleEvent, UseProductItem } from '../../../../api';
2021-06-12 04:53:56 +02:00
import { CreateEventDispatcherHook } from '../../../../hooks/events/event-dispatcher.base';
2021-06-17 19:23:34 +02:00
import { useRoomContext } from '../../context/RoomContext';
2021-06-24 09:58:43 +02:00
import { AvatarInfoWidgetAvatarView } from './views/avatar/AvatarInfoWidgetAvatarView';
import { AvatarInfoWidgetDecorateView } from './views/decorate/AvatarInfoWidgetDecorateView';
2021-06-13 10:13:46 +02:00
import { AvatarInfoWidgetNameView } from './views/name/AvatarInfoWidgetNameView';
2021-06-24 09:58:43 +02:00
import { AvatarInfoWidgetOwnAvatarView } from './views/own-avatar/AvatarInfoWidgetOwnAvatarView';
import { AvatarInfoWidgetOwnPetView } from './views/own-pet/AvatarInfoWidgetOwnPetView';
import { AvatarInfoWidgetPetView } from './views/pet/AvatarInfoWidgetPetView';
import { AvatarInfoRentableBotChatView } from './views/rentable-bot-chat/AvatarInfoRentableBotChatView';
2021-06-24 09:58:43 +02:00
import { AvatarInfoWidgetRentableBotView } from './views/rentable-bot/AvatarInfoWidgetRentableBotView';
2021-07-09 19:29:00 +02:00
import { AvatarInfoUseProductConfirmView } from './views/use-product-confirm/AvatarInfoUseProductConfirmView';
import { AvatarInfoUseProductView } from './views/use-product/AvatarInfoUseProductView';
2021-06-12 04:53:56 +02:00
2021-07-12 09:59:40 +02:00
export const AvatarInfoWidgetView: FC<{}> = props =>
2021-06-12 04:53:56 +02:00
{
2021-07-09 19:29:00 +02:00
const { roomSession = null, eventDispatcher = null, widgetHandler = null } = useRoomContext();
2021-06-17 19:23:34 +02:00
const [ name, setName ] = useState<RoomWidgetObjectNameEvent>(null);
2021-07-07 09:56:11 +02:00
const [ nameBubbles, setNameBubbles ] = useState<RoomWidgetObjectNameEvent[]>([]);
2021-07-09 19:29:00 +02:00
const [ productBubbles, setProductBubbles ] = useState<UseProductItem[]>([]);
const [ confirmingProduct, setConfirmingProduct ] = useState<UseProductItem>(null);
2021-06-17 19:23:34 +02:00
const [ infoStandEvent, setInfoStandEvent ] = useState<RoomWidgetUpdateInfostandEvent>(null);
2021-06-13 10:13:46 +02:00
const [ isGameMode, setGameMode ] = useState(false);
2021-06-24 09:58:43 +02:00
const [ isDancing, setIsDancing ] = useState(false);
2021-09-13 18:29:04 +02:00
const [ isDecorating, setIsDecorating ] = useState(false);
const [ rentableBotChatEvent, setRentableBotChatEvent ] = useState<RoomWidgetUpdateRentableBotChatEvent>(null);
2021-06-12 04:53:56 +02:00
2021-07-07 09:56:11 +02:00
const removeNameBubble = useCallback((index: number) =>
{
setNameBubbles(prevValue =>
{
const newValue = [ ...prevValue ];
newValue.splice(index, 1);
return newValue;
});
}, []);
2021-07-09 19:29:00 +02:00
const removeProductBubble = useCallback((index: number) =>
{
setProductBubbles(prevValue =>
{
const newValue = [ ...prevValue ];
const item = newValue.splice(index, 1)[0];
if(confirmingProduct === item) setConfirmingProduct(null);
return newValue;
});
}, [ confirmingProduct ]);
2021-07-11 08:37:56 +02:00
const clearProductBubbles = useCallback(() =>
{
setProductBubbles([]);
}, []);
2021-07-07 09:56:11 +02:00
const clearInfoStandEvent = useCallback(() =>
{
setInfoStandEvent(null);
}, []);
const clearName = useCallback(() =>
{
setName(null);
}, []);
2021-07-11 08:37:56 +02:00
const updateConfirmingProduct = useCallback((product: UseProductItem) =>
{
setConfirmingProduct(product);
setProductBubbles([]);
}, []);
2021-06-24 09:58:43 +02:00
const onRoomWidgetRoomEngineUpdateEvent = useCallback((event: RoomWidgetRoomEngineUpdateEvent) =>
2021-06-12 04:53:56 +02:00
{
2021-06-13 10:13:46 +02:00
switch(event.type)
2021-06-12 04:53:56 +02:00
{
2021-06-17 19:23:34 +02:00
case RoomWidgetRoomEngineUpdateEvent.NORMAL_MODE: {
2021-06-24 09:58:43 +02:00
if(isGameMode) setGameMode(false);
2021-06-17 19:23:34 +02:00
return;
}
case RoomWidgetRoomEngineUpdateEvent.GAME_MODE: {
2021-06-24 09:58:43 +02:00
if(!isGameMode) setGameMode(true);
2021-06-17 19:23:34 +02:00
return;
}
2021-06-24 09:58:43 +02:00
}
}, [ isGameMode ]);
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
CreateEventDispatcherHook(RoomWidgetRoomEngineUpdateEvent.NORMAL_MODE, eventDispatcher, onRoomWidgetRoomEngineUpdateEvent);
CreateEventDispatcherHook(RoomWidgetRoomEngineUpdateEvent.GAME_MODE, eventDispatcher, onRoomWidgetRoomEngineUpdateEvent);
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
const onRoomObjectRemoved = useCallback((event: RoomWidgetRoomObjectUpdateEvent) =>
{
if(name)
{
if(event.id === name.id) setName(null);
}
2021-06-12 04:53:56 +02:00
2021-07-07 09:56:11 +02:00
if(event.category === RoomObjectCategory.UNIT)
{
const nameBubbleIndex = nameBubbles.findIndex(bubble =>
{
return (bubble.roomIndex === event.id);
});
if(nameBubbleIndex > -1) removeNameBubble(nameBubbleIndex);
2021-07-09 19:29:00 +02:00
if(productBubbles.length)
{
setProductBubbles(prevValue =>
{
return prevValue.filter(bubble =>
{
return (bubble.id !== event.id);
});
});
}
}
else if(event.category === RoomObjectCategory.FLOOR)
{
if(productBubbles.length)
{
setProductBubbles(prevValue =>
{
return prevValue.filter(bubble =>
{
return (bubble.requestRoomObjectId !== event.id);
});
});
}
2021-07-07 09:56:11 +02:00
}
2021-06-24 09:58:43 +02:00
if(infoStandEvent)
{
if(infoStandEvent instanceof RoomWidgetUpdateInfostandFurniEvent)
{
2021-07-11 08:37:56 +02:00
if(infoStandEvent.id === event.id) clearInfoStandEvent();
2021-06-24 09:58:43 +02:00
}
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
else if((infoStandEvent instanceof RoomWidgetUpdateInfostandUserEvent) || (infoStandEvent instanceof RoomWidgetUpdateInfostandRentableBotEvent))
{
2021-07-11 08:37:56 +02:00
if(infoStandEvent.roomIndex === event.id) clearInfoStandEvent();
2021-06-24 09:58:43 +02:00
}
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
else if(infoStandEvent instanceof RoomWidgetUpdateInfostandPetEvent)
{
2021-07-11 08:37:56 +02:00
if(infoStandEvent.roomIndex === event.id) clearInfoStandEvent();
2021-06-13 10:13:46 +02:00
}
2021-06-24 09:58:43 +02:00
}
2021-07-11 08:37:56 +02:00
}, [ name, infoStandEvent, nameBubbles, productBubbles, removeNameBubble, clearInfoStandEvent ]);
2021-06-24 09:58:43 +02:00
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.USER_REMOVED, eventDispatcher, onRoomObjectRemoved);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.FURNI_REMOVED, eventDispatcher, onRoomObjectRemoved);
const onObjectRolled = useCallback((event: RoomWidgetRoomObjectUpdateEvent) =>
{
switch(event.type)
{
2021-06-17 19:23:34 +02:00
case RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OVER: {
const roomObjectEvent = (event as RoomWidgetRoomObjectUpdateEvent);
2021-06-24 09:58:43 +02:00
if(infoStandEvent) return;
2021-06-17 19:23:34 +02:00
widgetHandler.processWidgetMessage(new RoomWidgetRoomObjectMessage(RoomWidgetRoomObjectMessage.GET_OBJECT_NAME, roomObjectEvent.id, roomObjectEvent.category));
2021-06-24 09:58:43 +02:00
2021-06-17 19:23:34 +02:00
return;
}
case RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OUT: {
const roomObjectEvent = (event as RoomWidgetRoomObjectUpdateEvent);
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
if(!name || (name.roomIndex !== roomObjectEvent.id)) return;
2021-06-17 19:23:34 +02:00
setName(null);
2021-06-24 09:58:43 +02:00
2021-06-13 10:13:46 +02:00
return;
}
2021-06-24 09:58:43 +02:00
}
}, [ infoStandEvent, name, widgetHandler ]);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OVER, eventDispatcher, onObjectRolled);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OUT, eventDispatcher, onObjectRolled);
const onObjectDeselected = useCallback((event: RoomWidgetRoomObjectUpdateEvent) =>
{
2021-07-11 08:37:56 +02:00
if(infoStandEvent) clearInfoStandEvent();
2021-07-09 19:29:00 +02:00
if(productBubbles.length) setProductBubbles([]);
2021-07-11 08:37:56 +02:00
}, [ infoStandEvent, productBubbles, clearInfoStandEvent ]);
2021-06-24 09:58:43 +02:00
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.OBJECT_DESELECTED, eventDispatcher, onObjectDeselected);
const onRoomWidgetObjectNameEvent = useCallback((event: RoomWidgetObjectNameEvent) =>
{
if(event.category !== RoomObjectCategory.UNIT) return;
setName(event);
2021-07-11 08:37:56 +02:00
clearProductBubbles();
}, [ clearProductBubbles ]);
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
CreateEventDispatcherHook(RoomWidgetObjectNameEvent.TYPE, eventDispatcher, onRoomWidgetObjectNameEvent);
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
const onRoomWidgetUpdateInfostandEvent = useCallback((event: RoomWidgetUpdateInfostandEvent) =>
{
if(name) setName(null);
2021-06-26 19:32:32 +02:00
2021-07-11 08:37:56 +02:00
if(event.type === RoomWidgetUpdateInfostandFurniEvent.FURNI) clearInfoStandEvent();
2021-06-26 19:32:32 +02:00
else setInfoStandEvent(event);
2021-07-11 08:37:56 +02:00
clearProductBubbles();
}, [ name, clearInfoStandEvent, clearProductBubbles ]);
2021-06-24 09:58:43 +02:00
CreateEventDispatcherHook(RoomWidgetUpdateInfostandFurniEvent.FURNI, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandUserEvent.OWN_USER, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandUserEvent.PEER, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandUserEvent.BOT, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandRentableBotEvent.RENTABLE_BOT, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
2021-06-24 18:47:40 +02:00
CreateEventDispatcherHook(RoomWidgetUpdateInfostandPetEvent.PET_INFO, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
2021-06-24 09:58:43 +02:00
const onRoomWidgetUpdateDanceStatusEvent = useCallback((event: RoomWidgetUpdateDanceStatusEvent) =>
{
setIsDancing(event.isDancing);
}, []);
CreateEventDispatcherHook(RoomWidgetUpdateDanceStatusEvent.UPDATE_DANCE, eventDispatcher, onRoomWidgetUpdateDanceStatusEvent);
2021-07-07 09:56:11 +02:00
const onRoomWidgetUpdateRentableBotChatEvent = useCallback((event: RoomWidgetUpdateRentableBotChatEvent) =>
2021-06-24 09:58:43 +02:00
{
2021-07-07 09:56:11 +02:00
setRentableBotChatEvent(event);
2021-06-24 09:58:43 +02:00
}, []);
2021-07-07 09:56:11 +02:00
CreateEventDispatcherHook(RoomWidgetUpdateRentableBotChatEvent.UPDATE_CHAT, eventDispatcher, onRoomWidgetUpdateRentableBotChatEvent);
2021-06-24 09:58:43 +02:00
2021-07-09 19:29:00 +02:00
const onRoomWidgetUseProductBubbleEvent = useCallback((event: RoomWidgetUseProductBubbleEvent) =>
{
2021-07-11 08:37:56 +02:00
setConfirmingProduct(null);
2021-07-09 19:29:00 +02:00
setProductBubbles(prevValue =>
{
const newBubbles = [ ...prevValue ];
for(const item of event.items)
{
const index = newBubbles.findIndex(bubble => (bubble.id === item.id));
if(index > -1) newBubbles.splice(index, 1);
newBubbles.push(item);
}
return newBubbles;
});
}, []);
CreateEventDispatcherHook(RoomWidgetUseProductBubbleEvent.USE_PRODUCT_BUBBLES, eventDispatcher, onRoomWidgetUseProductBubbleEvent);
2021-08-17 20:30:15 +02:00
// const onFriendEnteredRoomEvent = useCallback((event: FriendEnteredRoomEvent) =>
// {
// setNameBubbles(prevValue =>
// {
// return [ ...prevValue, event ];
// })
// }, []);
2021-08-17 20:30:15 +02:00
// useUiEvent(FriendEnteredRoomEvent.ENTERED, onFriendEnteredRoomEvent);
2021-06-24 09:58:43 +02:00
const decorateView = useMemo(() =>
{
GetRoomSession().isDecorating = isDecorating;
if(!isDecorating) return null;
const userId = GetSessionDataManager().userId;
const userName = GetSessionDataManager().userName;
const roomIndex = GetRoomSession().ownRoomIndex;
2021-06-24 18:47:40 +02:00
return <AvatarInfoWidgetDecorateView userId={ userId } userName={ userName } roomIndex={ roomIndex } setIsDecorating={ setIsDecorating } />;
2021-06-24 09:58:43 +02:00
}, [ isDecorating ]);
const currentView = useMemo(() =>
{
if(isGameMode) return null;
if(decorateView) return decorateView;
2021-07-07 09:56:11 +02:00
if(name)
{
const nameBubbleIndex = nameBubbles.findIndex(bubble =>
{
return (bubble.roomIndex === name.roomIndex);
});
if(nameBubbleIndex > -1) removeNameBubble(nameBubbleIndex);
return <AvatarInfoWidgetNameView nameData={ name } close={ clearName } />;
}
2021-06-24 09:58:43 +02:00
if(infoStandEvent)
{
switch(infoStandEvent.type)
{
case RoomWidgetUpdateInfostandUserEvent.OWN_USER:
case RoomWidgetUpdateInfostandUserEvent.PEER: {
const event = (infoStandEvent as RoomWidgetUpdateInfostandUserEvent);
if(event.isSpectatorMode) return null;
2021-07-07 09:56:11 +02:00
const nameBubbleIndex = nameBubbles.findIndex(bubble =>
{
return (bubble.roomIndex === event.roomIndex);
});
if(nameBubbleIndex > -1) removeNameBubble(nameBubbleIndex);
2021-06-24 09:58:43 +02:00
if(event.isOwnUser)
{
if(RoomEnterEffect.isRunning()) return null;
2021-06-24 18:47:40 +02:00
return <AvatarInfoWidgetOwnAvatarView userData={ event } isDancing={ isDancing } setIsDecorating={ setIsDecorating } close={ clearInfoStandEvent } />;
2021-06-24 09:58:43 +02:00
}
return <AvatarInfoWidgetAvatarView userData={ event } close={ clearInfoStandEvent } />;
}
case RoomWidgetUpdateInfostandPetEvent.PET_INFO: {
const event = (infoStandEvent as RoomWidgetUpdateInfostandPetEvent);
if(event.isOwner)
{
return <AvatarInfoWidgetOwnPetView petData={ event } close={ clearInfoStandEvent } />;
}
return <AvatarInfoWidgetPetView petData={ event } close={ clearInfoStandEvent } />;
}
case RoomWidgetUpdateInfostandRentableBotEvent.RENTABLE_BOT: {
return <AvatarInfoWidgetRentableBotView rentableBotData={ (infoStandEvent as RoomWidgetUpdateInfostandRentableBotEvent) } close={ clearInfoStandEvent } />
}
case RoomWidgetUpdateInfostandFurniEvent.FURNI: {
return null;
}
}
}
return null;
2021-07-07 09:56:11 +02:00
}, [ isGameMode, decorateView, name, nameBubbles, isDancing, infoStandEvent, clearName, clearInfoStandEvent, removeNameBubble ]);
2021-06-13 10:13:46 +02:00
return (
<>
2021-06-24 09:58:43 +02:00
{ currentView }
2021-07-07 09:56:11 +02:00
{ (nameBubbles.length > 0) && nameBubbles.map((name, index) =>
{
2021-07-07 11:05:14 +02:00
return <AvatarInfoWidgetNameView key={ index } nameData={ name } close={ () => removeNameBubble(index) } />;
2021-07-07 09:56:11 +02:00
}) }
2021-07-09 19:29:00 +02:00
{ (productBubbles.length > 0) && productBubbles.map((item, index) =>
{
2021-07-11 08:37:56 +02:00
return <AvatarInfoUseProductView key={ item.id } item={ item } updateConfirmingProduct={ updateConfirmingProduct } close={ () => removeProductBubble(index) } />;
2021-07-09 19:29:00 +02:00
}) }
{ rentableBotChatEvent && <AvatarInfoRentableBotChatView chatEvent={ rentableBotChatEvent } /> }
2021-07-09 19:29:00 +02:00
{ confirmingProduct && <AvatarInfoUseProductConfirmView item={ confirmingProduct } close={ () => setConfirmingProduct(null) } /> }
2021-06-13 10:13:46 +02:00
</>
)
2021-06-12 04:53:56 +02:00
}