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

Add Statistics bindable to IAPIProvider and update it from SoloStatisticsWatcher #26356

Merged
merged 2 commits into from Jan 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneSoloStatisticsWatcher.cs
Expand Up @@ -268,6 +268,26 @@ public void TestStatisticsUpdateNotFiredAfterSubscriptionDisposal()
AddAssert("update not received", () => update == null);
}

[Test]
public void TestGlobalStatisticsUpdatedAfterRegistrationAddedAndScoreProcessed()
{
int userId = getUserId();
long scoreId = getScoreId();
setUpUser(userId);

var ruleset = new OsuRuleset().RulesetInfo;

SoloStatisticsUpdate? update = null;
registerForUpdates(scoreId, ruleset, receivedUpdate => update = receivedUpdate);

feignScoreProcessing(userId, ruleset, 5_000_000);

AddStep("signal score processed", () => ((ISpectatorClient)spectatorClient).UserScoreProcessed(userId, scoreId));
AddUntilStep("update received", () => update != null);
AddAssert("local user values are correct", () => dummyAPI.LocalUser.Value.Statistics.TotalScore, () => Is.EqualTo(5_000_000));
AddAssert("statistics values are correct", () => dummyAPI.Statistics.Value!.TotalScore, () => Is.EqualTo(5_000_000));
}

private int nextUserId = 2000;
private long nextScoreId = 50000;

Expand Down
17 changes: 16 additions & 1 deletion osu.Game/Online/API/APIAccess.cs
Expand Up @@ -53,6 +53,7 @@ public partial class APIAccess : Component, IAPIProvider
public IBindable<APIUser> LocalUser => localUser;
public IBindableList<APIUser> Friends => friends;
public IBindable<UserActivity> Activity => activity;
public IBindable<UserStatistics> Statistics => statistics;

public Language Language => game.CurrentLanguage.Value;

Expand All @@ -65,6 +66,8 @@ public partial class APIAccess : Component, IAPIProvider
private Bindable<UserStatus?> configStatus { get; } = new Bindable<UserStatus?>();
private Bindable<UserStatus?> localUserStatus { get; } = new Bindable<UserStatus?>();

private Bindable<UserStatistics> statistics { get; } = new Bindable<UserStatistics>();

protected bool HasLogin => authentication.Token.Value != null || (!string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password));

private readonly CancellationTokenSource cancellationToken = new CancellationTokenSource();
Expand Down Expand Up @@ -517,9 +520,21 @@ public void Logout()
flushQueue();
}

public void UpdateStatistics(UserStatistics newStatistics)
{
statistics.Value = newStatistics;

if (IsLoggedIn)
localUser.Value.Statistics = newStatistics;
}

private static APIUser createGuestUser() => new GuestUser();

private void setLocalUser(APIUser user) => Scheduler.Add(() => localUser.Value = user, false);
private void setLocalUser(APIUser user) => Scheduler.Add(() =>
{
localUser.Value = user;
statistics.Value = user.Statistics;
}, false);

protected override void Dispose(bool isDisposing)
{
Expand Down
17 changes: 17 additions & 0 deletions osu.Game/Online/API/DummyAPIAccess.cs
Expand Up @@ -28,6 +28,8 @@ public partial class DummyAPIAccess : Component, IAPIProvider

public Bindable<UserActivity> Activity { get; } = new Bindable<UserActivity>();

public Bindable<UserStatistics?> Statistics { get; } = new Bindable<UserStatistics?>();

public Language Language => Language.en;

public string AccessToken => "token";
Expand Down Expand Up @@ -115,6 +117,12 @@ public void Login(string username, string password)
Id = DUMMY_USER_ID,
};

Statistics.Value = new UserStatistics
{
GlobalRank = 1,
CountryRank = 1
};

state.Value = APIState.Online;
}

Expand All @@ -126,6 +134,14 @@ public void Logout()
LocalUser.Value = new GuestUser();
}

public void UpdateStatistics(UserStatistics newStatistics)
{
Statistics.Value = newStatistics;
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might want to set the statistics on LocalUser for consistency.

Untested sample code:

Suggested change
Statistics.Value = newStatistics;
if (IsLoggedIn)
LocalUser.Value.Statistics = newStatistics;
Statistics.Value = newStatistics;


if (IsLoggedIn)
LocalUser.Value.Statistics = newStatistics;
}

public IHubClientConnector? GetHubConnector(string clientName, string endpoint, bool preferMessagePack) => null;

public NotificationsClientConnector GetNotificationsConnector() => new PollingNotificationsClientConnector(this);
Expand All @@ -141,6 +157,7 @@ public void Logout()
IBindable<APIUser> IAPIProvider.LocalUser => LocalUser;
IBindableList<APIUser> IAPIProvider.Friends => Friends;
IBindable<UserActivity> IAPIProvider.Activity => Activity;
IBindable<UserStatistics?> IAPIProvider.Statistics => Statistics;

/// <summary>
/// During the next simulated login, the process will fail immediately.
Expand Down
10 changes: 10 additions & 0 deletions osu.Game/Online/API/IAPIProvider.cs
Expand Up @@ -28,6 +28,11 @@ public interface IAPIProvider
/// </summary>
IBindable<UserActivity> Activity { get; }

/// <summary>
/// The current user's online statistics.
/// </summary>
IBindable<UserStatistics?> Statistics { get; }

/// <summary>
/// The language supplied by this provider to API requests.
/// </summary>
Expand Down Expand Up @@ -111,6 +116,11 @@ public interface IAPIProvider
/// </summary>
void Logout();

/// <summary>
/// Sets Statistics bindable.
/// </summary>
void UpdateStatistics(UserStatistics newStatistics);

/// <summary>
/// Constructs a new <see cref="IHubClientConnector"/>. May be null if not supported.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions osu.Game/Online/Solo/SoloStatisticsWatcher.cs
Expand Up @@ -127,6 +127,8 @@ private void dispatchStatisticsUpdate(StatisticsUpdateCallback callback, UserSta
{
string rulesetName = callback.Score.Ruleset.ShortName;

api.UpdateStatistics(updatedStatistics);

if (latestStatistics == null)
return;

Expand Down