Skip to content

Commit

Permalink
Backfill SkillApplier tests
Browse files Browse the repository at this point in the history
Skills that target Character automatically heal
Negative damage multiplier isn't it any more.
  • Loading branch information
nightblade9 committed Dec 21, 2024
1 parent 61f2d07 commit 6bf9586
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using NUnit.Framework;
using TextBlade.Core.Battle;
using TextBlade.Core.Commands;
using TextBlade.Core.Commands.Display;
using TextBlade.Core.IO;
using TextBlade.Core.Locations;

Expand Down
102 changes: 102 additions & 0 deletions source/TextBlade.Core.Tests/Battle/SkillApplierTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using NSubstitute;
using NUnit.Framework;
using TextBlade.Core.Battle;
using TextBlade.Core.Characters;
using TextBlade.Core.IO;

namespace TextBlade.Core.Tests.Battle;

[TestFixture]
public class SkillApplierTests
{
[Test]
public void Apply_AppliesDamage_IfTargetIsMonster()
{
// Arrange
var applier = new SkillApplier(Substitute.For<IConsole>());
// Skills use Strength right now (total strength), not Special
var user = new Character("Skill User", 1, 20, 1, 1);
var skill = new Skill() { DamageMultiplier = 1.5f };
var target = new Monster("Slime", 100, 1, 5);
int expectedDamage = (int)((user.TotalStrength - target.Toughness) * skill.DamageMultiplier);

// Act
applier.Apply(user, skill, [target]);

// Assert
var actualDamage = target.TotalHealth - target.CurrentHealth;
Assert.That(actualDamage, Is.EqualTo(expectedDamage));
}

[Test]
public void Apply_Heals_IfTargetIsCharacter()
{
// Arrange
var applier = new SkillApplier(Substitute.For<IConsole>());
// Healing uses Special
var user = new Character("Skill User", 1, 1, 1, 20);
var skill = new Skill() { DamageMultiplier = 1.5f };
var target = new Character("Ex-Slime", 100, 1, 5) { CurrentHealth = 0 };
int expectedHealing = (int)(user.Special * skill.DamageMultiplier * 2);

// Act
applier.Apply(user, skill, [target]);

// Assert
var actualHealing = target.CurrentHealth;
Assert.That(actualHealing, Is.EqualTo(expectedHealing));
}

[Test]
public void Apply_DoublesDamage_IfTargetWeaknessMatchesSkillDamageType()
{
// Arrange
var applier = new SkillApplier(Substitute.For<IConsole>());
var user = new Character("Skill User", 1, 20, 1, 1);
var skill = new Skill() { DamageMultiplier = 1f, DamageType = "Time" };
var target = new Monster("Slime", 100, 1, 5, weakness: "Time");
int expectedDamage = (user.TotalStrength - target.Toughness) * 2; // x2 = weakness

// Act
applier.Apply(user, skill, [target]);

// Assert
var actualDamage = target.TotalHealth - target.CurrentHealth;
Assert.That(actualDamage, Is.EqualTo(expectedDamage));
}

[Test]
public void Apply_InflictsStatus()
{
// Arrange
var applier = new SkillApplier(Substitute.For<IConsole>());
// Skills use Strength right now (total strength), not Special
var user = new Character("Skill User", 1, 1, 1, 1);
var skill = new Skill() { StatusInflicted = "Anxiety", StatusStacks = 4 };
var target = new Monster("Slime", 1, 1, 1);

// Act
applier.Apply(user, skill, [target]);

// Assert
Assert.That(target.StatusStacks, Does.ContainKey(skill.StatusInflicted));
Assert.That(target.StatusStacks[skill.StatusInflicted], Is.EqualTo(skill.StatusStacks));
}

[Test]
public void Apply_DeductsSkillPointsCost()
{
// Arrange
var applier = new SkillApplier(Substitute.For<IConsole>());
// Skills use Strength right now (total strength), not Special
var user = new Character("Skill User", 1, 1, 1, 1) { TotalSkillPoints = 77, CurrentSkillPoints = 66 };
var skill = new Skill() { Cost = 7 };
var target = new Monster("Slime", 1, 1, 1);

// Act
applier.Apply(user, skill, [target]);

// Assert
Assert.That(user.CurrentSkillPoints, Is.EqualTo(66 - skill.Cost));
}
}
2 changes: 1 addition & 1 deletion source/TextBlade.Core.Tests/Content/Data/Skills.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"Heal":
{
"DamageMultiplier":-1.5,
"DamageMultiplier": 1.5,
"Cost": 5,
"Target":"Character"
}
Expand Down
6 changes: 3 additions & 3 deletions source/TextBlade.Core/Battle/SkillApplier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text;
using TextBlade.Core.Characters;
using TextBlade.Core.IO;

Expand Down Expand Up @@ -27,7 +26,8 @@ internal void Apply(Character user, Skill skill, IEnumerable<Entity> targets)

private void ApplyDamage(Character user, Skill skill, Entity target)
{
/////// TODO: REFACTOR so this method is not polymorphic
/////// TODO: REFACTOR so this method is not polymorphic: healing *and* damage.

ArgumentNullException.ThrowIfNull(target);
float damage = 0;
var hitWeakness = false;
Expand All @@ -45,7 +45,7 @@ private void ApplyDamage(Character user, Skill skill, Entity target)
else if (target is Character)
{
// If you're healing, heal for 2x
damage = (int)Math.Ceiling(user.Special * skill.DamageMultiplier * 2);
damage = (int)Math.Ceiling(user.Special * -skill.DamageMultiplier * 2);
}

var roundedDamage = (int)damage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal bool EquipIfRequested(Item itemData, Inventory inventory, List<Characte

_console.WriteLine("Equip for who? Or press 0 or b to cancel.");

var input = 0;
int input;
var rawInput = _console.ReadKey();
if (rawInput == '0' || rawInput == 'b' || !int.TryParse(rawInput.ToString(), out input))
{
Expand Down
2 changes: 1 addition & 1 deletion source/VoidWalker.Main/Content/Data/Skills.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"Heal":
{
"DamageMultiplier": -1.5,
"DamageMultiplier": 1.5,
"Cost": 5,
"Target":"Character"
}
Expand Down

0 comments on commit 6bf9586

Please sign in to comment.