Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store maximum statistics to spectator state #21587

Merged
merged 5 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ public virtual void SetUpSteps()
BeatmapID = 0,
RulesetID = 0,
Mods = user.Mods,
MaximumScoringValues = new ScoringValues
MaximumStatistics = new Dictionary<HitResult, int>
{
BaseScore = 10000,
MaxCombo = 1000,
CountBasicHitObjects = 1000
{ HitResult.Perfect, 100 }
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Online/Spectator/SpectatorClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void BeginPlaying(GameplayState state, Score score)
currentState.RulesetID = score.ScoreInfo.RulesetID;
currentState.Mods = score.ScoreInfo.Mods.Select(m => new APIMod(m)).ToArray();
currentState.State = SpectatedUserState.Playing;
currentState.MaximumScoringValues = state.ScoreProcessor.MaximumScoringValues;
currentState.MaximumStatistics = state.ScoreProcessor.MaximumStatistics;

currentBeatmap = state.Beatmap;
currentScore = score;
Expand Down
4 changes: 2 additions & 2 deletions osu.Game/Online/Spectator/SpectatorScoreProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ public void UpdateScore()

scoreInfo.MaxCombo = frame.Header.MaxCombo;
scoreInfo.Statistics = frame.Header.Statistics;
scoreInfo.MaximumStatistics = spectatorState.MaximumStatistics;

Accuracy.Value = frame.Header.Accuracy;
Combo.Value = frame.Header.Combo;

scoreProcessor.ExtractScoringValues(frame.Header, out var currentScoringValues, out _);
TotalScore.Value = scoreProcessor.ComputeScore(Mode.Value, currentScoringValues, spectatorState.MaximumScoringValues);
TotalScore.Value = scoreProcessor.ComputeScore(Mode.Value, scoreInfo);
}

protected override void Dispose(bool isDisposing)
Expand Down
4 changes: 2 additions & 2 deletions osu.Game/Online/Spectator/SpectatorState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Linq;
using MessagePack;
using osu.Game.Online.API;
using osu.Game.Scoring;
using osu.Game.Rulesets.Scoring;

