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: Check if the player has captured the opposing team's flag #29

Merged
merged 3 commits into from
Aug 19, 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
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();
}
}
Loading