Skip to content

Commit

Permalink
feat: multiple callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chillu1 committed Aug 8, 2024
1 parent e273168 commit ff7459d
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
47 changes: 47 additions & 0 deletions ModiBuff/ModiBuff.Tests/CallbackEffectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,52 @@ public void DamageOnStun_HealOnAnyNotStunStatusEffectRemoved()
Unit.Update(1);
Assert.AreEqual(UnitHealth, Unit.Health);
}

[Test]
public void MultipleEffectCallbacks()
{
AddRecipe("DamageEffectCallbacksRemoveOnDisarm")
.Effect(new DamageEffect(1), EffectOn.CallbackEffect)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Stun))
((ICallbackEffect)effect).CallbackEffect(target, source);
}))
.Effect(new DamageEffect(2), EffectOn.CallbackEffect2)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Freeze))
((ICallbackEffect)effect).CallbackEffect(target, source);
}))
.Effect(new DamageEffect(3), EffectOn.CallbackEffect3)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Root))
((ICallbackEffect)effect).CallbackEffect(target, source);
}))
.Remove(RemoveEffectOn.CallbackEffect4)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Disarm))
effect.Effect(target, source);
}));
Setup();

Unit.AddModifierSelf("DamageEffectCallbacksRemoveOnDisarm");
Unit.StatusEffectController.ChangeStatusEffect(0, 0, StatusEffectType.Stun, 1f, Unit);
Assert.AreEqual(UnitHealth - 1, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(1, 0, StatusEffectType.Freeze, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(2, 0, StatusEffectType.Root, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2 - 3, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(3, 0, StatusEffectType.Disarm, 1f, Unit);
Unit.Update(0);
Assert.AreEqual(UnitHealth - 1 - 2 - 3, Unit.Health);
Assert.False(Unit.ContainsModifier("DamageEffectCallbacksRemoveOnDisarm"));
}
}
}
39 changes: 39 additions & 0 deletions ModiBuff/ModiBuff.Tests/CallbackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,44 @@ public void Init_RegisterTimerCallback_TogglableState()
Enemy.Update(Unit.CallbackTimerCooldown);
Assert.AreEqual(EnemyDamage + 5, Enemy.Damage);
}

[Test]
public void MultipleEffectUnitCallbacks()
{
AddRecipe("DamageEffectUnitCallbacks")
.Effect(new DamageEffect(1), EffectOn.CallbackEffectUnits)
.CallbackEffectUnits(CallbackType.StatusEffectAdded, effect => (origTarget, origSource) =>
new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Stun))
((ICallbackEffect)effect).CallbackEffect(origTarget, origSource);
}))
.Effect(new DamageEffect(2), EffectOn.CallbackEffectUnits2)
.CallbackEffectUnits(CallbackType.StatusEffectAdded, effect => (origTarget, origSource) =>
new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Freeze))
((ICallbackEffect)effect).CallbackEffect(origTarget, origSource);
}))
.Effect(new DamageEffect(3), EffectOn.CallbackEffectUnits3)
.CallbackEffectUnits(CallbackType.StatusEffectAdded, effect => (origTarget, origSource) =>
new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Root))
((ICallbackEffect)effect).CallbackEffect(origTarget, origSource);
}));
Setup();

Unit.AddModifierSelf("DamageEffectUnitCallbacks");
Unit.StatusEffectController.ChangeStatusEffect(0, 0, StatusEffectType.Stun, 1f, Unit);
Assert.AreEqual(UnitHealth - 1, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(1, 0, StatusEffectType.Freeze, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(2, 0, StatusEffectType.Root, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2 - 3, Unit.Health);
}
}
}
26 changes: 26 additions & 0 deletions ModiBuff/ModiBuff.Tests/CallbackUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,31 @@ public void Init_AddDamage_HalfHealth_TriggerCallback_RemoveAndRevert_Twice()
Unit.TakeDamage(UnitHealth * 0.6f, Unit);
Assert.AreEqual(UnitDamage, Unit.Damage);
}

