performance notifcation addition, with some regular bugfixes regarding the flexing of the notifications
This commit is contained in:
@@ -10,9 +10,11 @@ using LightlessSync.UI.Models;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using FFXIVClientStructs.FFXIV.Client.UI;
|
||||
using LightlessSync.API.Data;
|
||||
using NotificationType = LightlessSync.LightlessConfiguration.Models.NotificationType;
|
||||
|
||||
namespace LightlessSync.Services;
|
||||
|
||||
public class NotificationService : DisposableMediatorSubscriberBase, IHostedService
|
||||
{
|
||||
private readonly ILogger<NotificationService> _logger;
|
||||
@@ -44,6 +46,7 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
{
|
||||
Mediator.Subscribe<NotificationMessage>(this, HandleNotificationMessage);
|
||||
Mediator.Subscribe<PairRequestsUpdatedMessage>(this, HandlePairRequestsUpdated);
|
||||
Mediator.Subscribe<PerformanceNotificationMessage>(this, HandlePerformanceNotification);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -107,23 +110,35 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
|
||||
public void ShowPairRequestNotification(string senderName, string senderId, Action onAccept, Action onDecline)
|
||||
{
|
||||
var notification = new LightlessNotification
|
||||
var location = GetNotificationLocation(NotificationType.PairRequest);
|
||||
|
||||
// Show in chat if configured
|
||||
if (location == NotificationLocation.Chat || location == NotificationLocation.ChatAndLightlessUi)
|
||||
{
|
||||
Id = $"pair_request_{senderId}",
|
||||
Title = "Pair Request Received",
|
||||
Message = $"{senderName} wants to directly pair with you.",
|
||||
Type = NotificationType.PairRequest,
|
||||
Duration = TimeSpan.FromSeconds(_configService.Current.PairRequestDurationSeconds),
|
||||
SoundEffectId = GetPairRequestSoundId(),
|
||||
Actions = CreatePairRequestActions(onAccept, onDecline)
|
||||
};
|
||||
|
||||
if (notification.SoundEffectId.HasValue)
|
||||
{
|
||||
PlayNotificationSound(notification.SoundEffectId.Value);
|
||||
ShowChat(new NotificationMessage("Pair Request Received", $"{senderName} wants to directly pair with you.", NotificationType.PairRequest));
|
||||
}
|
||||
|
||||
// Show Lightless notification if configured
|
||||
if (location == NotificationLocation.LightlessUi || location == NotificationLocation.ChatAndLightlessUi)
|
||||
{
|
||||
var notification = new LightlessNotification
|
||||
{
|
||||
Id = $"pair_request_{senderId}",
|
||||
Title = "Pair Request Received",
|
||||
Message = $"{senderName} wants to directly pair with you.",
|
||||
Type = NotificationType.PairRequest,
|
||||
Duration = TimeSpan.FromSeconds(_configService.Current.PairRequestDurationSeconds),
|
||||
SoundEffectId = GetPairRequestSoundId(),
|
||||
Actions = CreatePairRequestActions(onAccept, onDecline)
|
||||
};
|
||||
|
||||
Mediator.Publish(new LightlessNotificationMessage(notification));
|
||||
if (notification.SoundEffectId.HasValue)
|
||||
{
|
||||
PlayNotificationSound(notification.SoundEffectId.Value);
|
||||
}
|
||||
|
||||
Mediator.Publish(new LightlessNotificationMessage(notification));
|
||||
}
|
||||
}
|
||||
|
||||
private uint? GetPairRequestSoundId() =>
|
||||
@@ -356,6 +371,7 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
NotificationType.Error => TimeSpan.FromSeconds(_configService.Current.ErrorNotificationDurationSeconds),
|
||||
NotificationType.PairRequest => TimeSpan.FromSeconds(_configService.Current.PairRequestDurationSeconds),
|
||||
NotificationType.Download => TimeSpan.FromSeconds(_configService.Current.DownloadNotificationDurationSeconds),
|
||||
NotificationType.Performance => TimeSpan.FromSeconds(_configService.Current.PerformanceNotificationDurationSeconds),
|
||||
_ => TimeSpan.FromSeconds(10)
|
||||
};
|
||||
|
||||
@@ -371,7 +387,8 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
NotificationType.Info => _configService.Current.DisableInfoSound,
|
||||
NotificationType.Warning => _configService.Current.DisableWarningSound,
|
||||
NotificationType.Error => _configService.Current.DisableErrorSound,
|
||||
NotificationType.Download => _configService.Current.DisableDownloadSound,
|
||||
NotificationType.Performance => _configService.Current.DisablePerformanceSound,
|
||||
NotificationType.Download => true, // Download sounds always disabled
|
||||
_ => false
|
||||
};
|
||||
|
||||
@@ -380,7 +397,7 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
NotificationType.Info => _configService.Current.CustomInfoSoundId,
|
||||
NotificationType.Warning => _configService.Current.CustomWarningSoundId,
|
||||
NotificationType.Error => _configService.Current.CustomErrorSoundId,
|
||||
NotificationType.Download => _configService.Current.DownloadSoundId,
|
||||
NotificationType.Performance => _configService.Current.PerformanceSoundId,
|
||||
_ => NotificationSounds.GetDefaultSound(type)
|
||||
};
|
||||
|
||||
@@ -418,6 +435,7 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
NotificationType.Error => _configService.Current.LightlessErrorNotification,
|
||||
NotificationType.PairRequest => _configService.Current.LightlessPairRequestNotification,
|
||||
NotificationType.Download => _configService.Current.LightlessDownloadNotification,
|
||||
NotificationType.Performance => _configService.Current.LightlessPerformanceNotification,
|
||||
_ => NotificationLocation.LightlessUi
|
||||
};
|
||||
|
||||
@@ -505,6 +523,18 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
case NotificationType.Error:
|
||||
PrintErrorChat(msg.Message);
|
||||
break;
|
||||
|
||||
case NotificationType.PairRequest:
|
||||
PrintPairRequestChat(msg.Title, msg.Message);
|
||||
break;
|
||||
|
||||
case NotificationType.Performance:
|
||||
PrintPerformanceChat(msg.Title, msg.Message);
|
||||
break;
|
||||
|
||||
// Download notifications don't support chat output, will be a giga spam otherwise
|
||||
case NotificationType.Download:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -528,6 +558,22 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
_chatGui.Print(se.BuiltString);
|
||||
}
|
||||
|
||||
private void PrintPairRequestChat(string? title, string? message)
|
||||
{
|
||||
SeStringBuilder se = new SeStringBuilder().AddText("[Lightless Sync] ")
|
||||
.AddUiForeground("Pair Request: ", 541).AddUiForegroundOff()
|
||||
.AddText(title ?? message ?? string.Empty);
|
||||
_chatGui.Print(se.BuiltString);
|
||||
}
|
||||
|
||||
private void PrintPerformanceChat(string? title, string? message)
|
||||
{
|
||||
SeStringBuilder se = new SeStringBuilder().AddText("[Lightless Sync] ")
|
||||
.AddUiForeground("Performance: ", 508).AddUiForegroundOff()
|
||||
.AddText(title ?? message ?? string.Empty);
|
||||
_chatGui.Print(se.BuiltString);
|
||||
}
|
||||
|
||||
private void HandlePairRequestsUpdated(PairRequestsUpdatedMessage _)
|
||||
{
|
||||
var activeRequests = _pairRequestService.GetActiveRequests();
|
||||
@@ -557,5 +603,144 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void HandlePerformanceNotification(PerformanceNotificationMessage msg)
|
||||
{
|
||||
var location = GetNotificationLocation(NotificationType.Performance);
|
||||
|
||||
// Show in chat if configured
|
||||
if (location == NotificationLocation.Chat || location == NotificationLocation.ChatAndLightlessUi)
|
||||
{
|
||||
ShowChat(new NotificationMessage(msg.Title, msg.Message, NotificationType.Performance));
|
||||
}
|
||||
|
||||
// Show Lightless notification if configured and action buttons are enabled
|
||||
if ((location == NotificationLocation.LightlessUi || location == NotificationLocation.ChatAndLightlessUi)
|
||||
&& _configService.Current.UseLightlessNotifications
|
||||
&& _configService.Current.ShowPerformanceNotificationActions)
|
||||
{
|
||||
var actions = CreatePerformanceActions(msg.UserData, msg.IsPaused, msg.PlayerName);
|
||||
var notification = new LightlessNotification
|
||||
{
|
||||
Title = msg.Title,
|
||||
Message = msg.Message,
|
||||
Type = NotificationType.Performance,
|
||||
Duration = TimeSpan.FromSeconds(_configService.Current.PerformanceNotificationDurationSeconds),
|
||||
Actions = actions,
|
||||
SoundEffectId = GetSoundEffectId(NotificationType.Performance, null)
|
||||
};
|
||||
|
||||
if (notification.SoundEffectId.HasValue)
|
||||
{
|
||||
PlayNotificationSound(notification.SoundEffectId.Value);
|
||||
}
|
||||
|
||||
Mediator.Publish(new LightlessNotificationMessage(notification));
|
||||
}
|
||||
else if (location != NotificationLocation.Nowhere && location != NotificationLocation.Chat)
|
||||
{
|
||||
// Fall back to regular notification without action buttons
|
||||
HandleNotificationMessage(new NotificationMessage(msg.Title, msg.Message, NotificationType.Performance));
|
||||
}
|
||||
}
|
||||
|
||||
private List<LightlessNotificationAction> CreatePerformanceActions(UserData userData, bool isPaused, string playerName)
|
||||
{
|
||||
var actions = new List<LightlessNotificationAction>();
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
actions.Add(new LightlessNotificationAction
|
||||
{
|
||||
Label = "Unpause",
|
||||
Icon = FontAwesomeIcon.Play,
|
||||
Color = UIColors.Get("LightlessGreen"),
|
||||
IsPrimary = true,
|
||||
OnClick = (notification) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Mediator.Publish(new CyclePauseMessage(userData));
|
||||
DismissNotification(notification);
|
||||
|
||||
var displayName = GetUserDisplayName(userData, playerName);
|
||||
ShowNotification(
|
||||
"Player Unpaused",
|
||||
$"Successfully unpaused {displayName}",
|
||||
NotificationType.Info,
|
||||
TimeSpan.FromSeconds(3));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to unpause player {uid}", userData.UID);
|
||||
var displayName = GetUserDisplayName(userData, playerName);
|
||||
ShowNotification(
|
||||
"Unpause Failed",
|
||||
$"Failed to unpause {displayName}",
|
||||
NotificationType.Error,
|
||||
TimeSpan.FromSeconds(5));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
actions.Add(new LightlessNotificationAction
|
||||
{
|
||||
Label = "Pause",
|
||||
Icon = FontAwesomeIcon.Pause,
|
||||
Color = UIColors.Get("LightlessOrange"),
|
||||
IsPrimary = true,
|
||||
OnClick = (notification) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Mediator.Publish(new PauseMessage(userData));
|
||||
DismissNotification(notification);
|
||||
|
||||
var displayName = GetUserDisplayName(userData, playerName);
|
||||
ShowNotification(
|
||||
"Player Paused",
|
||||
$"Successfully paused {displayName}",
|
||||
NotificationType.Info,
|
||||
TimeSpan.FromSeconds(3));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to pause player {uid}", userData.UID);
|
||||
var displayName = GetUserDisplayName(userData, playerName);
|
||||
ShowNotification(
|
||||
"Pause Failed",
|
||||
$"Failed to pause {displayName}",
|
||||
NotificationType.Error,
|
||||
TimeSpan.FromSeconds(5));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Add dismiss button
|
||||
actions.Add(new LightlessNotificationAction
|
||||
{
|
||||
Label = "Dismiss",
|
||||
Icon = FontAwesomeIcon.Times,
|
||||
Color = UIColors.Get("DimRed"),
|
||||
IsPrimary = false,
|
||||
OnClick = (notification) =>
|
||||
{
|
||||
DismissNotification(notification);
|
||||
}
|
||||
});
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
private string GetUserDisplayName(UserData userData, string playerName)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(userData.Alias) && !string.Equals(userData.Alias, userData.UID, StringComparison.Ordinal))
|
||||
{
|
||||
return $"{playerName} ({userData.Alias})";
|
||||
}
|
||||
return $"{playerName} ({userData.UID})";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user