e7b056342f
Added computer event listeners
115 lines
5.0 KiB
C#
115 lines
5.0 KiB
C#
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 {
|
|
protected IWebSocketConnection? _socketField;
|
|
public override string HelpTextPrefix => "!";
|
|
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)) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
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();
|
|
|
|
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;
|
|
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!");
|
|
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);
|
|
}
|