|
| 1 | +using System.Collections.Concurrent; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using RustPlusBot.Abstractions.Events; |
| 4 | +using RustPlusBot.Features.Alarms.Posting; |
| 5 | +using RustPlusBot.Features.Alarms.Rendering; |
| 6 | +using RustPlusBot.Features.Workspace.Locating; |
| 7 | +using RustPlusBot.Persistence.Alarms; |
| 8 | +using RustPlusBot.Persistence.Workspace; |
| 9 | + |
| 10 | +namespace RustPlusBot.Features.Alarms.Pairing; |
| 11 | + |
| 12 | +/// <summary>Turns an <see cref="AlarmPairedEvent"/> into an "Add it?" prompt and, on Accept, a managed alarm.</summary> |
| 13 | +/// <param name="scopeFactory">Opens scopes for the scoped alarm/workspace stores.</param> |
| 14 | +/// <param name="locator">Resolves the #alarms channel id.</param> |
| 15 | +/// <param name="poster">Posts/edits alarm + prompt messages.</param> |
| 16 | +/// <param name="renderer">Renders the prompt and alarm embeds.</param> |
| 17 | +internal sealed class AlarmPairingCoordinator( |
| 18 | + IServiceScopeFactory scopeFactory, |
| 19 | + IAlarmChannelLocator locator, |
| 20 | + IAlarmChannelPoster poster, |
| 21 | + AlarmEmbedRenderer renderer) |
| 22 | +{ |
| 23 | + private readonly ConcurrentDictionary<(ulong Guild, Guid Server, ulong Entity), Pending> _pending = new(); |
| 24 | + |
| 25 | + /// <summary>Gets the held default name for a pending pairing, or null.</summary> |
| 26 | + /// <param name="guildId">The guild id.</param> |
| 27 | + /// <param name="serverId">The server id.</param> |
| 28 | + /// <param name="entityId">The alarm entity id.</param> |
| 29 | + /// <returns>The held default name, or null when no pending pairing exists.</returns> |
| 30 | + public string? PendingName(ulong guildId, Guid serverId, ulong entityId) => |
| 31 | + _pending.TryGetValue((guildId, serverId, entityId), out var p) ? p.DefaultName : null; |
| 32 | + |
| 33 | + /// <summary>Handles a paired alarm: ignore if already managed, else post the prompt and hold pending state.</summary> |
| 34 | + /// <param name="evt">The paired-alarm event.</param> |
| 35 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 36 | + /// <returns>A task that completes when the prompt has been posted (or the alarm was ignored).</returns> |
| 37 | + public async Task HandlePairedAsync(AlarmPairedEvent evt, CancellationToken cancellationToken) |
| 38 | + { |
| 39 | + ArgumentNullException.ThrowIfNull(evt); |
| 40 | + if (await ExistsAsync(evt.GuildId, evt.ServerId, evt.EntityId, cancellationToken).ConfigureAwait(false)) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var channelId = await locator.GetChannelIdAsync(evt.GuildId, evt.ServerId, cancellationToken) |
| 46 | + .ConfigureAwait(false); |
| 47 | + if (channelId is not { } channel) |
| 48 | + { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + var culture = await GetCultureAsync(evt.GuildId, cancellationToken).ConfigureAwait(false); |
| 53 | + var defaultName = $"Alarm {evt.EntityId}"; |
| 54 | + var (embed, components) = renderer.RenderPrompt(evt.ServerId, evt.EntityId, defaultName, culture); |
| 55 | + var messageId = await poster.EnsureAsync(channel, null, embed, components, cancellationToken) |
| 56 | + .ConfigureAwait(false); |
| 57 | + _pending[(evt.GuildId, evt.ServerId, evt.EntityId)] = new Pending(defaultName, messageId); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary>Accepts a pending pairing: persist + replace prompt with the alarm embed. Race-guarded.</summary> |
| 61 | + /// <param name="guildId">The guild id.</param> |
| 62 | + /// <param name="serverId">The server id.</param> |
| 63 | + /// <param name="entityId">The alarm entity id.</param> |
| 64 | + /// <param name="acceptingUserId">The id of the user who accepted the pairing.</param> |
| 65 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 66 | + /// <returns>True when the alarm was persisted; false when it was already managed (race).</returns> |
| 67 | + public async Task<bool> TryAcceptAsync( |
| 68 | + ulong guildId, |
| 69 | + Guid serverId, |
| 70 | + ulong entityId, |
| 71 | + ulong acceptingUserId, |
| 72 | + CancellationToken cancellationToken) |
| 73 | + { |
| 74 | + if (await ExistsAsync(guildId, serverId, entityId, cancellationToken).ConfigureAwait(false)) |
| 75 | + { |
| 76 | + _pending.TryRemove((guildId, serverId, entityId), out _); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + _pending.TryGetValue((guildId, serverId, entityId), out var pending); |
| 81 | + var name = pending?.DefaultName ?? $"Alarm {entityId}"; |
| 82 | + |
| 83 | + var scope = scopeFactory.CreateAsyncScope(); |
| 84 | + await using (scope.ConfigureAwait(false)) |
| 85 | + { |
| 86 | + var store = scope.ServiceProvider.GetRequiredService<IAlarmStore>(); |
| 87 | + var added = await store.AddAsync(guildId, serverId, entityId, name, acceptingUserId, cancellationToken) |
| 88 | + .ConfigureAwait(false); |
| 89 | + |
| 90 | + var channelId = await locator.GetChannelIdAsync(guildId, serverId, cancellationToken).ConfigureAwait(false); |
| 91 | + if (channelId is { } channel) |
| 92 | + { |
| 93 | + var culture = await GetCultureAsync(guildId, cancellationToken).ConfigureAwait(false); |
| 94 | + |
| 95 | + // The alarm is freshly accepted; unreachable is false (it just paired). |
| 96 | + // The supervisor's prime path will re-render real state shortly. |
| 97 | + var (embed, components) = renderer.RenderAlarm(added, unreachable: false, culture); |
| 98 | + var newMessageId = await poster |
| 99 | + .EnsureAsync(channel, pending?.MessageId, embed, components, cancellationToken) |
| 100 | + .ConfigureAwait(false); |
| 101 | + if (newMessageId is { } mid) |
| 102 | + { |
| 103 | + await store.SetMessageIdAsync(guildId, serverId, entityId, mid, cancellationToken) |
| 104 | + .ConfigureAwait(false); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + _pending.TryRemove((guildId, serverId, entityId), out _); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary>Drops a pending pairing; returns whether one was present.</summary> |
| 114 | + /// <param name="guildId">The guild id.</param> |
| 115 | + /// <param name="serverId">The server id.</param> |
| 116 | + /// <param name="entityId">The alarm entity id.</param> |
| 117 | + /// <returns>True when a pending pairing was removed; false when none was held.</returns> |
| 118 | + public bool TryDismiss(ulong guildId, Guid serverId, ulong entityId) => |
| 119 | + _pending.TryRemove((guildId, serverId, entityId), out _); |
| 120 | + |
| 121 | + private async Task<bool> ExistsAsync(ulong guildId, Guid serverId, ulong entityId, CancellationToken ct) |
| 122 | + { |
| 123 | + var scope = scopeFactory.CreateAsyncScope(); |
| 124 | + await using (scope.ConfigureAwait(false)) |
| 125 | + { |
| 126 | + var store = scope.ServiceProvider.GetRequiredService<IAlarmStore>(); |
| 127 | + return await store.ExistsAsync(guildId, serverId, entityId, ct).ConfigureAwait(false); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private async Task<string> GetCultureAsync(ulong guildId, CancellationToken ct) |
| 132 | + { |
| 133 | + var scope = scopeFactory.CreateAsyncScope(); |
| 134 | + await using (scope.ConfigureAwait(false)) |
| 135 | + { |
| 136 | + var store = scope.ServiceProvider.GetRequiredService<IWorkspaceStore>(); |
| 137 | + return await store.GetCultureAsync(guildId, ct).ConfigureAwait(false); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private sealed record Pending(string DefaultName, ulong? MessageId); |
| 142 | +} |
0 commit comments