Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add module to manage player health and armour vitality #132

Merged
merged 4 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Application/Common/Resources/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/Application/Common/Resources/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AddArmourToAllPlayers" xml:space="preserve">
<value>{PlayerName} has added {Armour}%% to the armour of all players!</value>
</data>
<data name="AddArmourToPlayer" xml:space="preserve">
<value>You have added {Armour}%% armour to {PlayerName}!</value>
</data>
<data name="AddHealthToAllPlayers" xml:space="preserve">
<value>{PlayerName} has added {Health}%% to the health of all players!</value>
</data>
<data name="AddHealthToPlayer" xml:space="preserve">
<value>You have added {Health}%% health to {PlayerName}!</value>
</data>
<data name="AddScoreToAllPlayers" xml:space="preserve">
<value>{PlayerName} has added {Score} to the score of all players!</value>
</data>
Expand Down Expand Up @@ -198,6 +210,9 @@
<data name="InvalidTeam" xml:space="preserve">
<value>Invalid team has been passed. Should be Alpha = 0, Beta = 1 or NoTeam = 255</value>
</data>
<data name="InvalidVitality" xml:space="preserve">
<value>Vitality must be between 0 to 100</value>
</data>
<data name="InvalidWeapon" xml:space="preserve">
<value>Invalid weapon has been passed</value>
</data>
Expand Down Expand Up @@ -303,6 +318,12 @@
<data name="RankUpAward" xml:space="preserve">
<value>You have gained +100 points, +100 armour, and +100 health</value>
</data>
<data name="ReceiveArmourFromPlayer" xml:space="preserve">
<value>Congratulations! You've received {Armour}%% armour from {PlayerName}!</value>
</data>
<data name="ReceiveHealthFromPlayer" xml:space="preserve">
<value>Congratulations! You've received {Health}%% health from {PlayerName}!</value>
</data>
<data name="ReceivePointsFromPlayer" xml:space="preserve">
<value>Congratulations! You've received {Points} points from {PlayerName}!</value>
</data>
Expand Down
69 changes: 69 additions & 0 deletions src/Application/Players/Vitalities/ArmourSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace CTF.Application.Players.Vitalities;

