Skip to content
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
25 changes: 13 additions & 12 deletions src/FourFoldAccountManager.Core/Tracking/PlayerProfileHtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public static PlayerProgressSnapshot Parse(string html)
{
ArgumentNullException.ThrowIfNull(html);
var document = new HtmlParser().ParseDocument(html);
var username = document.QuerySelector(".hero-title")?.TextContent.Trim();
var cards = document.QuerySelectorAll(".class-grid .class-card .class-body");
var username = document.QuerySelector(".social-profile-identity h1")?.TextContent.Trim();
var cards = document.QuerySelectorAll(".social-class-panels .social-loadout-card");
if (string.IsNullOrWhiteSpace(username) || cards.Length == 0)
{
throw new InvalidDataException("The player profile is missing its identity or class progression.");
Expand All @@ -24,13 +24,15 @@ public static PlayerProgressSnapshot Parse(string html)

foreach (var card in cards)
{
var className = card.QuerySelector("h3")?.TextContent.Trim();
var head = card.QuerySelector(".social-loadout-head");
var className = head?.QuerySelector("h3")?.TextContent.Trim();
if (string.IsNullOrWhiteSpace(className) || !seen.Add(className))
{
throw new InvalidDataException("The player profile has duplicate or unnamed classes.");
}

if (card.QuerySelectorAll(".badge").Any(badge => badge.TextContent.Trim() == "Active Class"))
if (head!.QuerySelectorAll("span").Any(label =>
string.Equals(label.TextContent.Trim(), "Active class", StringComparison.OrdinalIgnoreCase)))
{
if (activeClassName is not null)
{
Expand All @@ -40,7 +42,7 @@ public static PlayerProgressSnapshot Parse(string html)
activeClassName = className;
}

var levelText = ReadMeta(card, "Level");
var levelText = head.QuerySelector("strong")?.TextContent.Replace("Level", "").Trim();
var expText = ReadMeta(card, "EXP");
var xpParts = expText?.Split('/', 2);
var stats = ReadStats(card);
Expand All @@ -54,7 +56,8 @@ public static PlayerProgressSnapshot Parse(string html)
continue;
}

classes.Add(className, new ClassProfileSnapshot(level, currentXp, nextLevelXp, ReadMeta(card, "Updated"))
// The social profile page no longer shows a per-class updated time.
classes.Add(className, new ClassProfileSnapshot(level, currentXp, nextLevelXp, null)
{
ClassName = className,
Hp = stats.Hp,
Expand Down Expand Up @@ -99,12 +102,10 @@ private static IReadOnlyDictionary<string, string> ReadEquipment(IElement card)
return equipment;
}

private static string? ReadMeta(IElement card, string label)
{
var item = card.QuerySelectorAll(".meta-item")
.FirstOrDefault(element => element.QuerySelector("strong")?.TextContent.Trim() == label);
return item is null ? null : item.TextContent.Trim()[label.Length..].Trim();
}
// Every value is a <strong> whose label is the text or <span> right before it.
private static string? ReadMeta(IElement card, string label) =>
card.QuerySelectorAll(".social-loadout-vitals strong, .social-equipment-grid strong, .social-stat-strip strong")
.FirstOrDefault(value => value.PreviousSibling?.TextContent.Trim() == label)?.TextContent.Trim();

private static bool TryParsePositiveInt(string? value, out int result) =>
int.TryParse(value?.Replace(",", ""), NumberStyles.None, CultureInfo.InvariantCulture, out result) && result > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IReadOnlyList<RankingEntry> Parse(string html)
var href = link?.GetAttribute("href");
if (string.IsNullOrWhiteSpace(username) || href is null ||
!Uri.TryCreate(new Uri("https://fourfoldonline.com/"), href, out var uri) ||
uri.Host != "fourfoldonline.com" || uri.AbsolutePath != "/player.php" ||
uri.Host != "fourfoldonline.com" || !IsProfilePath(uri.AbsolutePath) ||
!TryReadPlayerId(uri.Query, out var playerId))
{
throw new InvalidDataException("An EXP ranking row has no valid player link.");
Expand All @@ -35,6 +35,9 @@ public static IReadOnlyList<RankingEntry> Parse(string html)
return entries;
}

// player.php is the old profile page; it now redirects to social_profile.php but the ranking still links it.
internal static bool IsProfilePath(string path) => path is "/social_profile.php" or "/player.php";

internal static bool TryReadPlayerId(string query, out int playerId)
{
playerId = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public static IdentityResolution Resolve(string username, IReadOnlyList<RankingE
if (!Uri.TryCreate(input, UriKind.Absolute, out var uri) ||
uri.Scheme != Uri.UriSchemeHttps ||
!string.Equals(uri.Host, "fourfoldonline.com", StringComparison.OrdinalIgnoreCase) ||
!uri.IsDefaultPort || uri.AbsolutePath != "/player.php" ||
uri.Fragment.Length > 0)
!uri.IsDefaultPort || !RankingHtmlParser.IsProfilePath(uri.AbsolutePath))
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<IReadOnlyList<RankingEntry>> GetRankingAsync(CancellationToken
public async Task<PlayerProgressSnapshot> GetProfileAsync(int playerId, CancellationToken cancellationToken)
{
if (playerId <= 0) throw new ArgumentOutOfRangeException(nameof(playerId));
return PlayerProfileHtmlParser.Parse(await GetPageAsync($"player.php?id={playerId}", cancellationToken));
return PlayerProfileHtmlParser.Parse(await GetPageAsync($"social_profile.php?id={playerId}", cancellationToken));
}

// Redirects are off, so a 3xx fails the success check. A page over MaxResponseContentBufferSize fails the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public async Task DisabledCollectionMakesNoRequestEvenWithConfiguredRoute()
public async Task ConfiguredHttpsRouteParsesPublicProfile()
{
var html = """
<h1 class="hero-title">Alice</h1>
<div class="class-grid"><div class="class-card"><div class="class-body">
<h3>Warrior</h3>
<div class="meta-item"><strong>Level</strong> 1</div>
<div class="meta-item"><strong>EXP</strong> 5 / 10</div>
</div></div></div>
<div class="social-profile-identity"><h1><span class="social-name">Alice</span></h1></div>
<div class="social-class-panels"><article class="social-loadout-card">
<div class="social-loadout-head"><div><span>Active class</span><h3>Warrior</h3><strong>Level 1</strong></div></div>
<div class="social-loadout-vitals"><span>HP <strong>9 / 1,200</strong></span><span>EXP <strong>5 / 10</strong></span></div>
<div class="social-equipment-grid"><div><span>Weapon</span><strong>Sword</strong></div></div>
<div class="social-stat-strip"><span>ATT<strong>7</strong></span></div>
</article></div>
""";
var handler = new StubHandler(html);
var source = new FourFoldPublicProfileSource(new LeaderboardCollectionOptions
Expand All @@ -43,7 +44,14 @@ public async Task ConfiguredHttpsRouteParsesPublicProfile()
var snapshot = await source.FetchAsync(42, default);

Assert.Equal("Alice", snapshot.Username);
Assert.Equal(5, snapshot.Classes["Warrior"].CurrentXp);
var warrior = snapshot.Classes["Warrior"];
Assert.Equal("Warrior", snapshot.ActiveClassName);
Assert.Equal(1, warrior.Level);
Assert.Equal(5, warrior.CurrentXp);
Assert.Equal(10, warrior.NextLevelXp);
Assert.Equal(1200, warrior.Hp);
Assert.Equal(7, warrior.Attack);
Assert.Equal("Sword", warrior.Equipment["Weapon"]);
Assert.Equal("https://example.invalid/player.php?id=42", handler.LastUrl);
}

Expand Down
Loading