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;
|
2020-12-16 14:25:27 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2021-08-09 16:04:06 +08:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
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;
|
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;
|
2021-08-09 16:04:06 +08:00
|
|
|
using osu.Game.Users;
|
|
|
|
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
|
|
|
|
2021-08-09 18:48:53 +08:00
|
|
|
public readonly SortedDictionary<int, BindableInt> TeamScores = new SortedDictionary<int, BindableInt>();
|
2021-08-09 16:07:50 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
2020-12-26 10:34:05 +08:00
|
|
|
[Resolved]
|
2021-05-20 14:55:07 +08:00
|
|
|
private SpectatorClient spectatorClient { get; set; }
|
2020-12-26 10:34:05 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2021-05-20 14:39:45 +08:00
|
|
|
private MultiplayerClient multiplayerClient { get; set; }
|
2020-12-26 10:34:05 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private UserLookupCache userLookupCache { get; set; }
|
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
private readonly ScoreProcessor scoreProcessor;
|
2021-08-10 17:39:20 +08:00
|
|
|
private readonly MultiplayerRoomUser[] playingUsers;
|
2021-04-12 21:00:27 +08:00
|
|
|
private Bindable<ScoringMode> scoringMode;
|
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>
|
|
|
|
/// <param name="scoreProcessor">A score processor instance to handle score calculation for scores of users in the match.</param>
|
2021-08-10 17:39:20 +08:00
|
|
|
/// <param name="users">IDs of all users in this match.</param>
|
|
|
|
public MultiplayerGameplayLeaderboard(ScoreProcessor scoreProcessor, MultiplayerRoomUser[] users)
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2020-12-16 15:05:46 +08:00
|
|
|
// todo: this will eventually need to be created per user to support different mod combinations.
|
2020-12-16 14:25:27 +08:00
|
|
|
this.scoreProcessor = scoreProcessor;
|
2020-12-16 15:05:46 +08:00
|
|
|
|
2021-08-10 17:39:20 +08:00
|
|
|
playingUsers = users;
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2020-12-18 15:55:55 +08:00
|
|
|
private void load(OsuConfigManager config, IAPIProvider api)
|
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
|
|
|
{
|
2021-08-06 19:06:57 +08:00
|
|
|
var trackedUser = CreateUserData(user, scoreProcessor);
|
2021-07-06 13:53:31 +08:00
|
|
|
trackedUser.ScoringMode.BindTo(scoringMode);
|
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))
|
|
|
|
TeamScores.Add(team, new BindableInt());
|
2021-07-06 13:53:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 17:39:20 +08:00
|
|
|
userLookupCache.GetUsersAsync(playingUsers.Select(u => u.UserID).ToArray()).ContinueWith(users => Schedule(() =>
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2021-07-05 18:56:36 +08:00
|
|
|
foreach (var user in users.Result)
|
|
|
|
{
|
2021-07-05 20:30:24 +08:00
|
|
|
if (user == null)
|
|
|
|
continue;
|
|
|
|
|
2021-07-06 13:53:31 +08:00
|
|
|
var trackedUser = UserScores[user.Id];
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-08-12 15:17:27 +08:00
|
|
|
var leaderboardScore = Add(user, user.Id == api.LocalUser.Value.Id);
|
2021-07-05 18:56:36 +08:00
|
|
|
leaderboardScore.Accuracy.BindTo(trackedUser.Accuracy);
|
|
|
|
leaderboardScore.TotalScore.BindTo(trackedUser.Score);
|
|
|
|
leaderboardScore.Combo.BindTo(trackedUser.CurrentCombo);
|
|
|
|
leaderboardScore.HasQuit.BindTo(trackedUser.UserQuit);
|
|
|
|
}
|
|
|
|
}));
|
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))
|
|
|
|
usersChanged(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);
|
|
|
|
playingUserIds.BindCollectionChanged(usersChanged);
|
2021-01-25 16:44:01 +08:00
|
|
|
|
|
|
|
// this leaderboard should be guaranteed to be completely loaded before the gameplay starts (is a prerequisite in MultiplayerPlayer).
|
2021-05-20 14:55:07 +08:00
|
|
|
spectatorClient.OnNewFrames += handleIncomingFrames;
|
2020-12-26 10:34:05 +08:00
|
|
|
}
|
|
|
|
|
2021-08-09 16:07:50 +08:00
|
|
|
protected virtual TrackedUserData CreateUserData(MultiplayerRoomUser user, ScoreProcessor scoreProcessor) => new TrackedUserData(user, scoreProcessor);
|
2021-08-09 16:04:06 +08:00
|
|
|
|
|
|
|
protected override GameplayLeaderboardScore CreateLeaderboardScoreDrawable(User user, bool isTracked)
|
|
|
|
{
|
|
|
|
var leaderboardScore = base.CreateLeaderboardScoreDrawable(user, isTracked);
|
|
|
|
|
|
|
|
if (UserScores[user.Id].Team is int team)
|
|
|
|
{
|
|
|
|
leaderboardScore.BackgroundColour = getTeamColour(team).Lighten(1.2f);
|
|
|
|
leaderboardScore.TextColour = Color4.White;
|
|
|
|
}
|
|
|
|
|
|
|
|
return leaderboardScore;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Color4 getTeamColour(int team)
|
|
|
|
{
|
|
|
|
switch (team)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return colours.TeamColourRed;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return colours.TeamColourBlue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:34:05 +08:00
|
|
|
private void usersChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
{
|
|
|
|
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-04-09 16:31:14 +08:00
|
|
|
private void handleIncomingFrames(int userId, FrameDataBundle bundle) => Schedule(() =>
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2021-04-12 21:00:27 +08:00
|
|
|
if (!UserScores.TryGetValue(userId, out var trackedData))
|
|
|
|
return;
|
|
|
|
|
|
|
|
trackedData.Frames.Add(new TimedFrame(bundle.Frames.First().Time, bundle.Header));
|
|
|
|
trackedData.UpdateScore();
|
2021-08-09 16:07:50 +08:00
|
|
|
|
|
|
|
updateTotals();
|
2021-04-09 16:31:14 +08:00
|
|
|
});
|
|
|
|
|
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))
|
2021-08-14 05:44:07 +08:00
|
|
|
team.Value += (int)Math.Round(u.Score.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);
|
|
|
|
|
2021-05-20 14:55:07 +08:00
|
|
|
if (spectatorClient != null)
|
2020-12-16 15:05:46 +08:00
|
|
|
{
|
2020-12-26 10:34:05 +08:00
|
|
|
foreach (var user in playingUsers)
|
2020-12-16 15:05:46 +08:00
|
|
|
{
|
2021-08-10 17:39:20 +08:00
|
|
|
spectatorClient.StopWatchingUser(user.UserID);
|
2020-12-16 15:05:46 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:55:07 +08:00
|
|
|
spectatorClient.OnNewFrames -= handleIncomingFrames;
|
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;
|
2021-04-12 21:00:27 +08:00
|
|
|
public readonly ScoreProcessor ScoreProcessor;
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public readonly BindableDouble Score = new BindableDouble();
|
|
|
|
public readonly BindableDouble Accuracy = new BindableDouble(1);
|
|
|
|
public readonly BindableInt CurrentCombo = new BindableInt();
|
|
|
|
public readonly BindableBool UserQuit = new BindableBool();
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public readonly IBindable<ScoringMode> ScoringMode = new Bindable<ScoringMode>();
|
2020-12-16 15:08:44 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public readonly List<TimedFrame> Frames = new List<TimedFrame>();
|
2020-12-16 15:08:44 +08:00
|
|
|
|
2021-08-09 16:04:06 +08:00
|
|
|
public int? Team => (User.MatchState as TeamVersusUserState)?.TeamID;
|
|
|
|
|
2021-08-06 19:06:57 +08:00
|
|
|
public TrackedUserData(MultiplayerRoomUser user, ScoreProcessor 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 15:08:44 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
ScoringMode.BindValueChanged(_ => UpdateScore());
|
|
|
|
}
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public void MarkUserQuit() => UserQuit.Value = true;
|
2020-12-26 10:35:51 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public virtual void UpdateScore()
|
|
|
|
{
|
|
|
|
if (Frames.Count == 0)
|
|
|
|
return;
|
2020-12-26 10:35:51 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
SetFrame(Frames.Last());
|
|
|
|
}
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
protected void SetFrame(TimedFrame frame)
|
|
|
|
{
|
|
|
|
var header = frame.Header;
|
2020-12-26 10:35:51 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
Score.Value = ScoreProcessor.GetImmediateScore(ScoringMode.Value, header.MaxCombo, header.Statistics);
|
|
|
|
Accuracy.Value = header.Accuracy;
|
|
|
|
CurrentCombo.Value = header.Combo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected class TimedFrame : IComparable<TimedFrame>
|
|
|
|
{
|
|
|
|
public readonly double Time;
|
|
|
|
public readonly FrameHeader Header;
|
|
|
|
|
|
|
|
public TimedFrame(double time)
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2021-04-12 21:00:27 +08:00
|
|
|
Time = time;
|
|
|
|
}
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2021-04-12 21:00:27 +08:00
|
|
|
public TimedFrame(double time, FrameHeader header)
|
|
|
|
{
|
|
|
|
Time = time;
|
|
|
|
Header = header;
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
2021-04-12 21:00:27 +08:00
|
|
|
|
|
|
|
public int CompareTo(TimedFrame other) => Time.CompareTo(other.Time);
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|