Skip to content

Commit

Permalink
Merge pull request #382 from Sidekick-Poe/feature/uncut-gems
Browse files Browse the repository at this point in the history
Implemented uncut gem functionality
  • Loading branch information
leMicin authored Dec 24, 2024
2 parents 541f61c + 76a6599 commit 265269d
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 169 deletions.
2 changes: 1 addition & 1 deletion src/Sidekick.Apis.Poe/ITradeFilterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public interface ITradeFilterService
/// </summary>
/// <param name="item">The item for which to get property filters.</param>
/// <returns>The property filters.</returns>
PropertyFilters GetPropertyFilters(Item item);
Task<PropertyFilters> GetPropertyFilters(Item item);
}
}
11 changes: 6 additions & 5 deletions src/Sidekick.Apis.Poe/Metadata/IInvariantMetadataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Sidekick.Common.Game.Items;
using Sidekick.Common.Initialization;

namespace Sidekick.Apis.Poe.Metadata
namespace Sidekick.Apis.Poe.Metadata;

public interface IInvariantMetadataProvider : IInitializableService
{
public interface IInvariantMetadataProvider : IInitializableService
{
Dictionary<string, ItemMetadata> IdDictionary { get; }
}
Dictionary<string, ItemMetadata> IdDictionary { get; }

List<string> UncutGemIds { get; }
}
17 changes: 17 additions & 0 deletions src/Sidekick.Apis.Poe/Metadata/InvariantMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ ISettingsService settingsService
{
public Dictionary<string, ItemMetadata> IdDictionary { get; } = new();

public List<string> UncutGemIds { get; } = [];

/// <inheritdoc/>
public int Priority => 100;

Expand All @@ -46,6 +48,8 @@ public async Task Initialize()
{
FillPattern(game, result.Result, category.Key, category.Value.Category);
}

InitializeUncutGemIds();
}

private void FillPattern(GameType game, List<ApiCategory> categories, string id, Category category)
Expand Down Expand Up @@ -93,5 +97,18 @@ private static Rarity GetRarityForCategory(Category category, ApiItem item)
_ => Rarity.Unknown
};
}

private void InitializeUncutGemIds()
{
UncutGemIds.Clear();

foreach (var item in IdDictionary.Values)
{
if(item.Type == "Uncut Skill Gem" || item.Type == "Uncut Spirit Gem" || item.Type == "Uncut Support Gem")
{
UncutGemIds.Add(item.Id);
}
}
}
}
}
71 changes: 49 additions & 22 deletions src/Sidekick.Apis.Poe/Parser/Headers/HeaderParser.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Text.RegularExpressions;
using FuzzySharp;
using FuzzySharp.SimilarityRatio;
using FuzzySharp.SimilarityRatio.Scorer.StrategySensitive;
using Microsoft.Extensions.Logging;
using Sidekick.Apis.Poe.Filters;
using Sidekick.Apis.Poe.Fuzzy;
using Sidekick.Apis.Poe.Parser.Headers.Models;
using Sidekick.Common.Exceptions;
using Sidekick.Apis.Poe.Parser.Patterns;
using Sidekick.Common.Game.Items;
using Sidekick.Common.Game.Languages;

Expand All @@ -15,42 +15,44 @@ public class HeaderParser
(
IGameLanguageProvider gameLanguageProvider,
IFuzzyService fuzzyService,
IFilterProvider filterProvider,
ILogger<HeaderParser> logger
IFilterProvider filterProvider
) : IHeaderParser
{
public int Priority => 100;

private List<ItemCategory> ItemCategories { get; set; } = [];

private Dictionary<Rarity, Regex> RarityPatterns { get; set; } = [];

public Task Initialize()
{
InitializeItemCategories();
InitializeRarityPatterns();
return Task.CompletedTask;
}

private void InitializeItemCategories()
{
ItemCategories = filterProvider.TypeCategoryOptions.ConvertAll(x => new ItemCategory()
{
Id = x.Id,
Text = x.Text,
FuzzyText = fuzzyService.CleanFuzzyText(x.Text),
});

return Task.CompletedTask;
}

public Header Parse(string itemText)
private void InitializeRarityPatterns()
{
if (string.IsNullOrEmpty(itemText))
RarityPatterns = new Dictionary<Rarity, Regex>
{
throw new UnparsableException();
}

try
{
return Parse(new ParsingItem(itemText));
}
catch (Exception e)
{
logger.LogWarning(e, "Could not parse item.");
throw new UnparsableException();
}
{ Rarity.Normal, gameLanguageProvider.Language.RarityNormal.ToRegexEndOfLine() },
{ Rarity.Magic, gameLanguageProvider.Language.RarityMagic.ToRegexEndOfLine() },
{ Rarity.Rare, gameLanguageProvider.Language.RarityRare.ToRegexEndOfLine() },
{ Rarity.Unique, gameLanguageProvider.Language.RarityUnique.ToRegexEndOfLine() },
{ Rarity.Currency, gameLanguageProvider.Language.RarityCurrency.ToRegexEndOfLine() },
{ Rarity.Gem, gameLanguageProvider.Language.RarityGem.ToRegexEndOfLine() },
{ Rarity.DivinationCard, gameLanguageProvider.Language.RarityDivinationCard.ToRegexEndOfLine() }
};
}

