1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-21 20:12:57 +08:00

Rework to create a derived tracked user data instead

This commit is contained in:
smoogipoo 2021-04-12 22:00:27 +09:00
parent 27660265b5
commit c531e38a36
3 changed files with 107 additions and 117 deletions

View File

@ -163,7 +163,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
break; break;
} }
((ISpectatorClient)this).UserSentFrames(userId, new FrameDataBundle(header, Array.Empty<LegacyReplayFrame>())); ((ISpectatorClient)this).UserSentFrames(userId, new FrameDataBundle(header, new[] { new LegacyReplayFrame(Time.Current, 0, 0, ReplayButtonState.None) }));
} }
} }
} }

View File

@ -2,10 +2,8 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic; using JetBrains.Annotations;
using System.Linq;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Scoring; using osu.Game.Rulesets.Scoring;
using osu.Game.Screens.Play.HUD; using osu.Game.Screens.Play.HUD;
@ -13,73 +11,62 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
{ {
public class MultiplayerSpectatorLeaderboard : MultiplayerGameplayLeaderboard public class MultiplayerSpectatorLeaderboard : MultiplayerGameplayLeaderboard
{ {
private readonly Dictionary<int, TrackedUserData> trackedData = new Dictionary<int, TrackedUserData>();
public MultiplayerSpectatorLeaderboard(ScoreProcessor scoreProcessor, int[] userIds) public MultiplayerSpectatorLeaderboard(ScoreProcessor scoreProcessor, int[] userIds)
: base(scoreProcessor, userIds) : base(scoreProcessor, userIds)
{ {
} }
public void AddClock(int userId, IClock source) => trackedData[userId] = new TrackedUserData(source); public void AddClock(int userId, IClock clock)
public void RemoveClock(int userId) => trackedData.Remove(userId);
protected override void OnIncomingFrames(int userId, FrameDataBundle bundle)
{ {
if (!trackedData.TryGetValue(userId, out var data)) if (!UserScores.TryGetValue(userId, out var data))
return; return;
data.Frames.Add(new TimedFrameHeader(bundle.Frames.First().Time, bundle.Header)); ((SpectatingTrackedUserData)data).Clock = clock;
} }
public void RemoveClock(int userId)
{
if (!UserScores.TryGetValue(userId, out var data))
return;
((SpectatingTrackedUserData)data).Clock = null;
}
protected override TrackedUserData CreateUserData(int userId, ScoreProcessor scoreProcessor) => new SpectatingTrackedUserData(userId, scoreProcessor);
protected override void Update() protected override void Update()
{ {
base.Update(); base.Update();
foreach (var (userId, data) in trackedData) foreach (var (_, data) in UserScores)
data.UpdateScore();
}
private class SpectatingTrackedUserData : TrackedUserData
{
[CanBeNull]
public IClock Clock;
public SpectatingTrackedUserData(int userId, ScoreProcessor scoreProcessor)
: base(userId, scoreProcessor)
{ {
var targetTime = data.Clock.CurrentTime; }
if (data.Frames.Count == 0) public override void UpdateScore()
continue; {
if (Frames.Count == 0)
return;
int frameIndex = data.Frames.BinarySearch(new TimedFrameHeader(targetTime)); if (Clock == null)
return;
int frameIndex = Frames.BinarySearch(new TimedFrame(Clock.CurrentTime));
if (frameIndex < 0) if (frameIndex < 0)
frameIndex = ~frameIndex; frameIndex = ~frameIndex;
frameIndex = Math.Clamp(frameIndex - 1, 0, data.Frames.Count - 1); frameIndex = Math.Clamp(frameIndex - 1, 0, Frames.Count - 1);
SetCurrentFrame(userId, data.Frames[frameIndex].Header); SetFrame(Frames[frameIndex]);
} }
} }
private class TrackedUserData
{
public readonly IClock Clock;
public readonly List<TimedFrameHeader> Frames = new List<TimedFrameHeader>();
public TrackedUserData(IClock clock)
{
Clock = clock;
}
}
private class TimedFrameHeader : IComparable<TimedFrameHeader>
{
public readonly double Time;
public readonly FrameHeader Header;
public TimedFrameHeader(double time)
{
Time = time;
}
public TimedFrameHeader(double time, FrameHeader header)
{
Time = time;
Header = header;
}
public int CompareTo(TimedFrameHeader other) => Time.CompareTo(other.Time);
}
} }
} }

View File

