Neatified the code

This commit is contained in:
Martynas Gestautas 2021-09-01 19:19:44 +03:00
parent a67c2ad807
commit e407393392
6 changed files with 48 additions and 98 deletions

View File

@ -12,28 +12,17 @@ using System.Linq;
namespace Generator
{
public class BaseBotGenerator
public static class BaseBotGenerator
{
private readonly List<Datum> _rawParsedBots;
private readonly string _workingPath;
private readonly string[] _botTypes;
//TODO: pass in bot types and use those to create the clases in rawBots list
public BaseBotGenerator(List<Datum> parsedBots, string workingPath, string[] botTypes)
{
_rawParsedBots = parsedBots;
_workingPath = workingPath;
_botTypes = botTypes;
}
public List<Bot> AddBaseDetails()
//TODO: pass in bot types and use those to create the classes in rawBots list
public static IEnumerable<Bot> GenerateBaseDetails(IEnumerable<Datum> parsedBots, string workingPath, IEnumerable<string> botTypes)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot base");
// Create a list of bot objects ready to be hydrated
var rawBots = new List<Bot>();
foreach (var botType in _botTypes)
foreach (var botType in botTypes)
{
var typeToAdd = (BotType)Enum.Parse(typeof(BotType), botType);
rawBots.Add(new Bot(typeToAdd));
@ -42,7 +31,7 @@ namespace Generator
// Iterate over each bot type wejust made and put some data into them
foreach (var botToUpdate in rawBots)
{
var rawBotsOfSameType = _rawParsedBots
var rawBotsOfSameType = parsedBots
.Where(x => string.Equals(x.Info.Settings.Role, botToUpdate.botType.ToString(), StringComparison.OrdinalIgnoreCase)).ToList();
if (rawBotsOfSameType.Count == 0)
@ -54,7 +43,7 @@ namespace Generator
LoggingHelpers.LogToConsole($"Found {rawBotsOfSameType.Count} bots of type: {botToUpdate.botType}");
UpdateBodyPartHealth(botToUpdate, rawBotsOfSameType);
AddDifficulties(botToUpdate, _workingPath);
AddDifficulties(botToUpdate, workingPath);
AddExperience(botToUpdate, rawBotsOfSameType);
AddStandingForKill(botToUpdate, rawBotsOfSameType);
AddSkills(botToUpdate, rawBotsOfSameType);
@ -73,7 +62,7 @@ namespace Generator
return rawBots;
}
private void AddSkills(Bot botToUpdate, List<Datum> rawBotsOfSameType)
private static void AddSkills(Bot botToUpdate, IEnumerable<Datum> rawBotsOfSameType)
{
var firstBotOfDesiredType = rawBotsOfSameType.FirstOrDefault();
@ -83,7 +72,7 @@ namespace Generator
}
}
private void AddStandingForKill(Bot botToUpdate, List<Datum> rawBotsOfSameType)
private static void AddStandingForKill(Bot botToUpdate, IEnumerable<Datum> rawBotsOfSameType)
{
var firstBotOfDesiredType = rawBotsOfSameType.FirstOrDefault();
@ -91,7 +80,7 @@ namespace Generator
botToUpdate.experience.aggressorBonus = firstBotOfDesiredType.Info.Settings.AggressorBonus;
}
private void AddExperience(Bot botToUpdate, List<Datum> rawBotsOfSameType)
private static void AddExperience(Bot botToUpdate, IEnumerable<Datum> rawBotsOfSameType)
{
var firstBotOfDesiredType = rawBotsOfSameType.FirstOrDefault();
@ -99,12 +88,12 @@ namespace Generator
botToUpdate.experience.reward.max = firstBotOfDesiredType.Info.Settings.Experience;
}
private void AddVoice(Bot bot, Datum rawParsedBot)
private static void AddVoice(Bot bot, Datum rawParsedBot)
{
bot.appearance.voice.AddUnique(rawParsedBot.Info.Voice);
}
private void AddDifficulties(Bot bot, string workingPath)
private static void AddDifficulties(Bot bot, string workingPath)
{
var botFiles = Directory
.GetFiles($"{workingPath}//Assets", "*.txt", SearchOption.TopDirectoryOnly)
@ -114,7 +103,7 @@ namespace Generator
DifficultyHelper.AddDifficultySettings(bot, botFiles);
}
private void UpdateBodyPartHealth(Bot botToUpdate, List<Datum> rawParsedBots)
private static void UpdateBodyPartHealth(Bot botToUpdate, List<Datum> rawParsedBots)
{
var firstBotOfDesiredType = rawParsedBots.FirstOrDefault();
if (firstBotOfDesiredType == null)
@ -145,7 +134,7 @@ namespace Generator
botToUpdate.health.BodyParts.RightLeg.max = firstBotOfDesiredType.Health.BodyParts.RightLeg.Health.Maximum;
}
private void AddVisualAppearanceItems(Bot botToUpdate, Datum rawBot)
private static void AddVisualAppearanceItems(Bot botToUpdate, Datum rawBot)
{
botToUpdate.appearance.head.AddUnique(rawBot.Customization.Head);
botToUpdate.appearance.body.AddUnique(rawBot.Customization.Body);
@ -153,7 +142,7 @@ namespace Generator
botToUpdate.appearance.feet.AddUnique(rawBot.Customization.Feet);
}
private void AddName(Bot botToUpdate, Datum rawBot)
private static void AddName(Bot botToUpdate, Datum rawBot)
{
var name = rawBot.Info.Nickname.Split();
botToUpdate.firstName.AddUnique(name[0]);

View File

@ -9,25 +9,16 @@ using System.Linq;
namespace Generator
{
internal class BotChancesGenerator
public static class BotChancesGenerator
{
private readonly List<Bot> _bots;
private readonly List<Datum> _rawParsedBots;
public BotChancesGenerator(List<Bot> botsWithGearAndLoot, List<Datum> parsedBots)
{
_bots = botsWithGearAndLoot;
_rawParsedBots = parsedBots;
}
internal List<Bot> AddChances()
public static IEnumerable<Bot> AddChances(this IEnumerable<Bot> botsWithGear, IEnumerable<Datum> parsedBots)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot gear");
foreach (var botToUpdate in _bots)
foreach (var botToUpdate in botsWithGear)
{
var rawParsedBotOfCurrentType = _rawParsedBots
var rawParsedBotOfCurrentType = parsedBots
.Where(x => x.Info.Settings.Role.Equals(botToUpdate.botType.ToString(), StringComparison.OrdinalIgnoreCase))
.ToList();
@ -36,6 +27,7 @@ namespace Generator
continue;
}
// TODO: Add check to make sure incoming bot list has gear
GearChanceHelpers.CalculateEquipmentChances(botToUpdate, rawParsedBotOfCurrentType);
GearChanceHelpers.AddGenerationChances(botToUpdate);
GearChanceHelpers.CalculateModChances(botToUpdate, rawParsedBotOfCurrentType);
@ -44,7 +36,7 @@ namespace Generator
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot chances. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
return _bots;
return botsWithGear;
}
}
}

View File

@ -1,5 +1,4 @@
using Common;
using Generator.Helpers;
using Generator.Helpers.Gear;
using Generator.Models.Input;
using Generator.Models.Output;
@ -10,25 +9,16 @@ using System.Linq;
namespace Generator
{
public class BotGearGenerator
public static class BotGearGenerator
{
private readonly List<Bot> _baseBots;
private readonly List<Datum> _rawParsedBots;
public BotGearGenerator(List<Bot> baseBots, List<Datum> parsedBots)
{
_baseBots = baseBots;
_rawParsedBots = parsedBots;
}
internal List<Bot> AddGear()
public static IEnumerable<Bot> AddGear(this IEnumerable<Bot> baseBots, IEnumerable<Datum> parsedBots)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot gear");
foreach (var botToUpdate in _baseBots)
foreach (var botToUpdate in baseBots)
{
var rawParsedBotOfCurrentType = _rawParsedBots
var rawParsedBotOfCurrentType = parsedBots
.Where(x => x.Info.Settings.Role.Equals(botToUpdate.botType.ToString(), StringComparison.OrdinalIgnoreCase))
.ToList();
@ -48,7 +38,7 @@ namespace Generator
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot gear. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
return _baseBots;
return baseBots;
}
}
}

View File

@ -1,6 +1,5 @@
using Common;
using Common.Extensions;
using Generator.Helpers;
using Generator.Helpers.Gear;
using Generator.Models.Input;
using Generator.Models.Output;
@ -12,26 +11,17 @@ using System.Threading.Tasks;
namespace Generator
{
public class BotLootGenerator
public static class BotLootGenerator
{
private readonly List<Bot> _botsWithGear;
private readonly List<Datum> _rawParsedBots;
public BotLootGenerator(List<Bot> botsWithGear, List<Datum> rawParsedBots)
{
_botsWithGear = botsWithGear;
_rawParsedBots = rawParsedBots;
}
internal List<Bot> AddLoot()
internal static IEnumerable<Bot> AddLoot(this IEnumerable<Bot> botsWithGear, IEnumerable<Datum> parsedBots)
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot loot");
// Iterate over assault/raider etc
Parallel.ForEach(_botsWithGear, botToUpdate =>
Parallel.ForEach(botsWithGear, botToUpdate =>
{
var rawBotsOfSameType = _rawParsedBots
var rawBotsOfSameType = parsedBots
.Where(x => x.Info.Settings.Role.Equals(botToUpdate.botType.ToString(), StringComparison.OrdinalIgnoreCase))
.ToList();
@ -54,10 +44,10 @@ namespace Generator
stopwatch.Stop();
LoggingHelpers.LogToConsole($"Finished processing bot loot. Took: {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
return _botsWithGear;
return botsWithGear;
}
private void AddPocketLoot(Bot finalBot, Datum bot)
private static void AddPocketLoot(Bot finalBot, Datum bot)
{
// pocket loot
foreach (var lootItem in bot.Inventory.items.Where(x => x?.slotId?.StartsWith("pocket") == true))
@ -66,13 +56,13 @@ namespace Generator
}
}
private void AddTacticalVestLoot(Bot finalBot, List<Datum> bots)
private static void AddTacticalVestLoot(Bot finalBot, IEnumerable<Datum> bots)
{
var tacVestItems = GetItemsStoredInEquipmentItem(bots, "TacticalVest");
finalBot.inventory.items.TacticalVest.AddRange(tacVestItems);
}
private void AddBackpackLoot(Bot finalBot, List<Datum> bots)
private static void AddBackpackLoot(Bot finalBot, IEnumerable<Datum> bots)
{
// add generic keys to bosses
if (finalBot.botType.IsBoss())
@ -84,18 +74,18 @@ namespace Generator
finalBot.inventory.items.Backpack.AddRange(backpackItems);
}
private void AddSecureContainerLoot(Bot finalAssaultBot, List<Datum> bots)
private static void AddSecureContainerLoot(Bot finalAssaultBot, IEnumerable<Datum> bots)
{
var tacVestItems = GetItemsStoredInEquipmentItem(bots, "SecuredContainer");
finalAssaultBot.inventory.items.SecuredContainer.AddRange(tacVestItems);
}
private void AddSpecialLoot(Bot botToUpdate)
private static void AddSpecialLoot(Bot botToUpdate)
{
botToUpdate.inventory.items.SpecialLoot.AddRange(SpecialLootHelper.GetSpecialLootForBotType(botToUpdate.botType));
}
private List<string> GetItemsStoredInEquipmentItem(List<Datum> bots, string containerName)
private static IEnumerable<string> GetItemsStoredInEquipmentItem(IEnumerable<Datum> bots, string containerName)
{
var itemsStoredInContainer = new List<string>();
var containers = new List<string>();
@ -120,8 +110,5 @@ namespace Generator
return itemsStoredInContainer;
}
}
}

View File

@ -1,4 +1,5 @@
using Generator.Models.Input;
using System;
using Generator.Models.Input;
using Generator.Models.Output;
using System.Collections.Generic;
using System.Linq;
@ -129,7 +130,7 @@ namespace Generator.Helpers.Gear
private static int GetPercent(int total, int count)
{
return ((200 * count) + 1) / (total * 2);
return (int)Math.Ceiling((double)(((200 * count) + 1) / (total * 2)));
}
}
}

View File

@ -1,11 +1,12 @@
using Common;
using System.IO;
using System.Linq;
namespace Generator
{
class Program
internal static class Program
{
static void Main(string[] args)
internal static void Main(string[] args)
{
// Create list of bots we want to process
string[] botTypes = {
@ -48,25 +49,15 @@ namespace Generator
return;
}
// Generate the base bot class and add basic details (health/body part hp etc)
var baseBotGenerator = new BaseBotGenerator(parsedBots, workingPath, botTypes);
var baseBots = baseBotGenerator.AddBaseDetails();
// Add weapons/armor to bots
var botGearGenerator = new BotGearGenerator(baseBots, parsedBots);
var botsWithGear = botGearGenerator.AddGear();
// Add loot to bots
var botLootGenerator = new BotLootGenerator(botsWithGear, parsedBots);
var botsWithGearAndLoot = botLootGenerator.AddLoot();
// Add mod/equipment chances
var botChancesGenerator = new BotChancesGenerator(botsWithGearAndLoot, parsedBots);
var botsWithGearAndLootAndChances = botChancesGenerator.AddChances();
// 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
// Output bot to json file
var jsonWriter = new JsonWriter(workingPath, "output");
jsonWriter.WriteJson(botsWithGearAndLootAndChances);
jsonWriter.WriteJson(bots.ToList());
}
}
}