1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 07:47:25 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/MultiplayerGameplayLeaderboard.cs

199 lines
7.1 KiB
C#
Raw Normal View History

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.
using System;
2020-12-16 14:25:27 +08:00
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
2020-12-16 14:25:27 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Configuration;
using osu.Game.Database;
2020-12-18 15:55:55 +08:00
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
2020-12-16 14:25:27 +08:00
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Scoring;
namespace osu.Game.Screens.Play.HUD
{
[LongRunningLoad]
2020-12-16 14:25:27 +08:00
public class MultiplayerGameplayLeaderboard : GameplayLeaderboard
{
protected readonly Dictionary<int, TrackedUserData> UserScores = new Dictionary<int, TrackedUserData>();
[Resolved]
private SpectatorClient spectatorClient { get; set; }
[Resolved]
2021-05-20 14:39:45 +08:00
private MultiplayerClient multiplayerClient { get; set; }
[Resolved]
private UserLookupCache userLookupCache { get; set; }
private readonly ScoreProcessor scoreProcessor;
private readonly BindableList<int> playingUsers;
private Bindable<ScoringMode> scoringMode;
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>
/// <param name="userIds">IDs of all users in this match.</param>
public MultiplayerGameplayLeaderboard(ScoreProcessor scoreProcessor, int[] userIds)
2020-12-16 14:25:27 +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;
// todo: this will likely be passed in as User instances.
playingUsers = new BindableList<int>(userIds);
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
{
scoringMode = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode);
foreach (var userId in playingUsers)
2020-12-16 14:25:27 +08:00
{
spectatorClient.WatchUser(userId);
// probably won't be required in the final implementation.
var resolvedUser = userLookupCache.GetUserAsync(userId).Result;
2020-12-16 14:25:27 +08:00
var trackedUser = CreateUserData(userId, scoreProcessor);
trackedUser.ScoringMode.BindTo(scoringMode);
2020-12-16 14:25:27 +08:00
var leaderboardScore = AddPlayer(resolvedUser, resolvedUser?.Id == api.LocalUser.Value.Id);
leaderboardScore.Accuracy.BindTo(trackedUser.Accuracy);
leaderboardScore.TotalScore.BindTo(trackedUser.Score);
leaderboardScore.Combo.BindTo(trackedUser.CurrentCombo);
leaderboardScore.HasQuit.BindTo(trackedUser.UserQuit);
2020-12-18 16:13:51 +08:00
UserScores[userId] = trackedUser;
2020-12-16 14:25:27 +08:00
}
}
protected override void LoadComplete()
{
base.LoadComplete();
// BindableList handles binding in a really bad way (Clear then AddRange) so we need to do this manually..
foreach (int userId in playingUsers)
{
if (!multiplayerClient.CurrentMatchPlayingUserIds.Contains(userId))
usersChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, new[] { userId }));
}
playingUsers.BindTo(multiplayerClient.CurrentMatchPlayingUserIds);
playingUsers.BindCollectionChanged(usersChanged);
// this leaderboard should be guaranteed to be completely loaded before the gameplay starts (is a prerequisite in MultiplayerPlayer).
spectatorClient.OnNewFrames += handleIncomingFrames;
}
private void usersChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Remove:
foreach (var userId in e.OldItems.OfType<int>())
{
spectatorClient.StopWatchingUser(userId);
if (UserScores.TryGetValue(userId, out var trackedData))
trackedData.MarkUserQuit();
}
break;
}
}
private void handleIncomingFrames(int userId, FrameDataBundle bundle) => Schedule(() =>
2020-12-16 14:25:27 +08:00
{
if (!UserScores.TryGetValue(userId, out var trackedData))
return;
trackedData.Frames.Add(new TimedFrame(bundle.Frames.First().Time, bundle.Header));
trackedData.UpdateScore();
});
protected virtual TrackedUserData CreateUserData(int userId, ScoreProcessor scoreProcessor) => new TrackedUserData(userId, scoreProcessor);
2020-12-16 14:25:27 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (spectatorClient != null)
{
foreach (var user in playingUsers)
{
spectatorClient.StopWatchingUser(user);
}
spectatorClient.OnNewFrames -= handleIncomingFrames;
}
}
protected class TrackedUserData
2020-12-16 14:25:27 +08:00
{
public readonly int UserId;
public readonly ScoreProcessor ScoreProcessor;
2020-12-16 14:25: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
public readonly IBindable<ScoringMode> ScoringMode = new Bindable<ScoringMode>();
public readonly List<TimedFrame> Frames = new List<TimedFrame>();
public TrackedUserData(int userId, ScoreProcessor scoreProcessor)
{
UserId = userId;
ScoreProcessor = scoreProcessor;
ScoringMode.BindValueChanged(_ => UpdateScore());
}
2020-12-16 14:25:27 +08:00
public void MarkUserQuit() => UserQuit.Value = true;
public virtual void UpdateScore()
{
if (Frames.Count == 0)
return;
SetFrame(Frames.Last());
}
2020-12-16 14:25:27 +08:00
protected void SetFrame(TimedFrame frame)
{
var header = frame.Header;
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
{
Time = time;
}
2020-12-16 14:25:27 +08:00
public TimedFrame(double time, FrameHeader header)
{
Time = time;
Header = header;
2020-12-16 14:25:27 +08:00
}
public int CompareTo(TimedFrame other) => Time.CompareTo(other.Time);
2020-12-16 14:25:27 +08:00
}
}
}