Server/project/src/helpers/NotificationSendHelper.ts
Refringe 4ac12ef70a Formatting/Linting Changes (!168)
These are the formatting & linting configuration changes from the `3.8.0` branch and the changes that they make to the overall project.

The majority of these changes are from running two commands:

`npm run lint:fix`
`npm run style:fix`

This has already been run on the `3.8.0` branch and this PR should make `master` play nicer when it comes to merges going forward.

There are now four VSCode plugins recommended for server development. They've been added to the workspace file and a user should get a UI notification when the workspace is opened if they're not installed.

The four plugins are:
https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
https://marketplace.visualstudio.com/items?itemName=dprint.dprint
https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
https://marketplace.visualstudio.com/items?itemName=biomejs.biome

Once installed they should just work within the workspace.

Also, be sure to `npm i` to get the new dprint application.

Co-authored-by: Refringe <brownelltyler@gmail.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/168
2023-11-16 21:42:06 +00:00

113 lines
3.9 KiB
TypeScript

import { inject, injectable } from "tsyringe";
import { INotification, NotificationType } from "@spt-aki/models/eft/notifier/INotifier";
import { Dialogue, IUserDialogInfo, Message } from "@spt-aki/models/eft/profile/IAkiProfile";
import { MemberCategory } from "@spt-aki/models/enums/MemberCategory";
import { MessageType } from "@spt-aki/models/enums/MessageType";
import { SaveServer } from "@spt-aki/servers/SaveServer";
import { WebSocketServer } from "@spt-aki/servers/WebSocketServer";
import { NotificationService } from "@spt-aki/services/NotificationService";
import { HashUtil } from "@spt-aki/utils/HashUtil";
@injectable()
export class NotificationSendHelper
{
constructor(
@inject("WebSocketServer") protected webSocketServer: WebSocketServer,
@inject("HashUtil") protected hashUtil: HashUtil,
@inject("SaveServer") protected saveServer: SaveServer,
@inject("NotificationService") protected notificationService: NotificationService,
)
{}
/**
* Send notification message to the appropriate channel
* @param sessionID
* @param notificationMessage
*/
public sendMessage(sessionID: string, notificationMessage: INotification): void
{
if (this.webSocketServer.isConnectionWebSocket(sessionID))
{
this.webSocketServer.sendMessage(sessionID, notificationMessage);
}
else
{
this.notificationService.add(sessionID, notificationMessage);
}
}
/**
* Send a message directly to the player
* @param sessionId Session id
* @param senderDetails Who is sendin the message to player
* @param messageText Text to send player
* @param messageType Underlying type of message being sent
*/
public sendMessageToPlayer(
sessionId: string,
senderDetails: IUserDialogInfo,
messageText: string,
messageType: MessageType,
): void
{
const dialog = this.getDialog(sessionId, messageType, senderDetails);
dialog.new += 1;
const message: Message = {
_id: this.hashUtil.generate(),
uid: dialog._id,
type: messageType,
dt: Math.round(Date.now() / 1000),
text: messageText,
hasRewards: undefined,
rewardCollected: undefined,
items: undefined,
};
dialog.messages.push(message);
const notification: INotification = {
type: NotificationType.NEW_MESSAGE,
eventId: message._id,
dialogId: message.uid,
message: message,
};
this.sendMessage(sessionId, notification);
}
/**
* Helper function for sendMessageToPlayer(), get new dialog for storage in profile or find existing by sender id
* @param sessionId Session id
* @param messageType Type of message to generate
* @param senderDetails Who is sending the message
* @returns Dialogue
*/
protected getDialog(sessionId: string, messageType: MessageType, senderDetails: IUserDialogInfo): Dialogue
{
// Use trader id if sender is trader, otherwise use nickname
const key = (senderDetails.info.MemberCategory === MemberCategory.TRADER)
? senderDetails._id
: senderDetails.info.Nickname;
const dialogueData = this.saveServer.getProfile(sessionId).dialogues;
const isNewDialogue = !(key in dialogueData);
let dialogue: Dialogue = dialogueData[key];
// Existing dialog not found, make new one
if (isNewDialogue)
{
dialogue = {
_id: key,
type: messageType,
messages: [],
pinned: false,
new: 0,
attachmentsNew: 0,
Users: (senderDetails.info.MemberCategory === MemberCategory.TRADER) ? undefined : [senderDetails],
};
dialogueData[key] = dialogue;
}
return dialogue;
}
}