Create static helper classes and move some code to them

This commit is contained in:
Chomp 2021-08-13 16:24:05 +01:00
parent 98e3cf15cd
commit 7e742d6bc3
3 changed files with 286 additions and 192 deletions

View File

@ -1,8 +1,7 @@
using Generator.Helpers; using Generator.Helpers;
using Generator.Models; using Generator.Helpers.Gear;
using Generator.Models.Input; using Generator.Models.Input;
using Generator.Models.Output; using Generator.Models.Output;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -28,13 +27,15 @@ namespace Generator
foreach (var bot in _baseBots) foreach (var bot in _baseBots)
{ {
AddEquipmentChances(bot); GearChanceHelpers.AddEquipmentChances(bot);
AddGenerationChances(bot); GearChanceHelpers.AddGenerationChances(bot);
AddModChances(bot); GearChanceHelpers.AddModChances(bot);
foreach (var rawParsedBot in _rawParsedBots)
foreach (var rawParsedBot in _rawParsedBots.Where(x => x.Info.Settings.Role.Equals(bot.botType.ToString(), StringComparison.OrdinalIgnoreCase)))
{ {
AddEquippedGear(bot, rawParsedBot); GearHelpers.AddEquippedGear(bot, rawParsedBot);
AddEquippedMods(bot, rawParsedBot); GearHelpers.AddEquippedMods(bot, rawParsedBot);
GearHelpers.AddCartridges(bot, rawParsedBot);
} }
} }
@ -43,189 +44,5 @@ namespace Generator
return _baseBots; return _baseBots;
} }
private void AddEquippedMods(Bot botToUpdate, Datum rawParsedBot)
{
var modItemsInRawBot = new List<Item>();
var itemsWithModsInRawBot = new List<Item>();
modItemsInRawBot = rawParsedBot.Inventory.items
.Where(x => x.slotId?.StartsWith("mod_") == true).ToList();
// get items with Mods by iterating over mod items and getting the parent item
itemsWithModsInRawBot.AddRange(modItemsInRawBot
.Select(modItem => rawParsedBot.Inventory.items
.Find(x => x._id == modItem.parentId)));
var itemsWithModsDictionary = botToUpdate.inventory.mods;
foreach (var itemToAdd in itemsWithModsInRawBot)
{
var modsToAdd = modItemsInRawBot.Where(x => x.parentId == itemToAdd._id).ToList();
AddItemToDictionary(itemToAdd, modsToAdd, itemsWithModsDictionary);
}
botToUpdate.inventory.mods = itemsWithModsDictionary;
}
private void AddItemToDictionary(
Item itemToAdd,
List<Item> modsToAdd,
Dictionary<string, Dictionary<string, List<string>>> itemsWithModsDictionary)
{
// item key already exists, need to merge mods
if (itemsWithModsDictionary.ContainsKey(itemToAdd.slotId))
{
foreach (var modItem in modsToAdd)
{
var itemToAddModsTo = itemsWithModsDictionary[itemToAdd.slotId];
// Item doesnt have this mod, add it then add template id
if (!itemToAddModsTo.ContainsKey(modItem.slotId))
{
// Mod doesnt exist on item
itemToAddModsTo.Add(modItem.slotId, new List<string>()); // add mod
itemToAddModsTo[modItem.slotId].AddUnique(modItem._tpl);
}
itemToAddModsTo[modItem.slotId].AddUnique(modItem._tpl); // add template id to it
}
}
else // item doesnt exist, create it
{
itemsWithModsDictionary.Add(itemToAdd.slotId, new Dictionary<string, List<string>>());
// Add mod types to item
foreach (var modItem in modsToAdd)
{
itemsWithModsDictionary[itemToAdd.slotId].Add(modItem.slotId, new List<string>());
}
// Get item we're adding mod templateIds to
var itemToUpdate = itemsWithModsDictionary[itemToAdd.slotId];
foreach (var modItem in modsToAdd)
{
var modToUpdate = itemToUpdate[modItem.slotId];
modToUpdate.Add(modItem._tpl);
}
}
var result = JsonConvert.SerializeObject(itemsWithModsDictionary, Formatting.Indented);
}
private void AddModChances(Bot bot)
{
switch (bot.botType)
{
case BotType.assault:
bot.chances.mods = new Mods(muzzle: 18, barrel: 100, handguard: 100, stock: 66, magazine: 100,
mount: 15, flashlight: 100, tactical_001: 99, tactical_002: 0, tactical_003: 0,
mount_000: 56, pistol_grip: 97, tactical: 87, scope: 17, reciever: 92,
sight_rear: 56, charge: 13, mount_001: 0, equipment: 30, gas_block: 100,
launcher: 0, sight_front: 25, stock_000: 0, foregrip: 0, tactical_000: 0,
nvg: 0, pistol_grip_akms: 100, stock_akms: 100, equipment_000: 0, equipment_001: 0,
equipment_002: 0, bipod: 0);
break;
case BotType.pmcBot:
bot.chances.mods = new Mods(muzzle: 67, barrel: 100, handguard: 97, stock: 81, magazine: 100,
mount: 35, flashlight: 100, tactical_001: 9, tactical_002: 0, tactical_003: 0,
mount_000: 28, pistol_grip: 97, tactical: 32, scope: 57, reciever: 100,
sight_rear: 58, charge: 81, mount_001: 38, equipment: 0, gas_block: 100,
launcher: 0, sight_front: 65, stock_000: 100, foregrip: 30, tactical_000: 18,
nvg: 25, pistol_grip_akms: 97, stock_akms: 0, equipment_000: 0, equipment_001: 0,
equipment_002: 0, bipod: 0);
break;
case BotType.marksman:
bot.chances.mods = new Mods(muzzle: 0, barrel: 100, handguard: 0, stock: 73, magazine: 100,
mount: 100, flashlight: 0, tactical_001: 0, tactical_002: 0, tactical_003: 0,
mount_000: 0, pistol_grip: 0, tactical: 33, scope: 89, reciever: 0,
sight_rear: 17, charge: 0, mount_001: 0, equipment: 0, gas_block: 0,
launcher: 0, sight_front: 25, stock_000: 0, foregrip: 0, tactical_000: 33,
nvg: 100, pistol_grip_akms: 0, stock_akms: 0, equipment_000: 0, equipment_001: 0,
equipment_002: 0, bipod: 0);
break;
}
}
private void AddGenerationChances(Bot bot)
{
switch (bot.botType)
{
case BotType.assault:
case BotType.pmcBot:
case BotType.marksman:
bot.generation = new GenerationChances(0, 1, 1, 2, 0, 3, 2, 4, 0, 5);
break;
}
}
private void AddEquipmentChances(Bot bot)
{
switch (bot.botType)
{
case BotType.assault:
bot.chances.equipment = new EquipmentChances(73, 0, 62, 28, 36, 0, 100, 38, 95, 0, 5, 72, 100, 100);
break;
case BotType.pmcBot:
bot.chances.equipment = new EquipmentChances(89, 56, 58, 49, 84, 0, 100, 58, 100, 0, 18, 0, 100, 100);
break;
case BotType.marksman:
bot.chances.equipment = new EquipmentChances(8, 8, 8, 42, 0, 0, 100, 25, 100, 0, 0, 33, 100, 100);
break;
}
}
private void AddEquippedGear(Bot finalAssaultBot, Datum bot)
{
// add equipped gear
foreach (var inventoryItem in bot.Inventory.items)
{
switch (inventoryItem.slotId?.ToLower())
{
case "headwear":
finalAssaultBot.inventory.equipment.Headwear.AddUnique(inventoryItem._tpl);
break;
case "earpiece":
finalAssaultBot.inventory.equipment.Earpiece.AddUnique(inventoryItem._tpl);
break;
case "facecover":
finalAssaultBot.inventory.equipment.FaceCover.AddUnique(inventoryItem._tpl);
break;
case "armorvest":
finalAssaultBot.inventory.equipment.ArmorVest.AddUnique(inventoryItem._tpl);
break;
case "eyewear":
finalAssaultBot.inventory.equipment.Eyewear.AddUnique(inventoryItem._tpl);
break;
case "armband":
finalAssaultBot.inventory.equipment.ArmBand.AddUnique(inventoryItem._tpl);
break;
case "tacticalvest":
finalAssaultBot.inventory.equipment.TacticalVest.AddUnique(inventoryItem._tpl);
break;
case "backpack":
finalAssaultBot.inventory.equipment.Backpack.AddUnique(inventoryItem._tpl);
break;
case "firstprimaryweapon":
finalAssaultBot.inventory.equipment.FirstPrimaryWeapon.AddUnique(inventoryItem._tpl);
break;
case "secondprimaryweapon":
finalAssaultBot.inventory.equipment.SecondPrimaryWeapon.AddUnique(inventoryItem._tpl);
break;
case "holster":
finalAssaultBot.inventory.equipment.Holster.AddUnique(inventoryItem._tpl);
break;
case "scabbard":
finalAssaultBot.inventory.equipment.Scabbard.AddUnique(inventoryItem._tpl);
break;
case "pockets":
finalAssaultBot.inventory.equipment.Pockets.AddUnique(inventoryItem._tpl);
break;
case "securedcontainer":
finalAssaultBot.inventory.equipment.SecuredContainer.AddUnique(inventoryItem._tpl);
break;
default:
break;
}
}
}
} }
} }

