Cleanup, ordering and added host variable to client script
This commit is contained in:
parent
612435eb09
commit
fd3e6fdcc8
@ -20,6 +20,9 @@ public class BotConfiguration : IBotConfiguration, IBotConfigurator {
|
||||
[JsonProperty("prefix", Required = Required.DisallowNull)]
|
||||
[Option("prefix", Default = DEFAULT_PREFIX, HelpText = "The Discord bot command prefix")]
|
||||
public string Prefix { get; init; } = DEFAULT_PREFIX;
|
||||
[JsonProperty("host", Required = Required.Always)]
|
||||
[Option("host", Default = DEFAULT_PREFIX, HelpText = "The Discord bot command prefix", Required = true)]
|
||||
public string SocketHost { get; init; } = default!;
|
||||
[JsonIgnore]
|
||||
public BotConfiguration Config => this;
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
namespace MinecraftDiscordBot;
|
||||
using MinecraftDiscordBot.Services;
|
||||
|
||||
namespace MinecraftDiscordBot;
|
||||
|
||||
public class ChunkWaiter<T> : IChunkWaiter {
|
||||
public int ID { get; }
|
||||
|
@ -1,5 +1,5 @@
|
||||
local secretToken = "$TOKEN"
|
||||
local connectionUri = "ws://ws.cnml.de:8081"
|
||||
local connectionUri = "$HOST"
|
||||
local waitSeconds = 5
|
||||
|
||||
local function chunkString(value, chunkSize)
|
||||
|
8
MinecraftDiscordBot/Commands/CommandHandlerAttribute.cs
Normal file
8
MinecraftDiscordBot/Commands/CommandHandlerAttribute.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace MinecraftDiscordBot.Commands;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
|
||||
public sealed class CommandHandlerAttribute : Attribute {
|
||||
public CommandHandlerAttribute(string commandName) => CommandName = commandName;
|
||||
public string CommandName { get; }
|
||||
public string? HelpText { get; init; }
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Discord.WebSocket;
|
||||
using MinecraftDiscordBot.Services;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace MinecraftDiscordBot;
|
||||
namespace MinecraftDiscordBot.Commands;
|
||||
|
||||
public abstract class CommandRouter : ICommandHandler<ResponseType> {
|
||||
public record struct HandlerStruct(HandleCommandDelegate<ResponseType> Delegate, CommandHandlerAttribute Attribute);
|
||||
private readonly Dictionary<string, HandlerStruct> _handlers = new();
|
||||
public abstract string HelpTextPrefix { get; }
|
||||
public CommandRouter() {
|
||||
@ -43,5 +44,3 @@ public abstract class CommandRouter : ICommandHandler<ResponseType> {
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public record struct HandlerStruct(HandleCommandDelegate<ResponseType> Delegate, CommandHandlerAttribute Attribute);
|
@ -1,6 +1,6 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace MinecraftDiscordBot;
|
||||
namespace MinecraftDiscordBot.Commands;
|
||||
|
||||
public interface ICommandHandler {
|
||||
Task HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
@ -1,179 +0,0 @@
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Fleck;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace MinecraftDiscordBot;
|
||||
|
||||
public delegate Task<TResponse> HandleCommandDelegate<TResponse>(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
||||
public delegate Task HandleCommandDelegate(SocketUserMessage message, string[] parameters, CancellationToken ct);
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
|
||||
public sealed class CommandHandlerAttribute : Attribute {
|
||||
public CommandHandlerAttribute(string commandName) => CommandName = commandName;
|
||||
public string CommandName { get; }
|
||||
public string? HelpText { get; init; }
|
||||
}
|
||||
|
||||
public class ConnectedComputer : CommandRouter, ITaskWaitSource {
|
||||
protected readonly IWebSocketConnection _socket;
|
||||
public override string HelpTextPrefix => "!";
|
||||
public ConnectedComputer(IWebSocketConnection socket) : base() {
|
||||
socket.OnMessage = OnMessage;
|
||||
_socket = socket;
|
||||
_rs = new RefinedStorageService(this);
|
||||
}
|
||||
|
||||
private void OnMessage(string message) {
|
||||
if (JsonConvert.DeserializeObject<ReplyMessage>(message) is not ReplyMessage msg) return;
|
||||
IChunkWaiter? waiter;
|
||||
lock (_syncRoot) {
|
||||
if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
|
||||
Program.LogWarningAsync("Socket", $"Invalid wait id '{msg.AnswerId}'!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!msg.Success) waiter.SetUnsuccessful();
|
||||
waiter.AddChunk(msg.Chunk, msg.Total, msg.Result);
|
||||
if (waiter.Finished || waiter.IsCancellationRequested)
|
||||
lock (_syncRoot)
|
||||
_waits.Remove(waiter.ID);
|
||||
}
|
||||
|
||||
public Task Send(string message) => _socket.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;
|
||||
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;
|
||||
[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);
|
||||
|
||||
public static Func<string, T> Deserialize<T>() => msg
|
||||
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
|
||||
public override Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct)
|
||||
=> Task.FromResult(ResponseType.AsString("The Minecraft server is connected!"));
|
||||
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);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class ReplyException : Exception {
|
||||
public ReplyException() { }
|
||||
public ReplyException(string message) : base(message) { }
|
||||
public ReplyException(string message, Exception inner) : base(message, inner) { }
|
||||
protected ReplyException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes an item in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Item : Fluid {
|
||||
[JsonProperty("fingerprint", Required = Required.Always)]
|
||||
public Md5Hash Fingerprint { get; set; } = default!;
|
||||
[JsonProperty("nbt", Required = Required.DisallowNull)]
|
||||
public dynamic? NBT { get; set; }
|
||||
public override string ToString() => $"{Amount:n0}x {DisplayName}";
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes a fluid in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Fluid {
|
||||
[JsonProperty("amount", Required = Required.Always)]
|
||||
public int Amount { get; set; }
|
||||
[JsonProperty("displayName", Required = Required.Always)]
|
||||
public string DisplayName { get; set; } = default!;
|
||||
[JsonProperty("tags", Required = Required.DisallowNull)]
|
||||
public string[]? Tags { get; set; } = default;
|
||||
[JsonProperty("name", Required = Required.Always)]
|
||||
public ModItemId ItemId { get; set; } = default!;
|
||||
public override string ToString() => Amount > 10000
|
||||
? $"{Amount / 1000.0f:n2} B of {DisplayName}"
|
||||
: $"{Amount:n0} mB of {DisplayName}";
|
||||
[JsonIgnore]
|
||||
public string CleanDisplayName => DisplayName[1..^2];
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(ModItemIdJsonConverter))]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class ModItemId {
|
||||
public ModItemId(string name) {
|
||||
var colon = name.IndexOf(':');
|
||||
if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name));
|
||||
ModName = name[..colon];
|
||||
ModItem = name[(colon + 1)..];
|
||||
if (ToString() != name) throw new InvalidProgramException("Bad Parsing!");
|
||||
}
|
||||
public override string ToString() => $"{ModName}:{ModItem}";
|
||||
public string ModName { get; }
|
||||
public string ModItem { get; }
|
||||
|
||||
public class ModItemIdJsonConverter : JsonConverter<ModItemId> {
|
||||
public override ModItemId? ReadJson(JsonReader reader, Type objectType, ModItemId? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
=> reader.Value is string value
|
||||
? new(value)
|
||||
: throw new JsonException($"Could not parse mod name with token '{reader.Value}'");
|
||||
public override void WriteJson(JsonWriter writer, ModItemId? value, JsonSerializer serializer) {
|
||||
if (value is null) writer.WriteNull();
|
||||
else writer.WriteValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(Md5JsonConverter))]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Md5Hash : IEquatable<Md5Hash?> {
|
||||
private readonly byte[] _hash;
|
||||
public Md5Hash(string hash) : this(Convert.FromHexString(hash)) { }
|
||||
public Md5Hash(byte[] hash) {
|
||||
if (hash is not { Length: 16 }) throw new ArgumentException("Invalid digest size!", nameof(hash));
|
||||
_hash = hash;
|
||||
}
|
||||
public override bool Equals(object? obj) => Equals(obj as Md5Hash);
|
||||
public bool Equals(Md5Hash? other) => other != null && _hash.SequenceEqual(other._hash);
|
||||
public override int GetHashCode() {
|
||||
var hashCode = new HashCode();
|
||||
hashCode.AddBytes(_hash);
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
public override string ToString() => Convert.ToHexString(_hash);
|
||||
|
||||
public class Md5JsonConverter : JsonConverter<Md5Hash> {
|
||||
public override Md5Hash? ReadJson(JsonReader reader, Type objectType, Md5Hash? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
=> reader.Value is string { Length: 32 } value
|
||||
? new(value)
|
||||
: throw new JsonException($"Could not parse MD5 hash with token '{reader.Value}'");
|
||||
public override void WriteJson(JsonWriter writer, Md5Hash? value, JsonSerializer serializer) {
|
||||
if (value is null) writer.WriteNull();
|
||||
else writer.WriteValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
namespace MinecraftDiscordBot;
|
||||
using MinecraftDiscordBot.Models;
|
||||
|
||||
namespace MinecraftDiscordBot;
|
||||
|
||||
public abstract class ItemFilter {
|
||||
public abstract bool Match(Fluid item);
|
||||
|
22
MinecraftDiscordBot/Models/Fluid.cs
Normal file
22
MinecraftDiscordBot/Models/Fluid.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes a fluid in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Fluid {
|
||||
[JsonProperty("amount", Required = Required.Always)]
|
||||
public int Amount { get; set; }
|
||||
[JsonProperty("displayName", Required = Required.Always)]
|
||||
public string DisplayName { get; set; } = default!;
|
||||
[JsonProperty("tags", Required = Required.DisallowNull)]
|
||||
public string[]? Tags { get; set; } = default;
|
||||
[JsonProperty("name", Required = Required.Always)]
|
||||
public ModItemId ItemId { get; set; } = default!;
|
||||
public override string ToString() => Amount > 10000
|
||||
? $"{Amount / 1000.0f:n2} B of {DisplayName}"
|
||||
: $"{Amount:n0} mB of {DisplayName}";
|
||||
[JsonIgnore]
|
||||
public string CleanDisplayName => DisplayName[1..^1];
|
||||
}
|
14
MinecraftDiscordBot/Models/Item.cs
Normal file
14
MinecraftDiscordBot/Models/Item.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes an item in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Item : Fluid {
|
||||
[JsonProperty("fingerprint", Required = Required.Always)]
|
||||
public Md5Hash Fingerprint { get; set; } = default!;
|
||||
[JsonProperty("nbt", Required = Required.DisallowNull)]
|
||||
public dynamic? NBT { get; set; }
|
||||
public override string ToString() => $"{Amount:n0}x {DisplayName}";
|
||||
}
|
34
MinecraftDiscordBot/Models/Md5Hash.cs
Normal file
34
MinecraftDiscordBot/Models/Md5Hash.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonConverter(typeof(Md5JsonConverter))]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Md5Hash : IEquatable<Md5Hash?> {
|
||||
private readonly byte[] _hash;
|
||||
public Md5Hash(string hash) : this(Convert.FromHexString(hash)) { }
|
||||
public Md5Hash(byte[] hash) {
|
||||
if (hash is not { Length: 16 }) throw new ArgumentException("Invalid digest size!", nameof(hash));
|
||||
_hash = hash;
|
||||
}
|
||||
public override bool Equals(object? obj) => Equals(obj as Md5Hash);
|
||||
public bool Equals(Md5Hash? other) => other != null && _hash.SequenceEqual(other._hash);
|
||||
public override int GetHashCode() {
|
||||
var hashCode = new HashCode();
|
||||
hashCode.AddBytes(_hash);
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
public override string ToString() => Convert.ToHexString(_hash);
|
||||
|
||||
public class Md5JsonConverter : JsonConverter<Md5Hash> {
|
||||
public override Md5Hash? ReadJson(JsonReader reader, Type objectType, Md5Hash? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
=> reader.Value is string { Length: 32 } value
|
||||
? new(value)
|
||||
: throw new JsonException($"Could not parse MD5 hash with token '{reader.Value}'");
|
||||
public override void WriteJson(JsonWriter writer, Md5Hash? value, JsonSerializer serializer) {
|
||||
if (value is null) writer.WriteNull();
|
||||
else writer.WriteValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
using Discord.WebSocket;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MinecraftDiscordBot;
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
public abstract class Message {
|
||||
[JsonProperty("type")]
|
30
MinecraftDiscordBot/Models/ModItemId.cs
Normal file
30
MinecraftDiscordBot/Models/ModItemId.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonConverter(typeof(ModItemIdJsonConverter))]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class ModItemId {
|
||||
public ModItemId(string name) {
|
||||
var colon = name.IndexOf(':');
|
||||
if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name));
|
||||
ModName = name[..colon];
|
||||
ModItem = name[(colon + 1)..];
|
||||
if (ToString() != name) throw new InvalidProgramException("Bad Parsing!");
|
||||
}
|
||||
public override string ToString() => $"{ModName}:{ModItem}";
|
||||
public string ModName { get; }
|
||||
public string ModItem { get; }
|
||||
|
||||
public class ModItemIdJsonConverter : JsonConverter<ModItemId> {
|
||||
public override ModItemId? ReadJson(JsonReader reader, Type objectType, ModItemId? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
=> reader.Value is string value
|
||||
? new(value)
|
||||
: throw new JsonException($"Could not parse mod name with token '{reader.Value}'");
|
||||
public override void WriteJson(JsonWriter writer, ModItemId? value, JsonSerializer serializer) {
|
||||
if (value is null) writer.WriteNull();
|
||||
else writer.WriteValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,8 @@ using Discord.Commands;
|
||||
using Discord.Rest;
|
||||
using Discord.WebSocket;
|
||||
using Fleck;
|
||||
using MinecraftDiscordBot.Commands;
|
||||
using MinecraftDiscordBot.Services;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
@ -22,26 +24,29 @@ public class Program : IDisposable, ICommandHandler<ResponseType> {
|
||||
private readonly WebSocketServer _wssv;
|
||||
private readonly BotConfiguration _config;
|
||||
private readonly HashSet<ulong> _whitelistedChannels;
|
||||
private readonly ConcurrentDictionary<Guid, ConnectedComputer> _connections = new();
|
||||
private readonly ConcurrentDictionary<Guid, RootCommandService> _connections = new();
|
||||
private static readonly char[] WhiteSpace = new char[] { '\t', '\n', ' ', '\r' };
|
||||
public ITextChannel[] _channels = Array.Empty<ITextChannel>();
|
||||
private ConnectedComputer? _rsSystem = null;
|
||||
private RootCommandService? _rsSystem = null;
|
||||
private bool disposedValue;
|
||||
public static bool OnlineNotifications => false;
|
||||
public static readonly string ClientScript = GetClientScript();
|
||||
private const string ClientScriptName = "MinecraftDiscordBot.ClientScript.lua";
|
||||
public readonly string ClientScript;
|
||||
private readonly ITokenProvider _tokenProvider = new TimeoutTokenProvider(InstanceId, 10);
|
||||
private static readonly int InstanceId = new Random().Next();
|
||||
|
||||
private string GetVerifiedClientScript() => ClientScript.Replace("$TOKEN", _tokenProvider.GenerateToken());
|
||||
private string GetVerifiedClientScript() => ClientScript
|
||||
.Replace("$TOKEN", _tokenProvider.GenerateToken());
|
||||
|
||||
private static string GetClientScript() {
|
||||
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MinecraftDiscordBot.ClientScript.lua");
|
||||
private string GetClientScript(BotConfiguration config) {
|
||||
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ClientScriptName);
|
||||
if (stream is null) throw new FileNotFoundException("Client script could not be loaded!");
|
||||
using var sr = new StreamReader(stream);
|
||||
return sr.ReadToEnd();
|
||||
return sr.ReadToEnd()
|
||||
.Replace("$HOST", $"ws://{config.SocketHost}:{config.Port}");
|
||||
}
|
||||
|
||||
public ConnectedComputer? Computer {
|
||||
public RootCommandService? Computer {
|
||||
get => _rsSystem; set {
|
||||
if (_rsSystem != value) {
|
||||
_rsSystem = value;
|
||||
@ -56,6 +61,7 @@ public class Program : IDisposable, ICommandHandler<ResponseType> {
|
||||
private async Task Broadcast(Func<ITextChannel, Task<IUserMessage>> message) => _ = await Task.WhenAll(_channels.Select(message));
|
||||
public Program(BotConfiguration config) {
|
||||
_config = config;
|
||||
ClientScript = GetClientScript(config);
|
||||
_client.Log += LogAsync;
|
||||
_client.MessageReceived += (msg) => DiscordMessageReceived(msg);
|
||||
_client.ReactionAdded += DiscordReactionAdded;
|
||||
@ -159,7 +165,7 @@ public class Program : IDisposable, ICommandHandler<ResponseType> {
|
||||
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Script sent to client!");
|
||||
}
|
||||
|
||||
private void AddComputerSocket(IWebSocketConnection socket, ConnectedComputer pc) => Computer = pc;
|
||||
private void AddComputerSocket(IWebSocketConnection socket, RootCommandService pc) => Computer = pc;
|
||||
|
||||
private void RemoveComputerSocket(IWebSocketConnection socket) {
|
||||
if (Computer is { ConnectionInfo.Id: Guid id } && id == socket.ConnectionInfo.Id) Computer = null;
|
||||
|
@ -1,7 +1,9 @@
|
||||
using Discord.WebSocket;
|
||||
using MinecraftDiscordBot.Commands;
|
||||
using MinecraftDiscordBot.Models;
|
||||
using System.Text;
|
||||
|
||||
namespace MinecraftDiscordBot;
|
||||
namespace MinecraftDiscordBot.Services;
|
||||
|
||||
public class RefinedStorageService : CommandRouter {
|
||||
private readonly ITaskWaitSource _taskSource;
|
||||
@ -28,12 +30,12 @@ public class RefinedStorageService : CommandRouter {
|
||||
|
||||
public async Task<int> GetEnergyUsageAsync(CancellationToken ct) => await Method(CmdEnergyUsage, int.Parse, ct);
|
||||
public async Task<int> GetEnergyStorageAsync(CancellationToken ct) => await Method(CmdEnergyStorage, int.Parse, ct);
|
||||
public async Task<IEnumerable<Item>> ListItemsAsync(CancellationToken ct) => await Method(CmdListItems, ConnectedComputer.Deserialize<IEnumerable<Item>>(), ct);
|
||||
public async Task<IEnumerable<Fluid>> ListFluidsAsync(CancellationToken ct) => await Method(CmdListFluids, ConnectedComputer.Deserialize<IEnumerable<Fluid>>(), ct);
|
||||
public async Task<Item> GetItemData(string itemid, CancellationToken ct) => await Method(CmdGetItem, ConnectedComputer.Deserialize<Item>(), ct, new() {
|
||||
public async Task<IEnumerable<Item>> ListItemsAsync(CancellationToken ct) => await Method(CmdListItems, RootCommandService.Deserialize<IEnumerable<Item>>(), ct);
|
||||
public async Task<IEnumerable<Fluid>> ListFluidsAsync(CancellationToken ct) => await Method(CmdListFluids, RootCommandService.Deserialize<IEnumerable<Fluid>>(), ct);
|
||||
public async Task<Item> GetItemData(string itemid, CancellationToken ct) => await Method(CmdGetItem, RootCommandService.Deserialize<Item>(), ct, new() {
|
||||
["name"] = itemid
|
||||
});
|
||||
public async Task<bool> CraftItem(string itemid, int amount, CancellationToken ct) => await Method(CmdCraftItem, ConnectedComputer.Deserialize<bool>(), ct, new() {
|
||||
public async Task<bool> CraftItem(string itemid, int amount, CancellationToken ct) => await Method(CmdCraftItem, RootCommandService.Deserialize<bool>(), ct, new() {
|
||||
["name"] = itemid,
|
||||
["count"] = amount
|
||||
});
|
||||
@ -138,7 +140,7 @@ public class RefinedStorageService : CommandRouter {
|
||||
sb.Append("The Refined Storage system currently stores these items:");
|
||||
var items = await RefreshItemList(ct);
|
||||
lock (_itemLock) {
|
||||
int taken = 0;
|
||||
var taken = 0;
|
||||
foreach (var item in items) {
|
||||
if (sb.Length > 500) break;
|
||||
sb.AppendFormat("\n{0:n0}x {1}", item.Amount, item.DisplayName);
|
11
MinecraftDiscordBot/Services/ReplyException.cs
Normal file
11
MinecraftDiscordBot/Services/ReplyException.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace MinecraftDiscordBot.Services;
|
||||
|
||||
[Serializable]
|
||||
public class ReplyException : Exception {
|
||||
public ReplyException() { }
|
||||
public ReplyException(string message) : base(message) { }
|
||||
public ReplyException(string message, Exception inner) : base(message, inner) { }
|
||||
protected ReplyException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
77
MinecraftDiscordBot/Services/RootCommandService.cs
Normal file
77
MinecraftDiscordBot/Services/RootCommandService.cs
Normal file
@ -0,0 +1,77 @@
|
||||
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 readonly IWebSocketConnection _socket;
|
||||
public override string HelpTextPrefix => "!";
|
||||
public RootCommandService(IWebSocketConnection socket) : base() {
|
||||
socket.OnMessage = OnMessage;
|
||||
_socket = socket;
|
||||
_rs = new RefinedStorageService(this);
|
||||
}
|
||||
|
||||
private void OnMessage(string message) {
|
||||
if (JsonConvert.DeserializeObject<ReplyMessage>(message) is not ReplyMessage msg) return;
|
||||
IChunkWaiter? waiter;
|
||||
lock (_syncRoot) if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
|
||||
Program.LogWarningAsync("Socket", $"Invalid wait id '{msg.AnswerId}'!");
|
||||
return;
|
||||
}
|
||||
if (!msg.Success) waiter.SetUnsuccessful();
|
||||
waiter.AddChunk(msg.Chunk, msg.Total, msg.Result);
|
||||
if (waiter.Finished || waiter.IsCancellationRequested)
|
||||
lock (_syncRoot)
|
||||
_waits.Remove(waiter.ID);
|
||||
}
|
||||
|
||||
public Task Send(string message) => _socket.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;
|
||||
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;
|
||||
[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);
|
||||
|
||||
public static Func<string, T> Deserialize<T>() => msg
|
||||
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
|
||||
public override Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct)
|
||||
=> Task.FromResult(ResponseType.AsString("The Minecraft server is connected!"));
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user