using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Helpers;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Eft.Notifier;
using SPTarkov.Server.Core.Models.Eft.Ws;
using SPTarkov.Server.Core.Services;
namespace SPTarkov.Server.Core.Controllers;
[Injectable]
public class NotifierController(HttpServerHelper httpServerHelper, NotifierHelper notifierHelper, NotificationService notificationService)
{
protected const int PollInterval = 300;
protected const int Timeout = 15000;
///
/// Resolve an array of session notifications.
/// If no notifications are currently queued then intermittently check for new notifications until either
/// one or more appear or when a timeout expires.
/// If no notifications are available after the timeout, use a default message.
///
/// Session/Player id
public Task> NotifyAsync(MongoId sessionId)
{
return Task.Factory.StartNew(() =>
{
// keep track of our timeout
var counter = 0;
while (counter < Timeout)
{
if (!notificationService.Has(sessionId))
{
counter += PollInterval;
Thread.Sleep(PollInterval);
}
else
{
var messages = notificationService.Get(sessionId);
notificationService.UpdateMessageOnQueue(sessionId, []);
return messages;
}
}
return [notifierHelper.GetDefaultNotification()];
});
}
///
/// Handle client/notifier/channel/create
///
/// Session/Player id
/// NotifierChannel
public NotifierChannel GetChannel(MongoId sessionId)
{
return new NotifierChannel
{
Server = httpServerHelper.BuildUrl(),
ChannelId = sessionId,
Url = string.Empty,
NotifierServer = GetServer(sessionId),
WebSocket = notifierHelper.GetWebSocketServer(sessionId),
};
}
///
/// Get the notifier server url
///
/// Session/Player id
/// Notification server url
public string GetServer(MongoId sessionId)
{
return $"{httpServerHelper.GetBackendUrl()}/notifierServer/get/{sessionId.ToString()}";
}
}