mirror of
https://github.com/ppy/osu.git
synced 2025-02-20 05:03:20 +08:00
Rework to create a derived tracked user data instead
This commit is contained in:
parent
27660265b5
commit
c531e38a36
@ -163,7 +163,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
|
||||
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) }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,8 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using osu.Framework.Timing;
|
||||
using osu.Game.Online.Spectator;
|
||||
using osu.Game.Rulesets.Scoring;
|
||||
using osu.Game.Screens.Play.HUD;
|
||||
|
||||
@ -13,73 +11,62 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
||||
{
|
||||
public class MultiplayerSpectatorLeaderboard : MultiplayerGameplayLeaderboard
|
||||
{
|
||||
private readonly Dictionary<int, TrackedUserData> trackedData = new Dictionary<int, TrackedUserData>();
|
||||
|
||||
public MultiplayerSpectatorLeaderboard(ScoreProcessor scoreProcessor, int[] userIds)
|
||||
: base(scoreProcessor, userIds)
|
||||
{
|
||||
}
|
||||
|
||||
public void AddClock(int userId, IClock source) => trackedData[userId] = new TrackedUserData(source);
|
||||
|
||||
public void RemoveClock(int userId) => trackedData.Remove(userId);
|
||||
|
||||
protected override void OnIncomingFrames(int userId, FrameDataBundle bundle)
|
||||
public void AddClock(int userId, IClock clock)
|
||||
{
|
||||
if (!trackedData.TryGetValue(userId, out var data))
|
||||
if (!UserScores.TryGetValue(userId, out var data))
|
||||
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()
|
||||
{
|
||||
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)
|
||||
continue;
|
||||
public override void UpdateScore()
|
||||
{
|
||||
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)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
// 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Game.Configuration;
|
||||
@ -19,8 +19,7 @@ namespace osu.Game.Screens.Play.HUD
|
||||
[LongRunningLoad]
|
||||
public class MultiplayerGameplayLeaderboard : GameplayLeaderboard
|
||||
{
|
||||
private readonly ScoreProcessor scoreProcessor;
|
||||
private readonly Dictionary<int, TrackedUserData> userScores = new Dictionary<int, TrackedUserData>();
|
||||
protected readonly Dictionary<int, TrackedUserData> UserScores = new Dictionary<int, TrackedUserData>();
|
||||
|
||||
[Resolved]
|
||||
private SpectatorStreamingClient streamingClient { get; set; }
|
||||
@ -31,9 +30,9 @@ namespace osu.Game.Screens.Play.HUD
|
||||
[Resolved]
|
||||
private UserLookupCache userLookupCache { get; set; }
|
||||
|
||||
private Bindable<ScoringMode> scoringMode;
|
||||
|
||||
private readonly ScoreProcessor scoreProcessor;
|
||||
private readonly BindableList<int> playingUsers;
|
||||
private Bindable<ScoringMode> scoringMode;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new leaderboard.
|
||||
@ -52,6 +51,8 @@ namespace osu.Game.Screens.Play.HUD
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuConfigManager config, IAPIProvider api)
|
||||
{
|
||||
scoringMode = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode);
|
||||
|
||||
foreach (var userId in playingUsers)
|
||||
{
|
||||
streamingClient.WatchUser(userId);
|
||||
@ -59,19 +60,17 @@ namespace osu.Game.Screens.Play.HUD
|
||||
// probably won't be required in the final implementation.
|
||||
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);
|
||||
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);
|
||||
((IBindable<double>)leaderboardScore.TotalScore).BindTo(trackedUser.Score);
|
||||
((IBindable<int>)leaderboardScore.Combo).BindTo(trackedUser.CurrentCombo);
|
||||
((IBindable<bool>)leaderboardScore.HasQuit).BindTo(trackedUser.UserQuit);
|
||||
UserScores[userId] = trackedUser;
|
||||
}
|
||||
|
||||
scoringMode = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode);
|
||||
scoringMode.BindValueChanged(updateAllScores, true);
|
||||
}
|
||||
|
||||
protected override void LoadComplete()
|
||||
@ -101,7 +100,7 @@ namespace osu.Game.Screens.Play.HUD
|
||||
{
|
||||
streamingClient.StopWatchingUser(userId);
|
||||
|
||||
if (userScores.TryGetValue(userId, out var trackedData))
|
||||
if (UserScores.TryGetValue(userId, out var trackedData))
|
||||
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(() =>
|
||||
{
|
||||
if (userScores.ContainsKey(userId))
|
||||
OnIncomingFrames(userId, bundle);
|
||||
if (!UserScores.TryGetValue(userId, out var trackedData))
|
||||
return;
|
||||
|
||||
trackedData.Frames.Add(new TimedFrame(bundle.Frames.First().Time, bundle.Header));
|
||||
trackedData.UpdateScore();
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 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 virtual TrackedUserData CreateUserData(int userId, ScoreProcessor scoreProcessor) => new TrackedUserData(userId, scoreProcessor);
|
||||
|
||||
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;
|
||||
|
||||
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)
|
||||
public TrackedUserData(int userId, ScoreProcessor scoreProcessor)
|
||||
{
|
||||
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;
|
||||
|
||||
score.Value = processor.GetImmediateScore(mode, LastHeader.MaxCombo, LastHeader.Statistics);
|
||||
accuracy.Value = LastHeader.Accuracy;
|
||||
currentCombo.Value = LastHeader.Combo;
|
||||
SetFrame(Frames.Last());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user