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.Collections.Generic;
|
2020-12-26 10:34:05 +08:00
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2020-12-16 14:25:27 +08:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
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;
|
2020-12-26 10:34:05 +08:00
|
|
|
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
|
|
|
|
{
|
2020-12-17 14:48:53 +08:00
|
|
|
[LongRunningLoad]
|
2020-12-16 14:25:27 +08:00
|
|
|
public class MultiplayerGameplayLeaderboard : GameplayLeaderboard
|
|
|
|
{
|
|
|
|
private readonly ScoreProcessor scoreProcessor;
|
|
|
|
|
2020-12-16 15:05:46 +08:00
|
|
|
private readonly Dictionary<int, TrackedUserData> userScores = new Dictionary<int, TrackedUserData>();
|
|
|
|
|
2020-12-26 10:34:05 +08:00
|
|
|
[Resolved]
|
|
|
|
private SpectatorStreamingClient streamingClient { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private StatefulMultiplayerClient multiplayerClient { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private UserLookupCache userLookupCache { get; set; }
|
|
|
|
|
|
|
|
private Bindable<ScoringMode> scoringMode;
|
|
|
|
|
|
|
|
private readonly BindableList<int> playingUsers;
|
|
|
|
|
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>
|
2020-12-16 15:05:46 +08:00
|
|
|
/// <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
|
|
|
{
|
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
|
|
|
|
|
|
|
// todo: this will likely be passed in as User instances.
|
2020-12-26 10:34:05 +08:00
|
|
|
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
|
|
|
{
|
|
|
|
streamingClient.OnNewFrames += handleIncomingFrames;
|
|
|
|
|
2020-12-26 10:34:05 +08:00
|
|
|
foreach (var userId in playingUsers)
|
2020-12-16 14:25:27 +08:00
|
|
|
{
|
2020-12-26 10:34:05 +08:00
|
|
|
streamingClient.WatchUser(userId);
|
2020-12-16 15:05:46 +08:00
|
|
|
|
|
|
|
// probably won't be required in the final implementation.
|
2020-12-26 10:34:05 +08:00
|
|
|
var resolvedUser = userLookupCache.GetUserAsync(userId).Result;
|
2020-12-16 14:25:27 +08:00
|
|
|
|
|
|
|
var trackedUser = new TrackedUserData();
|
|
|
|
|
2020-12-26 10:34:05 +08:00
|
|
|
userScores[userId] = trackedUser;
|
2020-12-18 16:13:51 +08:00
|
|
|
var leaderboardScore = AddPlayer(resolvedUser, resolvedUser.Id == api.LocalUser.Value.Id);
|
|
|
|
|
|
|
|
((IBindable<double>)leaderboardScore.Accuracy).BindTo(trackedUser.Accuracy);
|
|
|
|
((IBindable<double>)leaderboardScore.TotalScore).BindTo(trackedUser.Score);
|
|
|
|
((IBindable<int>)leaderboardScore.Combo).BindTo(trackedUser.CurrentCombo);
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
scoringMode = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode);
|
|
|
|
scoringMode.BindValueChanged(updateAllScores, true);
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:34:05 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
playingUsers.BindCollectionChanged(usersChanged);
|
|
|
|
playingUsers.BindTo(multiplayerClient.PlayingUsers);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void usersChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.Action)
|
|
|
|
{
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
foreach (var userId in e.OldItems.OfType<int>())
|
|
|
|
{
|
|
|
|
streamingClient.StopWatchingUser(userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:25:27 +08:00
|
|
|
private void updateAllScores(ValueChangedEvent<ScoringMode> mode)
|
|
|
|
{
|
|
|
|
foreach (var trackedData in userScores.Values)
|
|
|
|
trackedData.UpdateScore(scoreProcessor, mode.NewValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIncomingFrames(int userId, FrameDataBundle bundle)
|
|
|
|
{
|
|
|
|
if (userScores.TryGetValue(userId, out var trackedData))
|
|
|
|
{
|
|
|
|
trackedData.LastHeader = bundle.Header;
|
|
|
|
trackedData.UpdateScore(scoreProcessor, scoringMode.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:05:46 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
if (streamingClient != null)
|
|
|
|
{
|
2020-12-26 10:34:05 +08:00
|
|
|
foreach (var user in playingUsers)
|
2020-12-16 15:05:46 +08:00
|
|
|
{
|
|
|
|
streamingClient.StopWatchingUser(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
streamingClient.OnNewFrames -= handleIncomingFrames;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:25:27 +08:00
|
|
|
private class TrackedUserData
|
|
|
|
{
|
2020-12-16 15:08:44 +08:00
|
|
|
public IBindableNumber<double> Score => score;
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2020-12-16 15:08:44 +08:00
|
|
|
private readonly BindableDouble score = new BindableDouble();
|
2020-12-16 14:25:27 +08:00
|
|
|
|
2020-12-16 15:08:44 +08:00
|
|
|
public IBindableNumber<double> Accuracy => accuracy;
|
|
|
|
|
2020-12-20 04:31:17 +08:00
|
|
|
private readonly BindableDouble accuracy = new BindableDouble(1);
|
2020-12-16 15:08:44 +08:00
|
|
|
|
|
|
|
public IBindableNumber<int> CurrentCombo => currentCombo;
|
|
|
|
|
|
|
|
private readonly BindableInt currentCombo = new BindableInt();
|
2020-12-16 14:25:27 +08:00
|
|
|
|
|
|
|
[CanBeNull]
|
|
|
|
public FrameHeader LastHeader;
|
|
|
|
|
|
|
|
public void UpdateScore(ScoreProcessor processor, ScoringMode mode)
|
|
|
|
{
|
|
|
|
if (LastHeader == null)
|
|
|
|
return;
|
|
|
|
|
2020-12-24 13:57:23 +08:00
|
|
|
score.Value = processor.GetImmediateScore(mode, LastHeader.MaxCombo, LastHeader.Statistics);
|
|
|
|
accuracy.Value = LastHeader.Accuracy;
|
2020-12-16 15:08:44 +08:00
|
|
|
currentCombo.Value = LastHeader.Combo;
|
2020-12-16 14:25:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|