AssortVerifier/AssortUpdater/AssortFlattener.cs

162 lines
6.0 KiB
C#
Raw Normal View History

using AssortValidator.Common;
using AssortValidator.Common.Models;
using static AssortValidator.Common.Models.Assort;
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(),
RawItem = assort
});
}
// 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(),
RawItem = assort
});
}
// 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 Assort.Item RawItem { get;set;}
public int Level { get; set; }
public bool IsMoney { get; set; }
public List<Assort.BarterDetails> BarterDetails { get; set; }
}
public static Assort UnFlatten(List<FlatAssort> flatAssorts)
{
var result = new Assort();
foreach(var assort in flatAssorts)
{
// add loyalty level key/value
result.loyal_level_items.Add(assort.AssortId, assort.Level);
// add barter scheme details
var barterItemsList = ConvertToOutputBarterItemsCollection(assort);
result.barter_scheme.Add(assort.AssortId, barterItemsList);
// add base item
result.items.Add(assort.RawItem);
// add sub items
foreach (var item in assort.SubItems)
{
result.items.Add(item);
}
}
return result;
}
private static List<List<BarterDetails>> ConvertToOutputBarterItemsCollection(FlatAssort assort)
{
var barterItems = new List<BarterDetails>();
foreach (var item in assort.BarterDetails)
{
barterItems.Add(new BarterDetails
{
_tpl = item._tpl,
2022-01-15 17:20:38 +00:00
count = item.count,
level = item.level,
side = item.side,
});
}
return new List<List<BarterDetails>>
{
barterItems
};
}
}
}