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 pseudo modifiers #422

Merged
merged 7 commits into from
Dec 29, 2024
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
37 changes: 23 additions & 14 deletions src/Sidekick.Apis.Poe/Clients/PoeTradeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return response;
}

if (response.StatusCode == HttpStatusCode.TooManyRequests)
{
var errorResponse = await ParseErrorResponse(response);
throw new SidekickException("Rate limit exceeded.", "The official trade website has a rate limit to avoid spam. Sidekick cannot change this.", errorResponse?.Error?.Message ?? string.Empty);
}

if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.RedirectKeepVerb)
{
response = await HandleRedirect(request, response, cancellationToken);
}

if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.RedirectKeepVerb)
{
logger.LogWarning("[PoeTradeHandler] Received redirect response.");
Expand All @@ -64,10 +53,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}
}

// Sidekick does not support authentication yet.
if (response.StatusCode == HttpStatusCode.Unauthorized)
if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.RedirectKeepVerb)
{
throw new SidekickException("Sidekick failed to communicate with the trade API.", "The trade website requires authentication, which Sidekick does not support currently.", "Try using a different game language and/or force to search using English only in the settings.");
response = await HandleRedirect(request, response, cancellationToken);
}

// 403 probably means a cloudflare issue.
Expand Down Expand Up @@ -108,6 +96,27 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
logger.LogWarning("[PoeTradeHandler] Query Failed: {responseCode} {responseMessage}", response.StatusCode, content);
logger.LogWarning("[PoeTradeHandler] Uri: {uri}", request.RequestUri);
logger.LogWarning("[PoeTradeHandler] Body: {uri}", body);

var errorResponse = await ParseErrorResponse(response);
if (response.StatusCode == HttpStatusCode.BadRequest)
{
if (errorResponse?.Error?.Message?.StartsWith("Query is too complex.") ?? false)
{
throw new SidekickException("Query is too complex.", "The official trade website has limit on complex queries. Sidekick cannot change this.", "Use the official website to search for your current item.");
}
}

if (response.StatusCode == HttpStatusCode.TooManyRequests)
{
throw new SidekickException("Rate limit exceeded.", "The official trade website has a rate limit to avoid spam. Sidekick cannot change this.", errorResponse?.Error?.Message ?? string.Empty);
}

// Sidekick does not support authentication yet.
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new SidekickException("Sidekick failed to communicate with the trade API.", "The trade website requires authentication, which Sidekick does not support currently.", "Try using a different game language and/or force to search using English only in the settings.");
}

throw new ApiErrorException();
}

Expand Down
12 changes: 11 additions & 1 deletion src/Sidekick.Apis.Poe/Modifiers/InvariantModifierProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,21 @@ public async Task<List<ApiCategory>> GetList()
var game = leagueId.GetGameFromLeagueId();
var cacheKey = $"{game.GetValueAttribute()}_InvariantModifiers";

return await cacheProvider.GetOrSet(cacheKey,
var apiCategories = await cacheProvider.GetOrSet(cacheKey,
async () =>
{
var result = await poeTradeClient.Fetch<ApiCategory>(game, gameLanguageProvider.InvariantLanguage, "data/stats");
return result.Result;
}, (cache) => cache.Any());

apiCategories.ForEach(category =>
{
category.Entries.ForEach(entry =>
{
entry.Text = ModifierProvider.RemoveSquareBrackets(entry.Text);
});
});

return apiCategories;
}
}
17 changes: 3 additions & 14 deletions src/Sidekick.Apis.Poe/Parser/ItemParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
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.Sockets;
using Sidekick.Apis.Poe.Pseudo;
using Sidekick.Common.Exceptions;
using Sidekick.Common.Game;
using Sidekick.Common.Game.Items;

namespace Sidekick.Apis.Poe.Parser
Expand All @@ -20,7 +19,7 @@ public class ItemParser
ILogger<ItemParser> logger,
IMetadataParser metadataProvider,
IModifierParser modifierParser,
IPseudoModifierProvider pseudoModifierProvider,
IPseudoParser pseudoParser,
IParserPatterns patterns,
ClusterJewelParser clusterJewelParser,
IInvariantMetadataProvider invariantMetadataProvider,
Expand Down Expand Up @@ -72,7 +71,7 @@ public Item ParseItem(string itemText)
var sockets = socketParser.Parse(parsingItem);
var modifierLines = ParseModifiers(parsingItem);
var properties = propertyParser.Parse(parsingItem, modifierLines);
var pseudoModifiers = parsingItem.Metadata.Game == GameType.PathOfExile ? ParsePseudoModifiers(modifierLines) : [];
var pseudoModifiers = pseudoParser.Parse(modifierLines);
var item = new Item(metadata: metadata,
invariant: invariant,
header: header,
Expand Down Expand Up @@ -137,16 +136,6 @@ private List<ModifierLine> ParseModifiers(ParsingItem parsingItem)
};
}

private List<PseudoModifier> ParsePseudoModifiers(List<ModifierLine> modifierLines)
{
if (modifierLines.Count == 0)
{
return new();
}

return pseudoModifierProvider.Parse(modifierLines);
}

#region Helpers

private static bool GetBool(Regex pattern, ParsingItem parsingItem)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game;

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

