cast to int, change type and lock before accessing bots
This commit is contained in:
@@ -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 = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user