using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Models.Common; using SPTarkov.Server.Core.Models.Eft.Profile; using SPTarkov.Server.Core.Models.Eft.Ws; using SPTarkov.Server.Core.Models.Enums; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Servers; using SPTarkov.Server.Core.Servers.Ws; using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; namespace SPTarkov.Server.Core.Helpers; [Injectable] public class NotificationSendHelper( ISptLogger _logger, SptWebSocketConnectionHandler _sptWebSocketConnectionHandler, HashUtil _hashUtil, SaveServer _saveServer, NotificationService _notificationService, TimeUtil _timeUtil, JsonUtil _jsonUtil ) { /// /// Send notification message to the appropriate channel /// /// Session/player id /// public void SendMessage(string sessionID, WsNotificationEvent notificationMessage) { if (_logger.IsLogEnabled(LogLevel.Debug)) { _logger.Debug( $"Send message for {sessionID} started, message: {_jsonUtil.Serialize(notificationMessage)}" ); } if (_sptWebSocketConnectionHandler.IsWebSocketConnected(sessionID)) { if (_logger.IsLogEnabled(LogLevel.Debug)) { _logger.Debug( $"Send message for {sessionID} websocket available, message being sent" ); } _sptWebSocketConnectionHandler.SendMessage(sessionID, notificationMessage); } else { if (_logger.IsLogEnabled(LogLevel.Debug)) { _logger.Debug( $"Send message for {sessionID} websocket not available, queuing into profile" ); } _notificationService.Add(sessionID, notificationMessage); } } /// /// Send a message directly to the player /// /// Session id /// Who is sending the message to player /// Text to send player /// Underlying type of message being sent public void SendMessageToPlayer( string sessionId, UserDialogInfo senderDetails, string messageText, MessageType messageType ) { var dialog = GetDialog(sessionId, messageType, senderDetails); dialog.New += 1; var message = new Message { Id = new MongoId(), UserId = dialog.Id, MessageType = messageType, DateTime = _timeUtil.GetTimeStamp(), Text = messageText, HasRewards = null, RewardCollected = null, Items = null, }; dialog.Messages.Add(message); var notification = new WsChatMessageReceived { EventType = NotificationEventType.new_message, EventIdentifier = message.Id, DialogId = message.UserId, Message = message, }; SendMessage(sessionId, notification); } /// /// Helper function for SendMessageToPlayer(), get new dialog for storage in profile or find existing by sender id /// /// Session id /// Type of message to generate /// Who is sending the message /// Dialogue protected Models.Eft.Profile.Dialogue GetDialog( string sessionId, MessageType messageType, UserDialogInfo senderDetails ) { // Use trader id if sender is trader, otherwise use nickname var dialogKey = senderDetails.Id; // Get all dialogs with pmcs/traders player has var dialogueData = _saveServer.GetProfile(sessionId).DialogueRecords; // Ensure empty dialog exists based on sender details passed in dialogueData.TryAdd( dialogKey, GetEmptyDialogTemplate(dialogKey, messageType, senderDetails) ); return dialogueData[dialogKey]; } protected Models.Eft.Profile.Dialogue GetEmptyDialogTemplate( string dialogKey, MessageType messageType, UserDialogInfo senderDetails ) { return new Models.Eft.Profile.Dialogue { Id = dialogKey, Type = messageType, Messages = [], Pinned = false, New = 0, AttachmentsNew = 0, Users = senderDetails.Info.MemberCategory == MemberCategory.Trader ? null : [senderDetails], }; } }