Skip to content

Commit

Permalink
Merge pull request #110 from MrDave1999/feat/TeamStats
Browse files Browse the repository at this point in the history
feat: Allow players to view statistics of both teams (Alpha and Beta)
  • Loading branch information
MrDave1999 authored Sep 27, 2024
2 parents 869b340 + bd26cb4 commit fbf4c8e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/Application/Teams/Flags/Flag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public class Flag
/// </summary>
public bool IsCaptured() => Carrier is not null;

/// <summary>
/// Gets the name of the player who captured the flag.
/// </summary>
/// <remarks>
/// If the flag is not captured, returns <c>None</c>.
/// </remarks>
public string CarrierName => IsCaptured() ? Carrier.Name : "None";

/// <summary>
/// Sets the player who holds the flag.
/// </summary>
Expand Down
50 changes: 50 additions & 0 deletions src/Application/Teams/Systems/TeamStatsSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace CTF.Application.Teams.Systems;

public class TeamStatsSystem(
IDialogService dialogService,
TeamTextDrawRenderer teamTextDrawRenderer) : ISystem
{
[Event]
public void OnPlayerSpawn(Player player)
{
teamTextDrawRenderer.Show(player);
}

[Event]
public void OnPlayerDeath(Player deadPlayer, Player killer, Weapon reason)
{
PlayerInfo deadPlayerInfo = deadPlayer.GetInfo();
deadPlayerInfo.Team.StatsPerRound.AddDeaths();

if (killer.IsInvalidPlayer())
return;

PlayerInfo killerInfo = killer.GetInfo();
killerInfo.Team.StatsPerRound.AddKills();
}

[PlayerCommand("tstats")]
public void ShowStats(Player player)
{
Team alphaTeam = Team.Alpha;
Team betaTeam = Team.Beta;
var content =
$"""
{alphaTeam.ColorHex}>>> Alpha Team:
Members: {alphaTeam.Members.Count}
Score: {alphaTeam.StatsPerRound.Score}
Kills: {alphaTeam.StatsPerRound.Kills}
Deaths: {alphaTeam.StatsPerRound.Deaths}
Flag captured by: {alphaTeam.Flag.CarrierName}
{betaTeam.ColorHex}>>> Beta Team:
Members: {betaTeam.Members.Count}
Score: {betaTeam.StatsPerRound.Score}
Kills: {betaTeam.StatsPerRound.Kills}
Deaths: {betaTeam.StatsPerRound.Deaths}
Flag captured by: {betaTeam.Flag.CarrierName}
""";
var dialog = new MessageDialog("Team Stats", content, "Close");
dialogService.ShowAsync(player, dialog);
}
}
10 changes: 0 additions & 10 deletions src/Application/Teams/Systems/TeamSystem.cs

This file was deleted.

42 changes: 42 additions & 0 deletions tests/Application.Tests/Teams/Flags/FlagTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,48 @@ public void IsCaptured_WhenFlagIsNotCapturedByPlayer_ShouldReturnsFalse()
actual.Should().BeFalse();
}

[Test]
public void CarrierName_WhenFlagIsCaptured_ShouldReturnsCarrierName()
{
// Arrange
var flag = new Flag
{
Model = FlagModel.Blue,
Icon = FlagIcon.Blue,
Name = "Blue",
ColorHex = Color.Blue
};
var expectedCarrierName = "Bob";
var fakeCarrier = new FakePlayer(id: 1, expectedCarrierName);
flag.SetCarrier(fakeCarrier);

// Act
string actual = flag.CarrierName;

// Assert
actual.Should().Be(expectedCarrierName);
}

[Test]
public void CarrierName_WhenFlagIsNotCaptured_ShouldReturnsNone()
{
// Arrange
var flag = new Flag
{
Model = FlagModel.Blue,
Icon = FlagIcon.Blue,
Name = "Blue",
ColorHex = Color.Blue
};
var expectedCarrierName = "None";

// Act
string actual = flag.CarrierName;

// Assert
actual.Should().Be(expectedCarrierName);
}

[Test]
public void SetCarrier_WhenArgumentIsNull_ShouldThrowArgumentNullException()
{
Expand Down

0 comments on commit fbf4c8e

Please sign in to comment.