View File

@ -0,0 +1,71 @@
using Generator.Models;
using Generator.Models.Output;
namespace Generator.Helpers.Gear
{
public static class GearChanceHelpers
{
public static void AddModChances(Bot bot)
{
switch (bot.botType)
{
case BotType.assault:
bot.chances.mods = new Mods(muzzle: 18, barrel: 100, handguard: 100, stock: 66, magazine: 100,
mount: 15, flashlight: 100, tactical_001: 99, tactical_002: 0, tactical_003: 0,
mount_000: 56, pistol_grip: 97, tactical: 87, scope: 17, reciever: 92,
sight_rear: 56, charge: 13, mount_001: 0, equipment: 30, gas_block: 100,
launcher: 0, sight_front: 25, stock_000: 0, foregrip: 0, tactical_000: 0,
nvg: 0, pistol_grip_akms: 100, stock_akms: 100, equipment_000: 0, equipment_001: 0,
equipment_002: 0, bipod: 0);
break;
case BotType.pmcBot:
bot.chances.mods = new Mods(muzzle: 67, barrel: 100, handguard: 97, stock: 81, magazine: 100,
mount: 35, flashlight: 100, tactical_001: 9, tactical_002: 0, tactical_003: 0,
mount_000: 28, pistol_grip: 97, tactical: 32, scope: 57, reciever: 100,
sight_rear: 58, charge: 81, mount_001: 38, equipment: 0, gas_block: 100,
launcher: 0, sight_front: 65, stock_000: 100, foregrip: 30, tactical_000: 18,
nvg: 25, pistol_grip_akms: 97, stock_akms: 0, equipment_000: 0, equipment_001: 0,
equipment_002: 0, bipod: 0);
break;
case BotType.marksman:
bot.chances.mods = new Mods(muzzle: 0, barrel: 100, handguard: 0, stock: 73, magazine: 100,
mount: 100, flashlight: 0, tactical_001: 0, tactical_002: 0, tactical_003: 0,
mount_000: 0, pistol_grip: 0, tactical: 33, scope: 89, reciever: 0,
sight_rear: 17, charge: 0, mount_001: 0, equipment: 0, gas_block: 0,
launcher: 0, sight_front: 25, stock_000: 0, foregrip: 0, tactical_000: 33,
nvg: 100, pistol_grip_akms: 0, stock_akms: 0, equipment_000: 0, equipment_001: 0,
equipment_002: 0, bipod: 0);
break;
}
}
public static void AddGenerationChances(Bot bot)
{
switch (bot.botType)
{
case BotType.assault:
case BotType.pmcBot:
case BotType.marksman:
bot.generation = new GenerationChances(0, 1, 1, 2, 0, 3, 2, 4, 0, 5);
break;
}
}
public static void AddEquipmentChances(Bot bot)
{
switch (bot.botType)
{
case BotType.assault:
bot.chances.equipment = new EquipmentChances(73, 0, 62, 28, 36, 0, 100, 38, 95, 0, 5, 72, 100, 100);
break;
case BotType.pmcBot:
bot.chances.equipment = new EquipmentChances(89, 56, 58, 49, 84, 0, 100, 58, 100, 0, 18, 0, 100, 100);
break;
case BotType.marksman:
bot.chances.equipment = new EquipmentChances(8, 8, 8, 42, 0, 0, 100, 25, 100, 0, 0, 33, 100, 100);
break;
}
}
}
}

