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

refactor: Use primary constructors to simplify code #215

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
108 changes: 42 additions & 66 deletions src/Application/Maps/Services/MapRotationService.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,35 @@
namespace CTF.Application.Maps.Services;

public class MapRotationService
public class MapRotationService(
IServerService serverService,
IWorldService worldService,
ITimerService timerService,
MapInfoService mapInfoService,
MapTextDrawRenderer mapTextDrawRenderer,
TeamIconService teamIconService,
TeamPickupService teamPickupService,
TeamBalancer teamBalancer,
FlagAutoReturnTimer flagAutoReturnTimer)
{
private readonly IServerService _serverService;
private readonly IWorldService _worldService;
private readonly ITimerService _timerService;
private readonly MapInfoService _mapInfoService;
private readonly MapTextDrawRenderer _mapTextDrawRenderer;
private readonly TeamIconService _teamIconService;
private readonly TeamPickupService _teamPickupService;
private readonly TeamBalancer _teamBalancer;
private readonly FlagAutoReturnTimer _flagAutoReturnTimer;
private readonly TimeLeft _timeLeft;
private readonly LoadTime _loadTime;
private LoadTime _loadTime;
private TimerReference _timerReference;
private bool _isMapLoading;
private readonly TimeLeft _timeLeft = new();
public TimeLeft TimeLeft => _timeLeft;
private bool _isMapLoading;
public bool IsMapLoading() => _isMapLoading;

public MapRotationService(
IServerService serverService,
IWorldService worldService,
ITimerService timerService,
MapInfoService mapInfoService,
MapTextDrawRenderer mapTextDrawRenderer,
TeamIconService teamIconService,
TeamPickupService teamPickupService,
TeamBalancer teamBalancer,
FlagAutoReturnTimer flagAutoReturnTimer)
{
_serverService = serverService;
_worldService = worldService;
_timerService = timerService;
_mapInfoService = mapInfoService;
_mapTextDrawRenderer = mapTextDrawRenderer;
_teamIconService = teamIconService;
_teamPickupService = teamPickupService;
_teamBalancer = teamBalancer;
_flagAutoReturnTimer = flagAutoReturnTimer;
_timeLeft = new TimeLeft();
_loadTime = new LoadTime(OnLoadingMap, OnLoadedMap);
_isMapLoading = false;
}

public void StartRotationTimer()
{
_timerReference ??= _timerService.Start(action: OnTimer, interval: TimeSpan.FromMilliseconds(1000));
_loadTime ??= new LoadTime(OnLoadingMap, OnLoadedMap);
_timerReference ??= timerService.Start(action: OnTimer, interval: TimeSpan.FromMilliseconds(1000));
}

public void StopRotationTimer()
{
if (_timerReference is null)
return;

_timerService.Stop(_timerReference);
timerService.Stop(_timerReference);
_timerReference = default;
}

Expand All @@ -62,26 +38,26 @@ private void OnTimer(IServiceProvider serviceProvider)
if(_timeLeft.IsCompleted())
{
_loadTime.Decrease();
_mapTextDrawRenderer.UpdateLoadTime(_loadTime);
mapTextDrawRenderer.UpdateLoadTime(_loadTime);
return;
}

_timeLeft.Decrease();
_mapTextDrawRenderer.UpdateTimeLeft(_timeLeft);
mapTextDrawRenderer.UpdateTimeLeft(_timeLeft);
}

private void OnLoadingMap()
{
_isMapLoading = true;
if (Team.Alpha.IsWinner())
_worldService.SendClientMessage(Color.Yellow, Messages.AlphaIsWinner);
worldService.SendClientMessage(Color.Yellow, Messages.AlphaIsWinner);
else if(Team.Beta.IsWinner())
_worldService.SendClientMessage(Color.Yellow, Messages.BetaIsWinner);
worldService.SendClientMessage(Color.Yellow, Messages.BetaIsWinner);
else
_worldService.SendClientMessage(Color.Yellow, Messages.TiedTeams);
worldService.SendClientMessage(Color.Yellow, Messages.TiedTeams);

CurrentMap currentMap = _mapInfoService.Read();
_serverService.SendRconCommand($"unloadfs {currentMap.Name}");
CurrentMap currentMap = mapInfoService.Read();
serverService.SendRconCommand($"unloadfs {currentMap.Name}");

IEnumerable<Player> players = AlphaBetaTeamPlayers.GetAll();
if (currentMap.Id == currentMap.NextMap.Id)
Expand All @@ -101,31 +77,31 @@ private void OnLoadingMap()
}

string message = Smart.Format(Messages.NextMapWillBeLoadedSoon, new { currentMap.NextMap.Name });
_worldService.SendClientMessage(Color.Orange, message);
worldService.SendClientMessage(Color.Orange, message);
IMap nextMap = currentMap.NextMap;
_mapInfoService.Load(nextMap);
mapInfoService.Load(nextMap);
Team.Alpha.Flag.RemoveCarrier();
Team.Beta.Flag.RemoveCarrier();
_teamPickupService.DestroyAllPickups();
_teamPickupService.CreateFlagFromBasePosition(Team.Alpha);
_teamPickupService.CreateFlagFromBasePosition(Team.Beta);
_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}");
teamPickupService.DestroyAllPickups();
teamPickupService.CreateFlagFromBasePosition(Team.Alpha);
teamPickupService.CreateFlagFromBasePosition(Team.Beta);
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}");
}

private void OnLoadedMap()
{
_isMapLoading = false;
_mapTextDrawRenderer.HideLoadTimeForAll();
mapTextDrawRenderer.HideLoadTimeForAll();
TimeLeft.Reset();
CurrentMap currentMap = _mapInfoService.Read();
CurrentMap currentMap = mapInfoService.Read();
string message = Smart.Format(Messages.MapSuccessfullyLoaded, new { currentMap.Name });
_worldService.SendClientMessage(Color.Orange, message);
worldService.SendClientMessage(Color.Orange, message);
static void HandlePlayerAction(Player player, PlayerInfo playerInfo)
{
playerInfo.StatsPerRound.ResetStats();
Expand All @@ -135,9 +111,9 @@ static void HandlePlayerAction(Player player, PlayerInfo playerInfo)
player.SetScore(0);
player.Spawn();
}
_teamBalancer.Balance(action: HandlePlayerAction);
_worldService.SetWeather(currentMap.Weather);
_serverService.SetWorldTime(currentMap.WorldTime);
_mapTextDrawRenderer.UpdateMapName(currentMap);
teamBalancer.Balance(action: HandlePlayerAction);
worldService.SetWeather(currentMap.Weather);
serverService.SetWorldTime(currentMap.WorldTime);
mapTextDrawRenderer.UpdateMapName(currentMap);
}
}
Loading