public Header Parse(ParsingItem parsingItem)
Expand All @@ -76,13 +78,38 @@ public Header Parse(ParsingItem parsingItem)
apiItemCategoryId = null;
}

string? type = null;
if (parsingItem.Blocks[0].Lines.Count >= 2)
{
type = parsingItem.Blocks[0].Lines[^1].Text;
}

string? name = null;
if (parsingItem.Blocks[0].Lines.Count >= 3)
{
name = parsingItem.Blocks[0].Lines[^2].Text;
}

return new Header()
{
Name = parsingItem.Blocks[0].Lines.ElementAtOrDefault(2)?.Text,
Type = parsingItem.Blocks[0].Lines.ElementAtOrDefault(3)?.Text,
Name = name,
Type = type,
ItemCategory = apiItemCategoryId
};
}


public Rarity ParseRarity(ParsingItem parsingItem)
{
foreach (var pattern in RarityPatterns)
{
if (pattern.Value.IsMatch(parsingItem.Blocks[0].Lines[1].Text))
{
return pattern.Key;
}
}

return Rarity.Unknown;
}
}

2 changes: 2 additions & 0 deletions src/Sidekick.Apis.Poe/Parser/Headers/IHeaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ namespace Sidekick.Apis.Poe.Parser.Headers;
public interface IHeaderParser : IInitializableService
{
Header Parse(ParsingItem parsingItem);

Rarity ParseRarity(ParsingItem parsingItem);
}
19 changes: 3 additions & 16 deletions src/Sidekick.Apis.Poe/Parser/Metadata/MetadataParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.RegularExpressions;
using Sidekick.Apis.Poe.Metadata;
using Sidekick.Apis.Poe.Parser.Patterns;
using Sidekick.Apis.Poe.Parser.Headers;
using Sidekick.Common.Game.Items;
using Sidekick.Common.Game.Languages;

Expand All @@ -9,7 +9,7 @@ namespace Sidekick.Apis.Poe.Parser.Metadata
public class MetadataParser
(
IGameLanguageProvider gameLanguageProvider,
IParserPatterns parserPatterns,
IHeaderParser headerParser,
IMetadataProvider data
) : IMetadataParser
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public Task Initialize()
var parsingBlock = parsingItem.Blocks.First();
parsingBlock.Parsed = true;

var itemRarity = GetRarity(parsingBlock);
var itemRarity = headerParser.ParseRarity(parsingItem);

var canBeVaalGem = itemRarity == Rarity.Gem && parsingItem.Blocks.Count > 7;
if (canBeVaalGem && data.NameAndTypeDictionary.TryGetValue(parsingItem.Blocks[5].Lines[0].Text, out var vaalGem))
Expand Down Expand Up @@ -169,18 +169,5 @@ public Task Initialize()

return orderedResults.FirstOrDefault();
}

private Rarity GetRarity(ParsingBlock parsingBlock)
{
foreach (var pattern in parserPatterns.Rarity)
{
if (pattern.Value.IsMatch(parsingBlock.Lines[1].Text))
{
return pattern.Key;
}
}

return Rarity.Unknown;
}
}
}
2 changes: 0 additions & 2 deletions src/Sidekick.Apis.Poe/Parser/Patterns/IParserPatterns.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game.Items;
using Sidekick.Common.Initialization;

