Skip to content

Commit

Permalink
feat: Implement a timer to apply automatic flag return
Browse files Browse the repository at this point in the history
This timer starts when the player drops the flag on the ground. After 120 seconds, the flag will automatically return to its base position.
  • Loading branch information
MrDave1999 committed Sep 22, 2024
1 parent dd9e973 commit 2e05bd3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Application/Common/ServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class ServerSettings
public string MapName { get; init; } = string.Empty;
public string WebUrl { get; init; } = string.Empty;
public string IntroAudioUrl { get; init; } = string.Empty;
public int FlagAutoReturnTime { get; init; } = 120;
}
7 changes: 6 additions & 1 deletion src/Application/Maps/Services/MapRotationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MapRotationService
private readonly TeamPickupService _teamPickupService;
private readonly TeamTextDrawRenderer _teamTextDrawRenderer;
private readonly PlayerStatsRenderer _playerStatsRenderer;
private readonly FlagAutoReturnTimer _flagAutoReturnTimer;
private readonly TimeLeft _timeLeft;
private readonly LoadTime _loadTime;
private TimerReference _timerReference;
Expand All @@ -27,7 +28,8 @@ public MapRotationService(
TeamIconService teamIconService,
TeamPickupService teamPickupService,
TeamTextDrawRenderer teamTextDrawRenderer,
PlayerStatsRenderer playerStatsRenderer)
PlayerStatsRenderer playerStatsRenderer,
FlagAutoReturnTimer flagAutoReturnTimer)
{
_serverService = serverService;
_worldService = worldService;
Expand All @@ -38,6 +40,7 @@ public MapRotationService(
_teamPickupService = teamPickupService;
_teamTextDrawRenderer = teamTextDrawRenderer;
_playerStatsRenderer = playerStatsRenderer;
_flagAutoReturnTimer = flagAutoReturnTimer;
_timeLeft = new TimeLeft();
_loadTime = new LoadTime(OnLoadingMap, OnLoadedMap);
_isMapLoading = false;
Expand Down Expand Up @@ -112,6 +115,8 @@ private void OnLoadingMap()
_teamIconService.DestroyAll();
_teamIconService.CreateFromBasePosition(Team.Alpha);
_teamIconService.CreateFromBasePosition(Team.Beta);
_flagAutoReturnTimer.Stop(Team.Alpha);
_flagAutoReturnTimer.Stop(Team.Beta);
_serverService.SendRconCommand($"loadfs {nextMap.Name}");
_serverService.SendRconCommand($"mapname {nextMap.Name}");
}
Expand Down
4 changes: 3 additions & 1 deletion src/Application/Teams/Flags/Events/OnFlagDropped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ public class OnFlagDropped(
IPlayerRepository playerRepository,
IWorldService worldService,
TeamPickupService teamPickupService,
TeamSoundsService teamSoundsService) : IFlagEvent
TeamSoundsService teamSoundsService,
FlagAutoReturnTimer flagAutoReturnTimer) : IFlagEvent
{
public FlagStatus FlagStatus => FlagStatus.Dropped;

public void Handle(Team team, Player player)
{
teamPickupService.CreateFlagFromVector3(team, player.Position);
teamSoundsService.PlayFlagDroppedSound(team);
flagAutoReturnTimer.Start(team);
team.Flag.RemoveCarrier();
var message = Smart.Format(Messages.OnFlagDropped, new
{
Expand Down
4 changes: 3 additions & 1 deletion src/Application/Teams/Flags/Events/OnFlagReturned.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class OnFlagReturned(
IWorldService worldService,
TeamPickupService teamPickupService,
TeamSoundsService teamSoundsService,
PlayerStatsRenderer playerStatsRenderer) : IFlagEvent
PlayerStatsRenderer playerStatsRenderer,
FlagAutoReturnTimer flagAutoReturnTimer) : IFlagEvent
{
public FlagStatus FlagStatus => FlagStatus.Returned;

Expand All @@ -17,6 +18,7 @@ public void Handle(Team team, Player player)
teamPickupService.CreateFlagFromBasePosition(team);
teamPickupService.DestroyPickupWithInfo(team);
teamSoundsService.PlayFlagReturnedSound(team);
flagAutoReturnTimer.Stop(team);
var message = Smart.Format(Messages.OnFlagReturned, new
{
PlayerName = player.Name,
Expand Down
4 changes: 3 additions & 1 deletion src/Application/Teams/Flags/Events/OnFlagTaken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
public class OnFlagTaken(
IWorldService worldService,
TeamPickupService teamPickupService,
TeamSoundsService teamSoundsService) : IFlagEvent
TeamSoundsService teamSoundsService,
FlagAutoReturnTimer flagAutoReturnTimer) : IFlagEvent
{
public FlagStatus FlagStatus => FlagStatus.Taken;

public void Handle(Team team, Player player)
{
teamPickupService.DestroyFlag(team);
teamSoundsService.PlayFlagTakenSound(team);
flagAutoReturnTimer.Stop(team);
var message = Smart.Format(Messages.OnFlagTaken, new
{
PlayerName = player.Name,
Expand Down
1 change: 1 addition & 0 deletions src/Application/Teams/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static IServiceCollection AddTeamServices(this IServiceCollection service
.AddSingleton<TeamIconService>()
.AddSingleton<TeamSoundsService>()
.AddSingleton<TeamTextDrawRenderer>()
.AddSingleton<FlagAutoReturnTimer>()
.AddSingleton<ClassSelectionTextDrawRenderer>()
.AddFlagServices();

Expand Down
56 changes: 56 additions & 0 deletions src/Application/Teams/Services/FlagAutoReturnTimer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace CTF.Application.Teams.Services;

public class FlagAutoReturnTimer(
ITimerService timerService,
IWorldService worldService,
TeamPickupService teamPickupService,
TeamSoundsService teamSoundsService,
ServerSettings serverSettings)
{
private TimerReference _alphaTeamTimer;
private TimerReference _betaTeamTimer;

public void Start(Team team)
{
void OnComplete(IServiceProvider serviceProvider)
{
teamPickupService.CreateFlagFromBasePosition(team);
teamPickupService.DestroyPickupWithInfo(team);
teamSoundsService.PlayFlagReturnedSound(team);
team.IsFlagAtBasePosition = true;
var message = Smart.Format(Messages.FlagAutoReturn, new
{
Seconds = serverSettings.FlagAutoReturnTime,
team.ColorName
});
worldService.SendClientMessage(team.ColorHex, message);
worldService.GameText($"~n~~n~~n~{team.GameText}{team.ColorName} flag returned!", 5000, 3);
Stop(team);
}

if (team.Id == TeamId.Alpha)
{
TimeSpan interval = TimeSpan.FromSeconds(serverSettings.FlagAutoReturnTime);
_alphaTeamTimer ??= timerService.Start(OnComplete, interval);
}
else if (team.Id == TeamId.Beta)
{
TimeSpan interval = TimeSpan.FromSeconds(serverSettings.FlagAutoReturnTime);
_betaTeamTimer ??= timerService.Start(OnComplete, interval);
}
}

public void Stop(Team team)
{
if (team.Id == TeamId.Alpha && _alphaTeamTimer is not null)
{
timerService.Stop(_alphaTeamTimer);
_alphaTeamTimer = default;
}
else if (team.Id == TeamId.Beta && _betaTeamTimer is not null)
{
timerService.Stop(_betaTeamTimer);
_betaTeamTimer = default;
}
}
}

0 comments on commit 2e05bd3

Please sign in to comment.