cast to int, change type and lock before accessing bots

This commit is contained in:
CWX
2025-01-25 20:13:30 +00:00
parent 5f663d70af
commit 573cf786a9
4 changed files with 37 additions and 21 deletions
+1 -1
View File
@@ -230,7 +230,7 @@ public class BotGenerator(
bot.Info.Experience = botLevel.Exp;
bot.Info.Level = botLevel.Level;
bot.Info.Settings.Experience = GetExperienceRewardForKillByDifficulty(
bot.Info.Settings.Experience = (int)GetExperienceRewardForKillByDifficulty(
botJsonTemplate.BotExperience.Reward,
botGenerationDetails.BotDifficulty,
botGenerationDetails.Role
@@ -196,7 +196,7 @@ public record BotInfoSettings
{
public string? Role { get; set; }
public string? BotDifficulty { get; set; }
public double? Experience { get; set; }
public int? Experience { get; set; }
public double? StandingForKill { get; set; }
public double? AggressorBonus { get; set; }
public bool? UseSimpleAnimator { get; set; }
@@ -13,6 +13,7 @@ public class BotGenerationCacheService(
{
protected Dictionary<string, List<BotBase>> _storedBots = new Dictionary<string, List<BotBase>>();
protected Queue<BotBase> _activeBotsInRaid = [];
protected Lock _lock = new Lock();
/**
@@ -21,11 +22,14 @@ public class BotGenerationCacheService(
*/
public void StoreBots(string key, List<BotBase> botsToStore)
{
foreach (var bot in botsToStore)
lock (_lock)
{
if (!_storedBots.TryAdd(key, [bot]))
foreach (var bot in botsToStore)
{
_storedBots[key].Add(bot);
if (!_storedBots.TryAdd(key, [bot]))
{
_storedBots[key].Add(bot);
}
}
}
}
@@ -38,21 +42,24 @@ public class BotGenerationCacheService(
*/
public BotBase? GetBot(string key)
{
if (_storedBots.TryGetValue(key, out var bots))
lock (_lock)
{
if (bots.Count > 0)
if (_storedBots.TryGetValue(key, out var bots))
{
try
if (bots.Count > 0)
{
return bots.Pop();
}
catch (Exception _)
{
_logger.Error(_localisationService.GetText("bot-cache_has_zero_bots_of_requested_type", key));
return bots.PopFirst();
try
{
}
catch (Exception _)
{
_logger.Error(_localisationService.GetText("bot-cache_has_zero_bots_of_requested_type", key));
}
}
}
}
_logger.Error(_localisationService.GetText("bot-no_bot_type_in_cache", key));
return null;
}
@@ -63,7 +70,10 @@ public class BotGenerationCacheService(
*/
public void StoreUsedBot(BotBase botToStore)
{
_activeBotsInRaid.Enqueue(botToStore);
lock (_lock)
{
_activeBotsInRaid.Enqueue(botToStore);
}
}
/**
@@ -74,7 +84,10 @@ public class BotGenerationCacheService(
*/
public BotBase? GetUsedBot(string profileId)
{
return _activeBotsInRaid.FirstOrDefault(x => x.Id == profileId);
lock (_lock)
{
return _activeBotsInRaid.FirstOrDefault(x => x.Id == profileId);
}
}
/**
@@ -82,8 +95,11 @@ public class BotGenerationCacheService(
*/
public void ClearStoredBots()
{
_storedBots.Clear();
_activeBotsInRaid = [];
lock (_lock)
{
_storedBots.Clear();
_activeBotsInRaid = [];
}
}
/**