using DeluxeBackend.Hubs; using DeluxeBackend.Services; using Microsoft.AspNetCore.SignalR; using System.Net.WebSockets; using System.Text.Json; using static DeluxeBackend.Enums; namespace DeluxeBackend.Services { public interface INotificationService { Task SendToPlayer(long playerId, string id, object msg); Task SendToPlayer(long playerId, PushNotification id, object msg); Task SendToPlayerSubs(long targetPlayerId, string id, object msg); Task SendToAllPlayer(string id, object msg); Task SendToAllPlayer(PushNotification id, object msg); } public enum PushNotification { [Token(Token = "0x4002F84")] RelationshipChanged = 1, [Token(Token = "0x4002F85")] MessageReceived, [Token(Token = "0x4002F86")] MessageDeleted, [Token(Token = "0x4002F87")] PresenceHeartbeatResponse, [Token(Token = "0x4002F88")] RefreshLogin, [Token(Token = "0x4002F89")] Logout, [Token(Token = "0x4002F8A")] SubscriptionUpdateProfile = 11, [Token(Token = "0x4002F8B")] SubscriptionUpdatePresence, [Token(Token = "0x4002F8C")] SubscriptionUpdateGameSession, [Token(Token = "0x4002F8D")] SubscriptionUpdateRoom = 15, [Token(Token = "0x4002F8E")] SubscriptionUpdateRoomPlaylist, [Token(Token = "0x4002F8F")] ModerationQuitGame = 20, [Token(Token = "0x4002F90")] ModerationUpdateRequired, [Token(Token = "0x4002F91")] ModerationKick, [Token(Token = "0x4002F92")] ModerationKickAttemptFailed, [Token(Token = "0x4002F93")] ModerationRoomBan, [Token(Token = "0x4002F94")] ServerMaintenance, [Token(Token = "0x4002F95")] GiftPackageReceived = 30, [Token(Token = "0x4002F96")] GiftPackageReceivedImmediate, [Token(Token = "0x4002F97")] GiftPackageRewardSelectionReceived, [Token(Token = "0x4002F98")] ProfileJuniorStatusUpdate = 40, [Token(Token = "0x4002F99")] RelationshipsInvalid = 50, [Token(Token = "0x4002F9A")] StorefrontBalanceAdd = 60, [Token(Token = "0x4002F9B")] StorefrontBalanceUpdate, [Token(Token = "0x4002F9C")] StorefrontBalancePurchase, [Token(Token = "0x4002F9D")] ConsumableMappingAdded = 70, [Token(Token = "0x4002F9E")] ConsumableMappingRemoved, [Token(Token = "0x4002F9F")] PlayerEventCreated = 80, [Token(Token = "0x4002FA0")] PlayerEventUpdated, [Token(Token = "0x4002FA1")] PlayerEventDeleted, [Token(Token = "0x4002FA2")] PlayerEventResponseChanged, [Token(Token = "0x4002FA3")] PlayerEventResponseDeleted, [Token(Token = "0x4002FA4")] PlayerEventStateChanged, [Token(Token = "0x4002FA5")] ChatMessageReceived = 90, [Token(Token = "0x4002FA6")] CommunityBoardUpdate = 95, [Token(Token = "0x4002FA7")] CommunityBoardAnnouncementUpdate, [Token(Token = "0x4002FA8")] InventionModerationStateChanged = 100, [Token(Token = "0x4002FA9")] FreeGiftButtonItemsAdded = 110, [Token(Token = "0x4002FAA")] LocalRoomKeyCreated = 120, [Token(Token = "0x4002FAB")] LocalRoomKeyDeleted } public class NotificationService : INotificationService { private readonly IHubContext _hubContext; private readonly ILiteDbService _db; private readonly JsonSerializerOptions _jsonOptions = new() { PropertyNamingPolicy = null }; public NotificationService(IHubContext hubContext, ILiteDbService db) { _hubContext = hubContext; _db = db; } private string GetJsonStr(string id, object msg) { var json = JsonSerializer.Serialize(new { Id = id, Msg = msg }, _jsonOptions); return json; } private string GetJsonStr(PushNotification id, object msg) { var json = JsonSerializer.Serialize(new { Id = ((int)id).ToString(), Msg = msg }, _jsonOptions); return json; } public async Task SendToPlayer(long playerId, string id, object msg) { await _hubContext.Clients.User(playerId.ToString()).SendAsync("Notification", GetJsonStr(id, msg)); } public async Task SendToPlayer(long playerId, PushNotification id, object msg) { await _hubContext.Clients.User(playerId.ToString()).SendAsync("Notification", GetJsonStr(id, msg)); } public async Task SendToPlayerSubs(long targetPlayerId, string id, object msg) { await _hubContext.Clients.All.SendAsync("Notification", GetJsonStr(id, msg)); //await _hubContext.Clients.Group($"subs_{targetPlayerId}").SendAsync("Notification", GetJsonStr(id, msg)); //send it do yourself //await _hubContext.Clients.User(targetPlayerId.ToString()).SendAsync("Notification", GetJsonStr(id, msg)); } public async Task SendToAllPlayer(string id, object msg) { await _hubContext.Clients.All.SendAsync("Notification", GetJsonStr(id, msg)); } public async Task SendToAllPlayer(PushNotification id, object msg) { await _hubContext.Clients.All.SendAsync("Notification", GetJsonStr(id, msg)); } } }