[Test]
public void MultipleUnitCallbacks()
{
AddRecipe("AddDamageUnitCallbacksRemoveOnStrongDispel")
.Effect(new AddDamageEffect(1, EffectState.IsRevertible), EffectOn.CallbackUnit)
.CallbackUnit(CallbackUnitType.WhenAttacked)
.Effect(new AddDamageEffect(2, EffectState.IsRevertible), EffectOn.CallbackUnit2)
.CallbackUnit(CallbackUnitType.WhenHealed)
.Effect(new AddDamageEffect(3, EffectState.IsRevertible, targeting: Targeting.SourceTarget),
EffectOn.CallbackUnit3)
.CallbackUnit(CallbackUnitType.OnAttack)
.Remove(RemoveEffectOn.CallbackUnit4)
.CallbackUnit(CallbackUnitType.StrongDispel);
Setup();

Unit.AddModifierSelf("AddDamageUnitCallbacksRemoveOnStrongDispel");
Enemy.Attack(Unit);
Assert.AreEqual(UnitDamage + 1, Unit.Damage);
Unit.Heal(0, Unit);
Assert.AreEqual(UnitDamage + 1 + 2, Unit.Damage);
Unit.Attack(Enemy);
Assert.AreEqual(UnitDamage + 1 + 2 + 3, Unit.Damage);
Unit.StrongDispel(Unit);
Assert.AreEqual(UnitDamage, Unit.Damage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ public ModifierRecipe ModifierAction(ModifierAction modifierAction, EffectOn eff
/// <summary>
/// Registers a callback register effect to a unit, will trigger all <see cref="EffectOn.CallbackUnit"/>
/// effects when <see cref="callbackType"/> is triggered.
/// When using multiple CallbackUnits, the order matters.
/// </summary>
public ModifierRecipe CallbackUnit<TCallbackUnit>(TCallbackUnit callbackType)
{
Expand Down Expand Up @@ -472,7 +473,7 @@ public ModifierRecipe Callback<TCallback, TStateData>(TCallback callbackType,
/// Special callbacks, all EffectOn.<see cref="EffectOn.CallbackEffect"/> effects will
/// trigger when <see cref="callbackType"/> is triggered.
/// Supports custom callback signatures (beside <see cref="UnitCallback"/>.
/// Callback effects now have to be in order
/// When using multiple CallbackEffects, the order matters.
/// </summary>
public ModifierRecipe CallbackEffect<TCallbackEffect>(TCallbackEffect callbackType,
Func<IEffect, object> @event)
Expand All @@ -489,6 +490,7 @@ public ModifierRecipe CallbackEffect<TCallbackEffect>(TCallbackEffect callbackTy
/// trigger when <see cref="callbackType"/> is triggered.
/// Supports custom callback signatures (beside <see cref="UnitCallback"/>.
/// Allows to save state through state context.
/// When using multiple CallbackEffects, the order matters.
/// </summary>
public ModifierRecipe CallbackEffect<TCallbackEffect, TStateData>(TCallbackEffect callbackType,
Func<IEffect, CallbackStateContext<TStateData>> @event)
Expand All @@ -505,6 +507,7 @@ public ModifierRecipe CallbackEffect<TCallbackEffect, TStateData>(TCallbackEffec
/// trigger when <see cref="callbackType"/> is triggered.
/// Supports custom callback signatures (beside <see cref="UnitCallback"/>.
/// Allows to store the target and source when registering the callback, for further access.
/// When using multiple CallbackEffectUnits, the order matters.
/// </summary>
public ModifierRecipe CallbackEffectUnits<TCallbackEffect>(TCallbackEffect callbackType,
Func<IEffect, Func<IUnit, IUnit, object>> @event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ public enum RemoveEffectOn
Stack = EffectOn.Stack,
CallbackUnit = EffectOn.CallbackUnit,
CallbackEffect = EffectOn.CallbackEffect,

CallbackUnit2 = EffectOn.CallbackUnit2,
CallbackUnit3 = EffectOn.CallbackUnit3,
CallbackUnit4 = EffectOn.CallbackUnit4,

CallbackEffect2 = EffectOn.CallbackEffect2,
CallbackEffect3 = EffectOn.CallbackEffect3,
CallbackEffect4 = EffectOn.CallbackEffect4,
}

public static class RemoveEffectOnExtensions
Expand Down

0 comments on commit ff7459d

Please sign in to comment.