namespace osu.Game.Online.Spectator
{
Expand All @@ -31,7 +31,7 @@ public class SpectatorState : IEquatable<SpectatorState>
public SpectatedUserState State { get; set; }

[Key(4)]
public ScoringValues MaximumScoringValues { get; set; }
public Dictionary<HitResult, int> MaximumStatistics { get; set; } = new Dictionary<HitResult, int>();

public bool Equals(SpectatorState other)
{
Expand Down
73 changes: 35 additions & 38 deletions osu.Game/Rulesets/Scoring/ScoreProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
Expand Down Expand Up @@ -90,17 +89,14 @@ public partial class ScoreProcessor : JudgementProcessor
private readonly double accuracyPortion;
private readonly double comboPortion;

/// <summary>
/// Scoring values for a perfect play.
/// </summary>
public ScoringValues MaximumScoringValues
public Dictionary<HitResult, int> MaximumStatistics
{
get
{
if (!beatmapApplied)
throw new InvalidOperationException($"Cannot access maximum scoring values before calling {nameof(ApplyBeatmap)}.");
throw new InvalidOperationException($"Cannot access maximum statistics before calling {nameof(ApplyBeatmap)}.");

return maximumScoringValues;
return new Dictionary<HitResult, int>(maximumResultCounts);
}
}

Expand Down Expand Up @@ -268,7 +264,7 @@ private static void revertResult(HitResult result, ref ScoringValues scoringValu
private void updateScore()
{
Accuracy.Value = currentMaximumScoringValues.BaseScore > 0 ? (double)currentScoringValues.BaseScore / currentMaximumScoringValues.BaseScore : 1;
TotalScore.Value = ComputeScore(Mode.Value, currentScoringValues, maximumScoringValues);
TotalScore.Value = computeScore(Mode.Value, currentScoringValues, maximumScoringValues);
}

/// <summary>
Expand Down Expand Up @@ -303,9 +299,9 @@ public long ComputeScore(ScoringMode mode, ScoreInfo scoreInfo)
if (!ruleset.RulesetInfo.Equals(scoreInfo.Ruleset))
throw new ArgumentException($"Unexpected score ruleset. Expected \"{ruleset.RulesetInfo.ShortName}\" but was \"{scoreInfo.Ruleset.ShortName}\".");

ExtractScoringValues(scoreInfo, out var current, out var maximum);
extractScoringValues(scoreInfo, out var current, out var maximum);

return ComputeScore(mode, current, maximum);
return computeScore(mode, current, maximum);
}

/// <summary>
Expand All @@ -316,7 +312,7 @@ public long ComputeScore(ScoringMode mode, ScoreInfo scoreInfo)
/// <param name="maximum">The maximum scoring values.</param>
/// <returns>The total score computed from the given scoring values.</returns>
[Pure]
public long ComputeScore(ScoringMode mode, ScoringValues current, ScoringValues maximum)
private long computeScore(ScoringMode mode, ScoringValues current, ScoringValues maximum)
{
double accuracyRatio = maximum.BaseScore > 0 ? (double)current.BaseScore / maximum.BaseScore : 1;
double comboRatio = maximum.MaxCombo > 0 ? (double)current.MaxCombo / maximum.MaxCombo : 1;
Expand Down Expand Up @@ -474,14 +470,14 @@ public override void ResetFromReplayFrame(ReplayFrame frame)
/// Consumers are expected to more accurately fill in the above values through external means.
/// <para>
/// <b>Ensure</b> to fill in the maximum <see cref="ScoringValues.CountBasicHitObjects"/> for use in
/// <see cref="ComputeScore(osu.Game.Rulesets.Scoring.ScoringMode,osu.Game.Scoring.ScoringValues,osu.Game.Scoring.ScoringValues)"/>.
/// <see cref="computeScore(osu.Game.Rulesets.Scoring.ScoringMode,ScoringValues,ScoringValues)"/>.
/// </para>
/// </remarks>
/// <param name="scoreInfo">The score to extract scoring values from.</param>
/// <param name="current">The "current" scoring values, representing the hit statistics as they appear.</param>
/// <param name="maximum">The "maximum" scoring values, representing the hit statistics as if the maximum hit result was attained each time.</param>
[Pure]
internal void ExtractScoringValues(ScoreInfo scoreInfo, out ScoringValues current, out ScoringValues maximum)
private void extractScoringValues(ScoreInfo scoreInfo, out ScoringValues current, out ScoringValues maximum)
{
extractScoringValues(scoreInfo.Statistics, out current, out maximum);
current.MaxCombo = scoreInfo.MaxCombo;
Expand All @@ -490,31 +486,6 @@ internal void ExtractScoringValues(ScoreInfo scoreInfo, out ScoringValues curren
extractScoringValues(scoreInfo.MaximumStatistics, out _, out maximum);
}

/// <summary>
/// Applies a best-effort extraction of hit statistics into <see cref="ScoringValues"/>.
/// </summary>
/// <remarks>
/// This method is useful in a variety of situations, with a few drawbacks that need to be considered:
/// <list type="bullet">
/// <item>The maximum <see cref="ScoringValues.BonusScore"/> will always be 0.</item>
/// <item>The current and maximum <see cref="ScoringValues.CountBasicHitObjects"/> will always be the same value.</item>
/// </list>
/// Consumers are expected to more accurately fill in the above values through external means.
/// <para>
/// <b>Ensure</b> to fill in the maximum <see cref="ScoringValues.CountBasicHitObjects"/> for use in
/// <see cref="ComputeScore(osu.Game.Rulesets.Scoring.ScoringMode,osu.Game.Scoring.ScoringValues,osu.Game.Scoring.ScoringValues)"/>.
/// </para>
/// </remarks>
/// <param name="header">The replay frame header to extract scoring values from.</param>
/// <param name="current">The "current" scoring values, representing the hit statistics as they appear.</param>
/// <param name="maximum">The "maximum" scoring values, representing the hit statistics as if the maximum hit result was attained each time.</param>
[Pure]
internal void ExtractScoringValues(FrameHeader header, out ScoringValues current, out ScoringValues maximum)
{
extractScoringValues(header.Statistics, out current, out maximum);
current.MaxCombo = header.MaxCombo;
}

/// <summary>
/// Applies a best-effort extraction of hit statistics into <see cref="ScoringValues"/>.
/// </summary>
Expand Down Expand Up @@ -589,6 +560,32 @@ protected override void Dispose(bool isDisposing)
base.Dispose(isDisposing);
hitEvents.Clear();
}

/// <summary>
/// Stores the required scoring data that fulfils the minimum requirements for a <see cref="ScoreProcessor"/> to calculate score.
/// </summary>
private struct ScoringValues
{
/// <summary>
/// The sum of all "basic" <see cref="HitObject"/> scoring values. See: <see cref="HitResultExtensions.IsBasic"/> and <see cref="Judgement.ToNumericResult"/>.
/// </summary>
public long BaseScore;

/// <summary>
/// The sum of all "bonus" <see cref="HitObject"/> scoring values. See: <see cref="HitResultExtensions.IsBonus"/> and <see cref="Judgement.ToNumericResult"/>.
/// </summary>
public long BonusScore;

/// <summary>
/// The highest achieved combo.
/// </summary>
public int MaxCombo;

/// <summary>
/// The count of "basic" <see cref="HitObject"/>s. See: <see cref="HitResultExtensions.IsBasic"/>.
/// </summary>
public int CountBasicHitObjects;
}
}

public enum ScoringMode
Expand Down
43 changes: 0 additions & 43 deletions osu.Game/Scoring/ScoringValues.cs

This file was deleted.