BotGenerator/Generator/BotChancesGenerator.cs

59 lines
2.3 KiB
C#
Raw Normal View History

2023-09-18 17:15:30 +01:00
using Generator.Helpers.Gear;
using Common.Models.Input;
using Common.Models.Output;
using System.Diagnostics;
2023-08-21 16:32:20 +01:00
using Generator.Weighting;
namespace Generator
{
2021-09-01 19:19:44 +03:00
public static class BotChancesGenerator
{
2023-09-18 17:15:30 +01:00
public static IEnumerable<Bot> AddChances(this IEnumerable<Bot> botsToUpdate, Dictionary<string, List<Datum>> rawBots)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot gear");
2023-09-18 17:15:30 +01:00
// use lock for lock safety
var dictionaryLock = new object();
2023-08-21 16:32:20 +01:00
var weightHelper = new WeightingService();
2023-09-18 17:15:30 +01:00
// multithread
var tasks = new List<Task>();
2021-09-05 12:27:17 +01:00
foreach (var botToUpdate in botsToUpdate)
{
2023-09-18 17:15:30 +01:00
tasks.Add(Task.Factory.StartNew(() =>
{
2023-09-18 17:15:30 +01:00
var botType = botToUpdate.botType.ToString().ToLower();
List<Datum> rawBotsOfSameType;
lock (dictionaryLock)
{
if (!rawBots.TryGetValue(botType, out rawBotsOfSameType))
{
2023-12-28 09:12:54 +00:00
Console.WriteLine($"(chances) Unable to find {botType} on rawBots data");
2023-09-18 17:15:30 +01:00
return;
}
}
if (rawBotsOfSameType.Count == 0)
{
return;
}
2023-09-18 17:15:30 +01:00
// TODO: Add check to make sure incoming bot list has gear
GearChanceHelpers.CalculateEquipmentChances(botToUpdate, rawBotsOfSameType);
GearChanceHelpers.AddGenerationChances(botToUpdate, weightHelper);
GearChanceHelpers.CalculateModChances(botToUpdate, rawBotsOfSameType);
GearChanceHelpers.CalculateEquipmentModChances(botToUpdate, rawBotsOfSameType);
2023-09-18 17:15:30 +01:00
GearChanceHelpers.ApplyModChanceOverrides(botToUpdate);
GearChanceHelpers.ApplyEquipmentChanceOverrides(botToUpdate);
}));
}
2023-09-18 17:15:30 +01:00
Task.WaitAll(tasks.ToArray());
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot chances. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
2021-09-05 12:27:17 +01:00
return botsToUpdate;
}
}
2023-09-18 17:15:30 +01:00
}