2022-01-16 22:29:50 +01:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Fleck;
|
|
|
|
|
using MinecraftDiscordBot.Commands;
|
|
|
|
|
using MinecraftDiscordBot.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace MinecraftDiscordBot.Services;
|
|
|
|
|
|
|
|
|
|
public delegate Task<TResponse> HandleCommandDelegate<TResponse>(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
|
|
|
|
public delegate Task HandleCommandDelegate(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
|
|
|
|
|
|
|
|
|
public class RootCommandService : CommandRouter, ITaskWaitSource {
|
2022-01-17 19:10:14 +01:00
|
|
|
|
protected IWebSocketConnection? _socketField;
|
2022-01-16 22:29:50 +01:00
|
|
|
|
public override string HelpTextPrefix => "!";
|
2022-01-17 19:10:14 +01:00
|
|
|
|
public RootCommandService(IUserRoleManager roleManager) : base() {
|
2022-01-17 15:24:04 +01:00
|
|
|
|
_rs = new RefinedStorageService(this, roleManager);
|
2022-01-17 19:10:14 +01:00
|
|
|
|
_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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-16 22:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMessage(string message) {
|
2022-01-17 16:22:15 +01:00
|
|
|
|
switch (Message.Deserialize(message)) {
|
2022-01-17 19:10:14 +01:00
|
|
|
|
case ChatEvent msg:
|
|
|
|
|
ChatMessageReceived?.Invoke(this, msg);
|
|
|
|
|
break;
|
|
|
|
|
case PeripheralAttachEvent msg:
|
|
|
|
|
PeripheralAttached?.Invoke(this, msg);
|
|
|
|
|
break;
|
|
|
|
|
case PeripheralDetachEvent msg:
|
|
|
|
|
PeripheralDetached?.Invoke(this, msg);
|
|
|
|
|
break;
|
2022-01-17 16:22:15 +01:00
|
|
|
|
case ReplyMessage msg:
|
|
|
|
|
IChunkWaiter? waiter;
|
|
|
|
|
lock (_syncRoot) if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
|
|
|
|
|
Program.LogWarningAsync("Socket", $"Invalid wait id '{msg.AnswerId}'!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
waiter.SetResultState(msg.State);
|
|
|
|
|
waiter.AddChunk(msg.Chunk, msg.Total, msg.Result);
|
|
|
|
|
if (waiter.Finished || waiter.IsCancellationRequested)
|
|
|
|
|
lock (_syncRoot)
|
|
|
|
|
_waits.Remove(waiter.ID);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Program.LogInfo(Program.WebSocketSource, $"Received unhandled message: {message}!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-01-16 22:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 19:10:14 +01:00
|
|
|
|
public Task Send(string message) => (Socket ?? throw new ReplyException("Minecraft server is not available!")).Send(message);
|
2022-01-16 22:29:50 +01:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
private int GetFreeId() {
|
|
|
|
|
var attempts = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
var id = _rnd.Next();
|
|
|
|
|
if (!_waits.ContainsKey(id))
|
|
|
|
|
return id;
|
|
|
|
|
Program.LogWarningAsync(Program.WebSocketSource, $"Could not get a free ID after {++attempts} attempts!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct) {
|
|
|
|
|
ChunkWaiter<T> waiter;
|
|
|
|
|
lock (_syncRoot) {
|
|
|
|
|
waiter = new ChunkWaiter<T>(GetFreeId(), resultParser, ct);
|
|
|
|
|
_waits.Add(waiter.ID, waiter);
|
|
|
|
|
}
|
|
|
|
|
return waiter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly ICommandHandler<ResponseType> _rs;
|
2022-01-17 19:10:14 +01:00
|
|
|
|
private readonly ICommandHandler<ResponseType> _pd;
|
|
|
|
|
|
2022-01-16 22:29:50 +01:00
|
|
|
|
[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);
|
2022-01-17 19:10:14 +01:00
|
|
|
|
[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);
|
2022-01-16 22:29:50 +01:00
|
|
|
|
|
|
|
|
|
public static Func<string, T> Deserialize<T>() => msg
|
|
|
|
|
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
|
|
|
|
|
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
|
|
|
|
|
=> throw new ReplyException($"What the fuck do you mean by '{method}'?");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface ITaskWaitSource {
|
|
|
|
|
ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct);
|
|
|
|
|
Task Send(Message requestMessage);
|
|
|
|
|
}
|