From 9a36c429b33712d3281f154059c0f6908594662d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 20 Jan 2022 19:26:58 +0100 Subject: [PATCH] feat(sample): use chat commands in WalkCommands sample --- Samples/WalkCommands/ChatPacketInterceptor.cs | 112 ------------------ .../WalkCommands/Commands/CombatCommands.cs | 45 +++++++ .../WalkCommands/Commands/DetachCommand.cs | 15 +-- Samples/WalkCommands/Commands/WalkCommands.cs | 26 ++-- Samples/WalkCommands/Startup.cs | 28 +++-- Samples/WalkCommands/WalkCommands.csproj | 5 +- 6 files changed, 92 insertions(+), 139 deletions(-) delete mode 100644 Samples/WalkCommands/ChatPacketInterceptor.cs create mode 100644 Samples/WalkCommands/Commands/CombatCommands.cs diff --git a/Samples/WalkCommands/ChatPacketInterceptor.cs b/Samples/WalkCommands/ChatPacketInterceptor.cs deleted file mode 100644 index ee65654..0000000 --- a/Samples/WalkCommands/ChatPacketInterceptor.cs +++ /dev/null @@ -1,112 +0,0 @@ -// -// ChatPacketInterceptor.cs -// -// Copyright (c) František Boháček. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using NosSmooth.Core.Client; -using NosSmooth.Core.Extensions; -using NosSmooth.LocalClient; -using NosSmooth.Packets.Enums; -using NosSmooth.Packets.Enums.Chat; -using NosSmooth.Packets.Server.Chat; -using Remora.Results; -using WalkCommands.Commands; - -namespace WalkCommands; - -/// -/// Interceptor of chat commands. -/// -public class ChatPacketInterceptor : IPacketInterceptor -{ - private readonly IServiceProvider _provider; - private readonly ILogger _logger; - private readonly INostaleClient _client; - - /// - /// Initializes a new instance of the class. - /// - /// The service provider. - /// The logger. - /// The nostale client. - public ChatPacketInterceptor - (IServiceProvider provider, ILogger logger, INostaleClient client) - { - _provider = provider; - _logger = logger; - _client = client; - } - - /// - public bool InterceptSend(ref string packet) - { - if (packet.StartsWith($"say #")) - { - var packetString = packet; - Task.Run - ( - async () => - { - try - { - await ExecuteCommand(packetString.Substring(5)); - } - catch (Exception ex) - { - _logger.LogError(ex, "Could not execute command."); - } - } - ); - return false; - } - - return true; - } - - /// - public bool InterceptReceive(ref string packet) - { - return true; - } - - private async Task ExecuteCommand(string command) - { - await _client.ReceivePacketAsync - (new SayPacket(EntityType.Map, 1, SayColor.Green, $"Handling a command {command}.")); - - var splitted = command.Split(new[] { ' ' }); - using var scope = _provider.CreateScope(); - Result result; - switch (splitted[0]) - { - case "walk": - var walkCommand = scope.ServiceProvider.GetRequiredService(); - result = await walkCommand.HandleWalkToAsync - ( - ushort.Parse(splitted[1]), - ushort.Parse(splitted[2]), - splitted.Length > 3 ? bool.Parse(splitted[3]) : true - ); - break; - case "detach": - var detachCommand = scope.ServiceProvider.GetRequiredService(); - result = await detachCommand.HandleDetach(); - break; - default: - await _client.ReceivePacketAsync - ( - new SayPacket(EntityType.Map, 1, SayColor.Green, $"The command {splitted[0]} was not found.") - ); - return; - } - - if (!result.IsSuccess) - { - _logger.LogError("Could not execute a command"); - _logger.LogResultError(result); - } - } -} diff --git a/Samples/WalkCommands/Commands/CombatCommands.cs b/Samples/WalkCommands/Commands/CombatCommands.cs new file mode 100644 index 0000000..1b29202 --- /dev/null +++ b/Samples/WalkCommands/Commands/CombatCommands.cs @@ -0,0 +1,45 @@ +// +// CombatCommands.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.ChatCommands; +using NosSmooth.Core.Client; +using NosSmooth.LocalBinding.Objects; +using Remora.Commands.Attributes; +using Remora.Commands.Groups; +using Remora.Results; + +namespace WalkCommands.Commands; + +/// +/// Represents command group for combat commands. +/// +public class CombatCommands : CommandGroup +{ + private readonly SceneManagerBinding _sceneManagerBinding; + private readonly FeedbackService _feedbackService; + + /// + /// Initializes a new instance of the class. + /// + /// The scene manager binding. + /// The feedback service. + public CombatCommands(SceneManagerBinding sceneManagerBinding, FeedbackService feedbackService) + { + _sceneManagerBinding = sceneManagerBinding; + _feedbackService = feedbackService; + } + + /// + /// Focus the given entity. + /// + /// The entity id to focus. + /// A task that may or may not have succeeded. + [Command("focus")] + public Task HandleFocusAsync(int entityId) + { + return Task.FromResult(_sceneManagerBinding.FocusEntity(entityId)); + } +} \ No newline at end of file diff --git a/Samples/WalkCommands/Commands/DetachCommand.cs b/Samples/WalkCommands/Commands/DetachCommand.cs index fc32178..e423b0b 100644 --- a/Samples/WalkCommands/Commands/DetachCommand.cs +++ b/Samples/WalkCommands/Commands/DetachCommand.cs @@ -4,10 +4,12 @@ // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using NosSmooth.ChatCommands; using NosSmooth.Core.Client; using NosSmooth.Packets.Enums; using NosSmooth.Packets.Enums.Chat; using NosSmooth.Packets.Server.Chat; +using Remora.Commands.Groups; using Remora.Results; namespace WalkCommands.Commands; @@ -15,20 +17,20 @@ namespace WalkCommands.Commands; /// /// Group for detaching command that detaches the dll. /// -public class DetachCommand +public class DetachCommand : CommandGroup { private readonly CancellationTokenSource _dllStop; - private readonly INostaleClient _client; + private readonly FeedbackService _feedbackService; /// /// Initializes a new instance of the class. /// /// The cancellation token source to stop the client. - /// The nostale client. - public DetachCommand(CancellationTokenSource dllStop, INostaleClient client) + /// The feedback service. + public DetachCommand(CancellationTokenSource dllStop, FeedbackService feedbackService) { _dllStop = dllStop; - _client = client; + _feedbackService = feedbackService; } /// @@ -37,8 +39,7 @@ public class DetachCommand /// A result that may or may not have succeeded. public async Task HandleDetach() { - var receiveResult = await _client.ReceivePacketAsync - (new SayPacket(EntityType.Map, 1, SayColor.Green, "Going to detach!")); + var receiveResult = await _feedbackService.SendInfoMessageAsync("Going to detach!", CancellationToken); if (!receiveResult.IsSuccess) { diff --git a/Samples/WalkCommands/Commands/WalkCommands.cs b/Samples/WalkCommands/Commands/WalkCommands.cs index 09ce13b..289eb7d 100644 --- a/Samples/WalkCommands/Commands/WalkCommands.cs +++ b/Samples/WalkCommands/Commands/WalkCommands.cs @@ -4,11 +4,14 @@ // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using NosSmooth.ChatCommands; using NosSmooth.Core.Client; using NosSmooth.Core.Commands; using NosSmooth.Packets.Enums; using NosSmooth.Packets.Enums.Chat; using NosSmooth.Packets.Server.Chat; +using Remora.Commands.Attributes; +using Remora.Commands.Groups; using Remora.Results; namespace WalkCommands.Commands; @@ -16,17 +19,20 @@ namespace WalkCommands.Commands; /// /// Represents command group for walking. /// -public class WalkCommands +public class WalkCommands : CommandGroup { private readonly INostaleClient _client; + private readonly FeedbackService _feedbackService; /// /// Initializes a new instance of the class. /// /// The nostale client. - public WalkCommands(INostaleClient client) + /// The feedback service. + public WalkCommands(INostaleClient client, FeedbackService feedbackService) { - _client = client ?? throw new ArgumentNullException(nameof(client)); + _client = client; + _feedbackService = feedbackService; } /// @@ -35,20 +41,19 @@ public class WalkCommands /// The x coordinate. /// The y coordinate. /// Whether the user can cancel the operation. - /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. + [Command("walk")] public async Task HandleWalkToAsync ( ushort x, ushort y, - bool isCancellable = true, - CancellationToken ct = default + bool isCancellable = true ) { var receiveResult = await _client.ReceivePacketAsync ( new SayPacket(EntityType.Map, 1, SayColor.Red, $"Going to walk to {x} {y}."), - ct + CancellationToken ); if (!receiveResult.IsSuccess) @@ -57,13 +62,14 @@ public class WalkCommands } var command = new WalkCommand(x, y, isCancellable); - var walkResult = await _client.SendCommandAsync(command, ct); + var walkResult = await _client.SendCommandAsync(command, CancellationToken); if (!walkResult.IsSuccess) { + await _feedbackService.SendErrorMessageAsync($"Could not finish walking. {walkResult.Error.Message}", CancellationToken); await _client.ReceivePacketAsync ( new SayPacket(EntityType.Map, 1, SayColor.Red, "Could not finish walking."), - ct + CancellationToken ); return walkResult; } @@ -71,7 +77,7 @@ public class WalkCommands return await _client.ReceivePacketAsync ( new SayPacket(EntityType.Map, 1, SayColor.Red, "Walk has finished successfully."), - ct + CancellationToken ); } } diff --git a/Samples/WalkCommands/Startup.cs b/Samples/WalkCommands/Startup.cs index 5175a91..bfbfd87 100644 --- a/Samples/WalkCommands/Startup.cs +++ b/Samples/WalkCommands/Startup.cs @@ -6,10 +6,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using NosSmooth.ChatCommands; using NosSmooth.Core.Client; using NosSmooth.LocalBinding; using NosSmooth.LocalClient; using NosSmooth.LocalClient.Extensions; +using Remora.Commands.Extensions; using WalkCommands.Commands; namespace WalkCommands; @@ -21,20 +23,28 @@ public class Startup { private IServiceProvider BuildServices() { - return new ServiceCollection() + var collection = new ServiceCollection() .AddLocalClient() .AddScoped() .AddScoped() .AddSingleton() .Configure(o => o.AllowIntercept = true) - .AddSingleton() - .AddLogging(b => - { - b.ClearProviders(); - b.AddConsole(); - b.SetMinimumLevel(LogLevel.Debug); - }) - .BuildServiceProvider(); + .AddNostaleChatCommands() + .AddLogging + ( + b => + { + b.ClearProviders(); + b.AddConsole(); + b.SetMinimumLevel(LogLevel.Debug); + } + ); + + collection.AddCommandTree() + .WithCommandGroup() + .WithCommandGroup() + .WithCommandGroup(); + return collection.BuildServiceProvider(); } /// diff --git a/Samples/WalkCommands/WalkCommands.csproj b/Samples/WalkCommands/WalkCommands.csproj index bdffb9c..dfa20e4 100644 --- a/Samples/WalkCommands/WalkCommands.csproj +++ b/Samples/WalkCommands/WalkCommands.csproj @@ -7,7 +7,9 @@ WalkCommands 10 true - true + true + true + PackageReference @@ -34,6 +36,7 @@ + \ No newline at end of file -- 2.49.0