From 00a24002469e668cfe7baaf570dfa509ca48074a Mon Sep 17 00:00:00 2001 From: Jebzou <22751386+Jebzou@users.noreply.github.com> Date: Thu, 12 Dec 2024 07:49:18 +0100 Subject: [PATCH] Senka leaderboard : Fix data inconsistencies by running multiple round of decoding --- .../SenkaLeaderboardManager.cs | 10 ++++ .../SenkaLeaderboardViewModel.cs | 46 +++++++++++++++---- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardManager.cs b/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardManager.cs index 026d3200c..9ce5e55fe 100644 --- a/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardManager.cs +++ b/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardManager.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Text.Json; +using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using ElectronicObserver.Data; using ElectronicObserver.KancolleApi.Types.ApiReqRanking.Models; @@ -23,6 +24,8 @@ public partial class SenkaLeaderboardManager : ObservableObject [ObservableProperty] private partial SenkaLeaderboardRefreshKind CurrentSenkaLeaderboardRefreshKind { get; set; } + private Task? LastDecodingTask { get; set; } + public SenkaLeaderboardManager(TimeChangeService timeChangeService, Tracker tracker) { APIObserver.Instance.ApiReqRanking_Mxltvkpyuklh.ResponseReceived += HandleData; @@ -54,6 +57,13 @@ private void OnSenkaLeaderboardRefreshKindChanged(object? sender, PropertyChange }; private void HandleData(string apiname, dynamic data) + { + if (CurrentCutoffData.IsDataReadyToBeSubmitted()) return; + + HandleData(data); + } + + private void HandleData(dynamic data) { try { diff --git a/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardViewModel.cs b/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardViewModel.cs index 6e7e32692..586cd8f9a 100644 --- a/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardViewModel.cs +++ b/ElectronicObserver/Window/Wpf/SenkaLeaderboard/SenkaLeaderboardViewModel.cs @@ -21,9 +21,11 @@ public partial class SenkaLeaderboardViewModel : AnchorableViewModel private List PossibleUserKey { get; } = []; - [ObservableProperty] + [ObservableProperty] private partial List SenkaData { get; set; } + private Dictionary SenkaRawData { get; set; } = []; + public PagingControlViewModel PagingViewModel { get; } public DataGridViewModel DataGridViewModel { get; } @@ -35,7 +37,7 @@ public partial class SenkaLeaderboardViewModel : AnchorableViewModel public BonodereSubmissionService BonodereSubmissionService { get; } public SenkaLeaderboardTranslationViewModel Translation { get; } - + public bool IsBonodereReady => !string.IsNullOrEmpty(Configuration.Config.DataSubmission.BonodereToken) && Configuration.Config.DataSubmission.BonodereIntegrationEnabled; public SenkaLeaderboardViewModel() : base(SenkaLeaderboardResources.Title, "SenkaLeaderboard", IconContent.FormResourceChart) @@ -64,6 +66,7 @@ public void Update() public void Reset() { SenkaData = NewLeaderboard(); + SenkaRawData = []; Update(); UpdateEntryCount(); } @@ -90,6 +93,26 @@ private bool CheckRate(int key, int userKey, decimal rate) } public void HandleEntry(ApiList entry) + { + if (ConvertApiToSenkaModel(entry) is not SenkaEntryModel parsedEntry) return; + + if (SenkaData.Count < entry.ApiMxltvkpyuklh) return; + + SenkaData[entry.ApiMxltvkpyuklh - 1] = parsedEntry; + + SenkaRawData.Add(entry.ApiMxltvkpyuklh, entry); + + UpdateEntryCount(); + + if (LoadedEntriesCount % 10 is 0) + { + RunAnotherRoundOfDecoding(); + } + + PagingViewModel.DisplayPageFromElementKey(entry.ApiMxltvkpyuklh - 1); + } + + private SenkaEntryModel? ConvertApiToSenkaModel(ApiList entry) { int key = PossibleRank[entry.ApiMxltvkpyuklh % 13]; @@ -118,10 +141,9 @@ public void HandleEntry(ApiList entry) PossibleUserKey.RemoveAll(toRemove.Contains); } - if (PossibleUserKey.Count is 0) return; - if (SenkaData.Count < entry.ApiMxltvkpyuklh) return; + if (PossibleUserKey.Count is 0) return null; - SenkaData[entry.ApiMxltvkpyuklh - 1] = new SenkaEntryModel + return new SenkaEntryModel { AdmiralName = entry.ApiMtjmdcwtvhdr, Comment = entry.ApiItbrdpdbkynm, @@ -130,10 +152,18 @@ public void HandleEntry(ApiList entry) Position = entry.ApiMxltvkpyuklh, IsKnown = true, }; + } - UpdateEntryCount(); - - PagingViewModel.DisplayPageFromElementKey(entry.ApiMxltvkpyuklh - 1); + private void RunAnotherRoundOfDecoding() + { + foreach (SenkaEntryModel entry in SenkaData.Where(entry => entry.IsKnown).OrderByDescending(entry => entry.Position)) + { + if (SenkaRawData.TryGetValue(entry.Position, out ApiList? rawData) && ConvertApiToSenkaModel(rawData) is SenkaEntryModel parsedEntry) + { + entry.MedalCount = parsedEntry.MedalCount; + entry.Points = parsedEntry.Points; + } + } } private void UpdateEntryCount()