From 228015dec62b4fcd211d2fbcbdc00273206d0827 Mon Sep 17 00:00:00 2001 From: Chomp Date: Sat, 25 Jan 2025 10:22:49 +0000 Subject: [PATCH] Implemented `CustomLocationWaveService` --- .../Services/CustomLocationWaveService.cs | 81 ++++++++++++++++--- 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/Libraries/Core/Services/CustomLocationWaveService.cs b/Libraries/Core/Services/CustomLocationWaveService.cs index 6843a098..b9149d4c 100644 --- a/Libraries/Core/Services/CustomLocationWaveService.cs +++ b/Libraries/Core/Services/CustomLocationWaveService.cs @@ -1,54 +1,111 @@ -using SptCommon.Annotations; using Core.Models.Eft.Common; +using Core.Models.Spt.Config; +using Core.Models.Utils; +using Core.Servers; +using SptCommon.Annotations; namespace Core.Services; [Injectable(InjectionType.Singleton)] -public class CustomLocationWaveService +public class CustomLocationWaveService( + ISptLogger _logger, + DatabaseService _databaseService, + ConfigServer _configServer) { + protected LocationConfig _locationConfig = _configServer.GetConfig(); + /// - /// Add a boss wave to a map + /// Add a boss wave to a map /// /// e.g. factory4_day, bigmap /// Boss wave to add to map public void AddBossWaveToMap(string locationId, BossLocationSpawn waveToAdd) { - throw new NotImplementedException(); + _locationConfig.CustomWaves.Boss[locationId].Add(waveToAdd); } /// - /// Add a normal bot wave to a map + /// Add a normal bot wave to a map /// /// e.g. factory4_day, bigmap /// Wave to add to map public void AddNormalWaveToMap(string locationId, Wave waveToAdd) { - throw new NotImplementedException(); + _locationConfig.CustomWaves.Normal[locationId].Add(waveToAdd); } /// - /// Clear all custom boss waves from a map + /// Clear all custom boss waves from a map /// /// e.g. factory4_day, bigmap public void ClearBossWavesForMap(string locationId) { - throw new NotImplementedException(); + _locationConfig.CustomWaves.Boss[locationId] = []; } /// - /// Clear all custom normal waves from a map + /// Clear all custom normal waves from a map /// /// e.g. factory4_day, bigmap public void ClearNormalWavesForMap(string locationId) { - throw new NotImplementedException(); + _locationConfig.CustomWaves.Normal[locationId] = []; } /// - /// Add custom boss and normal waves to maps found in config/location.json to db + /// Add custom boss and normal waves to maps found in config/location.json to db /// public void ApplyWaveChangesToAllMaps() { - throw new NotImplementedException(); + var bossWavesToApply = _locationConfig.CustomWaves.Boss; + var normalWavesToApply = _locationConfig.CustomWaves.Normal; + + foreach (var mapKvP in bossWavesToApply) + { + var locationBase = _databaseService.GetLocation(mapKvP.Key).Base; + if (locationBase is null) + { + _logger.Warning($"Unable to add custom boss wave to location: ${mapKvP}, location not found"); + + continue; + } + + foreach (var bossWave in mapKvP.Value) + { + if (locationBase.BossLocationSpawn.Any(x => x.SptId == bossWave.SptId)) + { + // Already exists, skip + continue; + } + + locationBase.BossLocationSpawn.Add(bossWave); + _logger.Debug( + $"Added custom boss wave to {mapKvP} of type {bossWave.BossName}, time: {bossWave.Time}, chance: {bossWave.BossChance}, zone: {bossWave.BossZone}" + ); + } + } + + foreach (var mapKvP in normalWavesToApply) + { + var locationBase = _databaseService.GetLocation(mapKvP.Key).Base; + if (locationBase is null) + { + _logger.Warning($"Unable to add custom wave to location: {mapKvP}, location not found"); + + continue; + } + + foreach (var normalWave in mapKvP.Value) + { + if (locationBase.Waves.Any(x => x.SptId == normalWave.SptId)) + { + // Already exists, skip + continue; + } + + normalWave.Number = locationBase.Waves.Count; + locationBase.Waves.Add(normalWave); + } + } } }