using System.Text.Json; using Microsoft.EntityFrameworkCore; using RadicadeServer.DB; using RadicadeServer.Extensions; using RadicadeServer.RecNet.Enums.Messages; using RadicadeServer.RecNet.Notifications; namespace RadicadeServer.API.Notifications { public static class RewardPushDiag { public const string RegisteredName = "RewardSelectionReceived"; public static string EnumNumber => Convert.ToInt64(NotificationType.GiftPackageRewardSelectionReceived, System.Globalization.CultureInfo.InvariantCulture) .ToString(System.Globalization.CultureInfo.InvariantCulture); public static async Task ReplayPendingRewardSelectionAsync(RecDBContext db, long accountId, string? idForm) { var id = Resolve(idForm); var progression = await db.Progression.AsNoTracking().FirstOrDefaultAsync(x => x.PlayerId == accountId); var pending = progression?.GetProgressionJson()?.PendingGameRewards; var reward = pending?.Where(r => !r.Consumed).OrderByDescending(r => r.CreatedAt).FirstOrDefault(); if (reward == null) return $"account {accountId} has no pending reward selection - finish a game or a daily challenge first.\n"; var connections = Notify.SnapshotClients().Count(c => c.AccountId == accountId); var notification = new Notification { Id = id, Msg = reward }; await Notify.SendNotify(accountId, notification); var idJson = JsonSerializer.Serialize(id); var lines = new System.Text.StringBuilder(); lines.AppendLine($"account {accountId}: pushed reward selectionId={reward.RewardSelectionId} ({reward.RewardType}, \"{reward.Message}\") on {connections} connection(s)."); lines.AppendLine($"Id token on the wire: {idJson}" + (id == RegisteredName ? " (the key the client registered a handler under)" : string.Empty)); lines.AppendLine($"expected: {(id == EnumNumber ? "the client parses the frame, finds no handler for a bare number unless its own table has one, and handles nothing." : "the client's dispatcher finds a handler under this exact key and the selection screen opens - no relog.")}"); lines.AppendLine($"envelope: {Notify.EnvelopeWithIdForm(notification, idJson)}"); lines.AppendLine($"pending for this account now: {pending!.Count(r => !r.Consumed)} selection(s)."); return lines.ToString(); } private static string Resolve(string? idForm) => (idForm ?? "name").Trim().ToLowerInvariant() switch { "name" => RegisteredName, "number" => EnumNumber, "" => RegisteredName, _ => idForm!.Trim(), }; } }