More work on item filter processing

This commit is contained in:
Ben Wallis
2015-12-28 17:30:34 +00:00
parent 89e98fc8c6
commit 1bdc8bf6fd
21 changed files with 420 additions and 216 deletions

View File

@@ -58,7 +58,7 @@
</ApplicationDefinition>
<Compile Include="Model\Item.cs" />
<Compile Include="Model\ItemCollection.cs" />
<Compile Include="Services\ItemBlockItemMatcher.cs" />
<Compile Include="Services\BlockItemMatcher.cs" />
<Compile Include="Services\ItemFilterProcessor.cs" />
<Compile Include="UserControls\ItemSocketsControl.xaml.cs">
<DependentUpon>ItemSocketsControl.xaml</DependentUpon>

View File

@@ -8,7 +8,6 @@ namespace Filtration.ItemFilterPreview.Model
{
public interface IItem
{
List<SocketGroup> SocketGroups { get; set; }
string ItemClass { get; set; }
string BaseType { get; set; }
int DropLevel { get; set; }
@@ -20,16 +19,23 @@ namespace Filtration.ItemFilterPreview.Model
int Sockets { get; }
int LinkedSockets { get; }
IEnumerable<SocketGroup> LinkedSocketGroups { get; }
List<SocketGroup> SocketGroups { get; set; }
}
public class Item : IItem
{
private List<SocketGroup> _socketGroups;
public Item(List<SocketGroup> socketGroups)
{
}
public string ItemClass { get; set; }
public string BaseType { get; set; }
public int DropLevel { get; set; }
public int ItemLevel { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public int Quality { get; set; }
public ItemRarity ItemRarity { get; set; }
public int Sockets { get; private set; }
public int LinkedSockets { get; private set; }
public IEnumerable<SocketGroup> LinkedSocketGroups
{
@@ -66,16 +72,5 @@ namespace Filtration.ItemFilterPreview.Model
LinkedSockets = value.Where(s => s.Linked).Max(s => s.Count);
}
}
public string ItemClass { get; set; }
public string BaseType { get; set; }
public int DropLevel { get; set; }
public int ItemLevel { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public int Quality { get; set; }
public ItemRarity ItemRarity { get; set; }
public int Sockets { get; private set; }
public int LinkedSockets { get; private set; }
}
}

View File

@@ -0,0 +1,154 @@
using System.Linq;
using Filtration.ItemFilterPreview.Model;
using Filtration.ObjectModel;
using Filtration.ObjectModel.BlockItemBaseTypes;
using Filtration.ObjectModel.BlockItemTypes;
namespace Filtration.ItemFilterPreview.Services
{
internal interface IBlockItemMatcher
{
bool ItemBlockMatch(IItemFilterBlock block, IItem item);
bool ItemBlockItemMatch(IItemFilterBlockItem blockItem, IItem item);
}
internal class BlockItemMatcher : IBlockItemMatcher
{
public bool ItemBlockMatch(IItemFilterBlock block, IItem item)
{
return block.BlockItems
.Where(blockItem => !(blockItem is IAudioVisualBlockItem) && !(blockItem is ActionBlockItem))
.All(blockItem => ItemBlockItemMatch(blockItem, item));
}
public bool ItemBlockItemMatch(IItemFilterBlockItem blockItem, IItem item)
{
var blockItemType = blockItem.GetType();
if (blockItemType == typeof (BaseTypeBlockItem))
return BaseTypeBlockItemMatch((BaseTypeBlockItem)blockItem, item);
if (blockItemType == typeof (ClassBlockItem))
return ClassBlockItemMatch((ClassBlockItem) blockItem, item);
if (blockItemType == typeof(DropLevelBlockItem))
return DropLevelBlockItemMatch((DropLevelBlockItem)blockItem, item);
if (blockItemType == typeof(HeightBlockItem))
return HeightBlockItemMatch((HeightBlockItem)blockItem, item);
if (blockItemType == typeof(ItemLevelBlockItem))
return ItemLevelBlockItemMatch((ItemLevelBlockItem)blockItem, item);
if (blockItemType == typeof(LinkedSocketsBlockItem))
return LinkedSocketsBlockItemMatch((LinkedSocketsBlockItem)blockItem, item);
if (blockItemType == typeof(QualityBlockItem))
return QualityBlockItemMatch((QualityBlockItem)blockItem, item);
if (blockItemType == typeof(RarityBlockItem))
return RarityBlockItemMatch((RarityBlockItem)blockItem, item);
if (blockItemType == typeof(SocketsBlockItem))
return SocketsBlockItemMatch((SocketsBlockItem)blockItem, item);
if (blockItemType == typeof(WidthBlockItem))
return WidthBlockItemMatch((WidthBlockItem)blockItem, item);
if (blockItemType == typeof(SocketGroupBlockItem))
return SocketGroupBlockItemMatch((SocketGroupBlockItem)blockItem, item);
return false;
}
private static bool BaseTypeBlockItemMatch(BaseTypeBlockItem baseTypeBlockItem, IItem item)
{
return baseTypeBlockItem.Items.Any(b => item.BaseType.StartsWith(b));
}
private static bool ClassBlockItemMatch(ClassBlockItem classBlockItem, IItem item)
{
return classBlockItem.Items.Any(c => item.ItemClass.StartsWith(c));
}
private static bool DropLevelBlockItemMatch(DropLevelBlockItem dropLevelBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(dropLevelBlockItem, item.DropLevel);
}
private static bool HeightBlockItemMatch(HeightBlockItem heightBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(heightBlockItem, item.Height);
}
private static bool ItemLevelBlockItemMatch(ItemLevelBlockItem itemLevelBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(itemLevelBlockItem, item.ItemLevel);
}
private static bool LinkedSocketsBlockItemMatch(LinkedSocketsBlockItem linkedSocketsBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(linkedSocketsBlockItem, item.LinkedSockets);
}
private static bool QualityBlockItemMatch(QualityBlockItem qualityBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(qualityBlockItem, item.Quality);
}
private static bool RarityBlockItemMatch(RarityBlockItem qualityBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(qualityBlockItem, (int)item.ItemRarity);
}
private static bool SocketsBlockItemMatch(SocketsBlockItem socketsBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(socketsBlockItem, item.Sockets);
}
private static bool WidthBlockItemMatch(WidthBlockItem widthBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(widthBlockItem, item.Width);
}
private static bool SocketGroupBlockItemMatch(SocketGroupBlockItem socketGroupBlockItem, IItem item)
{
foreach (var blockItemSocketGroup in socketGroupBlockItem.SocketGroups) // for each group of sockets in the block item
{
foreach (var itemLinkedSocketGroup in item.LinkedSocketGroups) // for each linked socket group in the item
{
if (SocketGroupHasRequiredSocketColors(itemLinkedSocketGroup, blockItemSocketGroup))
{
return true;
}
}
}
return false;
}
private static bool SocketGroupHasRequiredSocketColors(SocketGroup itemLinkedSocketGroup, SocketGroup blockItemSocketGroup)
{
var blockSocketGroupColorCounts = blockItemSocketGroup.GroupBy(i => i.Color, (key, values) => new { SocketColor = key, Count = values.Count() }).ToList();
var itemSocketGroupColorCounts = itemLinkedSocketGroup.GroupBy(i => i.Color, (key, values) => new {SocketColor = key, Count = values.Count()}).ToList();
foreach (var blockItemSocketColorCount in blockSocketGroupColorCounts)
{
var match = itemSocketGroupColorCounts.FirstOrDefault(i => i.SocketColor == blockItemSocketColorCount.SocketColor && i.Count >= blockItemSocketColorCount.Count);
if (match == null)
{
return false;
}
}
return true;
}
private static bool NumericFilterPredicateBlockItemMatch<T>(T numericFilterPredicateBlockItem, int matchValue) where T : NumericFilterPredicateBlockItem
{
return numericFilterPredicateBlockItem.FilterPredicate.CompareUsing(matchValue);
}
}
}

View File

@@ -1,112 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Filtration.ItemFilterPreview.Model;
using Filtration.ObjectModel;
using Filtration.ObjectModel.BlockItemBaseTypes;
using Filtration.ObjectModel.BlockItemTypes;
using Filtration.ObjectModel.Enums;
using Filtration.ObjectModel.Extensions;
namespace Filtration.ItemFilterPreview.Services
{
internal interface IItemBlockItemMatcher
{
bool BaseTypeBlockItemMatch(BaseTypeBlockItem baseTypeBlockItem, IItem item);
bool ClassBlockItemMatch(ClassBlockItem classBlockItem, IItem item);
bool DropLevelBlockItemMatch(DropLevelBlockItem dropLevelBlockItem, IItem item);
}
internal class ItemBlockItemMatcher : IItemBlockItemMatcher
{
public bool BaseTypeBlockItemMatch(BaseTypeBlockItem baseTypeBlockItem, IItem item)
{
return baseTypeBlockItem.Items.Any(b => item.BaseType.StartsWith(b));
}
public bool ClassBlockItemMatch(ClassBlockItem classBlockItem, IItem item)
{
return classBlockItem.Items.Any(c => item.ItemClass.StartsWith(c));
}
public bool DropLevelBlockItemMatch(DropLevelBlockItem dropLevelBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(dropLevelBlockItem, item.DropLevel);
}
public bool HeightBlockItemMatch(HeightBlockItem heightBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(heightBlockItem, item.Height);
}
public bool ItemLevelBlockItemMatch(ItemLevelBlockItem itemLevelBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(itemLevelBlockItem, item.ItemLevel);
}
public bool LinkedSocketsBlockItemMatch(LinkedSocketsBlockItem linkedSocketsBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(linkedSocketsBlockItem, item.LinkedSockets);
}
public bool QualityBlockItemMatch(QualityBlockItem qualityBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(qualityBlockItem, item.Quality);
}
public bool RarityBlockItemMatch(RarityBlockItem qualityBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(qualityBlockItem, (int)item.ItemRarity);
}
public bool SocketsBlockItemMatch(SocketsBlockItem socketsBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(socketsBlockItem, item.Sockets);
}
public bool WidthBlockItemMatch(WidthBlockItem widthBlockItem, IItem item)
{
return NumericFilterPredicateBlockItemMatch(widthBlockItem, item.Width);
}
public bool SocketGroupBlockItemMatch(SocketGroupBlockItem socketGroupBlockItem, IItem item)
{
foreach (var blockItemSocketGroup in socketGroupBlockItem.SocketGroups) // for each group of sockets in the block item
{
foreach (var itemLinkedSocketGroup in item.LinkedSocketGroups) // for each linked socket group in the item
{
if (SocketGroupHasRequiredSocketColors(itemLinkedSocketGroup, blockItemSocketGroup))
{
return true;
}
}
}
return false;
}
private bool SocketGroupHasRequiredSocketColors(SocketGroup itemLinkedSocketGroup, SocketGroup blockItemSocketGroup)
{
var blockSocketGroupColorCounts = blockItemSocketGroup.GroupBy(i => i.Color, (key, values) => new { SocketColor = key, Count = values.Count() }).ToList();
var itemSocketGroupColorCounts = itemLinkedSocketGroup.GroupBy(i => i.Color, (key, values) => new {SocketColor = key, Count = values.Count()}).ToList();
foreach (var blockItemSocketColorCount in blockSocketGroupColorCounts)
{
var match = itemSocketGroupColorCounts.FirstOrDefault(i => i.SocketColor == blockItemSocketColorCount.SocketColor && i.Count >= blockItemSocketColorCount.Count);
if (match == null)
{
return false;
}
}
return true;
}
private bool NumericFilterPredicateBlockItemMatch<T>(T numericFilterPredicateBlockItem, int matchValue) where T : NumericFilterPredicateBlockItem
{
return numericFilterPredicateBlockItem.FilterPredicate.CompareUsing(matchValue);
}
}
}

View File

@@ -1,34 +1,38 @@
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Filtration.ItemFilterPreview.Model;
using Filtration.ObjectModel;
using Filtration.ObjectModel.BlockItemBaseTypes;
using Filtration.ObjectModel.BlockItemTypes;
using Filtration.ObjectModel.Enums;
namespace Filtration.ItemFilterPreview.Services
{
public class ItemFilterProcessor
internal class ItemFilterProcessor
{
private IItemFilterScript _itemFilterScript;
private readonly IBlockItemMatcher _blockItemMatcher;
public ItemFilterProcessor()
internal ItemFilterProcessor(IBlockItemMatcher blockItemMatcher)
{
_blockItemMatcher = blockItemMatcher;
}
public void LoadItemFilterScript(IItemFilterScript itemFilterScript)
public IReadOnlyDictionary<IItem, IItemFilterBlock> ProcessItemsAgainstItemFilterScript(IItemFilterScript itemFilterScript, IEnumerable<IItem> items)
{
_itemFilterScript = itemFilterScript;
}
var matchedItemBlockPairs = new Dictionary<IItem, IItemFilterBlock>();
public void ItemIsVisible(IItem item)
{
foreach (var block in _itemFilterScript.ItemFilterBlocks)
var sw = Stopwatch.StartNew();
foreach (var item in items)
{
}
}
sw.Restart();
var matchedBlock = itemFilterScript.ItemFilterBlocks.FirstOrDefault(block => _blockItemMatcher.ItemBlockMatch(block, item));
matchedItemBlockPairs.Add(item, matchedBlock);
Debug.WriteLine("Processed Item in {0}ms", sw.ElapsedMilliseconds);
}
sw.Stop();
return matchedItemBlockPairs;
}
}
}