mirror of
https://github.com/oliverbooth/VPLink
synced 2024-11-09 23:45:40 +00:00
refactor: move avatar events to AvatarService
This commit is contained in:
parent
d442e4e9b3
commit
7d5eb0f2b2
@ -34,6 +34,7 @@ builder.Services.AddSingleton(new DiscordSocketConfig
|
|||||||
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent
|
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent
|
||||||
});
|
});
|
||||||
|
|
||||||
|
builder.Services.AddHostedSingleton<IAvatarService, AvatarService>();
|
||||||
builder.Services.AddHostedSingleton<IVirtualParadiseService, VirtualParadiseService>();
|
builder.Services.AddHostedSingleton<IVirtualParadiseService, VirtualParadiseService>();
|
||||||
builder.Services.AddHostedSingleton<IDiscordService, DiscordService>();
|
builder.Services.AddHostedSingleton<IDiscordService, DiscordService>();
|
||||||
builder.Services.AddHostedSingleton<RelayService>();
|
builder.Services.AddHostedSingleton<RelayService>();
|
||||||
|
70
VPLink/Services/AvatarService.cs
Normal file
70
VPLink/Services/AvatarService.cs
Normal file
@ -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;
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IAvatarService" />
|
||||||
|
internal sealed class AvatarService : BackgroundService, IAvatarService
|
||||||
|
{
|
||||||
|
private readonly ILogger<AvatarService> _logger;
|
||||||
|
private readonly IConfigurationService _configurationService;
|
||||||
|
private readonly VirtualParadiseClient _virtualParadiseClient;
|
||||||
|
private readonly Subject<VirtualParadiseAvatar> _avatarJoined = new();
|
||||||
|
private readonly Subject<VirtualParadiseAvatar> _avatarLeft = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="AvatarService" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="configurationService">The configuration service.</param>
|
||||||
|
/// <param name="virtualParadiseClient">The Virtual Paradise client.</param>
|
||||||
|
public AvatarService(ILogger<AvatarService> logger,
|
||||||
|
IConfigurationService configurationService,
|
||||||
|
VirtualParadiseClient virtualParadiseClient)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_configurationService = configurationService;
|
||||||
|
_virtualParadiseClient = virtualParadiseClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IObservable<VirtualParadiseAvatar> OnAvatarJoined => _avatarJoined.AsObservable();
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IObservable<VirtualParadiseAvatar> OnAvatarLeft => _avatarLeft.AsObservable();
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
25
VPLink/Services/IAvatarService.cs
Normal file
25
VPLink/Services/IAvatarService.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using VpSharp.Entities;
|
||||||
|
|
||||||
|
namespace VPLink.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a service that listens for, and triggers, avatar events.
|
||||||
|
/// </summary>
|
||||||
|
public interface IAvatarService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an observable that is triggered when an avatar enters the Virtual Paradise world.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// An observable that is triggered when an avatar enters the Virtual Paradise world.
|
||||||
|
/// </value>
|
||||||
|
IObservable<VirtualParadiseAvatar> OnAvatarJoined { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an observable that is triggered when an avatar exits the Virtual Paradise world.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// An observable that is triggered when an avatar exits the Virtual Paradise world.
|
||||||
|
/// </value>
|
||||||
|
IObservable<VirtualParadiseAvatar> OnAvatarLeft { get; }
|
||||||
|
}
|
@ -8,22 +8,6 @@ namespace VPLink.Services;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IVirtualParadiseService
|
public interface IVirtualParadiseService
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Gets an observable that is triggered when an avatar enters the Virtual Paradise world.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// An observable that is triggered when an avatar enters the Virtual Paradise world.
|
|
||||||
/// </value>
|
|
||||||
IObservable<VirtualParadiseAvatar> OnAvatarJoined { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets an observable that is triggered when an avatar exits the Virtual Paradise world.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// An observable that is triggered when an avatar exits the Virtual Paradise world.
|
|
||||||
/// </value>
|
|
||||||
IObservable<VirtualParadiseAvatar> OnAvatarLeft { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets an observable that is triggered when a message is received from the Virtual Paradise world server.
|
/// Gets an observable that is triggered when a message is received from the Virtual Paradise world server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -11,6 +11,7 @@ internal sealed class RelayService : BackgroundService
|
|||||||
{
|
{
|
||||||
private readonly ILogger<RelayService> _logger;
|
private readonly ILogger<RelayService> _logger;
|
||||||
private readonly IDiscordService _discordService;
|
private readonly IDiscordService _discordService;
|
||||||
|
private readonly IAvatarService _avatarService;
|
||||||
private readonly IVirtualParadiseService _virtualParadiseService;
|
private readonly IVirtualParadiseService _virtualParadiseService;
|
||||||
private readonly DiscordSocketClient _discordClient;
|
private readonly DiscordSocketClient _discordClient;
|
||||||
private readonly VirtualParadiseClient _virtualParadiseClient;
|
private readonly VirtualParadiseClient _virtualParadiseClient;
|
||||||
@ -20,17 +21,20 @@ internal sealed class RelayService : BackgroundService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">The logger.</param>
|
/// <param name="logger">The logger.</param>
|
||||||
/// <param name="discordService">The Discord service.</param>
|
/// <param name="discordService">The Discord service.</param>
|
||||||
|
/// <param name="avatarService">The avatar service.</param>
|
||||||
/// <param name="virtualParadiseService">The Virtual Paradise service.</param>
|
/// <param name="virtualParadiseService">The Virtual Paradise service.</param>
|
||||||
/// <param name="discordClient">The Discord client.</param>
|
/// <param name="discordClient">The Discord client.</param>
|
||||||
/// <param name="virtualParadiseClient">The Virtual Paradise client.</param>
|
/// <param name="virtualParadiseClient">The Virtual Paradise client.</param>
|
||||||
public RelayService(ILogger<RelayService> logger,
|
public RelayService(ILogger<RelayService> logger,
|
||||||
IDiscordService discordService,
|
IDiscordService discordService,
|
||||||
|
IAvatarService avatarService,
|
||||||
IVirtualParadiseService virtualParadiseService,
|
IVirtualParadiseService virtualParadiseService,
|
||||||
DiscordSocketClient discordClient,
|
DiscordSocketClient discordClient,
|
||||||
VirtualParadiseClient virtualParadiseClient)
|
VirtualParadiseClient virtualParadiseClient)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_discordService = discordService;
|
_discordService = discordService;
|
||||||
|
_avatarService = avatarService;
|
||||||
_virtualParadiseService = virtualParadiseService;
|
_virtualParadiseService = virtualParadiseService;
|
||||||
_discordClient = discordClient;
|
_discordClient = discordClient;
|
||||||
_virtualParadiseClient = virtualParadiseClient;
|
_virtualParadiseClient = virtualParadiseClient;
|
||||||
@ -45,8 +49,8 @@ internal sealed class RelayService : BackgroundService
|
|||||||
.Where(m => m.Author != _discordClient.CurrentUser)
|
.Where(m => m.Author != _discordClient.CurrentUser)
|
||||||
.SubscribeAsync(_virtualParadiseService.SendMessageAsync);
|
.SubscribeAsync(_virtualParadiseService.SendMessageAsync);
|
||||||
|
|
||||||
_virtualParadiseService.OnAvatarJoined.SubscribeAsync(_discordService.AnnounceArrival);
|
_avatarService.OnAvatarJoined.SubscribeAsync(_discordService.AnnounceArrival);
|
||||||
_virtualParadiseService.OnAvatarLeft.SubscribeAsync(_discordService.AnnounceDeparture);
|
_avatarService.OnAvatarLeft.SubscribeAsync(_discordService.AnnounceDeparture);
|
||||||
|
|
||||||
_virtualParadiseService.OnMessageReceived
|
_virtualParadiseService.OnMessageReceived
|
||||||
.Where(m => m.Author != _virtualParadiseClient.CurrentAvatar)
|
.Where(m => m.Author != _virtualParadiseClient.CurrentAvatar)
|
||||||
|
@ -3,7 +3,6 @@ using System.Reactive.Subjects;
|
|||||||
using Discord;
|
using Discord;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using VPLink.Configuration;
|
|
||||||
using VpSharp;
|
using VpSharp;
|
||||||
using VpSharp.Entities;
|
using VpSharp.Entities;
|
||||||
using Color = System.Drawing.Color;
|
using Color = System.Drawing.Color;
|
||||||
@ -18,8 +17,6 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi
|
|||||||
private readonly IConfigurationService _configurationService;
|
private readonly IConfigurationService _configurationService;
|
||||||
private readonly VirtualParadiseClient _virtualParadiseClient;
|
private readonly VirtualParadiseClient _virtualParadiseClient;
|
||||||
private readonly Subject<VirtualParadiseMessage> _messageReceived = new();
|
private readonly Subject<VirtualParadiseMessage> _messageReceived = new();
|
||||||
private readonly Subject<VirtualParadiseAvatar> _avatarJoined = new();
|
|
||||||
private readonly Subject<VirtualParadiseAvatar> _avatarLeft = new();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="VirtualParadiseService" /> class.
|
/// Initializes a new instance of the <see cref="VirtualParadiseService" /> class.
|
||||||
@ -36,12 +33,6 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi
|
|||||||
_virtualParadiseClient = virtualParadiseClient;
|
_virtualParadiseClient = virtualParadiseClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IObservable<VirtualParadiseAvatar> OnAvatarJoined => _avatarJoined.AsObservable();
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IObservable<VirtualParadiseAvatar> OnAvatarLeft => _avatarJoined.AsObservable();
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IObservable<VirtualParadiseMessage> OnMessageReceived => _messageReceived.AsObservable();
|
public IObservable<VirtualParadiseMessage> OnMessageReceived => _messageReceived.AsObservable();
|
||||||
|
|
||||||
@ -69,8 +60,6 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi
|
|||||||
{
|
{
|
||||||
_logger.LogInformation("Establishing relay");
|
_logger.LogInformation("Establishing relay");
|
||||||
_virtualParadiseClient.MessageReceived.Subscribe(_messageReceived);
|
_virtualParadiseClient.MessageReceived.Subscribe(_messageReceived);
|
||||||
_virtualParadiseClient.AvatarJoined.Subscribe(OnVirtualParadiseAvatarJoined);
|
|
||||||
_virtualParadiseClient.AvatarLeft.Subscribe(OnVirtualParadiseAvatarLeft);
|
|
||||||
|
|
||||||
VirtualParadiseConfiguration configuration = _configurationService.VirtualParadiseConfiguration;
|
VirtualParadiseConfiguration configuration = _configurationService.VirtualParadiseConfiguration;
|
||||||
|
|
||||||
@ -86,28 +75,4 @@ internal sealed class VirtualParadiseService : BackgroundService, IVirtualParadi
|
|||||||
_logger.LogInformation("Entering world {World}", world);
|
_logger.LogInformation("Entering world {World}", world);
|
||||||
await _virtualParadiseClient.EnterAsync(world).ConfigureAwait(false);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user