using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Spt.Config;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Servers;
namespace SPTarkov.Server.Core.Services;
///
/// Service for adding new zones to a maps OpenZones property.
///
[Injectable(InjectionType.Singleton)]
public class OpenZoneService(
ISptLogger logger,
DatabaseService databaseService,
ServerLocalisationService serverLocalisationService,
ConfigServer configServer
)
{
protected readonly LocationConfig LocationConfig = configServer.GetConfig();
///
/// Add open zone to specified map
///
/// map location (e.g. factory4_day)
/// zone to add
public void AddZoneToMap(string locationId, string zoneToAdd)
{
LocationConfig.OpenZones.TryAdd(locationId, []);
if (!LocationConfig.OpenZones[locationId].Contains(zoneToAdd))
{
LocationConfig.OpenZones[locationId].Add(zoneToAdd);
}
}
///
/// Add open zones to all maps found in config/location.json to db
///
public void ApplyZoneChangesToAllMaps()
{
var dbLocations = databaseService.GetLocations().GetDictionary();
foreach (var mapKvP in LocationConfig.OpenZones)
{
if (!dbLocations.ContainsKey(mapKvP.Key))
{
logger.Error(serverLocalisationService.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(",").ToHashSet();
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);
}
}
}
}