Fixed issues caused by inclusion of multithreading

Added stopwatch to bot generation
This commit is contained in:
Chomp
2025-01-28 08:49:01 +00:00
parent 6b305bee07
commit 9a9c5f3c72
5 changed files with 35 additions and 16 deletions
+5 -1
View File
@@ -16,6 +16,8 @@ using Core.Utils;
using Core.Utils.Cloners;
using SptCommon.Extensions;
using LogLevel = Core.Models.Spt.Logging.LogLevel;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
namespace Core.Controllers;
@@ -150,7 +152,7 @@ public class BotController(
var allPmcsHaveSameNameAsPlayer = _randomUtil.GetChance100(
_pmcConfig.AllPMCsHavePlayerNameWithRandomPrefixChance
);
var stopwatch = Stopwatch.StartNew();
var tasks = new List<Task>();
// Map conditions to promises for bot generation
foreach (var condition in request.Conditions ?? [])
@@ -176,6 +178,8 @@ public class BotController(
}
Task.WaitAll(tasks.ToArray());
stopwatch.Stop();
_logger.Info($"Took {stopwatch.ElapsedMilliseconds}ms to GenerateMultipleBotsAndCache");
return [];
}
@@ -639,7 +639,7 @@ public class BotEquipmentModGenerator(
var modFromService = _botEquipmentModPoolService.GetModsForWeaponSlot(modToAddTemplate.Value.Id);
if (modFromService?.Keys.Count > 0)
{
request.ModPool[modToAddTemplate.Value.Id] = modFromService;
request.ModPool[modToAddTemplate.Value.Id] = modFromService.ToDictionary();
containsModInPool = true;
}
}
@@ -543,7 +543,7 @@ public class BotInventoryGenerator(
modPool[modSlot.Key] = filteredMods.ToHashSet();
}
return modPool;
return modPool.ToDictionary();
}
/// <summary>
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using SptCommon.Annotations;
using Core.Models.Eft.Common.Tables;
using Core.Models.Utils;
@@ -19,8 +20,8 @@ public class BotEquipmentModPoolService
protected bool _weaponPoolGenerated;
protected bool _armorPoolGenerated;
protected Dictionary<string, Dictionary<string, HashSet<string>>> _weaponModPool;
protected Dictionary<string, Dictionary<string, HashSet<string>>> _gearModPool;
protected ConcurrentDictionary<string, ConcurrentDictionary<string, HashSet<string>>> _weaponModPool;
protected ConcurrentDictionary<string, ConcurrentDictionary<string, HashSet<string>>> _gearModPool;
protected BotConfig _botConfig;
public BotEquipmentModPoolService(
@@ -38,8 +39,8 @@ public class BotEquipmentModPoolService
_configServer = configServer;
_botConfig = _configServer.GetConfig<BotConfig>();
_weaponModPool = new Dictionary<string, Dictionary<string, HashSet<string>>>();
_gearModPool = new Dictionary<string, Dictionary<string, HashSet<string>>>();
_weaponModPool = new ConcurrentDictionary<string, ConcurrentDictionary<string, HashSet<string>>>();
_gearModPool = new ConcurrentDictionary<string, ConcurrentDictionary<string, HashSet<string>>>();
}
/**
@@ -75,10 +76,7 @@ public class BotEquipmentModPoolService
}
// Add base item (weapon/armor) to pool
if (!pool.ContainsKey(item.Id))
{
pool[item.Id] = new Dictionary<string, HashSet<string>>();
}
pool.TryAdd(item.Id, new ConcurrentDictionary<string, HashSet<string>>());
// iterate over each items mod slots e.g. mod_muzzle
foreach (var slot in item.Properties.Slots) {
@@ -143,7 +141,7 @@ public class BotEquipmentModPoolService
* @param itemTpl items tpl to look up mods for
* @returns Dictionary of mods (keys are mod slot names) with array of compatible mod tpls as value
*/
public Dictionary<string, HashSet<string>> GetModsForGearSlot(string itemTpl)
public ConcurrentDictionary<string, HashSet<string>> GetModsForGearSlot(string itemTpl)
{
if (!_armorPoolGenerated)
{
@@ -160,7 +158,7 @@ public class BotEquipmentModPoolService
* @param itemTpl Weapons tpl to look up mods for
* @returns Dictionary of mods (keys are mod slot names) with array of compatible mod tpls as value
*/
public Dictionary<string, HashSet<string>> GetModsForWeaponSlot(string itemTpl)
public ConcurrentDictionary<string, HashSet<string>> GetModsForWeaponSlot(string itemTpl)
{
if (!_weaponPoolGenerated)
{
+20 -3
View File
@@ -21,6 +21,7 @@ public class BotNameService(
{
protected BotConfig _botConfig = _configServer.GetConfig<BotConfig>();
protected HashSet<string> _usedNameCache = new HashSet<string>();
protected object _lock = new();
/// <summary>
/// Clear out any entries in Name Set
@@ -42,7 +43,7 @@ public class BotNameService(
BotType botJsonTemplate,
BotGenerationDetails botGenerationDetails,
string botRole,
List<string> uniqueRoles = null)
List<string>? uniqueRoles = null)
{
var isPmc = botGenerationDetails.IsPmc;
@@ -77,7 +78,7 @@ public class BotNameService(
if (roleShouldBeUnique.GetValueOrDefault(false))
{
// Check name in cache
if (_usedNameCache.Contains(name))
if (CacheContainsName(name))
{
// Not unique
if (attempts >= 5)
@@ -97,7 +98,7 @@ public class BotNameService(
}
// Add bot name to cache to prevent being used again
_usedNameCache.Add(name);
AddNameToCache(name);
return name;
}
@@ -106,6 +107,22 @@ public class BotNameService(
return $"BOT {botRole} {botGenerationDetails.BotDifficulty}";
}
private bool AddNameToCache(string name)
{
lock (_lock)
{
return _usedNameCache.Add(name);
}
}
protected bool CacheContainsName(string name)
{
lock (_lock)
{
return _usedNameCache.Contains(name);
}
}
/// <summary>
/// Add random PMC name to bots MainProfileNickname property
/// </summary>