nitro-react/src/views/notification-center/NotificationCenterMessageHandler.tsx

141 lines
6.1 KiB
TypeScript
Raw Normal View History

2021-08-26 08:38:16 +02:00
import { AchievementNotificationMessageEvent, ActivityPointNotificationMessageEvent, ClubGiftNotificationEvent, HabboBroadcastMessageEvent, HotelClosesAndWillOpenAtEvent, HotelWillShutdownEvent, ModeratorMessageEvent, MOTDNotificationEvent, NotificationDialogMessageEvent, PetAddedToInventoryEvent, RespectReceivedEvent, Vector3d } from '@nitrots/nitro-renderer';
2021-06-22 18:40:28 +02:00
import { FC, useCallback } from 'react';
2021-08-26 08:38:16 +02:00
import { GetRoomEngine, GetSessionDataManager, LocalizeBadgeName, LocalizeText } from '../../api';
2021-07-08 07:46:59 +02:00
import { NotificationCenterAlertEvent } from '../../events';
import { dispatchUiEvent } from '../../hooks/events';
2021-06-22 18:40:28 +02:00
import { CreateMessageHook } from '../../hooks/messages';
2021-08-05 19:32:09 +02:00
import { HotelWillShutdownNotification } from './common/HotelWillShutdownNotification';
2021-08-22 07:00:05 +02:00
import { NotificationType } from './common/NotificationType';
import { NotificationUtilities } from './common/NotificationUtilities';
2021-06-22 18:40:28 +02:00
import { useNotificationCenterContext } from './context/NotificationCenterContext';
import { INotificationCenterMessageHandlerProps } from './NotificationCenterMessageHandler.types';
import { NotificationCenterActions } from './reducers/NotificationCenterReducer';
export const NotificationCenterMessageHandler: FC<INotificationCenterMessageHandlerProps> = props =>
{
const { dispatchNotificationCenterState = null } = useNotificationCenterContext();
2021-08-26 08:38:16 +02:00
const onRespectReceivedEvent = useCallback((event: RespectReceivedEvent) =>
{
const parser = event.getParser();
if(parser.userId !== GetSessionDataManager().userId) return;
const text1 = LocalizeText('notifications.text.respect.1');
const text2 = LocalizeText('notifications.text.respect.2', [ 'count' ], [ parser.respectsReceived.toString() ]);
NotificationUtilities.showSingleBubble(text1, NotificationType.RESPECT);
NotificationUtilities.showSingleBubble(text2, NotificationType.RESPECT);
}, []);
CreateMessageHook(RespectReceivedEvent, onRespectReceivedEvent);
2021-06-22 18:40:28 +02:00
const onHabboBroadcastMessageEvent = useCallback((event: HabboBroadcastMessageEvent) =>
{
const parser = event.getParser();
2021-08-26 08:38:16 +02:00
NotificationUtilities.simpleAlert(parser.message.replace(/\\r/g, '\r'));
2021-07-08 07:46:59 +02:00
}, []);
2021-06-22 18:40:28 +02:00
2021-08-26 08:38:16 +02:00
CreateMessageHook(HabboBroadcastMessageEvent, onHabboBroadcastMessageEvent);
const onAchievementNotificationMessageEvent = useCallback((event: AchievementNotificationMessageEvent) =>
{
const parser = event.getParser();
const text1 = LocalizeText('achievements.levelup.desc');
const badgeName = LocalizeBadgeName(parser.data.badgeCode);
const badgeImage = GetSessionDataManager().getBadgeUrl(parser.data.badgeCode);
const internalLink = 'questengine/achievements/' + parser.data.category;
NotificationUtilities.showSingleBubble((text1 + ' ' + badgeName), NotificationType.ACHIEVEMENT, badgeImage, internalLink);
}, []);
CreateMessageHook(AchievementNotificationMessageEvent, onAchievementNotificationMessageEvent);
const onClubGiftNotificationEvent = useCallback((event: ClubGiftNotificationEvent) =>
{
const parser = event.getParser();
NotificationUtilities.showClubGiftNotification(parser.numGifts);
}, []);
CreateMessageHook(ClubGiftNotificationEvent, onClubGiftNotificationEvent);
2021-07-08 07:46:59 +02:00
const onModeratorMessageEvent = useCallback((event: ModeratorMessageEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
2021-07-08 07:46:59 +02:00
dispatchUiEvent(new NotificationCenterAlertEvent(NotificationCenterAlertEvent.HOTEL_ALERT, [ parser.message ], parser.link));
}, []);
2021-06-22 18:40:28 +02:00
2021-08-26 08:38:16 +02:00
CreateMessageHook(ModeratorMessageEvent, onModeratorMessageEvent);
const onActivityPointNotificationMessageEvent = useCallback((event: ActivityPointNotificationMessageEvent) =>
{
const parser = event.getParser();
// bubble for loyalty
}, []);
CreateMessageHook(ActivityPointNotificationMessageEvent, onActivityPointNotificationMessageEvent);
const onHotelClosesAndWillOpenAtEvent = useCallback((event: HotelClosesAndWillOpenAtEvent) =>
{
const parser = event.getParser();
NotificationUtilities.handleHotelClosedMessage(parser.openHour, parser.openMinute, parser.userThrowOutAtClose);
}, []);
CreateMessageHook(HotelClosesAndWillOpenAtEvent, onHotelClosesAndWillOpenAtEvent);
const onPetAddedToInventoryEvent = useCallback((event: PetAddedToInventoryEvent) =>
{
const parser = event.getParser();
const text = LocalizeText('notifications.text.' + (parser.boughtAsGift ? 'petbought' : 'petreceived'));
let imageUrl: string = null;
const imageResult = GetRoomEngine().getRoomObjectPetImage(parser.pet.typeId, parser.pet.paletteId, parseInt(parser.pet.color, 16), new Vector3d(45 * 3), 64, null, true);
if(imageResult) imageUrl = imageResult.getImage().src;
NotificationUtilities.showSingleBubble(text, NotificationType.PETLEVEL, imageUrl);
}, []);
CreateMessageHook(PetAddedToInventoryEvent, onPetAddedToInventoryEvent);
2021-07-08 07:46:59 +02:00
const onMOTDNotificationEvent = useCallback((event: MOTDNotificationEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
2021-07-08 07:46:59 +02:00
dispatchUiEvent(new NotificationCenterAlertEvent(NotificationCenterAlertEvent.HOTEL_ALERT, parser.messages));
}, []);
2021-06-22 18:40:28 +02:00
2021-07-08 07:46:59 +02:00
const onHotelWillShutdownEvent = useCallback((event: HotelWillShutdownEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
dispatchNotificationCenterState({
type: NotificationCenterActions.ADD_NOTIFICATION,
payload: {
2021-07-08 07:46:59 +02:00
notification: new HotelWillShutdownNotification(parser.minutes)
2021-06-22 18:40:28 +02:00
}
});
}, [ dispatchNotificationCenterState ]);
2021-07-08 07:46:59 +02:00
const onNotificationDialogMessageEvent = useCallback((event: NotificationDialogMessageEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
2021-08-22 07:00:05 +02:00
NotificationUtilities.showNotification(parser.type, parser.parameters);
}, []);
2021-08-05 19:32:09 +02:00
2021-06-22 18:40:28 +02:00
CreateMessageHook(MOTDNotificationEvent, onMOTDNotificationEvent);
CreateMessageHook(HotelWillShutdownEvent, onHotelWillShutdownEvent);
2021-07-08 07:46:59 +02:00
CreateMessageHook(NotificationDialogMessageEvent, onNotificationDialogMessageEvent);
2021-06-22 18:40:28 +02:00
return null;
}