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: Implement a timer to apply automatic flag return #108

Merged
merged 2 commits into from
Sep 22, 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
9 changes: 9 additions & 0 deletions src/Application/Common/Resources/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Application/Common/Resources/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<data name="EmptyWeaponPackage" xml:space="preserve">
<value>You have no items in your weapon package</value>
</data>
<data name="FlagAutoReturn" xml:space="preserve">
<value>The {ColorName} flag has automatically returned to its base position after {Seconds} seconds!</value>
</data>
<data name="GameModeDescription" xml:space="preserve">
<value>~r~Welcome to Capture The Flag mode~n~~y~It is a game mode in which two teams (Alpha and Beta) compete to capture the other team's flag and bring it back to their own base to score a point</value>
</data>
Expand Down
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;
}
}
}
Loading