~ruther/NosSmooth

ref: c02cf72a8c911f1127991832b4b050250a586ba4 NosSmooth/Extensions/NosSmooth.Extensions.Combat/Operations/UseSkillOperation.cs -rw-r--r-- 3.9 KiB
c02cf72a — Rutherther feat(combat): support self targeted and area skills 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
//  UseSkillOperation.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.Diagnostics;
using System.Xml.XPath;
using NosSmooth.Data.Abstractions.Enums;
using NosSmooth.Data.Abstractions.Infos;
using NosSmooth.Extensions.Combat.Errors;
using NosSmooth.Game.Data.Characters;
using NosSmooth.Game.Data.Entities;
using NosSmooth.Packets;
using NosSmooth.Packets.Client.Battle;
using Remora.Results;

namespace NosSmooth.Extensions.Combat.Operations;

/// <summary>
/// A combat operation to use a skill.
/// </summary>
/// <param name="Skill">The skill to use.</param>
/// <param name="Caster">The caster entity that is using the skill.</param>
/// <param name="Target">The target entity to use the skill at.</param>
public record UseSkillOperation(Skill Skill, ILivingEntity Caster, ILivingEntity Target) : ICombatOperation
{
    /// <inheritdoc />
    public Result<CanBeUsedResponse> CanBeUsed(ICombatState combatState)
    {
        if (Skill.Info is null)
        {
            return new MissingInfoError("skill", Skill.SkillVNum);
        }

        var character = combatState.Game.Character;
        if (character is not null && character.Mp is not null && character.Mp.Amount is not null)
        {
            if (character.Mp.Amount < Skill.Info.MpCost)
            { // The character is in combat, mp won't restore.
                return CanBeUsedResponse.WontBeUsable;
            }
        }

        return Skill.IsOnCooldown ? CanBeUsedResponse.MustWait : CanBeUsedResponse.CanBeUsed;
    }

    /// <inheritdoc />
    public async Task<Result> UseAsync(ICombatState combatState, CancellationToken ct = default)
    {
        if (Skill.Info is null)
        {
            return new MissingInfoError("skill", Skill.SkillVNum);
        }

        using var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(ct);
        await combatState.CombatManager.RegisterSkillCancellationTokenAsync(linkedSource, ct);
        var sendResponse = await combatState.Client.SendPacketAsync
        (
            CreateSkillUsePacket(Skill.Info),
            ct
        );

        if (!sendResponse.IsSuccess)
        {
            await combatState.CombatManager.UnregisterSkillCancellationTokenAsync(linkedSource, ct);
            return sendResponse;
        }

        try
        {
            // wait 10 times the cast delay in case su is not received.
            await Task.Delay(Skill.Info.CastTime * 1000, linkedSource.Token);
        }
        catch (TaskCanceledException)
        {
            // ignored
        }
        await combatState.CombatManager.UnregisterSkillCancellationTokenAsync(linkedSource, ct);
        await Task.Delay(1000, ct);

        return Result.FromSuccess();
    }

    private IPacket CreateSkillUsePacket(ISkillInfo info)
    {
        switch (info.TargetType)
        {
            case TargetType.SelfOrTarget: // a buff?
            case TargetType.Self:
                return CreateSelfTargetedSkillPacket(info);
            case TargetType.NoTarget: // area skill?
                return CreateAreaSkillPacket(info);
            case TargetType.Target:
                return CreateTargetedSkillPacket(info);
        }

        throw new UnreachableException();
    }

    private IPacket CreateAreaSkillPacket(ISkillInfo info)
        => new UseAOESkillPacket
        (
            info.CastId,
            Target.Position!.Value.X,
            Target.Position.Value.Y
        );

    private IPacket CreateTargetedSkillPacket(ISkillInfo info)
        => new UseSkillPacket
        (
            info.CastId,
            Target.Type,
            Target.Id,
            null,
            null
        );

    private IPacket CreateSelfTargetedSkillPacket(ISkillInfo info)
        => new UseSkillPacket
        (
            info.CastId,
            Caster.Type,
            Caster.Id,
            null,
            null
        );
}
Do not follow this link