public class ChaosResistancesDefinition(GameType game) : PseudoDefinition
{
protected override bool Enabled => game == GameType.PathOfExile;

protected override string? ModifierId => game == GameType.PathOfExile ? "pseudo.pseudo_total_chaos_resistance" : null;

protected override List<PseudoPattern> Patterns =>
[
new(new Regex("to Chaos Resistance$")),
new(new Regex("(?=.*Chaos)to (?:Fire|Cold|Lightning|Chaos) and (?:Fire|Cold|Lightning|Chaos) Resistances$")),
];

protected override Regex Exception => new("Minions|Enemies|Totems");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game;

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

public class DexterityDefinition(GameType game) : PseudoDefinition
{
protected override bool Enabled => game == GameType.PathOfExile;

protected override string? ModifierId => game == GameType.PathOfExile ? "pseudo.pseudo_total_dexterity" : null;

protected override List<PseudoPattern> Patterns =>
[
new(new Regex("to Dexterity$")),
new(new Regex("(?=.*Dexterity)to (?:Strength|Dexterity|Intelligence) and (?:Strength|Dexterity|Intelligence)$")),
new(new Regex("to all Attributes$")),
];

protected override Regex Exception => new("Passive");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game;

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

public class ElementalResistancesDefinition(GameType game) : PseudoDefinition
{
protected override bool Enabled => game == GameType.PathOfExile;

protected override string? ModifierId => game == GameType.PathOfExile ? "pseudo.pseudo_total_elemental_resistance" : null;

protected override List<PseudoPattern> Patterns =>
[
new(new Regex("to (?:Fire|Cold|Lightning) Resistance$")),
new(new Regex("to (?:Fire|Cold|Lightning) and (?:Fire|Cold|Lightning) Resistances$"), 2),
new(new Regex("(?=.*Chaos)to (?:Fire|Cold|Lightning|Chaos) and (?:Fire|Cold|Lightning|Chaos) Resistances$")),
new(new Regex("to all Elemental Resistances$"), 3),
];

protected override Regex Exception => new("Minions|Enemies|Totems");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game;

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

public class IntelligenceDefinition(GameType game) : PseudoDefinition
{
protected override bool Enabled => game == GameType.PathOfExile;

protected override string? ModifierId => game == GameType.PathOfExile ? "pseudo.pseudo_total_intelligence" : null;

protected override List<PseudoPattern> Patterns =>
[
new(new Regex("to Intelligence$")),
new(new Regex("(?=.*Intelligence)to (?:Strength|Dexterity|Intelligence) and (?:Strength|Dexterity|Intelligence)$")),
new(new Regex("to all Attributes$")),
];

protected override Regex Exception => new("Passive");
}
23 changes: 23 additions & 0 deletions src/Sidekick.Apis.Poe/Parser/Pseudo/Definitions/LifeDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game;

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

public class LifeDefinition(GameType game) : PseudoDefinition
{
protected override bool Enabled => game == GameType.PathOfExile;

protected override string? ModifierId => game == GameType.PathOfExile ? "pseudo.pseudo_total_life" : null;

protected override List<PseudoPattern> Patterns =>
[
new(new Regex("to maximum Life$")),
new(new Regex("to Strength$"), AttributeMultiplier),
new(new Regex("(?=.*Strength)to (?:Strength|Dexterity|Intelligence) and (?:Strength|Dexterity|Intelligence)$"), AttributeMultiplier),
new(new Regex("to all Attributes$"), AttributeMultiplier),
];

protected override Regex Exception => new("Zombies|Transformed");

private double AttributeMultiplier => game == GameType.PathOfExile ? 0.5 : 2;
}
23 changes: 23 additions & 0 deletions src/Sidekick.Apis.Poe/Parser/Pseudo/Definitions/ManaDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game;

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

public class ManaDefinition(GameType game) : PseudoDefinition
{
protected override bool Enabled => game == GameType.PathOfExile;

protected override string? ModifierId => game == GameType.PathOfExile ? "pseudo.pseudo_total_mana" : null;

protected override List<PseudoPattern> Patterns =>
[
new PseudoPattern(new Regex("to maximum Mana$")),
new PseudoPattern(new Regex("to Intelligence$"), AttributeMultiplier),
new PseudoPattern(new Regex("(?=.*Intelligence)to (?:Strength|Dexterity|Intelligence) and (?:Strength|Dexterity|Intelligence)$"), AttributeMultiplier),
new PseudoPattern(new Regex("to all Attributes$"), AttributeMultiplier),
];

protected override Regex Exception => new("Zombies|Transformed");

private double AttributeMultiplier => game == GameType.PathOfExile ? 0.5 : 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.RegularExpressions;
using Sidekick.Common.Game;

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

public class StrengthDefinition(GameType game) : PseudoDefinition
{
protected override bool Enabled => game == GameType.PathOfExile;

protected override string? ModifierId => game == GameType.PathOfExile ? "pseudo.pseudo_total_strength" : null;

protected override List<PseudoPattern> Patterns =>
[
new(new Regex("to Strength$")),
new(new Regex("(?=.*Strength)to (?:Strength|Dexterity|Intelligence) and (?:Strength|Dexterity|Intelligence)$")),
new(new Regex("to all Attributes$")),
];

protected override Regex Exception => new("Passive");
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Sidekick.Common.Game.Items;
using Sidekick.Common.Initialization;

namespace Sidekick.Apis.Poe.Pseudo
namespace Sidekick.Apis.Poe.Parser.Pseudo
{
public interface IPseudoModifierProvider : IInitializableService
public interface IPseudoParser : IInitializableService
{
List<PseudoModifier> Parse(List<ModifierLine> modifiers);
}
Expand Down
Loading
Loading