From 7d5eb0f2b278f933012a4019df6237ba10d1f80c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 26 Aug 2023 13:29:02 +0100 Subject: [PATCH] refactor: move avatar events to AvatarService --- VPLink/Program.cs | 1 + VPLink/Services/AvatarService.cs | 70 ++++++++++++++++++++++ VPLink/Services/IAvatarService.cs | 25 ++++++++ VPLink/Services/IVirtualParadiseService.cs | 16 ----- VPLink/Services/RelayService.cs | 8 ++- VPLink/Services/VirtualParadiseService.cs | 35 ----------- 6 files changed, 102 insertions(+), 53 deletions(-) create mode 100644 VPLink/Services/AvatarService.cs create mode 100644 VPLink/Services/IAvatarService.cs diff --git a/VPLink/Program.cs b/VPLink/Program.cs index c4cdbef..fce6fae 100644 --- a/VPLink/Program.cs +++ b/VPLink/Program.cs @@ -34,6 +34,7 @@ builder.Services.AddSingleton(new DiscordSocketConfig GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent }); +builder.Services.AddHostedSingleton(); builder.Services.AddHostedSingleton(); builder.Services.AddHostedSingleton(); builder.Services.AddHostedSingleton(); diff --git a/VPLink/Services/AvatarService.cs b/VPLink/Services/AvatarService.cs new file mode 100644 index 0000000..15c74d1 --- /dev/null +++ b/VPLink/Services/AvatarService.cs @@ -0,0 +1,70 @@ +using System.Reactive.Linq; +using System.Reactive.Subjects; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using VPLink.Configuration; +using VpSharp; +using VpSharp.Entities; + +namespace VPLink.Services; + +/// +internal sealed class AvatarService : BackgroundService, IAvatarService +{ + private readonly ILogger _logger; + private readonly IConfigurationService _configurationService; + private readonly VirtualParadiseClient _virtualParadiseClient; + private readonly Subject _avatarJoined = new(); + private readonly Subject _avatarLeft = new(); + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + /// The configuration service. + /// The Virtual Paradise client. + public AvatarService(ILogger logger, + IConfigurationService configurationService, + VirtualParadiseClient virtualParadiseClient) + { + _logger = logger; + _configurationService = configurationService; + _virtualParadiseClient = virtualParadiseClient; + } + + /// + public IObservable OnAvatarJoined => _avatarJoined.AsObservable(); + + /// + public IObservable OnAvatarLeft => _avatarLeft.AsObservable(); + + /// + protected override Task ExecuteAsync(CancellationToken stoppingToken) + { + _virtualParadiseClient.AvatarJoined.Subscribe(OnVPAvatarJoined); + _virtualParadiseClient.AvatarLeft.Subscribe(OnVPAvatarLeft); + return Task.CompletedTask; + } + + private void OnVPAvatarJoined(VirtualParadiseAvatar avatar) + { + _logger.LogInformation("{Avatar} joined", avatar); + + BotConfiguration configuration = _configurationService.BotConfiguration; + if (!configuration.AnnounceAvatarEvents || avatar.IsBot && !configuration.AnnounceBots) + return; + + _avatarJoined.OnNext(avatar); + } + + private void OnVPAvatarLeft(VirtualParadiseAvatar avatar) + { + _logger.LogInformation("{Avatar} left", avatar); + + BotConfiguration configuration = _configurationService.BotConfiguration; + if (!configuration.AnnounceAvatarEvents || avatar.IsBot && !configuration.AnnounceBots) + return; + + _avatarLeft.OnNext(avatar); + } +} diff --git a/VPLink/Services/IAvatarService.cs b/VPLink/Services/IAvatarService.cs new file mode 100644 index 0000000..0bee759 --- /dev/null +++ b/VPLink/Services/IAvatarService.cs @@ -0,0 +1,25 @@ +using VpSharp.Entities; + +namespace VPLink.Services; + +/// +/// Represents a service that listens for, and triggers, avatar events. +/// +public interface IAvatarService +{ + /// + /// Gets an observable that is triggered when an avatar enters the Virtual Paradise world. + /// + /// + /// An observable that is triggered when an avatar enters the Virtual Paradise world. + /// + IObservable OnAvatarJoined { get; } + + /// + /// Gets an observable that is triggered when an avatar exits the Virtual Paradise world. + /// + /// + /// An observable that is triggered when an avatar exits the Virtual Paradise world. + /// + IObservable OnAvatarLeft { get; } +} diff --git a/VPLink/Services/IVirtualParadiseService.cs b/VPLink/Services/IVirtualParadiseService.cs index 625d93a..1668fd6 100644 --- a/VPLink/Services/IVirtualParadiseService.cs +++ b/VPLink/Services/IVirtualParadiseService.cs @@ -8,22 +8,6 @@ namespace VPLink.Services; /// public interface IVirtualParadiseService { - /// - /// Gets an observable that is triggered when an avatar enters the Virtual Paradise world. - /// - /// - /// An observable that is triggered when an avatar enters the Virtual Paradise world. - /// - IObservable OnAvatarJoined { get; } - - /// - /// Gets an observable that is triggered when an avatar exits the Virtual Paradise world. - /// - /// - /// An observable that is triggered when an avatar exits the Virtual Paradise world. - /// - IObservable OnAvatarLeft { get; } - /// /// Gets an observable that is triggered when a message is received from the Virtual Paradise world server. /// diff --git a/VPLink/Services/RelayService.cs b/VPLink/Services/RelayService.cs index 02034f1..dd23d7f 100644 --- a/VPLink/Services/RelayService.cs +++ b/VPLink/Services/RelayService.cs @@ -11,6 +11,7 @@ internal sealed class RelayService : BackgroundService { private readonly ILogger _logger; private readonly IDiscordService _discordService; + private readonly IAvatarService _avatarService; private readonly IVirtualParadiseService _virtualParadiseService; private readonly DiscordSocketClient _discordClient; private readonly VirtualParadiseClient _virtualParadiseClient; @@ -20,17 +21,20 @@ internal sealed class RelayService : BackgroundService /// /// The logger. /// The Discord service. + /// The avatar service. /// The Virtual Paradise service. /// The Discord client. /// The Virtual Paradise client. public RelayService(ILogger logger, IDiscordService discordService, + IAvatarService avatarService, IVirtualParadiseService virtualParadiseService, DiscordSocketClient discordClient, VirtualParadiseClient virtualParadiseClient) { _logger = logger; _discordService = discordService; + _avatarService = avatarService; _virtualParadiseService = virtualParadiseService; _discordClient = discordClient; _virtualParadiseClient = virtualParadiseClient; @@ -45,8 +49,8 @@ internal sealed class RelayService : BackgroundService .Where(m => m.Author != _discordClient.CurrentUser) .SubscribeAsync(_virtualParadiseService.SendMessageAsync); - _virtualParadiseService.OnAvatarJoined.SubscribeAsync(_discordService.AnnounceArrival); - _virtualParadiseService.OnAvatarLeft.SubscribeAsync(_discordService.AnnounceDeparture); + _avatarService.OnAvatarJoined.SubscribeAsync(_discordService.AnnounceArrival); + _avatarService.OnAvatarLeft.SubscribeAsync(_discordService.AnnounceDeparture); _virtualParadiseService.OnMessageReceived .Where(m => m.Author != _virtualParadiseClient.CurrentAvatar) diff --git a/VPLink/Services/VirtualParadiseService.cs b/VPLink/Services/VirtualParadiseService.cs index f28bc03..bc3365f 100644 --- a/VPLink/Services/VirtualParadiseService.cs +++ b/VPLink/Services/VirtualParadiseService.cs @@ -3,7 +3,6 @@ using System.Reactive.Subjects; using Discord; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using VPLink.Configuration; using VpSharp; using VpSharp.Entities; using Color = System.Drawing.Color; @@ -18,8 +17,6 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi private readonly IConfigurationService _configurationService; private readonly VirtualParadiseClient _virtualParadiseClient; private readonly Subject _messageReceived = new(); - private readonly Subject _avatarJoined = new(); - private readonly Subject _avatarLeft = new(); /// /// Initializes a new instance of the class. @@ -36,12 +33,6 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi _virtualParadiseClient = virtualParadiseClient; } - /// - public IObservable OnAvatarJoined => _avatarJoined.AsObservable(); - - /// - public IObservable OnAvatarLeft => _avatarJoined.AsObservable(); - /// public IObservable OnMessageReceived => _messageReceived.AsObservable(); @@ -69,8 +60,6 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi { _logger.LogInformation("Establishing relay"); _virtualParadiseClient.MessageReceived.Subscribe(_messageReceived); - _virtualParadiseClient.AvatarJoined.Subscribe(OnVirtualParadiseAvatarJoined); - _virtualParadiseClient.AvatarLeft.Subscribe(OnVirtualParadiseAvatarLeft); VirtualParadiseConfiguration configuration = _configurationService.VirtualParadiseConfiguration; @@ -86,28 +75,4 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi _logger.LogInformation("Entering world {World}", world); await _virtualParadiseClient.EnterAsync(world).ConfigureAwait(false); } - - private void OnVirtualParadiseAvatarJoined(VirtualParadiseAvatar avatar) - { - BotConfiguration configuration = _configurationService.BotConfiguration; - - if (!configuration.AnnounceAvatarEvents || avatar.IsBot && !configuration.AnnounceBots) - { - return; - } - - _avatarJoined.OnNext(avatar); - } - - private void OnVirtualParadiseAvatarLeft(VirtualParadiseAvatar avatar) - { - BotConfiguration configuration = _configurationService.BotConfiguration; - - if (!configuration.AnnounceAvatarEvents || avatar.IsBot && !configuration.AnnounceBots) - { - return; - } - - _avatarLeft.OnNext(avatar); - } }