// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; using System.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Game.Configuration; using osu.Game.Database; using osu.Game.Online.API; using osu.Game.Online.Multiplayer; using osu.Game.Online.Spectator; using osu.Game.Rulesets.Scoring; namespace osu.Game.Screens.Play.HUD { [LongRunningLoad] public class MultiplayerGameplayLeaderboard : GameplayLeaderboard { private readonly ScoreProcessor scoreProcessor; private readonly Dictionary userScores = new Dictionary(); [Resolved] private SpectatorStreamingClient streamingClient { get; set; } [Resolved] private StatefulMultiplayerClient multiplayerClient { get; set; } [Resolved] private UserLookupCache userLookupCache { get; set; } private Bindable scoringMode; private readonly BindableList playingUsers; /// /// Construct a new leaderboard. /// /// A score processor instance to handle score calculation for scores of users in the match. /// IDs of all users in this match. public MultiplayerGameplayLeaderboard(ScoreProcessor scoreProcessor, int[] userIds) { // todo: this will eventually need to be created per user to support different mod combinations. this.scoreProcessor = scoreProcessor; // todo: this will likely be passed in as User instances. playingUsers = new BindableList(userIds); } [BackgroundDependencyLoader] private void load(OsuConfigManager config, IAPIProvider api) { streamingClient.OnNewFrames += handleIncomingFrames; foreach (var userId in playingUsers) { streamingClient.WatchUser(userId); // probably won't be required in the final implementation. var resolvedUser = userLookupCache.GetUserAsync(userId).Result; var trackedUser = new TrackedUserData(); userScores[userId] = trackedUser; var leaderboardScore = AddPlayer(resolvedUser, resolvedUser.Id == api.LocalUser.Value.Id); ((IBindable)leaderboardScore.Accuracy).BindTo(trackedUser.Accuracy); ((IBindable)leaderboardScore.TotalScore).BindTo(trackedUser.Score); ((IBindable)leaderboardScore.Combo).BindTo(trackedUser.CurrentCombo); } scoringMode = config.GetBindable(OsuSetting.ScoreDisplayMode); scoringMode.BindValueChanged(updateAllScores, true); } 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()) { streamingClient.StopWatchingUser(userId); if (userScores.TryGetValue(userId, out var trackedData)) trackedData.MarkUserQuit(); } break; } } private void updateAllScores(ValueChangedEvent 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); } } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); if (streamingClient != null) { foreach (var user in playingUsers) { streamingClient.StopWatchingUser(user); } streamingClient.OnNewFrames -= handleIncomingFrames; } } private class TrackedUserData { public IBindableNumber Score => score; private readonly BindableDouble score = new BindableDouble(); public IBindableNumber Accuracy => accuracy; private readonly BindableDouble accuracy = new BindableDouble(1); public IBindableNumber CurrentCombo => currentCombo; private readonly BindableInt currentCombo = new BindableInt(); public IBindable UserQuit => userQuit; private readonly BindableBool userQuit = new BindableBool(); [CanBeNull] public FrameHeader LastHeader; public void MarkUserQuit() => userQuit.Value = true; public void UpdateScore(ScoreProcessor processor, ScoringMode mode) { Debug.Assert(UserQuit.Value); if (LastHeader == null) return; score.Value = processor.GetImmediateScore(mode, LastHeader.MaxCombo, LastHeader.Statistics); accuracy.Value = LastHeader.Accuracy; currentCombo.Value = LastHeader.Combo; } } } }