~ruther/NosSmooth

ref: a90b9c10b4731ad72d35f1b012c52db37c3e3a94 NosSmooth/Local/NosSmooth.LocalClient/CommandHandlers/Walk/WalkPacketResponder.cs -rw-r--r-- 1.9 KiB
a90b9c10 — František Boháček feat: implement walk command handler 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
//
//  WalkPacketResponder.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 NosCore.Packets.ClientPackets.Battle;
using NosCore.Packets.ClientPackets.Movement;
using NosCore.Packets.ServerPackets.MiniMap;
using NosSmooth.Core.Commands;
using NosSmooth.Core.Packets;
using Remora.Results;

namespace NosSmooth.LocalClient.CommandHandlers.Walk;

/// <summary>
/// Responds to <see cref="WalkPacket"/> to manage <see cref="WalkCommand"/>.
/// </summary>
public class WalkPacketResponder : IPacketResponder<WalkPacket>, IPacketResponder<CMapPacket>
{
    private readonly WalkStatus _walkStatus;

    /// <summary>
    /// Initializes a new instance of the <see cref="WalkPacketResponder"/> class.
    /// </summary>
    /// <param name="walkStatus">The walk status.</param>
    public WalkPacketResponder(WalkStatus walkStatus)
    {
        _walkStatus = walkStatus;
    }

    /// <inheritdoc />
    public async Task<Result> Respond(PacketEventArgs<WalkPacket> packet, CancellationToken ct = default)
    {
        if (_walkStatus.IsWalking)
        {
            _walkStatus.UpdateWalkTime(packet.Packet.XCoordinate, packet.Packet.YCoordinate);
            if (packet.Packet.XCoordinate == _walkStatus.TargetX && packet.Packet.YCoordinate == _walkStatus.TargetY)
            {
                await _walkStatus.FinishWalkingAsync(ct);
            }
        }

        return Result.FromSuccess();
    }

    /// <inheritdoc />
    public async Task<Result> Respond(PacketEventArgs<CMapPacket> packet, CancellationToken ct = default)
    {
        if (_walkStatus.IsWalking)
        {
            await _walkStatus.CancelWalkingAsync(WalkCancelReason.MapChanged, ct);
        }

        return Result.FromSuccess();
    }

    // TODO: handle teleport on map
}
Do not follow this link