diff --git a/src/FourFoldAccountManager.Core/Tracking/PlayerProfileHtmlParser.cs b/src/FourFoldAccountManager.Core/Tracking/PlayerProfileHtmlParser.cs index 9948b94..3a68acf 100644 --- a/src/FourFoldAccountManager.Core/Tracking/PlayerProfileHtmlParser.cs +++ b/src/FourFoldAccountManager.Core/Tracking/PlayerProfileHtmlParser.cs @@ -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."); @@ -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) { @@ -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); @@ -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, @@ -99,12 +102,10 @@ private static IReadOnlyDictionary 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 whose label is the text or 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; diff --git a/src/FourFoldAccountManager.Core/Tracking/RankingHtmlParser.cs b/src/FourFoldAccountManager.Core/Tracking/RankingHtmlParser.cs index f751007..bf62047 100644 --- a/src/FourFoldAccountManager.Core/Tracking/RankingHtmlParser.cs +++ b/src/FourFoldAccountManager.Core/Tracking/RankingHtmlParser.cs @@ -23,7 +23,7 @@ public static IReadOnlyList 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."); @@ -35,6 +35,9 @@ public static IReadOnlyList 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; diff --git a/src/FourFoldAccountManager.Core/Tracking/RankingIdentityResolver.cs b/src/FourFoldAccountManager.Core/Tracking/RankingIdentityResolver.cs index 0eebeb0..9e8fe4c 100644 --- a/src/FourFoldAccountManager.Core/Tracking/RankingIdentityResolver.cs +++ b/src/FourFoldAccountManager.Core/Tracking/RankingIdentityResolver.cs @@ -44,8 +44,7 @@ public static IdentityResolution Resolve(string username, IReadOnlyList 0) + !uri.IsDefaultPort || !RankingHtmlParser.IsProfilePath(uri.AbsolutePath)) { return null; } diff --git a/src/FourFoldAccountManager.Desktop/Services/FourFoldRankingClient.cs b/src/FourFoldAccountManager.Desktop/Services/FourFoldRankingClient.cs index a89e03c..22bf207 100644 --- a/src/FourFoldAccountManager.Desktop/Services/FourFoldRankingClient.cs +++ b/src/FourFoldAccountManager.Desktop/Services/FourFoldRankingClient.cs @@ -33,7 +33,7 @@ public async Task> GetRankingAsync(CancellationToken public async Task 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 diff --git a/src/FourFoldAccountManager.Leaderboard.Tests/Collection/FourFoldPublicProfileSourceTests.cs b/src/FourFoldAccountManager.Leaderboard.Tests/Collection/FourFoldPublicProfileSourceTests.cs index 6217e0e..c6f2a40 100644 --- a/src/FourFoldAccountManager.Leaderboard.Tests/Collection/FourFoldPublicProfileSourceTests.cs +++ b/src/FourFoldAccountManager.Leaderboard.Tests/Collection/FourFoldPublicProfileSourceTests.cs @@ -25,12 +25,13 @@ public async Task DisabledCollectionMakesNoRequestEvenWithConfiguredRoute() public async Task ConfiguredHttpsRouteParsesPublicProfile() { var html = """ -

Alice

-
-

Warrior

-
Level 1
-
EXP 5 / 10
-
+ + """; var handler = new StubHandler(html); var source = new FourFoldPublicProfileSource(new LeaderboardCollectionOptions @@ -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); }