~ruther/NosSmooth

ref: 3c62fd452466a4f3f7bc2b8a1a1a8aebad5eb870 NosSmooth/Core/NosSmooth.Game/PacketHandlers/Entities/SkillUsedResponder.cs -rw-r--r-- 3.7 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
104
105
106
107
108
109
110
111
112
113
114
//
//  SkillUsedResponder.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.Packets;
using NosSmooth.Game.Data.Info;
using NosSmooth.Game.Events.Characters;
using NosSmooth.Game.Events.Core;
using NosSmooth.Game.Events.Players;
using NosSmooth.Game.Extensions;
using NosSmooth.Packets.Enums;
using NosSmooth.Packets.Packets.Server.Battle;
using NosSmooth.Packets.Packets.Server.Skills;
using Remora.Results;

namespace NosSmooth.Game.PacketHandlers.Entities;

/// <summary>
/// Responds to skill used packet.
/// </summary>
public class SkillUsedResponder : IPacketResponder<SuPacket>, IPacketResponder<SrPacket>
{
    private readonly Game _game;
    private readonly EventDispatcher _eventDispatcher;

    /// <summary>
    /// Initializes a new instance of the <see cref="SkillUsedResponder"/> class.
    /// </summary>
    /// <param name="game">The game.</param>
    /// <param name="eventDispatcher">The event dispatcher.</param>
    public SkillUsedResponder(Game game, EventDispatcher eventDispatcher)
    {
        _game = game;
        _eventDispatcher = eventDispatcher;
    }

    /// <inheritdoc />
    public async Task<Result> Respond(PacketEventArgs<SuPacket> packetArgs, CancellationToken ct = default)
    {
        var packet = packetArgs.Packet;
        var character = _game.Character;

        if (character is not null && character.Id == packet.CasterEntityId && character.Skills is not null)
        {
            var skillResult = character.Skills.TryGetSkill(packet.SkillVNum);

            if (skillResult.IsDefined(out var skillEntity))
            {
                skillEntity.LastUseTime = DateTimeOffset.Now;
                skillEntity.Cooldown = TimeSpan.FromSeconds(packet.SkillCooldown / 10.0);
                skillEntity.IsOnCooldown = true;
            }

            await _eventDispatcher.DispatchEvent(
                new SkillUsedEvent
                (
                    character,
                    character.Id,
                    skillResult.IsSuccess ? skillEntity : null,
                    packet.SkillVNum,
                    packet.TargetEntityId,
                    new Position { X = packet.PositionX, Y = packet.PositionY }
                ),
                ct);
        }
        else
        {
            // TODO: add entity from the map, if exists.
            await _eventDispatcher.DispatchEvent(
                new SkillUsedEvent
                (
                    null,
                    packet.CasterEntityId,
                    null,
                    packet.SkillVNum,
                    packet.TargetEntityId,
                    new Position
                    {
                        X = packet.PositionX, Y = packet.PositionY
                    }
                ),
                ct
            );
        }

        return Result.FromSuccess();
    }

    /// <inheritdoc />
    public async Task<Result> Respond(PacketEventArgs<SrPacket> packetArgs, CancellationToken ct = default)
    {
        var packet = packetArgs.Packet;
        var character = _game.Character;

        if (character is not null && character.Skills is not null)
        {
            var skillResult = character.Skills.TryGetSkill(packet.SkillId);

            if (skillResult.IsDefined(out var skillEntity))
            {
                skillEntity.IsOnCooldown = false;
                await _eventDispatcher.DispatchEvent(new SkillReadyEvent(skillEntity, skillEntity.SkillVNum), ct);
            }
        }
        else
        {
            await _eventDispatcher.DispatchEvent(new SkillReadyEvent(null, packet.SkillId), ct);
        }

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