From de11386c5a5a3ffc9761d1f1db33ae03adf2648e Mon Sep 17 00:00:00 2001 From: Dev Date: Tue, 15 Aug 2023 12:05:31 +0100 Subject: [PATCH] Code cleanup and attempt at reducing ram usage Added extra bosses to pmc enemy forcing code --- Common/Bot/BotParser.cs | 29 +++++++++++++++---------- Generator/BaseBotGenerator.cs | 26 +++++++--------------- Generator/Helpers/DifficultyHelper.cs | 27 +++++++++++++---------- Generator/Weighting/WeightingService.cs | 6 ----- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/Common/Bot/BotParser.cs b/Common/Bot/BotParser.cs index 2b0c4a3..744189d 100644 --- a/Common/Bot/BotParser.cs +++ b/Common/Bot/BotParser.cs @@ -1,6 +1,5 @@ using Common.Models.Input; using Newtonsoft.Json.Linq; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -12,7 +11,7 @@ namespace Common.Bots; public static class BotParser { - static JsonSerializerOptions serialiserOptions = new JsonSerializerOptions { }; + static readonly JsonSerializerOptions serialiserOptions = new() { }; public static async Task> ParseAsync(string dumpPath, string[] botTypes) { @@ -37,12 +36,6 @@ public static class BotParser int dupeCount = 0; var rawInputString = await ReadFileContentsAsync(file); - //var json = rawInputString; - //if (rawInputString.Contains("location\":1,")) - //{ - // json = PruneMalformedBsgJson(rawInputString, splitFilePath.Last()); - //} - List bots = null; try { @@ -50,38 +43,50 @@ public static class BotParser } catch (Exception ex) { - Console.WriteLine($"file parse fucked up: {file}"); + Console.WriteLine($"File parse fucked up: {file}"); throw; } - if (bots == null || bots.Count == 0) { - Console.WriteLine($"skipping file: {splitFilePath.Last()}. no bots found, "); + Console.WriteLine($"Skipping file: {splitFilePath.Last()}. no bots found, "); return; } - Console.WriteLine($"parsing: {bots.Count} bots in file {splitFilePath.Last()}"); + //Console.WriteLine($"parsing: {bots.Count} bots in file {splitFilePath.Last()}"); foreach (var bot in bots) { + // I have no idea if (bot._id == "6483938c53cc9087c70eae86") { Console.WriteLine("oh no"); } + // We dont know how to parse this bot type, need to add it to types enum if (!botTypes.Contains(bot.Info.Settings.Role.ToLower())) { continue; } + // Bot already exists in dictionary, skip if (parsedBotsDict.ContainsKey(bot._id)) { //var existingBot = parsedBotsDict[bot._id]; dupeCount++; continue; } + + if (!parsedBotsDict.ContainsKey(bot._id)) { + // Null out data we don't need for generating bots to save RAM + bot.Stats = null; + bot.Encyclopedia = null; + bot.Hideout = null; + bot.ConditionCounters = null; + bot.Bonuses = null; + bot.BackendCounters = null; + bot.InsuredItems = null; parsedBotsDict.Add(bot._id, bot); } } diff --git a/Generator/BaseBotGenerator.cs b/Generator/BaseBotGenerator.cs index 8d07e02..62dc5f3 100644 --- a/Generator/BaseBotGenerator.cs +++ b/Generator/BaseBotGenerator.cs @@ -1,14 +1,9 @@ -using Common; -using Common.Extensions; +using Common.Extensions; using Common.Models; using Common.Models.Input; using Common.Models.Output; using Generator.Helpers; -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.IO; -using System.Linq; namespace Generator { @@ -136,18 +131,13 @@ namespace Generator if (!alreadyExists) { - var bodyPartHpToAdd = new Common.Models.Output.BodyParts(); - bodyPartHpToAdd.Head.min = bot.Health.BodyParts.Head.Health.Current; - bodyPartHpToAdd.Head.max = bot.Health.BodyParts.Head.Health.Maximum; - - bodyPartHpToAdd.Chest.min = bot.Health.BodyParts.Chest.Health.Current; - bodyPartHpToAdd.Chest.max = bot.Health.BodyParts.Chest.Health.Maximum; - - bodyPartHpToAdd.Stomach.min = bot.Health.BodyParts.Stomach.Health.Current; - bodyPartHpToAdd.Stomach.max = bot.Health.BodyParts.Stomach.Health.Maximum; - - bodyPartHpToAdd.LeftArm.min = bot.Health.BodyParts.LeftArm.Health.Current; - bodyPartHpToAdd.LeftArm.max = bot.Health.BodyParts.LeftArm.Health.Maximum; + var bodyPartHpToAdd = new Common.Models.Output.BodyParts() + { + Head = new MinMax(bot.Health.BodyParts.Head.Health.Current, bot.Health.BodyParts.Head.Health.Maximum), + Chest = new MinMax(bot.Health.BodyParts.Chest.Health.Current, bot.Health.BodyParts.Chest.Health.Maximum), + Stomach = new MinMax(bot.Health.BodyParts.Stomach.Health.Current, bot.Health.BodyParts.Stomach.Health.Maximum), + LeftArm = new MinMax(bot.Health.BodyParts.LeftArm.Health.Current, bot.Health.BodyParts.LeftArm.Health.Maximum) + }; bodyPartHpToAdd.RightArm.min = bot.Health.BodyParts.RightArm.Health.Current; bodyPartHpToAdd.RightArm.max = bot.Health.BodyParts.RightArm.Health.Maximum; diff --git a/Generator/Helpers/DifficultyHelper.cs b/Generator/Helpers/DifficultyHelper.cs index 25c2b4d..fd6e969 100644 --- a/Generator/Helpers/DifficultyHelper.cs +++ b/Generator/Helpers/DifficultyHelper.cs @@ -1,8 +1,7 @@ -using Common.Models.Output; +using Common.Models; +using Common.Models.Output; using Common.Models.Output.Difficulty; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using System.Collections; namespace Generator.Helpers { @@ -15,12 +14,12 @@ namespace Generator.Helpers // Read bot setting files from assets folder that match this bots type // Save into dictionary with difficulty as key var difficultySettingsJsons = new Dictionary(); - var botType = botToUpdate.botType.ToString(); + BotType botType = botToUpdate.botType; var pathsWithBotType = difficultyFilePaths.Where(x => x.Contains($"_{botType}_", StringComparison.InvariantCultureIgnoreCase)); foreach (var path in pathsWithBotType) { - var json = File.ReadAllText(path); - var serialisedDifficultySettings = JsonConvert.DeserializeObject(json); + var difficultyJson = File.ReadAllText(path); + var serialisedDifficultySettings = JsonConvert.DeserializeObject(difficultyJson); serialisedDifficultySettings = ApplyCustomDifficultyValues(botType, serialisedDifficultySettings); @@ -91,16 +90,20 @@ namespace Generator.Helpers } - private static DifficultySettings ApplyCustomDifficultyValues(string botType, DifficultySettings difficultySettings) + private static DifficultySettings ApplyCustomDifficultyValues(BotType botType, DifficultySettings difficultySettings) { switch (botType) { // make all bosses fight PMCs - case "bosskilla": - case "bossgluhar": - case "bosstagilla": - case "bossbully": - case "bosskojaniy": + case BotType.bosskilla: + case BotType.bossgluhar: + case BotType.bosstagilla: + case BotType.bossbully: + case BotType.bosskojaniy: + case BotType.bossboar: + case BotType.bossboarsniper: + case BotType.bossknight: + case BotType.bosszryachiy: AddHostileToPMCSettings(difficultySettings); break; default: diff --git a/Generator/Weighting/WeightingService.cs b/Generator/Weighting/WeightingService.cs index 8b312ce..b4c9d48 100644 --- a/Generator/Weighting/WeightingService.cs +++ b/Generator/Weighting/WeightingService.cs @@ -1,14 +1,8 @@ using Common.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Text.Json; -using System.Threading.Tasks; namespace Generator.Weighting { - public class Weightings { public Dictionary> Equipment { get; set; }