// // 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.Server.Battle; using NosSmooth.Packets.Server.Skills; using Remora.Results; namespace NosSmooth.Game.PacketHandlers.Entities; /// /// Responds to skill used packet. /// public class SkillUsedResponder : IPacketResponder, IPacketResponder { private readonly Game _game; private readonly EventDispatcher _eventDispatcher; /// /// Initializes a new instance of the class. /// /// The game. /// The event dispatcher. public SkillUsedResponder(Game game, EventDispatcher eventDispatcher) { _game = game; _eventDispatcher = eventDispatcher; } /// public async Task Respond(PacketEventArgs 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(); } /// public async Task Respond(PacketEventArgs 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(); } }