From c55c0c1ee25ce0f6a93d8cd0f93194f4c77a4726 Mon Sep 17 00:00:00 2001 From: MyNameIsBatman Date: Sat, 12 Jun 2021 04:17:17 -0300 Subject: [PATCH] Exchange Credit Widget and fix Trophy title --- .../furniture/FurnitureWidgetsView.tsx | 2 + .../FurnitureExchangeCreditData.ts | 7 ++ .../FurnitureExchangeCreditView.tsx | 76 +++++++++++++++++++ .../FurnitureExchangeCreditView.types.ts | 4 + .../furniture/trophy/FurnitureTrophyView.tsx | 3 +- 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditData.ts create mode 100644 src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx create mode 100644 src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.types.ts diff --git a/src/views/room/widgets/furniture/FurnitureWidgetsView.tsx b/src/views/room/widgets/furniture/FurnitureWidgetsView.tsx index 98aed99b..06959d8d 100644 --- a/src/views/room/widgets/furniture/FurnitureWidgetsView.tsx +++ b/src/views/room/widgets/furniture/FurnitureWidgetsView.tsx @@ -1,3 +1,4 @@ +import { FurnitureExchangeCreditView } from './exchange-credit/FurnitureExchangeCreditView'; import { FurnitureWidgetsViewProps } from './FurnitureWidgetsView.types'; import { FurnitureHighScoreView } from './high-score/FurnitureHighScoreView'; import { FurnitureManipulationMenuView } from './manipulation-menu/FurnitureManipulationMenuView'; @@ -12,6 +13,7 @@ export function FurnitureWidgetsView(props: FurnitureWidgetsViewProps): JSX.Elem return (
+ diff --git a/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditData.ts b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditData.ts new file mode 100644 index 00000000..eeb128b3 --- /dev/null +++ b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditData.ts @@ -0,0 +1,7 @@ +export class FurnitureExchangeCreditData +{ + constructor( + public objectId: number, + public category: number, + public value: number) {} +} diff --git a/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx new file mode 100644 index 00000000..414e5ac0 --- /dev/null +++ b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx @@ -0,0 +1,76 @@ +import { FurnitureExchangeComposer, NitroEvent, RoomEngineTriggerWidgetEvent, RoomObjectVariable } from 'nitro-renderer'; +import { FC, useCallback, useState } from 'react'; +import { GetRoomEngine, GetRoomSession } from '../../../../../api'; +import { CreateEventDispatcherHook } from '../../../../../hooks/events/event-dispatcher.base'; +import { useRoomEngineEvent } from '../../../../../hooks/events/nitro/room/room-engine-event'; +import { NitroCardContentView, NitroCardHeaderView } from '../../../../../layout'; +import { NitroCardView } from '../../../../../layout/card/NitroCardView'; +import { LocalizeText } from '../../../../../utils/LocalizeText'; +import { RoomWidgetRoomObjectUpdateEvent } from '../../events'; +import { FurnitureExchangeCreditData } from './FurnitureExchangeCreditData'; +import { FurnitureExchangeCreditProps } from './FurnitureExchangeCreditView.types'; + +export const FurnitureExchangeCreditView: FC = props => +{ + const [ exchangeCreditData, setExchangeCreditData ] = useState(null); + + const onNitroEvent = useCallback((event: NitroEvent) => + { + switch(event.type) + { + case RoomEngineTriggerWidgetEvent.REQUEST_CREDITFURNI: { + const widgetEvent = (event as RoomEngineTriggerWidgetEvent); + + const roomObject = GetRoomEngine().getRoomObject(widgetEvent.roomId, widgetEvent.objectId, widgetEvent.category); + + const value = roomObject.model.getValue(RoomObjectVariable.FURNITURE_CREDIT_VALUE); + + setExchangeCreditData(new FurnitureExchangeCreditData(widgetEvent.objectId, widgetEvent.category, value)); + return; + } + case RoomWidgetRoomObjectUpdateEvent.FURNI_REMOVED: { + const widgetEvent = (event as RoomWidgetRoomObjectUpdateEvent); + + setExchangeCreditData(prevState => + { + if(!prevState || (widgetEvent.id !== prevState.objectId) || (widgetEvent.category !== prevState.category)) return prevState; + + return null; + }); + return; + } + } + }, []); + + useRoomEngineEvent(RoomEngineTriggerWidgetEvent.REQUEST_CREDITFURNI, onNitroEvent); + CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.FURNI_REMOVED, props.events, onNitroEvent); + + const processAction = useCallback((type: string, value: string = null) => + { + switch(type) + { + case 'close': + setExchangeCreditData(null); + return; + case 'redeem': + if(!exchangeCreditData) return null; + + GetRoomSession().connection.send(new FurnitureExchangeComposer(exchangeCreditData.objectId)); + return; + } + }, []); + + if(!exchangeCreditData) return null; + + return ( + + processAction('close') } /> + +
+ { LocalizeText('widgets.furniture.credit.redeem.value', [ 'value' ], [ exchangeCreditData.value.toString() ]) } +
+ +
+
+ ); +} diff --git a/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.types.ts b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.types.ts new file mode 100644 index 00000000..32a48de0 --- /dev/null +++ b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.types.ts @@ -0,0 +1,4 @@ +import { FurnitureWidgetProps } from '../FurnitureWidget.types'; + +export interface FurnitureExchangeCreditProps extends FurnitureWidgetProps +{} diff --git a/src/views/room/widgets/furniture/trophy/FurnitureTrophyView.tsx b/src/views/room/widgets/furniture/trophy/FurnitureTrophyView.tsx index d4d1d3e0..95446b44 100644 --- a/src/views/room/widgets/furniture/trophy/FurnitureTrophyView.tsx +++ b/src/views/room/widgets/furniture/trophy/FurnitureTrophyView.tsx @@ -4,6 +4,7 @@ import { GetRoomEngine } from '../../../../../api'; import { DraggableWindow } from '../../../../../hooks/draggable-window/DraggableWindow'; import { CreateEventDispatcherHook } from '../../../../../hooks/events/event-dispatcher.base'; import { useRoomEngineEvent } from '../../../../../hooks/events/nitro/room/room-engine-event'; +import { LocalizeText } from '../../../../../utils/LocalizeText'; import { RoomWidgetRoomObjectUpdateEvent } from '../../events'; import { FurnitureTrophyData } from './FurnitureTrophyData'; import { FurnitureTrophyViewProps } from './FurnitureTrophyView.types'; @@ -74,7 +75,7 @@ export const FurnitureTrophyView: FC = props =>
processAction('close') }>
- Trophy + { LocalizeText('widget.furni.trophy.title') }