Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored property parsing #461

Merged
merged 16 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Sidekick.Common.Initialization;

namespace Sidekick.Apis.Poe.Authentication
{
public interface IAuthenticationService
Expand Down
2 changes: 0 additions & 2 deletions src/Sidekick.Apis.Poe/Clients/States/ApiState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Sidekick.Common.Enums;

namespace Sidekick.Apis.Poe.Clients.States
{
public enum ApiState
Expand Down
1 change: 0 additions & 1 deletion src/Sidekick.Apis.Poe/Filters/FilterProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Sidekick.Apis.Poe.Clients;
using Sidekick.Apis.Poe.Filters.Models;
using Sidekick.Apis.Poe.Items.Models;
using Sidekick.Common.Cache;
using Sidekick.Common.Enums;
using Sidekick.Common.Extensions;
Expand Down
2 changes: 1 addition & 1 deletion src/Sidekick.Apis.Poe/IItemStaticDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public interface IItemStaticDataProvider : IInitializableService
{
string? GetImage(string id);

string? GetId(Header header);
string? GetId(ItemHeader itemHeader);
}
}
1 change: 1 addition & 0 deletions src/Sidekick.Apis.Poe/ITradeFilterService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sidekick.Apis.Poe.Parser.Properties.Filters;
using Sidekick.Apis.Poe.Trade.Models;
using Sidekick.Common.Game.Items;

Expand Down
1 change: 1 addition & 0 deletions src/Sidekick.Apis.Poe/ITradeSearchService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sidekick.Apis.Poe.Parser.Properties.Filters;
using Sidekick.Apis.Poe.Trade.Models;
using Sidekick.Apis.Poe.Trade.Results;
using Sidekick.Common.Game;
Expand Down
4 changes: 2 additions & 2 deletions src/Sidekick.Apis.Poe/Items/ApiItemConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Sidekick.Apis.Poe.Items;

public class ApiItemConstants
{
public static Dictionary<string, (Category Category, bool UseRegex)> Poe1Categories = new()
public static readonly Dictionary<string, (Category Category, bool UseRegex)> Poe1Categories = new()
{
{ "accessory", (Category.Accessory, true) },
{ "armour", (Category.Armour, true) },
Expand All @@ -26,7 +26,7 @@ public class ApiItemConstants
{ "corpse", (Category.Corpse, true) },
};

public static Dictionary<string, (Category Category, bool UseRegex)> Poe2Categories = new()
public static readonly Dictionary<string, (Category Category, bool UseRegex)> Poe2Categories = new()
{
{ "accessory", (Category.Accessory, true) },
{ "armour", (Category.Armour, true) },
Expand Down
8 changes: 4 additions & 4 deletions src/Sidekick.Apis.Poe/Items/Models/ApiItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ApiItem
[JsonIgnore]
public bool IsUnique => Flags?.Unique ?? false;

public Header ToHeader()
public ItemHeader ToHeader()
{
var categoryRarity = Category switch
{
Expand All @@ -39,15 +39,15 @@ public Header ToHeader()
_ => Rarity.Unknown
};

return new Header()
return new ItemHeader()
{
Name = Name,
Type = Text ?? Type,
Type = Type,
ApiItemId = Id ?? string.Empty,
ApiName = Name,
ApiType = Type,
ApiText = Text,
ApiDiscriminator = Discriminator,
ApiText = Text,
Game = Game,
Category = Category ?? Common.Game.Items.Category.Unknown,
Rarity = IsUnique ? Rarity.Unique : categoryRarity,
Expand Down
13 changes: 0 additions & 13 deletions src/Sidekick.Apis.Poe/Localization/FilterResources.cs

This file was deleted.

5 changes: 2 additions & 3 deletions src/Sidekick.Apis.Poe/Parser/Headers/HeaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Sidekick.Apis.Poe.Items;
using Sidekick.Apis.Poe.Items.Models;
using Sidekick.Apis.Poe.Parser.Headers.Models;
using Sidekick.Apis.Poe.Parser.Patterns;
using Sidekick.Common.Extensions;
using Sidekick.Common.Game;
using Sidekick.Common.Game.Items;
Expand Down Expand Up @@ -205,7 +204,7 @@ private void InitializeRarityPatterns()
};
}

public Header Parse(ParsingItem parsingItem)
public ItemHeader Parse(ParsingItem parsingItem)
{
var rarity = ParseRarity(parsingItem);

Expand All @@ -231,7 +230,7 @@ public Header Parse(ParsingItem parsingItem)
}

var apiItem = ParseApiItem(rarity, name, type);
var header = apiItem?.ToHeader() ?? new Header();
var header = apiItem?.ToHeader() ?? new ItemHeader();
header.Name = name;
header.Type = type;
header.ItemCategory = ParseItemCategory(parsingItem);
Expand Down
2 changes: 1 addition & 1 deletion src/Sidekick.Apis.Poe/Parser/Headers/IHeaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Sidekick.Apis.Poe.Parser.Headers;

public interface IHeaderParser : IInitializableService
{
Header Parse(ParsingItem parsingItem);
ItemHeader Parse(ParsingItem parsingItem);
}
70 changes: 9 additions & 61 deletions src/Sidekick.Apis.Poe/Parser/ItemParser.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using Sidekick.Apis.Poe.Items;
using Sidekick.Apis.Poe.Parser.AdditionalInformation;
using Sidekick.Apis.Poe.Parser.Headers;
using Sidekick.Apis.Poe.Parser.Modifiers;
using Sidekick.Apis.Poe.Parser.Patterns;
using Sidekick.Apis.Poe.Parser.Properties;
using Sidekick.Apis.Poe.Parser.Pseudo;
using Sidekick.Apis.Poe.Parser.Requirements;
using Sidekick.Apis.Poe.Parser.Sockets;
using Sidekick.Common.Exceptions;
using Sidekick.Common.Game.Items;
Expand All @@ -18,7 +17,7 @@ public class ItemParser
ILogger<ItemParser> logger,
IModifierParser modifierParser,
IPseudoParser pseudoParser,
IParserPatterns patterns,
IRequirementsParser requirementsParser,
ClusterJewelParser clusterJewelParser,
IApiInvariantItemProvider apiInvariantItemProvider,
ISocketParser socketParser,
Expand Down Expand Up @@ -47,24 +46,22 @@ public Item ParseItem(string itemText)
throw new UnparsableException();
}

Header? invariant = null;
ItemHeader? invariant = null;
if (parsingItem.Header.ApiItemId != null && apiInvariantItemProvider.IdDictionary.TryGetValue(parsingItem.Header.ApiItemId, out var invariantMetadata))
{
invariant = invariantMetadata.ToHeader();
}

// Order of parsing is important
ParseRequirements(parsingItem);

var influences = ParseInfluences(parsingItem);
requirementsParser.Parse(parsingItem);
var sockets = socketParser.Parse(parsingItem);
var modifierLines = ParseModifiers(parsingItem);
var properties = propertyParser.Parse(parsingItem, modifierLines);
var properties = propertyParser.Parse(parsingItem);
var modifierLines = modifierParser.Parse(parsingItem);
propertyParser.ParseAfterModifiers(parsingItem, properties, modifierLines);
var pseudoModifiers = pseudoParser.Parse(modifierLines);
var item = new Item(invariant: invariant,
header: parsingItem.Header,
properties: properties,
influences: influences,
itemHeader: parsingItem.Header,
itemProperties: properties,
sockets: sockets,
modifierLines: modifierLines,
pseudoModifiers: pseudoModifiers,
Expand All @@ -83,53 +80,4 @@ public Item ParseItem(string itemText)
throw;
}
}

private void ParseRequirements(ParsingItem parsingItem)
{
foreach (var block in parsingItem.Blocks.Where(x => !x.Parsed))
{
if (!block.TryParseRegex(patterns.Requirements, out _))
{
continue;
}

block.Parsed = true;
return;
}
}

private Influences ParseInfluences(ParsingItem parsingItem)
{
return parsingItem.Header?.Category switch
{
Category.Accessory or Category.Armour or Category.Weapon => new Influences()
{
Crusader = GetBool(patterns.Crusader, parsingItem),
Elder = GetBool(patterns.Elder, parsingItem),
Hunter = GetBool(patterns.Hunter, parsingItem),
Redeemer = GetBool(patterns.Redeemer, parsingItem),
Shaper = GetBool(patterns.Shaper, parsingItem),
Warlord = GetBool(patterns.Warlord, parsingItem),
},
_ => new Influences(),
};
}

private List<ModifierLine> ParseModifiers(ParsingItem parsingItem)
{
return parsingItem.Header?.Category switch
{
Category.DivinationCard or Category.Gem => new(),
_ => modifierParser.Parse(parsingItem),
};
}

#region Helpers

private static bool GetBool(Regex pattern, ParsingItem parsingItem)
{
return parsingItem.TryParseRegex(pattern, out _);
}

#endregion Helpers
}
5 changes: 5 additions & 0 deletions src/Sidekick.Apis.Poe/Parser/Modifiers/ModifierParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ IFuzzyService fuzzyService
/// <inheritdoc/>
public List<ModifierLine> Parse(ParsingItem parsingItem)
{
if (parsingItem.Header?.Category is Category.DivinationCard or Category.Gem)
{
return [];
}

var modifierLines = new List<ModifierLine>();

foreach (var match in MatchModifiers(parsingItem))
Expand Down
2 changes: 1 addition & 1 deletion src/Sidekick.Apis.Poe/Parser/ParsingItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ParsingItem(string text)
.ToList();
}