namespace Sidekick.Apis.Poe.Parser.Patterns
Expand All @@ -10,7 +9,6 @@ public interface IParserPatterns : IInitializableService
Regex Elder { get; }
Regex Hunter { get; }
Regex Requirements { get; }
Dictionary<Rarity, Regex> Rarity { get; }
Regex Redeemer { get; }
Regex Shaper { get; }
Regex Warlord { get; }
Expand Down
22 changes: 0 additions & 22 deletions src/Sidekick.Apis.Poe/Parser/Patterns/ParserPatterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class ParserPatterns(IGameLanguageProvider gameLanguageProvider) : IParse
/// <inheritdoc/>
public Task Initialize()
{
InitHeader();
InitInfluences();

Requirements = gameLanguageProvider.Language.DescriptionRequirements.ToRegexLine();
Expand All @@ -22,27 +21,6 @@ public Task Initialize()

#region Header (Rarity, Name, Type)

private void InitHeader()
{
Rarity = new Dictionary<Rarity, Regex>
{
{ Common.Game.Items.Rarity.Normal, gameLanguageProvider.Language.RarityNormal.ToRegexEndOfLine() },
{ Common.Game.Items.Rarity.Magic, gameLanguageProvider.Language.RarityMagic.ToRegexEndOfLine() },
{ Common.Game.Items.Rarity.Rare, gameLanguageProvider.Language.RarityRare.ToRegexEndOfLine() },
{ Common.Game.Items.Rarity.Unique, gameLanguageProvider.Language.RarityUnique.ToRegexEndOfLine() },
{ Common.Game.Items.Rarity.Currency, gameLanguageProvider.Language.RarityCurrency.ToRegexEndOfLine() },
{ Common.Game.Items.Rarity.Gem, gameLanguageProvider.Language.RarityGem.ToRegexEndOfLine() },
{ Common.Game.Items.Rarity.DivinationCard, gameLanguageProvider.Language.RarityDivinationCard.ToRegexEndOfLine() }
};

}

public Dictionary<Rarity, Regex> Rarity { get; private set; } = null!;

public Regex Scourged { get; private set; } = null!;

public Regex IsRelic { get; private set; } = null!;

public Regex Requirements { get; private set; } = null!;

#endregion Header (Rarity, Name, Type)
Expand Down
18 changes: 8 additions & 10 deletions src/Sidekick.Apis.Poe/Trade/Models/PropertyFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@ public PropertyFilter(
PropertyFilterType type,
string text,
object value,
decimal? min = null,
decimal? max = null)
double? normalizeValue)
{
Checked = @checked;
Type = type;
Text = text;
Value = value;
Max = max;
NormalizeEnabled = normalizeValue != null;
NormalizeValue = normalizeValue ?? 0;

if (min.HasValue)
if (@checked == true)
{
Min = min;
}

if (max.HasValue)
{
Max = max;
NormalizeMinValue();
}
}

Expand All @@ -40,6 +35,9 @@ public PropertyFilter(
public string Text { get; }

public object Value { get; }

private bool NormalizeEnabled { get; }

public double NormalizeValue { get; set; }

public FilterValueType ValueType
Expand Down
3 changes: 3 additions & 0 deletions src/Sidekick.Apis.Poe/Trade/Requests/Filters/MiscFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ internal class MiscFilters
[JsonPropertyName("gem_alternate_quality")]
public SearchFilterOption? GemQualityType { get; set; }

/// <remarks>
/// The item level filter for Path of Exile 1 is inside the misc filters instead of the type filters.
/// </remarks>
[JsonPropertyName("ilvl")]
public SearchFilterValue? ItemLevel { get; set; }

Expand Down
9 changes: 9 additions & 0 deletions src/Sidekick.Apis.Poe/Trade/Requests/Filters/TypeFilters.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
using System.Text.Json.Serialization;

namespace Sidekick.Apis.Poe.Trade.Requests.Filters
{
internal class TypeFilters
{
public SearchFilterOption? Category { get; set; }

public SearchFilterOption? Rarity { get; set; }

/// <remarks>
/// The item level filter for Path of Exile 2 is inside the type filters instead of the misc filters.
/// </remarks>
[JsonPropertyName("ilvl")]
public SearchFilterValue? ItemLevel { get; set; }
}
}
Loading

0 comments on commit 265269d

Please sign in to comment.