diff --git a/src/components/purse/PurseView.tsx b/src/components/purse/PurseView.tsx index 6c7cc299..832fc41f 100644 --- a/src/components/purse/PurseView.tsx +++ b/src/components/purse/PurseView.tsx @@ -1,16 +1,16 @@ -import { FriendlyTime, HabboClubLevelEnum, UserCurrencyComposer, UserSubscriptionComposer } from '@nitrots/nitro-renderer'; +import { ActivityPointNotificationMessageEvent, FriendlyTime, HabboClubLevelEnum, UserCreditsEvent, UserCurrencyComposer, UserCurrencyEvent, UserSubscriptionComposer, UserSubscriptionEvent, UserSubscriptionParser } from '@nitrots/nitro-renderer'; import { FC, useCallback, useEffect, useMemo, useState } from 'react'; import { CreateLinkEvent, GetConfiguration, LocalizeText } from '../../api'; +import { CREDITS, DUCKETS, PlaySound } from '../../api/utils/PlaySound'; import { Column, Flex, Grid, Text } from '../../common'; import { HcCenterEvent } from '../../events/hc-center/HcCenterEvent'; import { UserSettingsUIEvent } from '../../events/user-settings/UserSettingsUIEvent'; -import { dispatchUiEvent } from '../../hooks'; +import { CreateMessageHook, dispatchUiEvent } from '../../hooks'; import { SendMessageHook } from '../../hooks/messages/message-event'; import { CurrencyIcon } from '../../views/shared/currency-icon/CurrencyIcon'; import { IPurse } from './common/IPurse'; import { Purse } from './common/Purse'; import { PurseContextProvider } from './PurseContext'; -import { PurseMessageHandler } from './PurseMessageHandler'; import { CurrencyView } from './views/CurrencyView'; import { SeasonalView } from './views/SeasonalView'; @@ -19,20 +19,14 @@ export let GLOBAL_PURSE: IPurse = null; export const PurseView: FC<{}> = props => { const [ purse, setPurse ] = useState(new Purse()); - const [ updateId, setUpdateId ] = useState(-1); - - const handleUserSettingsClick = () => dispatchUiEvent(new UserSettingsUIEvent(UserSettingsUIEvent.TOGGLE_USER_SETTINGS)); - - const handleHelpCenterClick = () => CreateLinkEvent('help/show'); - - const handleHcCenterClick = () => dispatchUiEvent(new HcCenterEvent(HcCenterEvent.TOGGLE_HC_CENTER)); const displayedCurrencies = useMemo(() => GetConfiguration('system.currency.types', []), []); - const currencyDisplayNumberShort = useMemo(() => GetConfiguration('currency.display.number.short', false), []); const getClubText = useMemo(() => { + if(!purse) return null; + const totalDays = ((purse.clubPeriods * 31) + purse.clubDays); const minutesUntilExpiration = purse.minutesUntilExpiration; @@ -45,7 +39,7 @@ export const PurseView: FC<{}> = props => const getCurrencyElements = useCallback((offset: number, limit: number = -1, seasonal: boolean = false) => { - if(!purse.activityPoints.size) return null; + if(!purse || !purse.activityPoints || !purse.activityPoints.size) return null; const types = Array.from(purse.activityPoints.keys()).filter(type => (displayedCurrencies.indexOf(type) >= 0)); @@ -75,24 +69,89 @@ export const PurseView: FC<{}> = props => return elements; }, [ purse, displayedCurrencies, currencyDisplayNumberShort ]); - useEffect(() => + const onUserCreditsEvent = useCallback((event: UserCreditsEvent) => { - const purse = new Purse(); + const parser = event.getParser(); - GLOBAL_PURSE = purse; + setPurse(prevValue => + { + const newValue = { ...prevValue }; - purse.notifier = () => setUpdateId(prevValue => (prevValue + 1)); + newValue.credits = parseFloat(parser.credits); - setPurse(purse); + if(prevValue.credits !== newValue.credits) PlaySound(CREDITS); - return () => (purse.notifier = null); + return newValue; + }); }, []); + CreateMessageHook(UserCreditsEvent, onUserCreditsEvent); + + const onUserCurrencyEvent = useCallback((event: UserCurrencyEvent) => + { + const parser = event.getParser(); + + setPurse(prevValue => + { + const newValue = { ...prevValue }; + + newValue.activityPoints = parser.currencies; + + return newValue; + }); + }, []); + + CreateMessageHook(UserCurrencyEvent, onUserCurrencyEvent); + + const onActivityPointNotificationMessageEvent = useCallback((event: ActivityPointNotificationMessageEvent) => + { + const parser = event.getParser(); + + setPurse(prevValue => + { + const newValue = { ...prevValue }; + + newValue.activityPoints = new Map(newValue.activityPoints); + + newValue.activityPoints.set(parser.type, parser.amount); + + if(parser.type === 0) PlaySound(DUCKETS) + + return newValue; + }); + }, []); + + CreateMessageHook(ActivityPointNotificationMessageEvent, onActivityPointNotificationMessageEvent); + + const onUserSubscriptionEvent = useCallback((event: UserSubscriptionEvent) => + { + const parser = event.getParser(); + const productName = parser.productName; + + if((productName !== 'club_habbo') && (productName !== 'habbo_club')) return; + + setPurse(prevValue => + { + const newValue = { ...prevValue }; + + newValue.clubDays = Math.max(0, parser.daysToPeriodEnd); + newValue.clubPeriods = Math.max(0, parser.periodsSubscribedAhead); + newValue.isVip = parser.isVip; + newValue.pastClubDays = parser.pastClubDays; + newValue.pastVipDays = parser.pastVipDays; + newValue.isExpiring = ((parser.responseType === UserSubscriptionParser.RESPONSE_TYPE_DISCOUNT_AVAILABLE) ? true : false); + newValue.minutesUntilExpiration = parser.minutesUntilExpiration; + newValue.minutesSinceLastModified = parser.minutesSinceLastModified; + + return newValue; + }); + }, []); + + CreateMessageHook(UserSubscriptionEvent, onUserSubscriptionEvent); + useEffect(() => { - if(!purse) return; - - SendMessageHook(new UserCurrencyComposer()); + GLOBAL_PURSE = purse; }, [ purse ]); useEffect(() => @@ -104,11 +163,15 @@ export const PurseView: FC<{}> = props => return () => clearInterval(interval); }, [ purse ]); + useEffect(() => + { + SendMessageHook(new UserCurrencyComposer()); + }, []); + if(!purse) return null; return ( - @@ -116,15 +179,15 @@ export const PurseView: FC<{}> = props => { getCurrencyElements(0, 2) } - + dispatchUiEvent(new HcCenterEvent(HcCenterEvent.TOGGLE_HC_CENTER)) }> { getClubText } - + CreateLinkEvent('help/show') }> - + dispatchUiEvent(new UserSettingsUIEvent(UserSettingsUIEvent.TOGGLE_USER_SETTINGS)) } > diff --git a/src/components/purse/common/IPurse.ts b/src/components/purse/common/IPurse.ts index 2a709938..b08ded9b 100644 --- a/src/components/purse/common/IPurse.ts +++ b/src/components/purse/common/IPurse.ts @@ -12,6 +12,4 @@ export interface IPurse minutesUntilExpiration: number; minutesSinceLastModified: number; clubLevel: number; - notifier: () => void - notify(): void; } diff --git a/src/components/purse/common/Purse.ts b/src/components/purse/common/Purse.ts index 5e70358b..9f0e66a6 100644 --- a/src/components/purse/common/Purse.ts +++ b/src/components/purse/common/Purse.ts @@ -15,7 +15,6 @@ export class Purse implements IPurse private _minutesUntilExpiration: number = 0; private _minutesSinceLastModified: number; private _lastUpdated: number; - private _notifier: () => void; public get credits(): number { @@ -137,16 +136,6 @@ export class Purse implements IPurse return this._lastUpdated; } - public get notifier(): () => void - { - return this._notifier; - } - - public set notifier(notifier: () => void) - { - this._notifier = notifier; - } - public get clubLevel(): number { if(((this.clubDays === 0) && (this.clubPeriods === 0))) return HabboClubLevelEnum.NO_CLUB; @@ -155,9 +144,4 @@ export class Purse implements IPurse return HabboClubLevelEnum.CLUB; } - - public notify(): void - { - if(this._notifier) this._notifier(); - } }