~ruther/NosSmooth.Local

13d498f7ac7588f5f5d7455ca94d9f9ef7fc74e8 — Rutherther 3 years ago 20d87d0
feat(client): add attack command handler
A src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs => src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs +78 -0
@@ 0,0 1,78 @@
//
//  AttackCommandHandler.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.Core.Client;
using NosSmooth.Core.Commands;
using NosSmooth.Core.Commands.Attack;
using NosSmooth.Core.Commands.Control;
using NosSmooth.Core.Extensions;
using NosSmooth.LocalBinding.Objects;
using NosSmooth.LocalBinding.Structs;
using Remora.Results;

namespace NosSmooth.LocalClient.CommandHandlers.Attack;

/// <summary>
/// Handler of <see cref="AttackCommand"/>.
/// </summary>
public class AttackCommandHandler : ICommandHandler<AttackCommand>
{
    private readonly INostaleClient _nostaleClient;
    private readonly UnitManagerBinding _unitManagerBinding;
    private readonly SceneManager _sceneManager;

    /// <summary>
    /// Initializes a new instance of the <see cref="AttackCommandHandler"/> class.
    /// </summary>
    /// <param name="nostaleClient">The NosTale client.</param>
    /// <param name="unitManagerBinding">The unit manager binding.</param>
    /// <param name="sceneManager">The scene manager.</param>
    public AttackCommandHandler
        (INostaleClient nostaleClient, UnitManagerBinding unitManagerBinding, SceneManager sceneManager)
    {
        _nostaleClient = nostaleClient;
        _unitManagerBinding = unitManagerBinding;
        _sceneManager = sceneManager;
    }

    /// <inheritdoc />
    public async Task<Result> HandleCommand(AttackCommand command, CancellationToken ct = default)
    {
        if (command.TargetId is not null)
        {
            var entityResult = _sceneManager.FindEntity(command.TargetId.Value);
            if (entityResult.IsDefined(out var entity))
            {
                _unitManagerBinding.FocusEntity(entity);
            }
        }

        ControlCancelReason? reason = null;
        var takeControlCommand = command.CreateTakeControl
        (
            "Attack",
            command.HandleAttackCallback,
            (r) =>
            {
                reason = r;
                return Task.FromResult(Result.FromSuccess());
            }
        );

        var result = await _nostaleClient.SendCommandAsync(takeControlCommand, ct);
        if (!result.IsSuccess)
        {
            return result;
        }

        if (reason is not null)
        {
            return new GenericError($"The command could not finish, because {reason}");
        }

        return Result.FromSuccess();
    }
}
\ No newline at end of file

M src/Core/NosSmooth.LocalClient/Extensions/ServiceCollectionExtensions.cs => src/Core/NosSmooth.LocalClient/Extensions/ServiceCollectionExtensions.cs +2 -0
@@ 9,6 9,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using NosSmooth.Core.Client;
using NosSmooth.Core.Extensions;
using NosSmooth.LocalBinding.Extensions;
using NosSmooth.LocalClient.CommandHandlers.Attack;
using NosSmooth.LocalClient.CommandHandlers.Walk;

namespace NosSmooth.LocalClient.Extensions;


@@ 29,6 30,7 @@ public static class ServiceCollectionExtensions
        serviceCollection.AddNostaleBindings();
        serviceCollection
            .AddTakeControlCommand()
            .AddCommandHandler<AttackCommandHandler>()
            .AddCommandHandler<PlayerWalkCommandHandler>()
            .AddCommandHandler<PetWalkCommandHandler>();
        serviceCollection.TryAddSingleton<NostaleLocalClient>();

M src/Core/NosSmooth.LocalClient/NosSmooth.LocalClient.csproj => src/Core/NosSmooth.LocalClient/NosSmooth.LocalClient.csproj +1 -5
@@ 13,13 13,9 @@

    <ItemGroup>
      <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
      <PackageReference Include="NosSmooth.Core" Version="1.1.1" />
      <PackageReference Include="NosSmooth.Core" Version="1.2.1" />
      <PackageReference Include="Reloaded.Hooks" Version="3.5.1" />
      <PackageReference Include="Reloaded.Memory.Sigscan" Version="1.2.1" />
    </ItemGroup>

    <ItemGroup>
      <Folder Include="CommandHandlers\Attack" />
    </ItemGroup>

</Project>

Do not follow this link