public Header? Header { get; set; }
public ItemHeader? Header { get; set; }

/// <summary>
/// Item sections seperated by dashes when copying an item in-game.
Expand Down
16 changes: 0 additions & 16 deletions src/Sidekick.Apis.Poe/Parser/Patterns/IParserPatterns.cs

This file was deleted.

54 changes: 0 additions & 54 deletions src/Sidekick.Apis.Poe/Parser/Patterns/ParserPatterns.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Text.RegularExpressions;
using Sidekick.Apis.Poe.Parser.Properties.Filters;
using Sidekick.Apis.Poe.Trade.Requests.Filters;
using Sidekick.Common.Game.Items;
using Sidekick.Common.Game.Languages;

namespace Sidekick.Apis.Poe.Parser.Properties.Definitions;

public class AreaLevelProperty(IGameLanguageProvider gameLanguageProvider) : PropertyDefinition
{
private Regex? Pattern { get; set; }

public override List<Category> ValidCategories { get; } = [Category.Sanctum, Category.Logbook, Category.Contract, Category.Map];

public override void Initialize()
{
Pattern = gameLanguageProvider.Language.DescriptionAreaLevel.ToRegexIntCapture();
}

public override void Parse(ItemProperties itemProperties, ParsingItem parsingItem)
{
var propertyBlock = parsingItem.Blocks[1];
itemProperties.AreaLevel = GetInt(Pattern, propertyBlock);
if (itemProperties.AreaLevel > 0) propertyBlock.Parsed = true;
}

public override BooleanPropertyFilter? GetFilter(Item item, double normalizeValue)
{
if (item.Properties.AreaLevel <= 0) return null;

var filter = new IntPropertyFilter(this)
{
Text = gameLanguageProvider.Language.DescriptionAreaLevel,
NormalizeEnabled = false,
NormalizeValue = normalizeValue,
Value = item.Properties.AreaLevel,
Checked = true,
};
filter.NormalizeMinValue();
return filter;
}

public override void PrepareTradeRequest(SearchFilters searchFilters, Item item, BooleanPropertyFilter filter)
{
if (!filter.Checked || filter is not IntPropertyFilter intFilter) return;

searchFilters.GetOrCreateMapFilters().Filters.AreaLevel = intFilter.Checked ? new StatFilterValue(intFilter) : null;
}
}
Loading
Loading