Added player detector implementation
Added computer event listeners
This commit is contained in:
37
MinecraftDiscordBot/Services/PlayerDetectorService.cs
Normal file
37
MinecraftDiscordBot/Services/PlayerDetectorService.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Discord.WebSocket;
|
||||
using MinecraftDiscordBot.Commands;
|
||||
using MinecraftDiscordBot.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MinecraftDiscordBot.Services;
|
||||
|
||||
internal class PlayerDetectorService : CommandRouter {
|
||||
private readonly ITaskWaitSource _taskSource;
|
||||
public PlayerDetectorService(ITaskWaitSource taskSource) => _taskSource = taskSource;
|
||||
|
||||
public override string HelpTextPrefix => "!pd ";
|
||||
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
||||
=> throw new ReplyException($"The player detector cannot do '{method}'!");
|
||||
|
||||
private Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null)
|
||||
=> RootCommandService.Method(_taskSource, methodName, parser, ct, parameters);
|
||||
|
||||
public Task<string[]> GetOnlinePlayers(CancellationToken ct) => Method("getonline", RootCommandService.Deserialize<string[]>(), ct);
|
||||
public async Task<PlayerPosition> GetPlayerPosition(string username, CancellationToken ct)
|
||||
=> (await FindPlayer(username, ct)) ?? throw new ReplyException($"User '{username}' is not online!");
|
||||
private Task<PlayerPosition?> FindPlayer(string username, CancellationToken ct) => Method("whereis", i => JsonConvert.DeserializeObject<PlayerPosition?>(i), ct, new() {
|
||||
["username"] = username
|
||||
});
|
||||
|
||||
[CommandHandler("getonline", HelpText ="Get a list of online players.")]
|
||||
public async Task<ResponseType> HandleOnlinePlayers(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
||||
=> ResponseType.AsString($"The following players are currently online:\n{string.Join("\n", await GetOnlinePlayers(ct))}");
|
||||
[CommandHandler("whereis", HelpText = "Find a player in the world.")]
|
||||
public async Task<ResponseType> HandleFindPlayers(SocketUserMessage message, string[] parameters, CancellationToken ct) {
|
||||
if (parameters is not { Length: 1 }) throw new ReplyException($"Give me only one username!");
|
||||
var username = parameters[0];
|
||||
var player = await FindPlayer(username, ct);
|
||||
if (player is null) throw new ReplyException($"{username} is currently offline!");
|
||||
return ResponseType.AsString($"{username} is at coordinates {player.X} {player.Y} {player.Z} in dimension {player.Dimension}.");
|
||||
}
|
||||
}
|
@ -18,11 +18,8 @@ public class RefinedStorageService : CommandRouter {
|
||||
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
||||
=> throw new ReplyException($"The RS system has no command '{method}'!");
|
||||
|
||||
private async Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null) {
|
||||
var waiter = _taskSource.GetWaiter(parser, ct);
|
||||
await _taskSource.Send(new RequestMessage(waiter.ID, methodName, parameters));
|
||||
return await waiter.Task;
|
||||
}
|
||||
private Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null)
|
||||
=> RootCommandService.Method<T>(_taskSource, methodName, parser, ct, parameters);
|
||||
|
||||
private const string CmdEnergyUsage = "energyusage";
|
||||
private const string CmdEnergyStorage = "energystorage";
|
||||
|
@ -10,16 +10,45 @@ public delegate Task<TResponse> HandleCommandDelegate<TResponse>(SocketUserMessa
|
||||
public delegate Task HandleCommandDelegate(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
||||
|
||||
public class RootCommandService : CommandRouter, ITaskWaitSource {
|
||||
protected readonly IWebSocketConnection _socket;
|
||||
protected IWebSocketConnection? _socketField;
|
||||
public override string HelpTextPrefix => "!";
|
||||
public RootCommandService(IWebSocketConnection socket, IUserRoleManager roleManager) : base() {
|
||||
socket.OnMessage = OnMessage;
|
||||
_socket = socket;
|
||||
public RootCommandService(IUserRoleManager roleManager) : base() {
|
||||
_rs = new RefinedStorageService(this, roleManager);
|
||||
_pd = new PlayerDetectorService(this);
|
||||
}
|
||||
|
||||
public static async Task<T> Method<T>(ITaskWaitSource taskSource, string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters) {
|
||||
var waiter = taskSource.GetWaiter(parser, ct);
|
||||
await taskSource.Send(new RequestMessage(waiter.ID, methodName, parameters));
|
||||
return await waiter.Task;
|
||||
}
|
||||
|
||||
public event EventHandler<ChatEvent>? ChatMessageReceived;
|
||||
public event EventHandler<PeripheralAttachEvent>? PeripheralAttached;
|
||||
public event EventHandler<PeripheralDetachEvent>? PeripheralDetached;
|
||||
public event EventHandler<IWebSocketConnection?>? SocketChanged;
|
||||
|
||||
public IWebSocketConnection? Socket {
|
||||
get => _socketField; set {
|
||||
if (_socketField != value) {
|
||||
_socketField = value;
|
||||
if (value is not null) value.OnMessage = OnMessage;
|
||||
SocketChanged?.Invoke(this, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMessage(string message) {
|
||||
switch (Message.Deserialize(message)) {
|
||||
case ChatEvent msg:
|
||||
ChatMessageReceived?.Invoke(this, msg);
|
||||
break;
|
||||
case PeripheralAttachEvent msg:
|
||||
PeripheralAttached?.Invoke(this, msg);
|
||||
break;
|
||||
case PeripheralDetachEvent msg:
|
||||
PeripheralDetached?.Invoke(this, msg);
|
||||
break;
|
||||
case ReplyMessage msg:
|
||||
IChunkWaiter? waiter;
|
||||
lock (_syncRoot) if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
|
||||
@ -38,12 +67,11 @@ public class RootCommandService : CommandRouter, ITaskWaitSource {
|
||||
}
|
||||
}
|
||||
|
||||
public Task Send(string message) => _socket.Send(message);
|
||||
public Task Send(string message) => (Socket ?? throw new ReplyException("Minecraft server is not available!")).Send(message);
|
||||
public Task Send(Message message) => Send(JsonConvert.SerializeObject(message));
|
||||
private readonly object _syncRoot = new();
|
||||
private readonly Dictionary<int, IChunkWaiter> _waits = new();
|
||||
private readonly Random _rnd = new();
|
||||
public IWebSocketConnectionInfo ConnectionInfo => _socket.ConnectionInfo;
|
||||
|
||||
private int GetFreeId() {
|
||||
var attempts = 0;
|
||||
@ -65,9 +93,14 @@ public class RootCommandService : CommandRouter, ITaskWaitSource {
|
||||
}
|
||||
|
||||
private readonly ICommandHandler<ResponseType> _rs;
|
||||
private readonly ICommandHandler<ResponseType> _pd;
|
||||
|
||||
[CommandHandler("rs", HelpText = "Provides some commands for interacting with the Refined Storage system.")]
|
||||
public Task<ResponseType> RefinedStorageHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
||||
=> _rs.HandleCommand(message, parameters, ct);
|
||||
[CommandHandler("pd", HelpText = "Provides some commands for interacting with the Player Detector.")]
|
||||
public Task<ResponseType> PlayerDetectorHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
||||
=> _pd.HandleCommand(message, parameters, ct);
|
||||
|
||||
public static Func<string, T> Deserialize<T>() => msg
|
||||
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
|
||||
|
Reference in New Issue
Block a user