View File

@ -0,0 +1,206 @@
using Generator.Models.Input;
using Generator.Models.Output;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Generator.Helpers.Gear
{
public static class GearHelpers
{
public static void AddEquippedMods(Bot botToUpdate, Datum rawParsedBot)
{
var modItemsInRawBot = new List<Item>();
var itemsWithModsInRawBot = new List<Item>();
modItemsInRawBot = rawParsedBot.Inventory.items
.Where(x => x.slotId != null && (x.slotId.StartsWith("mod_") || x.slotId.StartsWith("patron_in_weapon"))).ToList();
// get items with Mods by iterating over mod items and getting the parent item
itemsWithModsInRawBot.AddRange(modItemsInRawBot
.Select(modItem => rawParsedBot.Inventory.items
.Find(x => x._id == modItem.parentId)));
var itemsWithModsDictionary = botToUpdate.inventory.mods;
foreach (var itemToAdd in itemsWithModsInRawBot)
{
var modsToAdd = modItemsInRawBot.Where(x => x.parentId == itemToAdd._id).ToList();
AddItemToDictionary(itemToAdd, modsToAdd, itemsWithModsDictionary);
}
botToUpdate.inventory.mods = itemsWithModsDictionary;
}
public static void AddEquippedGear(Bot finalAssaultBot, Datum bot)
{
// add equipped gear
foreach (var inventoryItem in bot.Inventory.items)
{
switch (inventoryItem.slotId?.ToLower())
{
case "headwear":
finalAssaultBot.inventory.equipment.Headwear.AddUnique(inventoryItem._tpl);
break;
case "earpiece":
finalAssaultBot.inventory.equipment.Earpiece.AddUnique(inventoryItem._tpl);
break;
case "facecover":
finalAssaultBot.inventory.equipment.FaceCover.AddUnique(inventoryItem._tpl);
break;
case "armorvest":
finalAssaultBot.inventory.equipment.ArmorVest.AddUnique(inventoryItem._tpl);
break;
case "eyewear":
finalAssaultBot.inventory.equipment.Eyewear.AddUnique(inventoryItem._tpl);
break;
case "armband":
finalAssaultBot.inventory.equipment.ArmBand.AddUnique(inventoryItem._tpl);
break;
case "tacticalvest":
finalAssaultBot.inventory.equipment.TacticalVest.AddUnique(inventoryItem._tpl);
break;
case "backpack":
finalAssaultBot.inventory.equipment.Backpack.AddUnique(inventoryItem._tpl);
break;
case "firstprimaryweapon":
finalAssaultBot.inventory.equipment.FirstPrimaryWeapon.AddUnique(inventoryItem._tpl);
break;
case "secondprimaryweapon":
finalAssaultBot.inventory.equipment.SecondPrimaryWeapon.AddUnique(inventoryItem._tpl);
break;
case "holster":
finalAssaultBot.inventory.equipment.Holster.AddUnique(inventoryItem._tpl);
break;
case "scabbard":
finalAssaultBot.inventory.equipment.Scabbard.AddUnique(inventoryItem._tpl);
break;
case "pockets":
finalAssaultBot.inventory.equipment.Pockets.AddUnique(inventoryItem._tpl);
break;
case "securedcontainer":
finalAssaultBot.inventory.equipment.SecuredContainer.AddUnique(inventoryItem._tpl);
break;
default:
break;
}
}
}
public static void AddCartridges(Bot botToUpdate, Datum rawParsedBot)
{
var cartridgesInRawBot = rawParsedBot.Inventory.items
.Where(x => x.slotId?.StartsWith("cartridges") == true).ToList();
var cartridgeParentIds = cartridgesInRawBot.Select(x => x.parentId).ToList();
var itemsThatTakeCartridges = rawParsedBot.Inventory.items.Where(x => cartridgeParentIds.Contains(x._id)).ToList();
var itemsThatTakeCartridgesDict = CreateDictionaryPopulateWithMagazinesAndCartridges(itemsThatTakeCartridges, cartridgesInRawBot);
foreach (var item in itemsThatTakeCartridgesDict)
{
// Item exists update
if (botToUpdate.inventory.mods.ContainsKey(item.Key))
{
UpdateExistingMagazine(botToUpdate, item);
}
else // No item found, add fresh
{
AddNewMagazine(botToUpdate, item);
}
}
}
private static void AddItemToDictionary(
Item itemToAdd,
List<Item> modsToAdd,
Dictionary<string, Dictionary<string, List<string>>> itemsWithModsDict)
{
// item key already exists, need to merge mods
if (itemsWithModsDict.ContainsKey(itemToAdd._tpl))
{
foreach (var modItem in modsToAdd)
{
var itemToAddModsTo = itemsWithModsDict[itemToAdd._tpl];
// Item doesnt have this mod, add it then add template id
if (!itemToAddModsTo.ContainsKey(modItem.slotId))
{
// Mod doesnt exist on item
itemToAddModsTo.Add(modItem.slotId, new List<string>()); // add mod
itemToAddModsTo[modItem.slotId].AddUnique(modItem._tpl);
}
itemToAddModsTo[modItem.slotId].AddUnique(modItem._tpl); // add template id to it
}
}
else // item doesnt exist, create it
{
// Add base item
itemsWithModsDict.Add(itemToAdd._tpl, new Dictionary<string, List<string>>());
// Add mod types to item
foreach (var modItem in modsToAdd)
{
itemsWithModsDict[itemToAdd._tpl].Add(modItem.slotId, new List<string>());
}
// Get mod we're adding mod templateIds to
var modItems = itemsWithModsDict[itemToAdd._tpl];
foreach (var modItem in modsToAdd)
{
var modToUpdate = modItems[modItem.slotId];
modToUpdate.Add(modItem._tpl);
}
}
var result = JsonConvert.SerializeObject(itemsWithModsDict, Formatting.Indented);
}
private static void UpdateExistingMagazine(Bot botToUpdate, KeyValuePair<string, List<string>> item)
{
var existingmagazineItem = botToUpdate.inventory.mods[item.Key];
var cartridges = existingmagazineItem["cartridges"];
foreach (var itemToAdd in item.Value)
{
cartridges.AddUnique(itemToAdd);
}
}
private static void AddNewMagazine(Bot botToUpdate, KeyValuePair<string, List<string>> item)
{
var cartridgeDict = new Dictionary<string, List<string>>
{
{ "cartridges", item.Value }
};
botToUpdate.inventory.mods.Add(item.Key, cartridgeDict);
}
private static Dictionary<string, List<string>> CreateDictionaryPopulateWithMagazinesAndCartridges(List<Item> itemsThatTakeCartridges, List<Item> cartridgesInRawBot)
{
var itemsThatTakeCartridgesDict = new Dictionary<string, List<string>>();
foreach (var item in itemsThatTakeCartridges)
{
var cartridgeIdsToAdd = cartridgesInRawBot.Where(x => x.parentId == item._id).Select(x => x._tpl).ToList();
// magazine id already exists, probably has cartridges in it already
if (itemsThatTakeCartridgesDict.ContainsKey(item._tpl))
{
//get existing magazine and add new cartridges to it
var existingMagazine = itemsThatTakeCartridgesDict[item._tpl];
foreach (var cartridge in cartridgeIdsToAdd)
{
existingMagazine.AddUnique(cartridge);
}
}
else // No magazine found, add new magazine + associated cartridges
{
itemsThatTakeCartridgesDict.Add(item._tpl, cartridgeIdsToAdd);
}
}
return itemsThatTakeCartridgesDict;
}
}
}