Skip to content

Commit

Permalink
Merge pull request #29 from MrDave1999/feat/HasCapturedFlag
Browse files Browse the repository at this point in the history
feat: Check if the player has captured the opposing team's flag
  • Loading branch information
MrDave1999 authored Aug 19, 2024
2 parents 2f1c4ed + da1de0a commit d2da11d
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/Application/Players/Accounts/PlayerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ public partial class PlayerInfo
private static partial Regex PlayerNameRegex();

public PlayerInfo() { }
public PlayerInfo(EntityId entityId) => EntityId = entityId;

/// <summary>
/// It is generated automatically by the database engine.
/// The entity id is generated by the SA-MP server.
/// </summary>
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();
Expand Down Expand Up @@ -169,6 +170,20 @@ public Result SetTeam(TeamId id)
return Result.Failure(Messages.InvalidTeam);
}

/// <summary>
/// Checks if the player has captured the opposing team's flag.
/// </summary>
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
121 changes: 121 additions & 0 deletions tests/Application.Tests/Players/Accounts/HasCapturedFlagTests.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit d2da11d

Please sign in to comment.