1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 18:23:04 +08:00

Merge pull request #11341 from peppy/fix-leaderboard-user-handling

Fix incorrect null handling in GameplayLeaderboard
This commit is contained in:
Dean Herbert 2020-12-28 22:44:55 +09:00 committed by GitHub
commit 9155671557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 8 deletions

View File

@ -95,7 +95,7 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
public bool CheckPositionByUsername(string username, int? expectedPosition) public bool CheckPositionByUsername(string username, int? expectedPosition)
{ {
var scoreItem = this.FirstOrDefault(i => i.User.Username == username); var scoreItem = this.FirstOrDefault(i => i.User?.Username == username);
return scoreItem != null && scoreItem.ScorePosition == expectedPosition; return scoreItem != null && scoreItem.ScorePosition == expectedPosition;
} }

View File

@ -20,10 +20,9 @@ using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Rulesets.Scoring; using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Screens.Play.HUD; using osu.Game.Screens.Play.HUD;
using osu.Game.Tests.Visual.Multiplayer;
using osu.Game.Tests.Visual.Online; using osu.Game.Tests.Visual.Online;
namespace osu.Game.Tests.Visual.Gameplay namespace osu.Game.Tests.Visual.Multiplayer
{ {
public class TestSceneMultiplayerGameplayLeaderboard : MultiplayerTestScene public class TestSceneMultiplayerGameplayLeaderboard : MultiplayerTestScene
{ {

View File

@ -90,11 +90,17 @@ namespace osu.Game.Tests.Visual.Online
}; };
protected override Task<User> ComputeValueAsync(int lookup, CancellationToken token = default) protected override Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)
=> Task.FromResult(new User {
// tests against failed lookups
if (lookup == 13)
return Task.FromResult<User>(null);
return Task.FromResult(new User
{ {
Id = lookup, Id = lookup,
Username = usernames[lookup % usernames.Length], Username = usernames[lookup % usernames.Length],
}); });
}
} }
} }
} }

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
@ -17,6 +18,13 @@ namespace osu.Game.Database
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private IAPIProvider api { get; set; }
/// <summary>
/// Perform an API lookup on the specified user, populating a <see cref="User"/> model.
/// </summary>
/// <param name="userId">The user to lookup.</param>
/// <param name="token">An optional cancellation token.</param>
/// <returns>The populated user, or null if the user does not exist or the request could not be satisfied.</returns>
[ItemCanBeNull]
public Task<User> GetUserAsync(int userId, CancellationToken token = default) => GetAsync(userId, token); public Task<User> GetUserAsync(int userId, CancellationToken token = default) => GetAsync(userId, token);
protected override async Task<User> ComputeValueAsync(int lookup, CancellationToken token = default) protected override async Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Caching; using osu.Framework.Caching;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -42,7 +43,7 @@ namespace osu.Game.Screens.Play.HUD
/// Whether the player should be tracked on the leaderboard. /// Whether the player should be tracked on the leaderboard.
/// Set to <c>true</c> for the local player or a player whose replay is currently being played. /// Set to <c>true</c> for the local player or a player whose replay is currently being played.
/// </param> /// </param>
public ILeaderboardScore AddPlayer(User user, bool isTracked) public ILeaderboardScore AddPlayer([CanBeNull] User user, bool isTracked)
{ {
var drawable = new GameplayLeaderboardScore(user, isTracked) var drawable = new GameplayLeaderboardScore(user, isTracked)
{ {

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
@ -56,6 +57,7 @@ namespace osu.Game.Screens.Play.HUD
} }
} }
[CanBeNull]
public User User { get; } public User User { get; }
private readonly bool trackedPlayer; private readonly bool trackedPlayer;
@ -68,7 +70,7 @@ namespace osu.Game.Screens.Play.HUD
/// </summary> /// </summary>
/// <param name="user">The score's player.</param> /// <param name="user">The score's player.</param>
/// <param name="trackedPlayer">Whether the player is the local user or a replay player.</param> /// <param name="trackedPlayer">Whether the player is the local user or a replay player.</param>
public GameplayLeaderboardScore(User user, bool trackedPlayer) public GameplayLeaderboardScore([CanBeNull] User user, bool trackedPlayer)
{ {
User = user; User = user;
this.trackedPlayer = trackedPlayer; this.trackedPlayer = trackedPlayer;
@ -180,7 +182,7 @@ namespace osu.Game.Screens.Play.HUD
Origin = Anchor.CentreLeft, Origin = Anchor.CentreLeft,
Colour = Color4.White, Colour = Color4.White,
Font = OsuFont.Torus.With(size: 14, weight: FontWeight.SemiBold), Font = OsuFont.Torus.With(size: 14, weight: FontWeight.SemiBold),
Text = User.Username, Text = User?.Username,
Truncate = true, Truncate = true,
Shadow = false, Shadow = false,
} }

View File

@ -65,7 +65,7 @@ namespace osu.Game.Screens.Play.HUD
var trackedUser = new TrackedUserData(); var trackedUser = new TrackedUserData();
userScores[userId] = trackedUser; userScores[userId] = trackedUser;
var leaderboardScore = AddPlayer(resolvedUser, resolvedUser.Id == api.LocalUser.Value.Id); var leaderboardScore = AddPlayer(resolvedUser, resolvedUser?.Id == api.LocalUser.Value.Id);
((IBindable<double>)leaderboardScore.Accuracy).BindTo(trackedUser.Accuracy); ((IBindable<double>)leaderboardScore.Accuracy).BindTo(trackedUser.Accuracy);
((IBindable<double>)leaderboardScore.TotalScore).BindTo(trackedUser.Score); ((IBindable<double>)leaderboardScore.TotalScore).BindTo(trackedUser.Score);