public class ArmourSystem(
IWorldService worldService,
IEntityManager entityManager) : ISystem
{
[PlayerCommand("addarmour")]
public void AddArmourToPlayer(
Player currentPlayer,
[CommandParameter(Name = "playerId")]Player targetPlayer,
float amount)
{
if (currentPlayer.HasLowerRoleThan(RoleId.Moderator))
return;

Result<Vitality> result = Vitality.Create(amount);
if (result.IsFailed)
{
currentPlayer.SendClientMessage(Color.Red, result.Message);
return;
}

{
var message = Smart.Format(Messages.AddArmourToPlayer, new
{
PlayerName = targetPlayer.Name,
Armour = amount
});
currentPlayer.SendClientMessage(Color.Yellow, message);
}

{
var message = Smart.Format(Messages.ReceiveArmourFromPlayer, new
{
PlayerName = currentPlayer.Name,
Armour = amount
});
targetPlayer.SendClientMessage(Color.Yellow, message);
targetPlayer.AddArmour(amount);
}
}

[PlayerCommand("addallarmour")]
public void AddArmourToAllPlayers(Player currentPlayer, float amount)
{
if (currentPlayer.HasLowerRoleThan(RoleId.Moderator))
return;

Result<Vitality> result = Vitality.Create(amount);
if (result.IsFailed)
{
currentPlayer.SendClientMessage(Color.Red, result.Message);
return;
}

IEnumerable<Player> players = entityManager.GetComponents<Player>();
foreach (Player targetPlayer in players)
{
targetPlayer.AddArmour(amount);
}

var message = Smart.Format(Messages.AddArmourToAllPlayers, new
{
PlayerName = currentPlayer.Name,
Armour = amount
});
worldService.SendClientMessage(Color.Yellow, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CTF.Application.Players.Extensions;
namespace CTF.Application.Players.Vitalities;

public static class HealthArmourExtensions
{
Expand Down
69 changes: 69 additions & 0 deletions src/Application/Players/Vitalities/HealthSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace CTF.Application.Players.Vitalities;

public class HealthSystem(
IWorldService worldService,
IEntityManager entityManager) : ISystem
{
[PlayerCommand("addhealth")]
public void AddHealthToPlayer(
Player currentPlayer,
[CommandParameter(Name = "playerId")]Player targetPlayer,
float amount)
{
if (currentPlayer.HasLowerRoleThan(RoleId.Moderator))
return;

Result<Vitality> result = Vitality.Create(amount);
if(result.IsFailed)
{
currentPlayer.SendClientMessage(Color.Red, result.Message);
return;
}

{
var message = Smart.Format(Messages.AddHealthToPlayer, new
{
PlayerName = targetPlayer.Name,
Health = amount
});
currentPlayer.SendClientMessage(Color.Yellow, message);
}

{
var message = Smart.Format(Messages.ReceiveHealthFromPlayer, new
{
PlayerName = currentPlayer.Name,
Health = amount
});
targetPlayer.SendClientMessage(Color.Yellow, message);
targetPlayer.AddHealth(amount);
}
}

[PlayerCommand("addallhealth")]
public void AddHealthToAllPlayers(Player currentPlayer, float amount)
{
if (currentPlayer.HasLowerRoleThan(RoleId.Moderator))
return;

Result<Vitality> result = Vitality.Create(amount);
if (result.IsFailed)
{
currentPlayer.SendClientMessage(Color.Red, result.Message);
return;
}

IEnumerable<Player> players = entityManager.GetComponents<Player>();
foreach (Player targetPlayer in players)
{
targetPlayer.AddHealth(amount);
}

var message = Smart.Format(Messages.AddHealthToAllPlayers, new
{
PlayerName = currentPlayer.Name,
Health = amount
});
worldService.SendClientMessage(Color.Yellow, message);
}
}
15 changes: 15 additions & 0 deletions src/Application/Players/Vitalities/Vitality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace CTF.Application.Players.Vitalities;

public class Vitality
{
public float Amount { get; private set; }
private Vitality(float amount) => Amount = amount;

public static Result<Vitality> Create(float amount)
{
if(amount < 0 || amount > 100)
return Result<Vitality>.Failure(Messages.InvalidVitality);

return Result<Vitality>.Success(new Vitality(amount));
}
}
1 change: 1 addition & 0 deletions src/Application/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
global using CTF.Application.Players.Combos.Services;
global using CTF.Application.Players.Chats;
global using CTF.Application.Players.Chats.Services;
global using CTF.Application.Players.Vitalities;
global using CTF.Application.Players.Extensions;
global using CTF.Application.Teams;
global using CTF.Application.Teams.ClassSelection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CTF.Application.Tests.Players.Extensions;
namespace CTF.Application.Tests.Players.Vitalities;

public class HealthArmourExtensionsTests
{
Expand All @@ -8,7 +8,7 @@ public class HealthArmourExtensionsTests
[TestCase(70, 10, 80)]
[TestCase(100, 0, 100)]
public void AddHealth_WhenAmountIsPositiveAndBelowLimit_ShouldIncreaseHealth(
float currentHealth,
float currentHealth,
float amount,
float expectedHealth)
{
Expand Down
40 changes: 40 additions & 0 deletions tests/Application.Tests/Players/Vitalities/VitalityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace CTF.Application.Tests.Players.Vitalities;

public class VitalityTests
{
[TestCase(0)]
[TestCase(1)]
[TestCase(10)]
[TestCase(20)]
[TestCase(35)]
[TestCase(50)]
[TestCase(100)]
public void Create_WhenCalledWithValidAmount_ShouldReturnsSuccessResult(float amount)
{
// Arrange

// Act
Result<Vitality> result = Vitality.Create(amount);

// Asserts
result.IsSuccess.Should().BeTrue();
result.Value.Amount.Should().Be(amount);
}

[TestCase(-1)]
[TestCase(-2)]
[TestCase(101)]
[TestCase(102)]
public void Create_WhenCalledWithInvalidAmount_ShouldReturnsFailureResult(float amount)
{
// Arrange
var expectedMessage = Messages.InvalidVitality;

// Act
Result<Vitality> result = Vitality.Create(amount);

// Asserts
result.IsSuccess.Should().BeFalse();
result.Message.Should().Be(expectedMessage);
}
}
1 change: 1 addition & 0 deletions tests/Application.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
global using CTF.Application.Players.Ranks;
global using CTF.Application.Players.Weapons;
global using CTF.Application.Players.Accounts;
global using CTF.Application.Players.Vitalities;
global using CTF.Application.Players.Extensions;
global using CTF.Application.Teams;
global using CTF.Application.Teams.Flags;
Expand Down
Loading