~ruther/NosSmooth

ref: 3c62fd452466a4f3f7bc2b8a1a1a8aebad5eb870 NosSmooth/Core/NosSmooth.Core/Commands/Walking/WalkCommandHandler.cs -rw-r--r-- 2.9 KiB
3c62fd45 — František Boháček chore: update namespaces 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
//  WalkCommandHandler.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 System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NosSmooth.Core.Client;
using NosSmooth.Core.Extensions;
using Remora.Results;

namespace NosSmooth.Core.Commands.Walking;

/// <summary>
/// Handles <see cref="WalkCommand"/>.
/// </summary>
public class WalkCommandHandler : ICommandHandler<WalkCommand>
{
    private readonly INostaleClient _nostaleClient;

    /// <summary>
    /// Initializes a new instance of the <see cref="WalkCommandHandler"/> class.
    /// </summary>
    /// <param name="nostaleClient">The NosTale client.</param>
    public WalkCommandHandler(INostaleClient nostaleClient)
    {
        _nostaleClient = nostaleClient;
    }

    /// <inheritdoc />
    public async Task<Result> HandleCommand(WalkCommand command, CancellationToken ct = default)
    {
        var tasks = new List<Task<Result>>();
        for (var i = 0; i < command.PetSelectors.Length; i++)
        {
            short xOffset = (short)(-1 + (i % 3));
            short yOffset = (short)(-1 + ((i / 3) % 5));
            if (xOffset == 0 && yOffset == 0)
            {
                yOffset += 2;
            }

            int x = command.TargetX;
            int y = command.TargetY;

            if (x + xOffset > 0)
            {
                x += xOffset;
            }
            if (y + yOffset > 0)
            {
                y += yOffset;
            }

            tasks.Add
            (
                _nostaleClient.SendCommandAsync
                (
                    new PetWalkCommand
                    (
                        command.PetSelectors[i],
                        (ushort)x,
                        (ushort)y,
                        command.CanBeCancelledByAnother,
                        command.WaitForCancellation,
                        command.AllowUserCancel
                    ),
                    ct
                )
            );
        }

        tasks.Add
        (
            _nostaleClient.SendCommandAsync
            (
                new PlayerWalkCommand
                (
                    command.TargetX,
                    command.TargetY,
                    command.CanBeCancelledByAnother,
                    command.WaitForCancellation,
                    command.AllowUserCancel
                ),
                ct
            )
        );

        var results = await Task.WhenAll(tasks);
        var errorResults = results.Where(x => !x.IsSuccess).ToArray();

        return errorResults.Length switch
        {
            0 => Result.FromSuccess(),
            1 => errorResults[0],
            _ => new AggregateError(errorResults.Cast<IResult>().ToArray())
        };
    }
}
Do not follow this link