BotGenerator/Generator/BotLootGenerator.cs

201 lines
7.4 KiB
C#
Raw Normal View History

2023-09-18 17:15:30 +01:00
using System.Diagnostics;
using System.Linq;
2021-08-24 12:08:30 +01:00
using Common.Extensions;
using Common.Models.Input;
using Common.Models.Output;
using Generator.Helpers.Gear;
2021-08-12 16:52:06 +01:00
namespace Generator
{
2021-09-01 19:19:44 +03:00
public static class BotLootGenerator
2021-08-12 16:52:06 +01:00
{
2023-09-18 17:15:30 +01:00
internal static IEnumerable<Bot> AddLoot(this IEnumerable<Bot> botsWithGear, Dictionary<string, List<Datum>> rawBots)
2021-08-12 16:52:06 +01:00
{
var stopwatch = Stopwatch.StartNew();
LoggingHelpers.LogToConsole("Started processing bot loot");
2023-09-18 17:15:30 +01:00
var dictionaryLock = new object();
var tasks = new List<Task>(50);
foreach (var botToUpdate in botsWithGear)
2021-08-12 16:52:06 +01:00
{
2023-09-18 17:15:30 +01:00
tasks.Add(Task.Factory.StartNew(() =>
2021-08-17 18:33:55 +01:00
{
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))
{
Console.WriteLine($"Unable to find {botType} on rawBots data");
return;
}
}
2021-08-17 18:33:55 +01:00
2023-09-18 17:15:30 +01:00
if (rawBotsOfSameType.Count == 0)
{
return;
}
AddLootToContainers(botToUpdate, rawBotsOfSameType);
//foreach (var rawParsedBot in rawBotsOfSameType)
//{
// AddPocketLoot(botToUpdate, rawParsedBot);
//}
2023-09-18 17:15:30 +01:00
//AddTacticalVestLoot(botToUpdate, rawBotsOfSameType);
//AddBackpackLoot(botToUpdate, rawBotsOfSameType);
//AddSecureContainerLoot(botToUpdate, rawBotsOfSameType);
//AddSpecialLoot(botToUpdate);
2023-09-18 17:15:30 +01:00
}));
}
Task.WaitAll(tasks.ToArray());
2021-08-12 16:52:06 +01:00
stopwatch.Stop();
2021-08-12 21:31:15 +01:00
LoggingHelpers.LogToConsole($"Finished processing bot loot. Took: {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
2021-08-12 16:52:06 +01:00
2021-09-01 19:19:44 +03:00
return botsWithGear;
2021-08-12 16:52:06 +01:00
}
2021-09-05 12:27:17 +01:00
private static void AddPocketLoot(Bot botToUpdate, Datum rawBot)
{
// pocket loot
2021-09-05 12:27:17 +01:00
foreach (var lootItem in rawBot.Inventory.items.Where(x => x?.slotId?.StartsWith("pocket") == true))
{
2021-09-05 12:20:40 +01:00
botToUpdate.inventory.items.Pockets.AddUnique(lootItem._tpl);
}
}
2021-09-05 12:27:17 +01:00
private static void AddTacticalVestLoot(Bot botToUpdate, IEnumerable<Datum> rawBots)
2021-08-12 16:52:06 +01:00
{
2021-09-05 12:27:17 +01:00
var tacVestItems = GetItemsStoredInEquipmentItem(rawBots, "TacticalVest");
2021-09-05 12:20:40 +01:00
botToUpdate.inventory.items.TacticalVest.AddRange(tacVestItems);
2021-08-12 16:52:06 +01:00
}
2021-09-05 12:27:17 +01:00
private static void AddBackpackLoot(Bot botToUpdate, IEnumerable<Datum> rawBots)
2021-08-12 16:52:06 +01:00
{
// add generic keys to bosses
2021-09-05 12:20:40 +01:00
if (botToUpdate.botType.IsBoss())
{
2021-09-05 12:20:40 +01:00
botToUpdate.inventory.items.Backpack.AddRange(SpecialLootHelper.GetGenericBossKeys());
}
2021-09-05 12:27:17 +01:00
var backpackItems = GetItemsStoredInEquipmentItem(rawBots, "Backpack");
2021-09-05 12:20:40 +01:00
botToUpdate.inventory.items.Backpack.AddRange(backpackItems);
2021-08-12 16:52:06 +01:00
}
2021-09-05 12:27:17 +01:00
private static void AddSecureContainerLoot(Bot botToUpdate, IEnumerable<Datum> rawBots)
2021-08-12 16:52:06 +01:00
{
2021-09-05 12:27:17 +01:00
var tacVestItems = GetItemsStoredInEquipmentItem(rawBots, "SecuredContainer");
2021-09-05 12:20:40 +01:00
botToUpdate.inventory.items.SecuredContainer.AddRange(tacVestItems);
2021-08-12 16:52:06 +01:00
}
private static void AddLootToContainers(Bot botToUpdate, List<Datum> rawBotsOfSameType)
{
var containerDict = new Dictionary<string, List<string>>();
foreach (var bot in rawBotsOfSameType)
{
var backpack = bot.Inventory.items.FirstOrDefault(x => x.slotId == "Backpack");
if (backpack != null)
{
containerDict.Add(backpack._id, new List<string>());
}
var pocket = bot.Inventory.items.FirstOrDefault(x => x.slotId == "Pockets");
if (pocket != null)
{
containerDict.Add(pocket._id, new List<string>());
}
var secure = bot.Inventory.items.FirstOrDefault(x => x.slotId == "SecuredContainer");
if (secure != null)
{
containerDict.Add(secure._id, new List<string>());
}
var tacVest = bot.Inventory.items.FirstOrDefault(x => x.slotId == "TacticalVest");
if (tacVest != null)
{
containerDict.Add(tacVest._id, new List<string>());
}
foreach (var item in bot.Inventory.items)
{
if (item.parentId == null)
{
continue;
}
// Container (backpack etc) exists in dict
if (containerDict.ContainsKey(item.parentId))
{
containerDict[item.parentId].AddUnique(item._tpl);
}
}
if (backpack != null)
{
botToUpdate.inventory.items.Backpack.AddUniqueRange(containerDict[backpack._id]);
}
if (pocket != null)
{
botToUpdate.inventory.items.Pockets.AddUniqueRange(containerDict[pocket._id]);
}
if (secure != null)
{
botToUpdate.inventory.items.SecuredContainer.AddUniqueRange(containerDict[secure._id]);
}
if (tacVest != null)
{
botToUpdate.inventory.items.TacticalVest.AddUniqueRange(containerDict[tacVest._id]);
}
containerDict.Clear();
}
// Add generic keys to bosses
if (botToUpdate.botType.IsBoss())
{
var keys = SpecialLootHelper.GetGenericBossKeys().ToList();
botToUpdate.inventory.items.Backpack.AddUniqueRange(keys);
}
AddSpecialLoot(botToUpdate);
}
2021-09-01 19:19:44 +03:00
private static void AddSpecialLoot(Bot botToUpdate)
2021-08-12 16:52:06 +01:00
{
botToUpdate.inventory.items.SpecialLoot.AddRange(SpecialLootHelper.GetSpecialLootForBotType(botToUpdate.botType));
2021-08-12 16:52:06 +01:00
}
2021-09-05 12:27:17 +01:00
private static IEnumerable<string> GetItemsStoredInEquipmentItem(IEnumerable<Datum> rawBots, string containerName)
2021-08-12 16:52:06 +01:00
{
var itemsStoredInContainer = new List<string>();
var containers = new List<string>();
2021-09-05 12:27:17 +01:00
foreach (var bot in rawBots)
2021-08-12 16:52:06 +01:00
{
// find the container type we want on this bot (backpack etc)
// Add to list
var botContainers = bot.Inventory.items.Where(x => x.slotId == containerName);
foreach (var container in botContainers)
2021-08-12 16:52:06 +01:00
{
containers.AddUnique(container._id);
2021-08-12 16:52:06 +01:00
}
foreach (var item in bot.Inventory.items)
{
if (containers.Contains(item.parentId))
{
itemsStoredInContainer.AddUnique(item._tpl);
}
}
}
return itemsStoredInContainer;
}
}
}