From 5a3d3e53548f64dc374f2df8f5c2593aaf35e8f2 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 23 Jul 2021 00:27:55 -0400 Subject: [PATCH] Badge updates --- .../inventory/InventoryBadgesRequestEvent.ts | 6 ++++ .../inventory/InventoryBadgesUpdatedEvent.ts | 20 +++++++++++ src/events/inventory/index.ts | 1 + .../inventory/InventoryMessageHandler.tsx | 36 +++++++++++++++++-- src/views/inventory/InventoryView.tsx | 11 ++++-- .../reducers/InventoryBadgeReducer.tsx | 19 +++++++--- .../views/badge/InventoryBadgeView.tsx | 1 + 7 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 src/events/inventory/InventoryBadgesRequestEvent.ts create mode 100644 src/events/inventory/InventoryBadgesUpdatedEvent.ts diff --git a/src/events/inventory/InventoryBadgesRequestEvent.ts b/src/events/inventory/InventoryBadgesRequestEvent.ts new file mode 100644 index 00000000..18683834 --- /dev/null +++ b/src/events/inventory/InventoryBadgesRequestEvent.ts @@ -0,0 +1,6 @@ +import { NitroEvent } from 'nitro-renderer'; + +export class InventoryBadgesRequestEvent extends NitroEvent +{ + public static REQUEST_BADGES: string = 'IBRE_REQUEST_BADGES'; +} diff --git a/src/events/inventory/InventoryBadgesUpdatedEvent.ts b/src/events/inventory/InventoryBadgesUpdatedEvent.ts new file mode 100644 index 00000000..0b5d2220 --- /dev/null +++ b/src/events/inventory/InventoryBadgesUpdatedEvent.ts @@ -0,0 +1,20 @@ +import { NitroEvent } from 'nitro-renderer'; + +export class InventoryBadgesUpdatedEvent extends NitroEvent +{ + public static BADGES_UPDATED: string = 'IBUE_BADGES_UPDATED'; + + private _badges: string[] = []; + + constructor(type: string, badges: string[] = []) + { + super(type); + + this._badges = badges; + } + + public get badges(): string[] + { + return this._badges; + } +} diff --git a/src/events/inventory/index.ts b/src/events/inventory/index.ts index 2b21bce9..90a8cd46 100644 --- a/src/events/inventory/index.ts +++ b/src/events/inventory/index.ts @@ -1,3 +1,4 @@ +export * from './InventoryBadgesUpdatedEvent'; export * from './InventoryEvent'; export * from './InventoryTradeRequestEvent'; export * from './InventoryTradeStartEvent'; diff --git a/src/views/inventory/InventoryMessageHandler.tsx b/src/views/inventory/InventoryMessageHandler.tsx index febf3af9..0a00a79b 100644 --- a/src/views/inventory/InventoryMessageHandler.tsx +++ b/src/views/inventory/InventoryMessageHandler.tsx @@ -1,7 +1,10 @@ -import { AdvancedMap, BadgesEvent, BotAddedToInventoryEvent, BotInventoryMessageEvent, BotRemovedFromInventoryEvent, FurnitureListAddOrUpdateEvent, FurnitureListEvent, FurnitureListInvalidateEvent, FurnitureListItemParser, FurnitureListRemovedEvent, FurniturePostItPlacedEvent, PetAddedToInventoryEvent, PetData, PetInventoryEvent, PetRemovedFromInventory, TradingAcceptEvent, TradingCloseEvent, TradingCompletedEvent, TradingConfirmationEvent, TradingListItemEvent, TradingNotOpenEvent, TradingOpenEvent, TradingOpenFailedEvent, TradingOtherNotAllowedEvent, TradingYouAreNotAllowedEvent, UnseenItemsEvent } from 'nitro-renderer'; +import { AdvancedMap, BadgeReceivedEvent, BadgesEvent, BotAddedToInventoryEvent, BotInventoryMessageEvent, BotRemovedFromInventoryEvent, FurnitureListAddOrUpdateEvent, FurnitureListEvent, FurnitureListInvalidateEvent, FurnitureListItemParser, FurnitureListRemovedEvent, FurniturePostItPlacedEvent, PetAddedToInventoryEvent, PetData, PetInventoryEvent, PetRemovedFromInventory, RequestBadgesComposer, TradingAcceptEvent, TradingCloseEvent, TradingCompletedEvent, TradingConfirmationEvent, TradingListItemEvent, TradingNotOpenEvent, TradingOpenEvent, TradingOpenFailedEvent, TradingOtherNotAllowedEvent, TradingYouAreNotAllowedEvent, UnseenItemsEvent } from 'nitro-renderer'; import { FC, useCallback } from 'react'; import { GetRoomSession, GetSessionDataManager } from '../../api'; -import { CreateMessageHook } from '../../hooks/messages/message-event'; +import { InventoryBadgesUpdatedEvent } from '../../events'; +import { InventoryBadgesRequestEvent } from '../../events/inventory/InventoryBadgesRequestEvent'; +import { dispatchUiEvent, useUiEvent } from '../../hooks'; +import { CreateMessageHook, SendMessageHook } from '../../hooks/messages/message-event'; import { mergeFurniFragments } from './common/FurnitureUtilities'; import { mergePetFragments } from './common/PetUtilities'; import { TradeState } from './common/TradeState'; @@ -17,7 +20,7 @@ let petMsgFragments: Map[] = null; export const InventoryMessageHandler: FC = props => { - const { dispatchFurnitureState = null, dispatchBotState = null, dispatchPetState = null, dispatchBadgeState = null, unseenTracker = null } = useInventoryContext(); + const { dispatchFurnitureState = null, dispatchBotState = null, dispatchPetState = null, badgeState = null, dispatchBadgeState = null, unseenTracker = null } = useInventoryContext(); const onFurnitureListAddOrUpdateEvent = useCallback((event: FurnitureListAddOrUpdateEvent) => { @@ -163,6 +166,18 @@ export const InventoryMessageHandler: FC = props = }); }, [ dispatchBadgeState ]); + const onBadgeReceivedEvent = useCallback((event: BadgeReceivedEvent) => + { + const parser = event.getParser(); + + dispatchBadgeState({ + type: InventoryBadgeActions.ADD_BADGE, + payload: { + badgeCode: parser.badgeCode + } + }); + }, [ dispatchBadgeState ]); + const onTradingAcceptEvent = useCallback((event: TradingAcceptEvent) => { const parser = event.getParser(); @@ -318,6 +333,7 @@ export const InventoryMessageHandler: FC = props = CreateMessageHook(PetRemovedFromInventory, onPetRemovedFromInventory); CreateMessageHook(PetAddedToInventoryEvent, onPetAddedToInventoryEvent); CreateMessageHook(BadgesEvent, onBadgesEvent); + CreateMessageHook(BadgeReceivedEvent, onBadgeReceivedEvent); CreateMessageHook(TradingAcceptEvent, onTradingAcceptEvent); CreateMessageHook(TradingCloseEvent, onTradingCloseEvent); CreateMessageHook(TradingCompletedEvent, onTradingCompletedEvent); @@ -330,5 +346,19 @@ export const InventoryMessageHandler: FC = props = CreateMessageHook(TradingYouAreNotAllowedEvent, onTradingYouAreNotAllowedEvent); CreateMessageHook(UnseenItemsEvent, onUnseenItemsEvent); + const onInventoryBadgesRequestEvent = useCallback((event: InventoryBadgesRequestEvent) => + { + if(badgeState.needsBadgeUpdate) + { + SendMessageHook(new RequestBadgesComposer()); + + return; + } + + dispatchUiEvent(new InventoryBadgesUpdatedEvent(InventoryBadgesUpdatedEvent.BADGES_UPDATED, badgeState.badges)); + }, [ badgeState ]) + + useUiEvent(InventoryBadgesRequestEvent.REQUEST_BADGES, onInventoryBadgesRequestEvent); + return null; } diff --git a/src/views/inventory/InventoryView.tsx b/src/views/inventory/InventoryView.tsx index 3a0a3f63..3d8b2601 100644 --- a/src/views/inventory/InventoryView.tsx +++ b/src/views/inventory/InventoryView.tsx @@ -1,10 +1,10 @@ import { IRoomSession, RoomEngineObjectEvent, RoomEngineObjectPlacedEvent, RoomPreviewer, RoomSessionEvent, TradingCancelComposer, TradingCloseComposer, TradingOpenComposer } from 'nitro-renderer'; import { FC, useCallback, useEffect, useReducer, useState } from 'react'; import { GetRoomEngine } from '../../api'; -import { InventoryEvent, InventoryTradeRequestEvent } from '../../events'; +import { InventoryBadgesUpdatedEvent, InventoryEvent, InventoryTradeRequestEvent } from '../../events'; import { useRoomEngineEvent } from '../../hooks/events/nitro/room/room-engine-event'; import { useRoomSessionManagerEvent } from '../../hooks/events/nitro/session/room-session-manager-event'; -import { useUiEvent } from '../../hooks/events/ui/ui-event'; +import { dispatchUiEvent, useUiEvent } from '../../hooks/events/ui/ui-event'; import { SendMessageHook } from '../../hooks/messages'; import { NitroCardContentView, NitroCardHeaderView, NitroCardTabsItemView, NitroCardTabsView, NitroCardView } from '../../layout'; import { LocalizeText } from '../../utils/LocalizeText'; @@ -186,6 +186,13 @@ export const InventoryView: FC = props => } }, [ furnitureState.tradeData, isVisible ]); + useEffect(() => + { + if(!badgeState.badges) return; + + dispatchUiEvent(new InventoryBadgesUpdatedEvent(InventoryBadgesUpdatedEvent.BADGES_UPDATED, badgeState.badges)); + }, [ badgeState.badges ]); + return ( diff --git a/src/views/inventory/reducers/InventoryBadgeReducer.tsx b/src/views/inventory/reducers/InventoryBadgeReducer.tsx index 912d9375..62b2c59d 100644 --- a/src/views/inventory/reducers/InventoryBadgeReducer.tsx +++ b/src/views/inventory/reducers/InventoryBadgeReducer.tsx @@ -23,11 +23,12 @@ export interface IInventoryBadgeAction export class InventoryBadgeActions { - public static SET_NEEDS_UPDATE: string = 'IBDA_SET_NEEDS_UPDATE'; - public static SET_BADGE: string = 'IBDA_SET_BADGE'; - public static SET_BADGES: string = 'IBDA_SET_BADGES'; - public static ADD_ACTIVE_BADGE: string = 'IBDA_ADD_ACTIVE_BADGE'; - public static REMOVE_ACTIVE_BADGE: string = 'IBDA_REMOVE_ACTIVE_BADGE'; + public static SET_NEEDS_UPDATE: string = 'IBA_SET_NEEDS_UPDATE'; + public static SET_BADGE: string = 'IBA_SET_BADGE'; + public static SET_BADGES: string = 'IBA_SET_BADGES'; + public static ADD_BADGE: string = 'IBA_ADD_BADGE'; + public static ADD_ACTIVE_BADGE: string = 'IBA_ADD_ACTIVE_BADGE'; + public static REMOVE_ACTIVE_BADGE: string = 'IBA_REMOVE_ACTIVE_BADGE'; } export const initialInventoryBadge: IInventoryBadgeState = { @@ -77,6 +78,14 @@ export const InventoryBadgeReducer: Reducer = props => { if(needsBadgeUpdate) { + console.log('yee') dispatchBadgeState({ type: InventoryBadgeActions.SET_NEEDS_UPDATE, payload: {