~ruther/NosSmooth

ref: 213fe53ae3a31d69b20543002b447347cf7893c0 NosSmooth/Extensions/NosSmooth.Extensions.Pathfinding/Responders/PtctlResponder.cs -rw-r--r-- 1.4 KiB
213fe53a — Rutherther Merge pull request #81 from Rutherther/feat/pathfinding-mates 2 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
//
//  PtctlResponder.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.Security;
using NosSmooth.Core.Packets;
using NosSmooth.Packets.Client.Mates;
using Remora.Results;

namespace NosSmooth.Extensions.Pathfinding.Responders;

/// <inheritdoc />
public class PtctlResponder : IPacketResponder<PtctlPacket>
{
    private readonly PathfinderState _state;

    /// <summary>
    /// Initializes a new instance of the <see cref="PtctlResponder"/> class.
    /// </summary>
    /// <param name="state">The state.</param>
    public PtctlResponder(PathfinderState state)
    {
        _state = state;
    }

    /// <inheritdoc />
    public Task<Result> Respond(PacketEventArgs<PtctlPacket> packetArgs, CancellationToken ct = default)
    {
        var packet = packetArgs.Packet;

        foreach (var walkControl in packet.Controls)
        {
            if (!_state.Entities.TryGetValue(walkControl.MateTransportId, out var entityState))
            {
                _state.AddEntity(walkControl.MateTransportId, walkControl.PositionX, walkControl.PositionY);
                continue;
            }

            entityState.X = walkControl.PositionX;
            entityState.Y = walkControl.PositionY;
        }

        return Task.FromResult(Result.FromSuccess());
    }
}
Do not follow this link