From 0fc49ceab1b62fd3bd809d33b534f0cdf1e870c5 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Wed, 6 Nov 2024 10:50:45 -0500 Subject: [PATCH] refactor: Use primary constructors to simplify code --- .../Maps/Services/MapRotationService.cs | 108 +++++++----------- 1 file changed, 42 insertions(+), 66 deletions(-) diff --git a/src/Application/Maps/Services/MapRotationService.cs b/src/Application/Maps/Services/MapRotationService.cs index 080e4a80..c61e1744 100644 --- a/src/Application/Maps/Services/MapRotationService.cs +++ b/src/Application/Maps/Services/MapRotationService.cs @@ -1,51 +1,27 @@ 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() @@ -53,7 +29,7 @@ public void StopRotationTimer() if (_timerReference is null) return; - _timerService.Stop(_timerReference); + timerService.Stop(_timerReference); _timerReference = default; } @@ -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 players = AlphaBetaTeamPlayers.GetAll(); if (currentMap.Id == currentMap.NextMap.Id) @@ -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(); @@ -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); } }