|
|
|
|
@@ -45,7 +45,9 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Mediator.Subscribe<NotificationMessage>(this, HandleNotificationMessage);
|
|
|
|
|
Mediator.Subscribe<PairRequestReceivedMessage>(this, HandlePairRequestReceived);
|
|
|
|
|
Mediator.Subscribe<PairRequestsUpdatedMessage>(this, HandlePairRequestsUpdated);
|
|
|
|
|
Mediator.Subscribe<PairDownloadStatusMessage>(this, HandlePairDownloadStatus);
|
|
|
|
|
Mediator.Subscribe<PerformanceNotificationMessage>(this, HandlePerformanceNotification);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
@@ -293,33 +295,8 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
|
|
|
|
return actions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ShowPairDownloadNotification(List<(string playerName, float progress, string status)> downloadStatus,
|
|
|
|
|
int queueWaiting = 0)
|
|
|
|
|
{
|
|
|
|
|
var userDownloads = downloadStatus.Where(x => x.playerName != "Pair Queue").ToList();
|
|
|
|
|
var totalProgress = userDownloads.Count > 0 ? userDownloads.Average(x => x.progress) : 0f;
|
|
|
|
|
var message = BuildPairDownloadMessage(userDownloads, queueWaiting);
|
|
|
|
|
|
|
|
|
|
var notification = new LightlessNotification
|
|
|
|
|
{
|
|
|
|
|
Id = "pair_download_progress",
|
|
|
|
|
Title = "Downloading Pair Data",
|
|
|
|
|
Message = message,
|
|
|
|
|
Type = NotificationType.Download,
|
|
|
|
|
Duration = TimeSpan.FromSeconds(_configService.Current.DownloadNotificationDurationSeconds),
|
|
|
|
|
ShowProgress = true,
|
|
|
|
|
Progress = totalProgress
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Mediator.Publish(new LightlessNotificationMessage(notification));
|
|
|
|
|
|
|
|
|
|
if (AreAllDownloadsCompleted(userDownloads))
|
|
|
|
|
{
|
|
|
|
|
DismissPairDownloadNotification();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string BuildPairDownloadMessage(List<(string playerName, float progress, string status)> userDownloads,
|
|
|
|
|
private string BuildPairDownloadMessage(List<(string PlayerName, float Progress, string Status)> userDownloads,
|
|
|
|
|
int queueWaiting)
|
|
|
|
|
{
|
|
|
|
|
var messageParts = new List<string>();
|
|
|
|
|
@@ -331,7 +308,7 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
|
|
|
|
|
|
|
|
|
if (userDownloads.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var completedCount = userDownloads.Count(x => x.progress >= 1.0f);
|
|
|
|
|
var completedCount = userDownloads.Count(x => x.Progress >= 1.0f);
|
|
|
|
|
messageParts.Add($"Progress: {completedCount}/{userDownloads.Count} completed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -344,29 +321,29 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
|
|
|
|
return string.Join("\n", messageParts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string BuildActiveDownloadLines(List<(string playerName, float progress, string status)> userDownloads)
|
|
|
|
|
private string BuildActiveDownloadLines(List<(string PlayerName, float Progress, string Status)> userDownloads)
|
|
|
|
|
{
|
|
|
|
|
var activeDownloads = userDownloads
|
|
|
|
|
.Where(x => x.progress < 1.0f)
|
|
|
|
|
.Where(x => x.Progress < 1.0f)
|
|
|
|
|
.Take(_configService.Current.MaxConcurrentPairApplications);
|
|
|
|
|
|
|
|
|
|
if (!activeDownloads.Any()) return string.Empty;
|
|
|
|
|
|
|
|
|
|
return string.Join("\n", activeDownloads.Select(x => $"• {x.playerName}: {FormatDownloadStatus(x)}"));
|
|
|
|
|
return string.Join("\n", activeDownloads.Select(x => $"• {x.PlayerName}: {FormatDownloadStatus(x)}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string FormatDownloadStatus((string playerName, float progress, string status) download) =>
|
|
|
|
|
download.status switch
|
|
|
|
|
private string FormatDownloadStatus((string PlayerName, float Progress, string Status) download) =>
|
|
|
|
|
download.Status switch
|
|
|
|
|
{
|
|
|
|
|
"downloading" => $"{download.progress:P0}",
|
|
|
|
|
"downloading" => $"{download.Progress:P0}",
|
|
|
|
|
"decompressing" => "decompressing",
|
|
|
|
|
"queued" => "queued",
|
|
|
|
|
"waiting" => "waiting for slot",
|
|
|
|
|
_ => download.status
|
|
|
|
|
_ => download.Status
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private bool AreAllDownloadsCompleted(List<(string playerName, float progress, string status)> userDownloads) =>
|
|
|
|
|
userDownloads.Any() && userDownloads.All(x => x.progress >= 1.0f);
|
|
|
|
|
private bool AreAllDownloadsCompleted(List<(string PlayerName, float Progress, string Status)> userDownloads) =>
|
|
|
|
|
userDownloads.Any() && userDownloads.All(x => x.Progress >= 1.0f);
|
|
|
|
|
|
|
|
|
|
public void DismissPairDownloadNotification() =>
|
|
|
|
|
Mediator.Publish(new LightlessNotificationDismissMessage("pair_download_progress"));
|
|
|
|
|
@@ -581,12 +558,25 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
|
|
|
|
_chatGui.Print(se.BuiltString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandlePairRequestReceived(PairRequestReceivedMessage msg)
|
|
|
|
|
{
|
|
|
|
|
var request = _pairRequestService.RegisterIncomingRequest(msg.HashedCid, msg.Message);
|
|
|
|
|
var senderName = string.IsNullOrEmpty(request.DisplayName) ? "Unknown User" : request.DisplayName;
|
|
|
|
|
|
|
|
|
|
_shownPairRequestNotifications.Add(request.HashedCid);
|
|
|
|
|
ShowPairRequestNotification(
|
|
|
|
|
senderName,
|
|
|
|
|
request.HashedCid,
|
|
|
|
|
onAccept: () => _pairRequestService.AcceptPairRequest(request.HashedCid, senderName),
|
|
|
|
|
onDecline: () => _pairRequestService.DeclinePairRequest(request.HashedCid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandlePairRequestsUpdated(PairRequestsUpdatedMessage _)
|
|
|
|
|
{
|
|
|
|
|
var activeRequests = _pairRequestService.GetActiveRequests();
|
|
|
|
|
var activeRequestIds = activeRequests.Select(r => r.HashedCid).ToHashSet();
|
|
|
|
|
|
|
|
|
|
// Dismiss notifications for requests that are no longer active
|
|
|
|
|
// Dismiss notifications for requests that are no longer active (expired)
|
|
|
|
|
var notificationsToRemove = _shownPairRequestNotifications
|
|
|
|
|
.Where(hashedCid => !activeRequestIds.Contains(hashedCid))
|
|
|
|
|
.ToList();
|
|
|
|
|
@@ -597,17 +587,30 @@ public class NotificationService : DisposableMediatorSubscriberBase, IHostedServ
|
|
|
|
|
Mediator.Publish(new LightlessNotificationDismissMessage(notificationId));
|
|
|
|
|
_shownPairRequestNotifications.Remove(hashedCid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Show/update notifications for all active requests
|
|
|
|
|
foreach (var request in activeRequests)
|
|
|
|
|
private void HandlePairDownloadStatus(PairDownloadStatusMessage msg)
|
|
|
|
|
{
|
|
|
|
|
var userDownloads = msg.DownloadStatus.Where(x => x.PlayerName != "Pair Queue").ToList();
|
|
|
|
|
var totalProgress = userDownloads.Count > 0 ? userDownloads.Average(x => x.Progress) : 0f;
|
|
|
|
|
var message = BuildPairDownloadMessage(userDownloads, msg.QueueWaiting);
|
|
|
|
|
|
|
|
|
|
var notification = new LightlessNotification
|
|
|
|
|
{
|
|
|
|
|
_shownPairRequestNotifications.Add(request.HashedCid);
|
|
|
|
|
ShowPairRequestNotification(
|
|
|
|
|
request.DisplayName,
|
|
|
|
|
request.HashedCid,
|
|
|
|
|
() => _pairRequestService.AcceptPairRequest(request.HashedCid, request.DisplayName),
|
|
|
|
|
() => _pairRequestService.DeclinePairRequest(request.HashedCid)
|
|
|
|
|
);
|
|
|
|
|
Id = "pair_download_progress",
|
|
|
|
|
Title = "Downloading Pair Data",
|
|
|
|
|
Message = message,
|
|
|
|
|
Type = NotificationType.Download,
|
|
|
|
|
Duration = TimeSpan.FromSeconds(_configService.Current.DownloadNotificationDurationSeconds),
|
|
|
|
|
ShowProgress = true,
|
|
|
|
|
Progress = totalProgress
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Mediator.Publish(new LightlessNotificationMessage(notification));
|
|
|
|
|
|
|
|
|
|
if (AreAllDownloadsCompleted(userDownloads))
|
|
|
|
|
{
|
|
|
|
|
Mediator.Publish(new LightlessNotificationDismissMessage("pair_download_progress"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|