From cc4f14ba9424b12b100a2f896c67cf28200525ed Mon Sep 17 00:00:00 2001 From: sven-n Date: Tue, 5 Nov 2024 07:25:09 +0100 Subject: [PATCH] Fix self defense: no outlaw for kills under self defense --- src/GameLogic/IGameContext.cs | 2 +- src/GameLogic/Player.cs | 2 +- src/GameLogic/PlugIns/SelfDefensePlugIn.cs | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/GameLogic/IGameContext.cs b/src/GameLogic/IGameContext.cs index 27ed23704..76a5bf083 100644 --- a/src/GameLogic/IGameContext.cs +++ b/src/GameLogic/IGameContext.cs @@ -103,7 +103,7 @@ public interface IGameContext DuelRoomManager DuelRoomManager { get; } /// - /// Gets the state of the active self defenses. + /// Gets the state of the active self defenses. The datetime holds the timestamp when self-defense ends. /// ConcurrentDictionary<(Player Attacker, Player Defender), DateTime> SelfDefenseState { get; } diff --git a/src/GameLogic/Player.cs b/src/GameLogic/Player.cs index 70e806708..90a185c01 100644 --- a/src/GameLogic/Player.cs +++ b/src/GameLogic/Player.cs @@ -610,7 +610,7 @@ public bool IsAnySelfDefenseActive() var hitInfo = await attacker.CalculateDamageAsync(this, skill, isCombo, damageFactor).ConfigureAwait(false); - if (hitInfo.HealthDamage == 0) + if (hitInfo is { HealthDamage: 0, ShieldDamage: 0 }) { await this.InvokeViewPlugInAsync(p => p.ShowHitAsync(this, hitInfo)).ConfigureAwait(false); if (attacker is IWorldObserver observer) diff --git a/src/GameLogic/PlugIns/SelfDefensePlugIn.cs b/src/GameLogic/PlugIns/SelfDefensePlugIn.cs index e9f70c71d..3a7c7da51 100644 --- a/src/GameLogic/PlugIns/SelfDefensePlugIn.cs +++ b/src/GameLogic/PlugIns/SelfDefensePlugIn.cs @@ -24,9 +24,8 @@ public class SelfDefensePlugIn : IPeriodicTaskPlugIn, IAttackableGotHitPlugIn, I /// public async ValueTask ExecuteTaskAsync(GameContext gameContext) { - var configuration = this.Configuration ??= CreateDefaultConfiguration(); - var timedOut = gameContext.SelfDefenseState.Where(s => DateTime.UtcNow.Subtract(s.Value) >= configuration.SelfDefenseTimeOut).ToList(); - foreach (var (pair, lastAttack) in timedOut) + var timedOut = gameContext.SelfDefenseState.Where(s => s.Value < DateTime.UtcNow).ToList(); + foreach (var (pair, _) in timedOut) { if (gameContext.SelfDefenseState.Remove(pair, out _)) { @@ -80,16 +79,16 @@ public void AttackableGotHit(IAttackable attackable, IAttacker attacker, HitInfo return; } - var now = DateTime.UtcNow; + var timeout = DateTime.UtcNow.Add(this.Configuration?.SelfDefenseTimeOut ?? TimeSpan.FromMinutes(1)); var gameContext = defender.GameContext; gameContext.SelfDefenseState.AddOrUpdate( (attackerPlayer, defender), tuple => { _ = this.BeginSelfDefenseAsync(attackerPlayer, defender); - return now; + return timeout; }, - (_, _) => now); + (_, _) => timeout); } ///