Skip to content

Commit

Permalink
Senka leaderboard : Fix data inconsistencies by running multiple roun…
Browse files Browse the repository at this point in the history
…d of decoding
  • Loading branch information
Jebzou committed Dec 12, 2024
1 parent 2ca1044 commit 00a2400
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public partial class SenkaLeaderboardViewModel : AnchorableViewModel

private List<int> PossibleUserKey { get; } = [];

[ObservableProperty]
[ObservableProperty]
private partial List<SenkaEntryModel> SenkaData { get; set; }

private Dictionary<int, ApiList> SenkaRawData { get; set; } = [];

public PagingControlViewModel PagingViewModel { get; }

public DataGridViewModel<SenkaEntryModel> DataGridViewModel { get; }
Expand All @@ -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)
Expand Down Expand Up @@ -64,6 +66,7 @@ public void Update()
public void Reset()
{
SenkaData = NewLeaderboard();
SenkaRawData = [];
Update();
UpdateEntryCount();
}
Expand All @@ -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];

Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 00a2400

Please sign in to comment.