import { BadgesEvent, ClubGiftInfoEvent, FigureUpdateEvent, FriendlyTime, GetClubGiftInfo, ILinkEventTracker, RequestBadgesComposer, ScrGetKickbackInfoMessageComposer, ScrKickbackData, ScrSendKickbackInfoMessageEvent, UserInfoEvent, UserSubscriptionEvent } from '@nitrots/nitro-renderer'; import { FC, useCallback, useEffect, useState } from 'react'; import { OverlayTrigger, Popover } from 'react-bootstrap'; import { AddEventLinkTracker, CreateLinkEvent, GetConfiguration, LocalizeText, RemoveLinkEventTracker, SendMessageComposer } from '../../api'; import { LayoutAvatarImageView, LayoutBadgeImageView, NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../common'; import { HcCenterEvent } from '../../events'; import { UseMessageEventHook, UseUiEvent } from '../../hooks'; import { BadgeResolver } from './util/BadgeResolver'; import { ClubStatus } from './util/ClubStatus'; export const HcCenterView: FC<{}> = props => { const [isVisible, setIsVisible] = useState(false); const [userFigure, setUserFigure] = useState(null); const [kickbackData, setKickbackData] = useState(null); const [clubDays, setClubDays] = useState(0); const [pastClubDays, setPastClubDays] = useState(0); const [clubPeriods, setPastClubPeriods] = useState(0); const [minsTillExpire, setMinsTillExpire] = useState(0); const [clubStatus, setClubStatus] = useState(ClubStatus.NONE); const [unclaimedGifts, setUnclaimedGifts] = useState(0); const [badgeCode, setBadgeCode] = useState(BadgeResolver.default_badge); const linkReceived = useCallback((url: string) => { const parts = url.split('/'); if(parts.length < 2) return; switch(parts[1]) { case 'open': if(parts.length > 2) { switch(parts[2]) { case 'hccenter': setIsVisible(true); break; } } return; } }, []); useEffect(() => { const linkTracker: ILinkEventTracker = { linkReceived, eventUrlPrefix: 'habboUI/' }; AddEventLinkTracker(linkTracker); return () => RemoveLinkEventTracker(linkTracker); }, [ linkReceived]); const onUserInfoEvent = useCallback((event: UserInfoEvent) => { const parser = event.getParser(); setUserFigure(parser.userInfo.figure); }, []); const onUserFigureEvent = useCallback((event: FigureUpdateEvent) => { const parser = event.getParser(); setUserFigure(parser.figure); }, []); UseMessageEventHook(UserInfoEvent, onUserInfoEvent); UseMessageEventHook(FigureUpdateEvent, onUserFigureEvent); const onHcCenterEvent = useCallback((event: HcCenterEvent) => { switch(event.type) { case HcCenterEvent.TOGGLE_HC_CENTER: setIsVisible(!isVisible); break; } }, [isVisible]); UseUiEvent(HcCenterEvent.TOGGLE_HC_CENTER, onHcCenterEvent); const onClubGiftInfoEvent = useCallback((event: ClubGiftInfoEvent) => { const parser = event.getParser(); setUnclaimedGifts(parser.giftsAvailable); }, []); const onScrSendKickbackInfo = useCallback((event: ScrSendKickbackInfoMessageEvent) => { const parser = event.getParser(); setKickbackData(parser.data) }, []); const onBadges = useCallback((event: BadgesEvent) => { const parser = event.getParser(); setBadgeCode(BadgeResolver.getClubBadge(parser.getAllBadgeCodes())); }, []) UseMessageEventHook(ClubGiftInfoEvent, onClubGiftInfoEvent); UseMessageEventHook(ScrSendKickbackInfoMessageEvent, onScrSendKickbackInfo); UseMessageEventHook(BadgesEvent, onBadges); useEffect(() => { SendMessageComposer(new GetClubGiftInfo()); SendMessageComposer(new ScrGetKickbackInfoMessageComposer()); SendMessageComposer(new RequestBadgesComposer()); }, []); const onUserSubscriptionEvent = useCallback((event: UserSubscriptionEvent) => { const parser = event.getParser(); const productName = parser.productName; if((productName !== 'club_habbo') && (productName !== 'habbo_club')) return; setClubDays(Math.max(0, parser.daysToPeriodEnd)); setPastClubPeriods(Math.max(0, parser.periodsSubscribedAhead)); setPastClubDays(Math.max(0, parser.pastClubDays)); setMinsTillExpire(Math.max(0, parser.minutesUntilExpiration)); }, []); useEffect(() => { if(clubDays > 0) return setClubStatus(ClubStatus.ACTIVE) if(pastClubDays > 0) return setClubStatus(ClubStatus.EXPIRED); }, [clubDays, pastClubDays]); UseMessageEventHook(UserSubscriptionEvent, onUserSubscriptionEvent); const getClubText = useCallback(() => { const totalDays = ((clubPeriods * 31) + clubDays); const minutesUntilExpiration = minsTillExpire; if(clubStatus !== ClubStatus.ACTIVE) { return LocalizeText('purse.clubdays.zero.amount.text'); } else if((minutesUntilExpiration > -1) && (minutesUntilExpiration < (60 * 24))) { return FriendlyTime.shortFormat(minutesUntilExpiration * 60); } else { return FriendlyTime.shortFormat(totalDays * 86400); } }, [clubStatus, clubDays, clubPeriods, minsTillExpire]); const getInfoText = useCallback(() => { switch(clubStatus) { case ClubStatus.ACTIVE: return LocalizeText('hccenter.status.' + clubStatus + '.info', ['timeleft', 'joindate', 'streakduration'], [getClubText(), kickbackData.firstSubscriptionDate, FriendlyTime.shortFormat(kickbackData.currentHcStreak * 86400)]); case ClubStatus.EXPIRED: return LocalizeText('hccenter.status.' + clubStatus + '.info', ['joindate'], [kickbackData.firstSubscriptionDate]) default: return LocalizeText('hccenter.status.' + clubStatus + '.info'); } }, [clubStatus, kickbackData, getClubText]); const getHcPaydayTime = useCallback(() => { if(kickbackData.timeUntilPayday < 60) return LocalizeText('hccenter.special.time.soon'); return FriendlyTime.shortFormat(kickbackData.timeUntilPayday * 60); }, [kickbackData]); const getHcPaydayAmount = useCallback(() => { return LocalizeText('hccenter.special.sum', ['credits'], [(kickbackData.creditRewardForStreakBonus + kickbackData.creditRewardForMonthlySpent).toString()]) }, [kickbackData]) if(!isVisible) return null; const popover = (
{LocalizeText('hccenter.breakdown.title')}
{LocalizeText('hccenter.breakdown.creditsspent', ['credits'], [kickbackData.totalCreditsSpent.toString()])}
{LocalizeText('hccenter.breakdown.paydayfactor.percent', ['percent'], [(kickbackData.kickbackPercentage * 100).toString()])}
{LocalizeText('hccenter.breakdown.streakbonus', ['credits'], [kickbackData.creditRewardForStreakBonus.toString()])}

{LocalizeText('hccenter.breakdown.total', ['credits', 'actual'], [getHcPaydayAmount(), ((((kickbackData.kickbackPercentage * kickbackData.totalCreditsSpent) + kickbackData.creditRewardForStreakBonus) * 100) / 100).toString()])}
{ CreateLinkEvent('habbopages/' + GetConfiguration('hc.center')['payday.habbopage']) }}>{ LocalizeText('hccenter.special.infolink')}
); return ( setIsVisible(false)} />
{LocalizeText('hccenter.status.' + clubStatus)}
{GetConfiguration('hc.center')['payday.info'] &&

{LocalizeText('hccenter.special.title')}

{LocalizeText('hccenter.special.info')}
{ CreateLinkEvent('habbopages/' + GetConfiguration('hc.center')['payday.habbopage']) }}>{LocalizeText('hccenter.special.infolink')}
{LocalizeText('hccenter.special.time.title')}
{getHcPaydayTime()}
{clubStatus === ClubStatus.ACTIVE &&
{LocalizeText('hccenter.special.amount.title')}
{getHcPaydayAmount()}
{LocalizeText('hccenter.breakdown.infolink')}
}
} {GetConfiguration('hc.center')['gift.info'] &&

{LocalizeText('hccenter.gift.title')}

0 ? LocalizeText('hccenter.unclaimedgifts', ['unclaimedgifts'], [unclaimedGifts.toString()]) : LocalizeText('hccenter.gift.info') }}>
} {GetConfiguration('hc.center')['benefits.info'] &&
{LocalizeText('hccenter.general.title')}
} ); }