using AssortValidator.Common; using AssortValidator.Common.Models; using static AssortValidator.Common.Models.Assort; namespace AssortUpdater { public static class AssortFlattener { public static List FlattenExistingAssorts(Assort existingAssorts) { var results = new List(); 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 FlattenLiveAssorts(Assort liveAssorts) { var results = new List(); 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 static Assort UnFlatten(List 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> ConvertToOutputBarterItemsCollection(FlatAssort assort) { var barterItems = new List(); foreach (var item in assort.BarterDetails) { barterItems.Add(new BarterDetails { _tpl = item._tpl, count = item.count, level = item.level, side = item.side, }); } return new List> { barterItems }; } } }