From 7f8476bf95b7deaa84917b4351779fb9a9882d8e Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Mon, 19 Aug 2024 16:28:02 -0500 Subject: [PATCH 1/3] test: Create FakePlayer from the root directory --- tests/Application.Tests/{Teams => }/FakePlayer.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) rename tests/Application.Tests/{Teams => }/FakePlayer.cs (79%) diff --git a/tests/Application.Tests/Teams/FakePlayer.cs b/tests/Application.Tests/FakePlayer.cs similarity index 79% rename from tests/Application.Tests/Teams/FakePlayer.cs rename to tests/Application.Tests/FakePlayer.cs index 781a6909..55bc80bb 100644 --- a/tests/Application.Tests/Teams/FakePlayer.cs +++ b/tests/Application.Tests/FakePlayer.cs @@ -1,18 +1,20 @@ -namespace CTF.Application.Tests.Teams; +namespace CTF.Application.Tests; public class FakePlayer : Player { private readonly int _id; + private readonly Guid _type; public FakePlayer(int id, string name, TeamId team = TeamId.NoTeam) { _id = id; + _type = Guid.NewGuid(); Name = name; Team = (int)team; } public override string Name { get; set; } public override int Team { get; set; } - public override EntityId Entity => new(Guid.NewGuid(), _id); + public override EntityId Entity => new(_type, _id); public override bool RemoveAttachedObject(int index) => true; public override bool SetAttachedObject( int index, From 302d76853f56b6ecc5c0db8bbd723a0412f9768c Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Mon, 19 Aug 2024 16:31:54 -0500 Subject: [PATCH 2/3] feat: Check if the player has captured the opposing team's flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * The property called “AccountNumber” has been removed. It is not needed for this class. --- .../Players/Accounts/PlayerInfo.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Application/Players/Accounts/PlayerInfo.cs b/src/Application/Players/Accounts/PlayerInfo.cs index d2d069ae..edaa438d 100644 --- a/src/Application/Players/Accounts/PlayerInfo.cs +++ b/src/Application/Players/Accounts/PlayerInfo.cs @@ -7,11 +7,12 @@ public partial class PlayerInfo private static partial Regex PlayerNameRegex(); public PlayerInfo() { } + public PlayerInfo(EntityId entityId) => EntityId = entityId; /// - /// It is generated automatically by the database engine. + /// The entity id is generated by the SA-MP server. /// - public int AccountNumber { get; private set; } + public EntityId EntityId { get; } = EntityId.Empty; public string Name { get; private set; } = "DefaultUser"; public string Password { get; private set; } = "DefaultPassword"; public PlayerStatsPerRound StatsPerRound { get; } = new(); @@ -169,6 +170,20 @@ public Result SetTeam(TeamId id) return Result.Failure(Messages.InvalidTeam); } + /// + /// Checks if the player has captured the opposing team's flag. + /// + public bool HasCapturedFlag() + { + Flag rivalTeamFlag = Team.RivalTeam.Flag; + if(rivalTeamFlag.IsCaptured()) + { + Player carrier = rivalTeamFlag.Carrier; + return carrier.Entity == EntityId; + } + return false; + } + public bool CanMoveUpToNextRank() { IRank currentRank = RankCollection.GetById(RankId).Value; From da1de0a5d6d7e235fac9c3500ecdfe44345f1828 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Mon, 19 Aug 2024 16:33:20 -0500 Subject: [PATCH 3/3] test: Add unit tests for the HasCapturedFlag method --- .../Players/Accounts/HasCapturedFlagTests.cs | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/Application.Tests/Players/Accounts/HasCapturedFlagTests.cs diff --git a/tests/Application.Tests/Players/Accounts/HasCapturedFlagTests.cs b/tests/Application.Tests/Players/Accounts/HasCapturedFlagTests.cs new file mode 100644 index 00000000..3fc3457d --- /dev/null +++ b/tests/Application.Tests/Players/Accounts/HasCapturedFlagTests.cs @@ -0,0 +1,121 @@ +namespace CTF.Application.Tests.Players.Accounts; + +public class HasCapturedFlagTests +{ + [SetUp] + public void Init() + { + Team.Alpha.Reset(); + Team.Beta.Reset(); + } + + [Test] + public void HasCapturedFlag_WhenPlayerFromTheAlphaTeamHasCapturedTheFlagOfTheBetaTeam_ShouldReturnsTrue() + { + // Arrange + Team betaTeam = Team.Beta; + var alphaTeamPlayer = new FakePlayer(id: 1, name: "Bob", team: TeamId.Alpha); + var player = new PlayerInfo(alphaTeamPlayer.Entity); + player.SetTeam(TeamId.Alpha); + player.SetName("Bob"); + betaTeam.Flag.SetCarrier(alphaTeamPlayer); + + // Act + bool actual = player.HasCapturedFlag(); + + // Assert + actual.Should().BeTrue(); + } + + [Test] + public void HasCapturedFlag_WhenPlayerFromTheBetaTeamHasCapturedTheFlagOfTheAlphaTeam_ShouldReturnsTrue() + { + // Arrange + Team alphaTeam = Team.Alpha; + var betaTeamPlayer = new FakePlayer(id: 1, name: "Bob", team: TeamId.Beta); + var player = new PlayerInfo(betaTeamPlayer.Entity); + player.SetTeam(TeamId.Beta); + player.SetName("Bob"); + alphaTeam.Flag.SetCarrier(betaTeamPlayer); + + // Act + bool actual = player.HasCapturedFlag(); + + // Assert + actual.Should().BeTrue(); + } + + [Test] + public void HasCapturedFlag_WhenPlayerFromTheAlphaTeamHasNotCapturedTheFlagOfTheBetaTeam_ShouldReturnsFalse() + { + // Arrange + Team betaTeam = Team.Beta; + var alphaTeamPlayer1 = new FakePlayer(id: 1, name: "Bob", team: TeamId.Alpha); + var alphaTeamPlayer2 = new FakePlayer(id: 2, name: "Alice", team: TeamId.Alpha); + var player = new PlayerInfo(alphaTeamPlayer1.Entity); + player.SetTeam(TeamId.Alpha); + player.SetName("Bob"); + betaTeam.Flag.SetCarrier(alphaTeamPlayer2); + + // Act + bool actual = player.HasCapturedFlag(); + + // Assert + actual.Should().BeFalse(); + } + + [Test] + public void HasCapturedFlag_WhenPlayerFromTheBetaTeamHasNotCapturedTheFlagOfTheAlphaTeam_ShouldReturnsFalse() + { + // Arrange + Team alphaTeam = Team.Alpha; + var betaTeamPlayer1 = new FakePlayer(id: 1, name: "Bob", team: TeamId.Beta); + var betaTeamPlayer2 = new FakePlayer(id: 2, name: "Alice", team: TeamId.Beta); + var player = new PlayerInfo(betaTeamPlayer1.Entity); + player.SetTeam(TeamId.Beta); + player.SetName("Bob"); + alphaTeam.Flag.SetCarrier(betaTeamPlayer2); + + // Act + bool actual = player.HasCapturedFlag(); + + // Assert + actual.Should().BeFalse(); + } + + [Test] + public void HasCapturedFlag_WhenAlphaTeamFlagIsNotCaptured_ShouldReturnsFalse() + { + // Arrange + Team alphaTeam = Team.Alpha; + var betaTeamPlayer = new FakePlayer(id: 1, name: "Bob", team: TeamId.Beta); + var player = new PlayerInfo(betaTeamPlayer.Entity); + player.SetTeam(TeamId.Beta); + player.SetName("Bob"); + alphaTeam.Flag.RemoveCarrier(); + + // Act + bool actual = player.HasCapturedFlag(); + + // Assert + actual.Should().BeFalse(); + } + + [Test] + public void HasCapturedFlag_WhenBetaTeamFlagIsNotCaptured_ShouldReturnsFalse() + { + // Arrange + Team betaTeam = Team.Beta; + var alphaTeamPlayer = new FakePlayer(id: 1, name: "Bob", team: TeamId.Alpha); + var player = new PlayerInfo(alphaTeamPlayer.Entity); + player.SetTeam(TeamId.Alpha); + player.SetName("Bob"); + betaTeam.Flag.RemoveCarrier(); + + // Act + bool actual = player.HasCapturedFlag(); + + // Assert + actual.Should().BeFalse(); + } +}