2020-12-16 14:25:27 +08:00
|
|
|
// 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.
|
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
using System;
|
2020-12-16 14:25:27 +08:00
|
|
|
using System.Collections.Generic;
|
2020-12-26 10:34:05 +08:00
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.Linq;
|
2022-05-30 18:18:38 +08:00
|
|
|
using System.Threading;
|
2020-12-16 14:25:27 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2022-01-03 16:31:12 +08:00
|
|
|
using osu.Framework.Extensions;
|
2021-08-09 16:04:06 +08:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2022-09-26 15:21:23 +08:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2020-12-16 14:25:27 +08:00
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Database;
|
2021-08-09 16:04:06 +08:00
|
|
|
using osu.Game.Graphics;
|
2020-12-18 15:55:55 +08:00
|
|
|
using osu.Game.Online.API;
|
2021-11-04 17:02:44 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2020-12-26 10:34:05 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2021-08-09 16:04:06 +08:00
|
|
|
using osu.Game.Online.Multiplayer.MatchTypes.TeamVersus;
|
2020-12-16 14:25:27 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
|
|
|
using osu.Game.Rulesets.Scoring;
|
2022-09-13 15:56:19 +08:00
|
|
|
using osu.Game.Users;
|
2021-08-09 16:04:06 +08:00
|
|
|
using osuTK.Graphics;
|
2020-12-16 14:25:27 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
|
|
|
{
|
2020-12-17 14:48:53 +08:00
|
|
|
[LongRunningLoad]
|
2020-12-16 14:25:27 +08:00
|
|
|
public class MultiplayerGameplayLeaderboard : GameplayLeaderboard
|
|
|
|
{
|
2021-04-12 21:00:27 +08:00
|
|
|
protected readonly Dictionary<int, TrackedUserData> UserScores = new Dictionary<int, TrackedUserData>();
|
2020-12-16 15:05:46 +08:00
|
|
|
|
2022-04-12 13:13:07 +08:00
|
|
|
public readonly SortedDictionary<int, BindableLong> TeamScores = new SortedDictionary<int, BindableLong>();
|
2021-08-09 16:07:50 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2022-09-26 15:21:23 +08:00
|
|
|
private OsuColour colours { get; set; } = null!;
|
2021-08-09 16:07:50 +08:00
|
|
|
|
2020-12-26 10:34:05 +08:00
|
|
|
[Resolved]
|
2022-09-26 15:21:23 +08:00
|
|
|
private SpectatorClient spectatorClient { get; set; } = null!;
|
2020-12-26 10:34:05 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2022-09-26 15:21:23 +08:00
|
|
|
private MultiplayerClient multiplayerClient { get; set; } = null!;
|
2020-12-26 10:34:05 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2022-09-26 15:21:23 +08:00
|
|
|
private UserLookupCache userLookupCache { get; set; } = null!;
|
|
|
|
|
|
|
|
private Bindable<ScoringMode> scoringMode = null!;
|
2020-12-26 10:34:05 +08:00
|
|
|
|
2021-08-10 17:39:20 +08:00
|
|
|
private readonly MultiplayerRoomUser[] playingUsers;
|
2020-12-26 10:34:05 +08:00
|
|
|
|
2021-08-10 17:39:20 +08:00
|
|
|
private readonly IBindableList<int> playingUserIds = new BindableList<int>();
|
|
|
|
|
2021-08-09 16:23:02 +08:00
|
|
|
private bool hasTeams => TeamScores.Count > 0;
|
2021-08-09 16:07:50 +08:00
|
|
|
|
2020-12-16 14:25:27 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a new leaderboard.
|
|
|
|
/// </summary>
|
2021-08-10 17:39:20 +08:00
|
|
|
/// <param name="users">IDs of all users in this match.</param>
|
2022-05-30 18:18:38 +08:00
|
|
|
public MultiplayerGameplayLeaderboard(MultiplayerRoomUser[] users)
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2021-08-10 17:39:20 +08:00
|
|
|
playingUsers = users;
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-05-30 18:18:38 +08:00
|
|
|
private void load(OsuConfigManager config, IAPIProvider api, CancellationToken cancellationToken)
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2021-04-12 21:00:27 +08:00
|
|
|
scoringMode = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode);
|
|
|
|
|
2021-08-10 17:39:20 +08:00
|
|
|
foreach (var user in playingUsers)
|
2021-07-06 13:53:31 +08:00
|
|
|
{
|
2022-05-30 18:18:38 +08:00
|
|
|
var scoreProcessor = new SpectatorScoreProcessor(user.UserID);
|
|
|
|
scoreProcessor.Mode.BindTo(scoringMode);
|
|
|
|
scoreProcessor.TotalScore.BindValueChanged(_ => Scheduler.AddOnce(updateTotals));
|
|
|
|
AddInternal(scoreProcessor);
|
2022-04-12 10:04:53 +08:00
|
|
|
|
2022-05-30 18:18:38 +08:00
|
|
|
var trackedUser = new TrackedUserData(user, scoreProcessor);
|
2021-08-10 17:39:20 +08:00
|
|
|
UserScores[user.UserID] = trackedUser;
|
2021-08-09 16:07:50 +08:00
|
|
|
|
2021-08-09 16:23:02 +08:00
|
|
|
if (trackedUser.Team is int team && !TeamScores.ContainsKey(team))
|
2022-04-12 13:13:07 +08:00
|
|
|
TeamScores.Add(team, new BindableLong());
|
2021-07-06 13:53:31 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 18:18:38 +08:00
|
|
|
userLookupCache.GetUsersAsync(playingUsers.Select(u => u.UserID).ToArray(), cancellationToken)
|
|
|
|
.ContinueWith(task =>
|
|
|
|
{
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
var users = task.GetResultSafely();
|
|
|
|
|
|
|
|
for (int i = 0; i < users.Length; i++)
|
|
|
|
{
|
|
|
|
var user = users[i] ?? new APIUser
|
|
|
|
{
|
|
|
|
Id = playingUsers[i].UserID,
|
|
|
|
Username = "Unknown user",
|
|
|
|
};
|
|
|
|
|
|
|
|
var trackedUser = UserScores[user.Id];
|
|
|
|
|
|
|
|
var leaderboardScore = Add(user, user.Id == api.LocalUser.Value.Id);
|
|
|
|
leaderboardScore.Accuracy.BindTo(trackedUser.ScoreProcessor.Accuracy);
|
|
|
|
leaderboardScore.TotalScore.BindTo(trackedUser.ScoreProcessor.TotalScore);
|
|
|
|
leaderboardScore.Combo.BindTo(trackedUser.ScoreProcessor.Combo);
|
|
|
|
leaderboardScore.HasQuit.BindTo(trackedUser.UserQuit);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, cancellationToken);
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
|
|
|
|
2021-07-06 00:15:30 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2020-12-26 10:43:10 +08:00
|
|
|
// BindableList handles binding in a really bad way (Clear then AddRange) so we need to do this manually..
|
2021-08-10 17:39:20 +08:00
|
|
|
foreach (var user in playingUsers)
|
2020-12-26 10:43:10 +08:00
|
|
|
{
|
2021-08-10 17:39:20 +08:00
|
|
|
spectatorClient.WatchUser(user.UserID);
|
2021-05-20 18:44:43 +08:00
|
|
|
|
2021-08-10 17:39:20 +08:00
|
|
|
if (!multiplayerClient.CurrentMatchPlayingUserIds.Contains(user.UserID))
|
2022-05-30 18:18:38 +08:00
|
|
|
playingUsersChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, new[] { user.UserID }));
|
2020-12-26 10:43:10 +08:00
|
|
|
}
|
|
|
|
|
2021-07-06 13:53:31 +08:00
|
|
|
// bind here is to support players leaving the match.
|
|
|
|
// new players are not supported.
|
2021-08-10 17:39:20 +08:00
|
|
|
playingUserIds.BindTo(multiplayerClient.CurrentMatchPlayingUserIds);
|
2022-05-30 18:18:38 +08:00
|
|
|
playingUserIds.BindCollectionChanged(playingUsersChanged);
|
2020-12-26 10:34:05 +08:00
|
|
|
}
|
|
|
|
|
2022-09-26 15:21:23 +08:00
|
|
|
protected override GameplayLeaderboardScore CreateLeaderboardScoreDrawable(IUser? user, bool isTracked)
|
2021-08-09 16:04:06 +08:00
|
|
|
{
|
|
|
|
var leaderboardScore = base.CreateLeaderboardScoreDrawable(user, isTracked);
|
|
|
|
|
2022-09-26 15:21:23 +08:00
|
|
|
if (user != null)
|
2021-08-09 16:04:06 +08:00
|
|
|
{
|
2022-09-26 15:21:23 +08:00
|
|
|
if (UserScores[user.OnlineID].Team is int team)
|
|
|
|
{
|
|
|
|
leaderboardScore.BackgroundColour = getTeamColour(team).Lighten(1.2f);
|
|
|
|
leaderboardScore.TextColour = Color4.White;
|
|
|
|
}
|
2021-08-09 16:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return leaderboardScore;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Color4 getTeamColour(int team)
|
|
|
|
{
|
|
|
|
switch (team)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return colours.TeamColourRed;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return colours.TeamColourBlue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:18:38 +08:00
|
|
|
private void playingUsersChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2020-12-26 10:34:05 +08:00
|
|
|
{
|
|
|
|
switch (e.Action)
|
|
|
|
{
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
2021-10-27 12:04:41 +08:00
|
|
|
foreach (int userId in e.OldItems.OfType<int>())
|
2020-12-26 10:34:05 +08:00
|
|
|
{
|
2021-05-20 14:55:07 +08:00
|
|
|
spectatorClient.StopWatchingUser(userId);
|
2020-12-26 10:35:51 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
if (UserScores.TryGetValue(userId, out var trackedData))
|
2020-12-26 10:35:51 +08:00
|
|
|
trackedData.MarkUserQuit();
|
2020-12-26 10:34:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 16:07:50 +08:00
|
|
|
private void updateTotals()
|
|
|
|
{
|
|
|
|
if (!hasTeams)
|
|
|
|
return;
|
|
|
|
|
2021-08-09 16:23:02 +08:00
|
|
|
foreach (var scores in TeamScores.Values) scores.Value = 0;
|
2021-08-09 16:07:50 +08:00
|
|
|
|
|
|
|
foreach (var u in UserScores.Values)
|
|
|
|
{
|
2021-08-09 16:23:02 +08:00
|
|
|
if (u.Team == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (TeamScores.TryGetValue(u.Team.Value, out var team))
|
2022-05-30 18:18:38 +08:00
|
|
|
team.Value += (int)Math.Round(u.ScoreProcessor.TotalScore.Value);
|
2021-08-09 16:07:50 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2020-12-16 15:05:46 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2022-09-26 15:21:23 +08:00
|
|
|
if (spectatorClient.IsNotNull())
|
2020-12-16 15:05:46 +08:00
|
|
|
{
|
2020-12-26 10:34:05 +08:00
|
|
|
foreach (var user in playingUsers)
|
2021-08-10 17:39:20 +08:00
|
|
|
spectatorClient.StopWatchingUser(user.UserID);
|
2020-12-16 15:05:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
protected class TrackedUserData
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2021-08-06 19:06:57 +08:00
|
|
|
public readonly MultiplayerRoomUser User;
|
2022-05-30 18:18:38 +08:00
|
|
|
public readonly SpectatorScoreProcessor ScoreProcessor;
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public readonly BindableBool UserQuit = new BindableBool();
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-08-09 16:04:06 +08:00
|
|
|
public int? Team => (User.MatchState as TeamVersusUserState)?.TeamID;
|
|
|
|
|
2022-05-30 18:18:38 +08:00
|
|
|
public TrackedUserData(MultiplayerRoomUser user, SpectatorScoreProcessor scoreProcessor)
|
2021-04-12 21:00:27 +08:00
|
|
|
{
|
2021-08-06 19:06:57 +08:00
|
|
|
User = user;
|
2021-04-12 21:00:27 +08:00
|
|
|
ScoreProcessor = scoreProcessor;
|
|
|
|
}
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public void MarkUserQuit() => UserQuit.Value = true;
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|