From b9f073f99ea18cb58e78e14fd3420b65994aa62d Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 24 Jan 2025 09:49:26 +0000 Subject: [PATCH] Implemented `OpenZoneService ` --- Libraries/Core/Services/OpenZoneService.cs | 46 ++++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/Libraries/Core/Services/OpenZoneService.cs b/Libraries/Core/Services/OpenZoneService.cs index f9a9d188..e59e245e 100644 --- a/Libraries/Core/Services/OpenZoneService.cs +++ b/Libraries/Core/Services/OpenZoneService.cs @@ -1,10 +1,20 @@ -using SptCommon.Annotations; +using Core.Models.Spt.Config; +using Core.Models.Utils; +using Core.Servers; +using SptCommon.Annotations; namespace Core.Services; [Injectable(InjectionType.Singleton)] -public class OpenZoneService +public class OpenZoneService( + ISptLogger _logger, + DatabaseService _databaseService, + LocalisationService _localisationService, + ConfigServer _configServer + ) { + protected LocationConfig _locationConfig = _configServer.GetConfig(); + /// /// Add open zone to specified map /// @@ -12,7 +22,16 @@ public class OpenZoneService /// zone to add public void AddZoneToMap(string locationId, string zoneToAdd) { - throw new NotImplementedException(); + var location = _locationConfig.OpenZones[locationId]; + if (location is null) + { + _locationConfig.OpenZones[locationId] = []; + } + + if (!_locationConfig.OpenZones[locationId].Contains(zoneToAdd)) + { + _locationConfig.OpenZones[locationId].Add(zoneToAdd); + } } /// @@ -20,6 +39,25 @@ public class OpenZoneService /// public void ApplyZoneChangesToAllMaps() { - throw new NotImplementedException(); + var dbLocations = _databaseService.GetLocations().GetDictionary(); + foreach (var mapKvP in _locationConfig.OpenZones) { + if (!dbLocations.ContainsKey(mapKvP.Key)) + { + _logger.Error(_localisationService.GetText("openzone-unable_to_find_map", mapKvP)); + + continue; + } + + var zonesToAdd = _locationConfig.OpenZones[mapKvP.Key]; + + // Convert openzones string into list, easier to work wih + var mapOpenZonesArray = dbLocations[mapKvP.Key].Base.OpenZones.Split(",").ToList(); + foreach (var zoneToAdd in zonesToAdd.Where(zoneToAdd => !mapOpenZonesArray.Contains(zoneToAdd))) + { + // Add new zone to array and convert array back into comma separated string + mapOpenZonesArray.Add(zoneToAdd); + dbLocations[mapKvP.Key].Base.OpenZones = string.Join(",", mapOpenZonesArray); + } + } } }