~ruther/NosSmooth.Local

ref: aa8a49f50bf6de1863c905186e291eb47b1e4b6c NosSmooth.Local/src/Core/NosSmooth.LocalClient/CommandHandlers/Attack/AttackCommandHandler.cs -rw-r--r-- 2.5 KiB
aa8a49f5 — Rutherther feat(samples): add pathfinding support into walkcommands 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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();
    }
}
Do not follow this link