Fix purse / hc

This commit is contained in:
Bill 2022-03-02 21:57:22 -05:00
parent 8982b3b920
commit dcd644dbff
3 changed files with 88 additions and 43 deletions

View File

@ -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 { FC, useCallback, useEffect, useMemo, useState } from 'react';
import { CreateLinkEvent, GetConfiguration, LocalizeText } from '../../api'; import { CreateLinkEvent, GetConfiguration, LocalizeText } from '../../api';
import { CREDITS, DUCKETS, PlaySound } from '../../api/utils/PlaySound';
import { Column, Flex, Grid, Text } from '../../common'; import { Column, Flex, Grid, Text } from '../../common';
import { HcCenterEvent } from '../../events/hc-center/HcCenterEvent'; import { HcCenterEvent } from '../../events/hc-center/HcCenterEvent';
import { UserSettingsUIEvent } from '../../events/user-settings/UserSettingsUIEvent'; import { UserSettingsUIEvent } from '../../events/user-settings/UserSettingsUIEvent';
import { dispatchUiEvent } from '../../hooks'; import { CreateMessageHook, dispatchUiEvent } from '../../hooks';
import { SendMessageHook } from '../../hooks/messages/message-event'; import { SendMessageHook } from '../../hooks/messages/message-event';
import { CurrencyIcon } from '../../views/shared/currency-icon/CurrencyIcon'; import { CurrencyIcon } from '../../views/shared/currency-icon/CurrencyIcon';
import { IPurse } from './common/IPurse'; import { IPurse } from './common/IPurse';
import { Purse } from './common/Purse'; import { Purse } from './common/Purse';
import { PurseContextProvider } from './PurseContext'; import { PurseContextProvider } from './PurseContext';
import { PurseMessageHandler } from './PurseMessageHandler';
import { CurrencyView } from './views/CurrencyView'; import { CurrencyView } from './views/CurrencyView';
import { SeasonalView } from './views/SeasonalView'; import { SeasonalView } from './views/SeasonalView';
@ -19,20 +19,14 @@ export let GLOBAL_PURSE: IPurse = null;
export const PurseView: FC<{}> = props => export const PurseView: FC<{}> = props =>
{ {
const [ purse, setPurse ] = useState<IPurse>(new Purse()); const [ purse, setPurse ] = useState<IPurse>(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<number[]>('system.currency.types', []), []); const displayedCurrencies = useMemo(() => GetConfiguration<number[]>('system.currency.types', []), []);
const currencyDisplayNumberShort = useMemo(() => GetConfiguration<boolean>('currency.display.number.short', false), []); const currencyDisplayNumberShort = useMemo(() => GetConfiguration<boolean>('currency.display.number.short', false), []);
const getClubText = useMemo(() => const getClubText = useMemo(() =>
{ {
if(!purse) return null;
const totalDays = ((purse.clubPeriods * 31) + purse.clubDays); const totalDays = ((purse.clubPeriods * 31) + purse.clubDays);
const minutesUntilExpiration = purse.minutesUntilExpiration; const minutesUntilExpiration = purse.minutesUntilExpiration;
@ -45,7 +39,7 @@ export const PurseView: FC<{}> = props =>
const getCurrencyElements = useCallback((offset: number, limit: number = -1, seasonal: boolean = false) => 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)); const types = Array.from(purse.activityPoints.keys()).filter(type => (displayedCurrencies.indexOf(type) >= 0));
@ -75,24 +69,89 @@ export const PurseView: FC<{}> = props =>
return elements; return elements;
}, [ purse, displayedCurrencies, currencyDisplayNumberShort ]); }, [ 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(() => useEffect(() =>
{ {
if(!purse) return; GLOBAL_PURSE = purse;
SendMessageHook(new UserCurrencyComposer());
}, [ purse ]); }, [ purse ]);
useEffect(() => useEffect(() =>
@ -104,11 +163,15 @@ export const PurseView: FC<{}> = props =>
return () => clearInterval(interval); return () => clearInterval(interval);
}, [ purse ]); }, [ purse ]);
useEffect(() =>
{
SendMessageHook(new UserCurrencyComposer());
}, []);
if(!purse) return null; if(!purse) return null;
return ( return (
<PurseContextProvider value={ { purse } }> <PurseContextProvider value={ { purse } }>
<PurseMessageHandler />
<Column className="nitro-purse-container" gap={ 1 }> <Column className="nitro-purse-container" gap={ 1 }>
<Flex className="nitro-purse rounded-bottom p-1"> <Flex className="nitro-purse rounded-bottom p-1">
<Grid fullWidth gap={ 1 }> <Grid fullWidth gap={ 1 }>
@ -116,15 +179,15 @@ export const PurseView: FC<{}> = props =>
<CurrencyView type={ -1 } amount={ purse.credits } short={ currencyDisplayNumberShort } /> <CurrencyView type={ -1 } amount={ purse.credits } short={ currencyDisplayNumberShort } />
{ getCurrencyElements(0, 2) } { getCurrencyElements(0, 2) }
</Column> </Column>
<Column center pointer size={ 4 } gap={ 1 } className="nitro-purse-subscription rounded" onClick={ handleHcCenterClick }> <Column center pointer size={ 4 } gap={ 1 } className="nitro-purse-subscription rounded" onClick={ event => dispatchUiEvent(new HcCenterEvent(HcCenterEvent.TOGGLE_HC_CENTER)) }>
<CurrencyIcon type="hc" /> <CurrencyIcon type="hc" />
<Text variant="white">{ getClubText }</Text> <Text variant="white">{ getClubText }</Text>
</Column> </Column>
<Column justifyContent="center" size={ 2 } gap={ 0 }> <Column justifyContent="center" size={ 2 } gap={ 0 }>
<Flex center pointer fullHeight className="nitro-purse-button p-1 rounded" onClick={ handleHelpCenterClick }> <Flex center pointer fullHeight className="nitro-purse-button p-1 rounded" onClick={ event => CreateLinkEvent('help/show') }>
<i className="icon icon-help"/> <i className="icon icon-help"/>
</Flex> </Flex>
<Flex center pointer fullHeight className="nitro-purse-button p-1 rounded" onClick={ handleUserSettingsClick } > <Flex center pointer fullHeight className="nitro-purse-button p-1 rounded" onClick={ event => dispatchUiEvent(new UserSettingsUIEvent(UserSettingsUIEvent.TOGGLE_USER_SETTINGS)) } >
<i className="icon icon-cog"/> <i className="icon icon-cog"/>
</Flex> </Flex>
</Column> </Column>

View File

@ -12,6 +12,4 @@ export interface IPurse
minutesUntilExpiration: number; minutesUntilExpiration: number;
minutesSinceLastModified: number; minutesSinceLastModified: number;
clubLevel: number; clubLevel: number;
notifier: () => void
notify(): void;
} }

View File

@ -15,7 +15,6 @@ export class Purse implements IPurse
private _minutesUntilExpiration: number = 0; private _minutesUntilExpiration: number = 0;
private _minutesSinceLastModified: number; private _minutesSinceLastModified: number;
private _lastUpdated: number; private _lastUpdated: number;
private _notifier: () => void;
public get credits(): number public get credits(): number
{ {
@ -137,16 +136,6 @@ export class Purse implements IPurse
return this._lastUpdated; return this._lastUpdated;
} }
public get notifier(): () => void
{
return this._notifier;
}
public set notifier(notifier: () => void)
{
this._notifier = notifier;
}
public get clubLevel(): number public get clubLevel(): number
{ {
if(((this.clubDays === 0) && (this.clubPeriods === 0))) return HabboClubLevelEnum.NO_CLUB; if(((this.clubDays === 0) && (this.clubPeriods === 0))) return HabboClubLevelEnum.NO_CLUB;
@ -155,9 +144,4 @@ export class Purse implements IPurse
return HabboClubLevelEnum.CLUB; return HabboClubLevelEnum.CLUB;
} }
public notify(): void
{
if(this._notifier) this._notifier();
}
} }