AssortVerifier/AssortUpdater/AssortFlattener.cs

115 lines
4.4 KiB
C#

using AssortValidator.Common;
using AssortValidator.Common.Models;
namespace AssortUpdater
{
public static class AssortFlattener
{
public static List<FlatAssort> FlattenExistingAssorts(Assort existingAssorts)
{
var results = new List<FlatAssort>();
foreach (var assort in existingAssorts.items.Where(x => x.slotId == "hideout"))
{
var level = existingAssorts.loyal_level_items.FirstOrDefault(x => x.Key == assort._id);
var barterDetails = existingAssorts.barter_scheme.FirstOrDefault(x => x.Key == assort._id);
var firstBarterDetails = barterDetails.Value[0].First();
results.Add(new FlatAssort
{
AssortId = assort._id,
ItemId = assort._tpl,
ItemName = ItemTemplateHelper.GetNameById(assort._tpl),
Level = level.Value,
IsMoney = barterDetails.Value.Count == 1 && ItemTemplateHelper.IsMoney(firstBarterDetails._tpl),
BarterDetails = barterDetails.Value.First(),
});
}
// Add in sub items
foreach (var subAssort in existingAssorts.items.Where(x => x.slotId != "hideout"))
{
// find parent item in assort list we created
var assortToAddTo = results.Find(x => x.AssortId == subAssort.parentId);
if (assortToAddTo is null)
{
// check items subitems as some have mods of their own
foreach (var result in results)
{
if (result.SubItems.Any(x => x._id == subAssort.parentId))
{
assortToAddTo = result;
break;
}
}
}
assortToAddTo.SubItems.Add(subAssort);
}
return results;
}
public static List<FlatAssort> FlattenLiveAssorts(Assort liveAssorts)
{
var results = new List<FlatAssort>();
foreach (var assort in liveAssorts.items.Where(x => x.slotId == "hideout"))
{
var level = liveAssorts.loyal_level_items.FirstOrDefault(x => x.Key == assort._id);
var barterDetails = liveAssorts.barter_scheme.FirstOrDefault(x => x.Key == assort._id);
var firstBarterDetails = barterDetails.Value[0].First();
results.Add(new FlatAssort
{
AssortId = assort._id,
ItemId = assort._tpl,
ItemName = ItemTemplateHelper.GetNameById(assort._tpl),
Level = level.Value,
IsMoney = barterDetails.Value.Count == 1 && ItemTemplateHelper.IsMoney(firstBarterDetails._tpl),
BarterDetails = barterDetails.Value.First(),
});
}
// Add in sub items
foreach (var subAssort in liveAssorts.items.Where(x => x.slotId != "hideout"))
{
// find parent item in assort list we created
var assortToAddTo = results.Find(x => x.AssortId == subAssort.parentId);
if (assortToAddTo is null)
{
// check items subitems as some have mods of their own
foreach (var result in results)
{
if (result.SubItems.Any(x => x._id == subAssort.parentId))
{
assortToAddTo = result;
break;
}
}
}
assortToAddTo.SubItems.Add(subAssort);
}
return results;
}
public class FlatAssort
{
public FlatAssort()
{
SubItems = new List<Assort.Item>();
BarterDetails = new List<Assort.BarterDetails>();
}
public string AssortId { get; set; }
public string ItemId { get; set; }
public string ItemName { get; set; }
public List<Assort.Item> SubItems { get; set; }
public int Level { get; set; }
public bool IsMoney { get; set; }
public List<Assort.BarterDetails> BarterDetails { get; set; }
}
}
}