2022-01-16 22:29:50 +01:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Fleck;
|
|
|
|
|
using MinecraftDiscordBot.Commands;
|
|
|
|
|
using MinecraftDiscordBot.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
2022-01-18 14:44:31 +01:00
|
|
|
|
using System.Linq;
|
2022-01-16 22:29:50 +01:00
|
|
|
|
|
|
|
|
|
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-18 10:10:49 +01:00
|
|
|
|
RefinedStorage = new RefinedStorageService(this, roleManager);
|
|
|
|
|
Players = new PlayerDetectorService(this);
|
|
|
|
|
Chat = new ChatBoxService(this);
|
2022-01-17 19:10:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 14:44:31 +01:00
|
|
|
|
public static async Task<T> Method<T>(ITaskWaitSource taskSource, string methodName, Func<string, T> parser, CancellationToken ct, Dictionary<string, object>? parameters = null) {
|
2022-01-17 19:10:14 +01:00
|
|
|
|
var waiter = taskSource.GetWaiter(parser, ct);
|
|
|
|
|
await taskSource.Send(new RequestMessage(waiter.ID, methodName, parameters));
|
|
|
|
|
return await waiter.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event EventHandler<ChatEvent>? ChatMessageReceived;
|
2022-01-18 16:47:11 +01:00
|
|
|
|
public event EventHandler<PlayerStatusEvent>? PlayerStatusChanged;
|
2022-01-17 19:10:14 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 10:10:49 +01:00
|
|
|
|
public RefinedStorageService RefinedStorage { get; }
|
|
|
|
|
public PlayerDetectorService Players { get; }
|
|
|
|
|
public ChatBoxService Chat { get; }
|
|
|
|
|
|
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;
|
2022-01-18 16:47:11 +01:00
|
|
|
|
case PlayerStatusEvent msg:
|
|
|
|
|
PlayerStatusChanged?.Invoke(this, msg);
|
|
|
|
|
break;
|
2022-01-17 19:10:14 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 14:44:31 +01:00
|
|
|
|
public Task<Dictionary<string, Peripheral>> GetPeripherals(CancellationToken ct) => Method(this, "peripherals", Deserialize<Dictionary<string, Peripheral>>(), ct);
|
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)
|
2022-01-18 10:10:49 +01:00
|
|
|
|
=> RefinedStorage.HandleCommand(message, parameters, ct);
|
2022-01-18 14:44:31 +01:00
|
|
|
|
[CommandHandler("peripherals", HelpText = "Gets a list of peripherals that are attached.")]
|
|
|
|
|
public async Task<ResponseType> HandleGetPeripherals(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
|
|
|
=> ResponseType.AsString(string.Join("\n", (await GetPeripherals(ct)).Values.Select(i => $"On side {i.Side}: {i.Type}")));
|
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)
|
2022-01-18 10:10:49 +01:00
|
|
|
|
=> Players.HandleCommand(message, parameters, ct);
|
|
|
|
|
[CommandHandler("chat", HelpText = "Provides some commands for chatting.")]
|
|
|
|
|
public Task<ResponseType> ChatBoxHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
|
|
|
|
|
=> Chat.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);
|
|
|
|
|
}
|