|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using RustPlusBot.Abstractions.Events; |
| 3 | +using RustPlusBot.Domain.Connections; |
| 4 | +using RustPlusBot.Domain.Switches; |
| 5 | +using RustPlusBot.Features.Switches.Posting; |
| 6 | +using RustPlusBot.Features.Switches.Rendering; |
| 7 | +using RustPlusBot.Features.Workspace.Locating; |
| 8 | +using RustPlusBot.Persistence.Connections; |
| 9 | +using RustPlusBot.Persistence.Switches; |
| 10 | +using RustPlusBot.Persistence.Workspace; |
| 11 | + |
| 12 | +namespace RustPlusBot.Features.Switches.Relaying; |
| 13 | + |
| 14 | +/// <summary>Keeps switch embeds in sync: live state changes re-render; a non-Connected server marks them unreachable.</summary> |
| 15 | +/// <param name="scopeFactory">Opens scopes for the scoped stores.</param> |
| 16 | +/// <param name="locator">Resolves the #switches channel id.</param> |
| 17 | +/// <param name="poster">Posts/edits switch embeds.</param> |
| 18 | +/// <param name="renderer">Renders switch embeds.</param> |
| 19 | +internal sealed class SwitchStateRelay( |
| 20 | + IServiceScopeFactory scopeFactory, |
| 21 | + ISwitchChannelLocator locator, |
| 22 | + ISwitchChannelPoster poster, |
| 23 | + SwitchEmbedRenderer renderer) |
| 24 | +{ |
| 25 | + /// <summary>Handles a live state change: persist + re-render the switch's embed.</summary> |
| 26 | + /// <param name="evt">The switch state change.</param> |
| 27 | + /// <param name="cancellationToken">A cancellation token.</param> |
| 28 | + /// <returns>A task that completes when the embed has been re-rendered.</returns> |
| 29 | + public async Task HandleStateChangedAsync(SwitchStateChangedEvent evt, CancellationToken cancellationToken) |
| 30 | + { |
| 31 | + ArgumentNullException.ThrowIfNull(evt); |
| 32 | + var scope = scopeFactory.CreateAsyncScope(); |
| 33 | + await using (scope.ConfigureAwait(false)) |
| 34 | + { |
| 35 | + var store = scope.ServiceProvider.GetRequiredService<ISwitchStore>(); |
| 36 | + await store.UpdateStateAsync(evt.GuildId, evt.ServerId, evt.EntityId, evt.IsActive, cancellationToken) |
| 37 | + .ConfigureAwait(false); |
| 38 | + var sw = await store.GetAsync(evt.GuildId, evt.ServerId, evt.EntityId, cancellationToken) |
| 39 | + .ConfigureAwait(false); |
| 40 | + if (sw is null) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var culture = await GetCultureAsync(scope.ServiceProvider, evt.GuildId, cancellationToken) |
| 46 | + .ConfigureAwait(false); |
| 47 | + await RenderAsync(store, sw, evt.IsActive, evt.GuildId, evt.ServerId, culture, cancellationToken) |
| 48 | + .ConfigureAwait(false); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary>Handles a connection-status change: a non-Connected server marks its switch embeds unreachable.</summary> |
| 53 | + /// <param name="evt">The connection-status change.</param> |
| 54 | + /// <param name="cancellationToken">A cancellation token.</param> |
| 55 | + /// <returns>A task that completes when every affected embed has been re-rendered.</returns> |
| 56 | + public async Task HandleConnectionStatusAsync( |
| 57 | + ConnectionStatusChangedEvent evt, CancellationToken cancellationToken) |
| 58 | + { |
| 59 | + ArgumentNullException.ThrowIfNull(evt); |
| 60 | + var scope = scopeFactory.CreateAsyncScope(); |
| 61 | + await using (scope.ConfigureAwait(false)) |
| 62 | + { |
| 63 | + var connections = scope.ServiceProvider.GetRequiredService<IConnectionStore>(); |
| 64 | + var state = await connections.GetStateAsync(evt.GuildId, evt.ServerId, cancellationToken) |
| 65 | + .ConfigureAwait(false); |
| 66 | + if (state is { Status: ConnectionStatus.Connected }) |
| 67 | + { |
| 68 | + // The supervisor's prime path republishes real state on connect; nothing to do here. |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + var store = scope.ServiceProvider.GetRequiredService<ISwitchStore>(); |
| 73 | + var switches = await store.ListByServerAsync(evt.GuildId, evt.ServerId, cancellationToken) |
| 74 | + .ConfigureAwait(false); |
| 75 | + if (switches.Count == 0) |
| 76 | + { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + var culture = await GetCultureAsync(scope.ServiceProvider, evt.GuildId, cancellationToken) |
| 81 | + .ConfigureAwait(false); |
| 82 | + foreach (var sw in switches) |
| 83 | + { |
| 84 | + await RenderAsync(store, sw, isActive: null, evt.GuildId, evt.ServerId, culture, cancellationToken) |
| 85 | + .ConfigureAwait(false); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private async Task RenderAsync( |
| 91 | + ISwitchStore store, SmartSwitch sw, bool? isActive, ulong guildId, Guid serverId, string culture, |
| 92 | + CancellationToken cancellationToken) |
| 93 | + { |
| 94 | + var channelId = await locator.GetChannelIdAsync(guildId, serverId, cancellationToken).ConfigureAwait(false); |
| 95 | + if (channelId is not { } channel) |
| 96 | + { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + var (embed, components) = renderer.RenderSwitch(sw, isActive, culture); |
| 101 | + var newMessageId = await poster.EnsureAsync(channel, sw.MessageId, embed, components, cancellationToken) |
| 102 | + .ConfigureAwait(false); |
| 103 | + if (newMessageId is { } mid && mid != sw.MessageId) |
| 104 | + { |
| 105 | + await store.SetMessageIdAsync(guildId, serverId, sw.EntityId, mid, cancellationToken) |
| 106 | + .ConfigureAwait(false); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static async Task<string> GetCultureAsync( |
| 111 | + IServiceProvider provider, ulong guildId, CancellationToken cancellationToken) |
| 112 | + { |
| 113 | + var store = provider.GetRequiredService<IWorkspaceStore>(); |
| 114 | + return await store.GetCultureAsync(guildId, cancellationToken).ConfigureAwait(false); |
| 115 | + } |
| 116 | +} |
0 commit comments