@ -1,10 +1,10 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Game.Configuration; using osu.Game.Configuration;
@ -19,8 +19,7 @@ namespace osu.Game.Screens.Play.HUD
[LongRunningLoad] [LongRunningLoad]
public class MultiplayerGameplayLeaderboard : GameplayLeaderboard public class MultiplayerGameplayLeaderboard : GameplayLeaderboard
{ {
private readonly ScoreProcessor scoreProcessor; protected readonly Dictionary<int, TrackedUserData> UserScores = new Dictionary<int, TrackedUserData>();
private readonly Dictionary<int, TrackedUserData> userScores = new Dictionary<int, TrackedUserData>();
[Resolved] [Resolved]
private SpectatorStreamingClient streamingClient { get; set; } private SpectatorStreamingClient streamingClient { get; set; }
@ -31,9 +30,9 @@ namespace osu.Game.Screens.Play.HUD
[Resolved] [Resolved]
private UserLookupCache userLookupCache { get; set; } private UserLookupCache userLookupCache { get; set; }
private Bindable<ScoringMode> scoringMode; private readonly ScoreProcessor scoreProcessor;
private readonly BindableList<int> playingUsers; private readonly BindableList<int> playingUsers;
private Bindable<ScoringMode> scoringMode;
/// <summary> /// <summary>
/// Construct a new leaderboard. /// Construct a new leaderboard.
@ -52,6 +51,8 @@ namespace osu.Game.Screens.Play.HUD
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuConfigManager config, IAPIProvider api) private void load(OsuConfigManager config, IAPIProvider api)
{ {
scoringMode = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode);
foreach (var userId in playingUsers) foreach (var userId in playingUsers)
{ {
streamingClient.WatchUser(userId); streamingClient.WatchUser(userId);
@ -59,19 +60,17 @@ namespace osu.Game.Screens.Play.HUD
// probably won't be required in the final implementation. // probably won't be required in the final implementation.
var resolvedUser = userLookupCache.GetUserAsync(userId).Result; var resolvedUser = userLookupCache.GetUserAsync(userId).Result;
var trackedUser = new TrackedUserData(); var trackedUser = CreateUserData(userId, scoreProcessor);
trackedUser.ScoringMode.BindTo(scoringMode);
userScores[userId] = trackedUser;
var leaderboardScore = AddPlayer(resolvedUser, resolvedUser?.Id == api.LocalUser.Value.Id); 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);
((IBindable<double>)leaderboardScore.Accuracy).BindTo(trackedUser.Accuracy); UserScores[userId] = trackedUser;
((IBindable<double>)leaderboardScore.TotalScore).BindTo(trackedUser.Score);
((IBindable<int>)leaderboardScore.Combo).BindTo(trackedUser.CurrentCombo);
((IBindable<bool>)leaderboardScore.HasQuit).BindTo(trackedUser.UserQuit);
} }
scoringMode = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode);
scoringMode.BindValueChanged(updateAllScores, true);
} }
protected override void LoadComplete() protected override void LoadComplete()
@ -101,7 +100,7 @@ namespace osu.Game.Screens.Play.HUD
{ {
streamingClient.StopWatchingUser(userId); streamingClient.StopWatchingUser(userId);
if (userScores.TryGetValue(userId, out var trackedData)) if (UserScores.TryGetValue(userId, out var trackedData))
trackedData.MarkUserQuit(); trackedData.MarkUserQuit();
} }
@ -109,39 +108,16 @@ namespace osu.Game.Screens.Play.HUD
} }
} }
private void updateAllScores(ValueChangedEvent<ScoringMode> mode)
{
foreach (var trackedData in userScores.Values)
trackedData.UpdateScore(scoreProcessor, mode.NewValue);
}
private void handleIncomingFrames(int userId, FrameDataBundle bundle) => Schedule(() => private void handleIncomingFrames(int userId, FrameDataBundle bundle) => Schedule(() =>
{ {
if (userScores.ContainsKey(userId)) if (!UserScores.TryGetValue(userId, out var trackedData))
OnIncomingFrames(userId, bundle); return;
trackedData.Frames.Add(new TimedFrame(bundle.Frames.First().Time, bundle.Header));
trackedData.UpdateScore();
}); });
/// <summary> protected virtual TrackedUserData CreateUserData(int userId, ScoreProcessor scoreProcessor) => new TrackedUserData(userId, scoreProcessor);
/// Invoked when new frames have arrived for a user.
/// </summary>
/// <remarks>
/// By default, this immediately sets the current frame to be displayed for the user.
/// </remarks>
/// <param name="userId">The user which the frames arrived for.</param>
/// <param name="bundle">The bundle of frames.</param>
protected virtual void OnIncomingFrames(int userId, FrameDataBundle bundle) => SetCurrentFrame(userId, bundle.Header);
/// <summary>
/// Sets the current frame to be displayed for a user.
/// </summary>
/// <param name="userId">The user to set the frame of.</param>
/// <param name="header">The frame to set.</param>
protected void SetCurrentFrame(int userId, FrameHeader header)
{
var trackedScore = userScores[userId];
trackedScore.LastHeader = header;
trackedScore.UpdateScore(scoreProcessor, scoringMode.Value);
}
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
@ -158,38 +134,65 @@ namespace osu.Game.Screens.Play.HUD
} }
} }
private class TrackedUserData protected class TrackedUserData
{ {
public IBindableNumber<double> Score => score; public readonly int UserId;
public readonly ScoreProcessor ScoreProcessor;
private readonly BindableDouble score = new BindableDouble(); 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();
public IBindableNumber<double> Accuracy => accuracy; public readonly IBindable<ScoringMode> ScoringMode = new Bindable<ScoringMode>();
private readonly BindableDouble accuracy = new BindableDouble(1); public readonly List<TimedFrame> Frames = new List<TimedFrame>();
public IBindableNumber<int> CurrentCombo => currentCombo; public TrackedUserData(int userId, ScoreProcessor scoreProcessor)
private readonly BindableInt currentCombo = new BindableInt();
public IBindable<bool> 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)
{ {
if (LastHeader == null) UserId = userId;
ScoreProcessor = scoreProcessor;
ScoringMode.BindValueChanged(_ => UpdateScore());
}
public void MarkUserQuit() => UserQuit.Value = true;
public virtual void UpdateScore()
{
if (Frames.Count == 0)
return; return;
score.Value = processor.GetImmediateScore(mode, LastHeader.MaxCombo, LastHeader.Statistics); SetFrame(Frames.Last());
accuracy.Value = LastHeader.Accuracy;
currentCombo.Value = LastHeader.Combo;
} }
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)
{
Time = time;
}
public TimedFrame(double time, FrameHeader header)
{
Time = time;
Header = header;
}
public int CompareTo(TimedFrame other) => Time.CompareTo(other.Time);
} }
} }
} }