2022-01-13 15:22:40 +00:00
using AssortValidator.Common.Helpers ;
2022-01-19 20:22:59 +00:00
using AssortValidator.Common.Models ;
2022-01-13 15:22:40 +00:00
using static AssortUpdater . AssortFlattener ;
namespace AssortUpdater
{
public static class AssortMerger
{
public static List < FlatAssort > CreateMergedFlattenedAssorts ( string trader , List < FlatAssort > flattenedLiveAssorts , List < FlatAssort > flattenedExistingAssorts )
{
var completeAssorts = new List < FlatAssort > ( ) ;
2022-01-15 15:00:21 +00:00
// Add live assorts to merged list
2022-01-13 15:22:40 +00:00
foreach ( var liveAssort in flattenedLiveAssorts )
{
2022-01-15 17:21:24 +00:00
if ( liveAssort . RawItem . upd . StackObjectsCount = = 0 )
{
var newStackCount = liveAssort . RawItem . upd . BuyRestrictionMax . HasValue
? ( liveAssort . RawItem . upd . BuyRestrictionMax . Value * 10 )
: 1000 ;
liveAssort . RawItem . upd . StackObjectsCount = newStackCount ;
LoggingHelpers . LogWarning ( $"live assort: {liveAssort.ItemName} level: {liveAssort.Level} has a stack of 0, adding a new value of {newStackCount}" ) ;
}
2022-02-03 10:28:02 +00:00
if ( liveAssort . RawItem . upd . BuyRestrictionCurrent > 0 )
{
liveAssort . RawItem . upd . BuyRestrictionCurrent = 0 ;
}
2022-01-13 15:22:40 +00:00
completeAssorts . Add ( liveAssort ) ;
}
LoggingHelpers . LogSuccess ( $"{trader} Added {flattenedLiveAssorts.Count} items from live" ) ;
2022-01-14 11:51:57 +00:00
LoggingHelpers . LogNewLine ( ) ;
2022-01-13 15:22:40 +00:00
// Check for missing existing assorts and add those
var ExistingItemCount = 0 ;
foreach ( var existingAssort in flattenedExistingAssorts )
{
var matchInCompleteAssorts = GetMatchingAssortFromCompleteList ( trader , existingAssort , completeAssorts ) ;
if ( matchInCompleteAssorts ! = null )
{
// Match found, dont add dupe item
continue ;
}
// no assort found, add
LoggingHelpers . LogWarning ( $"{trader} Adding Item found in existing assorts but not live: {existingAssort.ItemId} {existingAssort.ItemName} Level: {existingAssort.Level}" ) ;
completeAssorts . Add ( existingAssort ) ;
ExistingItemCount + + ;
}
2022-01-14 11:51:57 +00:00
LoggingHelpers . LogNewLine ( ) ;
2022-01-13 15:22:40 +00:00
LoggingHelpers . LogSuccess ( $"{trader} Added {ExistingItemCount} existing items" ) ;
return completeAssorts ;
}
private static FlatAssort ? GetMatchingAssortFromCompleteList ( string traderName , FlatAssort assortToFind , List < FlatAssort > completeAssorts )
{
2022-01-15 15:00:21 +00:00
IEnumerable < FlatAssort > ? existingAssort = GetAssortByItemId ( assortToFind , completeAssorts ) ;
if ( existingAssort . Any ( ) & & existingAssort . Count ( ) = = 1 )
{
// one match found by id, dont add
return existingAssort . First ( ) ;
}
existingAssort = GetAssortByItemIdAndLevel ( assortToFind , completeAssorts ) ;
2022-01-13 15:22:40 +00:00
if ( SingleAssortFound ( existingAssort ) )
{
//LoggingHelpers.LogSuccess($"{traderName} match found for item: {assortToFind.ItemId} {assortToFind.ItemName} Level: {assortToFind.Level}");
return existingAssort . First ( ) ;
}
if ( MultipleAssortsFound ( existingAssort ) )
{
// Multiple matches found, be more specific
existingAssort = GetAssortByItemIdLevelAndMoney ( assortToFind , completeAssorts ) ;
if ( SingleAssortFound ( existingAssort ) )
{
return existingAssort . First ( ) ;
}
if ( MultipleAssortsFound ( existingAssort ) )
{
// Multiple matches found, be more specific...again
existingAssort = GetAssortByItemIdLevelMoneySubItemCountAndCostItemCount ( assortToFind , completeAssorts ) ;
if ( SingleAssortFound ( existingAssort ) )
{
return existingAssort . First ( ) ;
}
}
}
existingAssort = GetAssortByBarterCostAndCount ( assortToFind , completeAssorts ) ;
if ( SingleAssortFound ( existingAssort ) )
{
return existingAssort . First ( ) ;
}
2022-01-15 17:21:24 +00:00
existingAssort = GetAssortByItemIdIsMoneyAndSubItemCount ( assortToFind , completeAssorts ) ;
if ( MultipleAssortsFound ( existingAssort ) )
{
throw new Exception ( "oh shit" ) ;
}
if ( SingleAssortFound ( existingAssort ) )
{
return existingAssort . First ( ) ;
}
2022-01-13 15:22:40 +00:00
//LoggingHelpers.LogError($"{traderName} No match found for item: {assortToFind.ItemId} {assortToFind.ItemName} Level: {assortToFind.Level}");
// No matches found, probably doesnt exist
return null ;
}
private static bool SingleAssortFound ( IEnumerable < FlatAssort > existingAssort )
{
return existingAssort ? . Count ( ) = = 1 ;
}
private static bool MultipleAssortsFound ( IEnumerable < FlatAssort > ? existingAssort )
{
return existingAssort ? . Count ( ) > 1 ;
}
private static IEnumerable < FlatAssort > GetAssortByItemIdLevelMoneySubItemCountAndCostItemCount ( FlatAssort assortToFind , List < FlatAssort > completeAssorts )
{
return completeAssorts . Where ( x = > x . ItemId = = assortToFind . ItemId
2022-01-15 17:21:24 +00:00
& & x . Level = = assortToFind . Level
& & x . IsMoney = = assortToFind . IsMoney
& & x . SubItems . Count = = assortToFind . SubItems . Count
& & x . BarterDetails [ 0 ] . count = = assortToFind . BarterDetails [ 0 ] . count ) ;
2022-01-13 15:22:40 +00:00
}
2022-01-15 15:00:21 +00:00
private static IEnumerable < FlatAssort > GetAssortByItemId ( FlatAssort assortToFind , List < FlatAssort > completeAssorts )
{
2022-01-15 17:21:24 +00:00
return completeAssorts . Where ( x = > x . ItemId = = assortToFind . ItemId
& & x . AssortId ! = assortToFind . AssortId ) ;
2022-01-15 15:00:21 +00:00
}
2022-01-13 15:22:40 +00:00
private static IEnumerable < FlatAssort > GetAssortByItemIdAndLevel ( FlatAssort assortToFind , List < FlatAssort > completeAssorts )
{
return completeAssorts . Where ( x = > x . ItemId = = assortToFind . ItemId
2022-01-15 17:21:24 +00:00
& & x . Level = = assortToFind . Level ) ;
2022-01-13 15:22:40 +00:00
}
private static IEnumerable < FlatAssort > GetAssortByItemIdLevelAndMoney ( FlatAssort assortToFind , List < FlatAssort > completeAssorts )
{
return completeAssorts . Where ( x = > x . ItemId = = assortToFind . ItemId
2022-01-15 17:21:24 +00:00
& & x . Level = = assortToFind . Level
& & x . IsMoney = = assortToFind . IsMoney ) ;
}
private static IEnumerable < FlatAssort > ? GetAssortByItemIdIsMoneyAndSubItemCount ( FlatAssort assortToFind , List < FlatAssort > completeAssorts )
{
return completeAssorts . Where ( x = > x . ItemId = = assortToFind . ItemId
& & x . IsMoney = = assortToFind . IsMoney
& & x . SubItems . Count = = assortToFind . SubItems . Count
& & x . BarterDetails . Count = = assortToFind . BarterDetails . Count ) ;
2022-01-13 15:22:40 +00:00
}
private static IEnumerable < FlatAssort > ? GetAssortByBarterCostAndCount ( FlatAssort assortToFind , List < FlatAssort > completeAssorts )
{
var itemsThatHaveSameCost = completeAssorts . Where ( x = > x . ItemId = = assortToFind . ItemId
& & x . Level = = assortToFind . Level ) ;
foreach ( var item in itemsThatHaveSameCost )
{
var matches = 0 ;
foreach ( var barter in item . BarterDetails )
{
var match = assortToFind . BarterDetails . Any ( x = > x . _tpl = = barter . _tpl & & x . count = = barter . count ) ;
if ( match )
{
matches + + ;
// collection already has item
}
}
if ( matches = = assortToFind . BarterDetails . Count )
{
//LoggingHelpers.LogSuccess($"{traderName} item found with same cost: {assortToFind.ItemId} {assortToFind.ItemName} Level: {assortToFind.Level}");
return new List < FlatAssort > ( ) { item } ;
}
}
return null ;
}
2022-01-15 15:02:37 +00:00
public static void ListDuplicatesInMergedAssorts ( List < FlatAssort > flatAssorts )
{
foreach ( var item in flatAssorts )
{
2022-01-16 11:21:35 +00:00
var dupes = flatAssorts . Where ( x = > x . ItemId = = item . ItemId
& & x . IsMoney = = item . IsMoney
& & x . BarterDetails [ 0 ] . _tpl = = item . BarterDetails [ 0 ] . _tpl
& & x . AssortId ! = item . AssortId ) ;
2022-01-15 15:02:37 +00:00
if ( dupes . Any ( ) )
{
LoggingHelpers . LogError ( $"{dupes.Count()} Dupes found for assort {item.AssortId} item: {item.ItemName} id:{item.ItemId} level: {item.Level} isMoney: {item.IsMoney}" ) ;
foreach ( var dupeItem in dupes )
{
LoggingHelpers . LogError ( $"Dupe assort{dupeItem.AssortId}: {dupeItem.ItemId} - {dupeItem.ItemName} id:{item.ItemId} level: {dupeItem.Level} isMoney:{dupeItem.IsMoney}" ) ;
}
}
}
}
2022-01-13 15:22:40 +00:00
}
}