Further optimisation from Clodan

This commit is contained in:
Dev 2023-09-18 17:15:30 +01:00
parent 64a1c7a0a4
commit e77214b2de
7 changed files with 115 additions and 75 deletions

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.Serialization;

View File

@ -1,43 +1,53 @@
using Common;
using Generator.Helpers.Gear;
using Generator.Helpers.Gear;
using Common.Models.Input;
using Common.Models.Output;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Generator.Weighting;
namespace Generator
{
public static class BotChancesGenerator
{
public static IEnumerable<Bot> AddChances(this IEnumerable<Bot> botsToUpdate, IEnumerable<Datum> rawBots)
public static IEnumerable<Bot> AddChances(this IEnumerable<Bot> botsToUpdate, Dictionary<string, List<Datum>> rawBots)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot gear");
// use lock for lock safety
var dictionaryLock = new object();
var weightHelper = new WeightingService();
// multithread
var tasks = new List<Task>();
foreach (var botToUpdate in botsToUpdate)
{
var botType = botToUpdate.botType.ToString();
var rawParsedBotOfCurrentType = rawBots
.Where(x => x.Info.Settings.Role.Equals(botType, StringComparison.OrdinalIgnoreCase))
.ToList();
if (rawParsedBotOfCurrentType.Count == 0)
tasks.Add(Task.Factory.StartNew(() =>
{
continue;
var botType = botToUpdate.botType.ToString().ToLower();
List<Datum> rawBotsOfSameType;
lock (dictionaryLock)
{
if (!rawBots.TryGetValue(botType, out rawBotsOfSameType))
{
Console.WriteLine($"Unable to find {botType} on rawBots data");
return;
}
}
if (rawBotsOfSameType.Count == 0)
{
return;
}
// TODO: Add check to make sure incoming bot list has gear
GearChanceHelpers.CalculateEquipmentChances(botToUpdate, rawParsedBotOfCurrentType);
GearChanceHelpers.AddGenerationChances(botToUpdate, rawBots, weightHelper);
GearChanceHelpers.CalculateModChances(botToUpdate, rawParsedBotOfCurrentType);
GearChanceHelpers.CalculateEquipmentChances(botToUpdate, rawBotsOfSameType);
GearChanceHelpers.AddGenerationChances(botToUpdate, weightHelper);
GearChanceHelpers.CalculateModChances(botToUpdate, rawBotsOfSameType);
GearChanceHelpers.ApplyModChanceOverrides(botToUpdate);
GearChanceHelpers.ApplyEquipmentChanceOverrides(botToUpdate);
}));
}
Task.WaitAll(tasks.ToArray());
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot chances. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");

View File

@ -1,41 +1,51 @@
using Common;
using Common.Models.Input;
using Common.Models.Input;
using Common.Models.Output;
using Generator.Helpers.Gear;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Generator
{
public static class BotGearGenerator
{
public static IEnumerable<Bot> AddGear(this IEnumerable<Bot> baseBots, IEnumerable<Datum> rawBots)
public static IEnumerable<Bot> AddGear(this IEnumerable<Bot> baseBots, Dictionary<string, List<Datum>> rawBots)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot gear");
var dictionaryLock = new object();
var tasks = new List<Task>();
foreach (var botToUpdate in baseBots)
{
var botType = botToUpdate.botType.ToString();
var rawParsedBotOfCurrentType = rawBots.Where(x => x.Info.Settings.Role.Equals(botType, StringComparison.OrdinalIgnoreCase))
.ToList();
if (rawParsedBotOfCurrentType.Count == 0)
tasks.Add(Task.Factory.StartNew(() =>
{
continue;
var botType = botToUpdate.botType.ToString().ToLower();
List<Datum> rawBotsOfSameType;
lock (dictionaryLock)
{
if (!rawBots.TryGetValue(botType, out rawBotsOfSameType))
{
Console.WriteLine($"Unable to find {botType} on rawBots data");
return;
}
}
foreach (var rawParsedBot in rawParsedBotOfCurrentType)
if (rawBotsOfSameType.Count == 0)
{
return;
}
foreach (var rawParsedBot in rawBotsOfSameType)
{
GearHelpers.AddEquippedGear(botToUpdate, rawParsedBot);
GearHelpers.AddAmmo(botToUpdate, rawParsedBot);
GearHelpers.AddEquippedMods(botToUpdate, rawParsedBot);
//GearHelpers.AddCartridges(botToUpdate, rawParsedBot);
}
}));
}
Task.WaitAll(tasks.ToArray());
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot gear. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");

View File

@ -1,30 +1,35 @@
using Common;
using System.Diagnostics;
using Common.Extensions;
using Common.Models.Input;
using Common.Models.Output;
using Generator.Helpers.Gear;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Generator
{
public static class BotLootGenerator
{
internal static IEnumerable<Bot> AddLoot(this IEnumerable<Bot> botsWithGear, IEnumerable<Datum> rawBots)
internal static IEnumerable<Bot> AddLoot(this IEnumerable<Bot> botsWithGear, Dictionary<string, List<Datum>> rawBots)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot loot");
// Iterate over assault/raider etc
Parallel.ForEach(botsWithGear, botToUpdate =>
var dictionaryLock = new object();
var tasks = new List<Task>(50);
foreach (var botToUpdate in botsWithGear)
{
var botType = botToUpdate.botType.ToString();
var rawBotsOfSameType = rawBots
.Where(x => x.Info.Settings.Role.Equals(botType, StringComparison.OrdinalIgnoreCase))
.ToList();
tasks.Add(Task.Factory.StartNew(() =>
{
var botType = botToUpdate.botType.ToString().ToLower();
List<Datum> rawBotsOfSameType;
lock (dictionaryLock)
{
if (!rawBots.TryGetValue(botType, out rawBotsOfSameType))
{
Console.WriteLine($"Unable to find {botType} on rawBots data");
return;
}
}
if (rawBotsOfSameType.Count == 0)
{
@ -40,7 +45,10 @@ namespace Generator
AddBackpackLoot(botToUpdate, rawBotsOfSameType);
AddSecureContainerLoot(botToUpdate, rawBotsOfSameType);
AddSpecialLoot(botToUpdate);
});
}));
}
Task.WaitAll(tasks.ToArray());
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot loot. Took: {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");

View File

@ -150,7 +150,7 @@ namespace Generator.Helpers.Gear
}
}
public static void AddGenerationChances(Bot bot, IEnumerable<Datum> rawBots, WeightingService weightingService)
public static void AddGenerationChances(Bot bot, WeightingService weightingService)
{
var weightsData = weightingService.GetBotGenerationWeights(bot.botType);
bot.generation = new GenerationChances(

View File

@ -1,4 +1,5 @@
using Generator.Helpers;
using Common.Models.Input;
using Generator.Helpers;
namespace Generator;
@ -52,6 +53,16 @@ internal static class Program
var dumpPath = $"{workingPath}//dumps";
var parsedBots = BotParser.ParseAsync(dumpPath, botTypes.ToHashSet());
// put in dictionary for better use later on
var rawBotsCache = new Dictionary<string, List<Datum>>(40);
foreach (var rawBot in parsedBots)
{
if (rawBotsCache.TryGetValue(rawBot.Info.Settings.Role.ToLower(), out var botList))
botList.Add(rawBot);
else
rawBotsCache.Add(rawBot.Info.Settings.Role.ToLower(), new List<Datum> {rawBot});
}
if (parsedBots.Count == 0)
{
LoggingHelpers.LogToConsole("no bots found, unable to continue");
@ -61,9 +72,9 @@ internal static class Program
// Generate the base bot class with basic details (health/body part hp etc) and then append everything else
var bots = BaseBotGenerator.GenerateBaseDetails(parsedBots, workingPath, botTypes)
.AddGear(parsedBots) // Add weapons/armor
.AddLoot(parsedBots)
.AddChances(parsedBots); // Add mod/equipment chances
.AddGear(rawBotsCache) // Add weapons/armor
.AddLoot(rawBotsCache)
.AddChances(rawBotsCache); // Add mod/equipment chances
// Output bot to json file
var jsonWriter = new JsonWriter(workingPath, "output");