Fix HUD unstable rate counter not including judgements before load complete

Also unifies UR calculation logic with the results screen statistic item
to reduce duplication.
This commit is contained in:
Bartłomiej Dach
2021-11-11 13:09:48 +01:00
parent 40cffd1682
commit 69809390d3
4 changed files with 52 additions and 54 deletions

View File

@ -1,9 +1,7 @@
// 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.Linq;
using osu.Game.Rulesets.Scoring;
namespace osu.Game.Screens.Ranking.Statistics
@ -11,7 +9,7 @@ namespace osu.Game.Screens.Ranking.Statistics
/// <summary>
/// Displays the unstable rate statistic for a given play.
/// </summary>
public class UnstableRate : SimpleStatisticItem<double>
public class UnstableRate : SimpleStatisticItem<double?>
{
/// <summary>
/// Creates and computes an <see cref="UnstableRate"/> statistic.
@ -20,21 +18,9 @@ namespace osu.Game.Screens.Ranking.Statistics
public UnstableRate(IEnumerable<HitEvent> hitEvents)
: base("Unstable Rate")
{
double[] timeOffsets = hitEvents.Where(e => !(e.HitObject.HitWindows is HitWindows.EmptyHitWindows) && e.Result.IsHit())
.Select(ev => ev.TimeOffset).ToArray();
Value = 10 * standardDeviation(timeOffsets);
Value = hitEvents.CalculateUnstableRate();
}
private static double standardDeviation(double[] timeOffsets)
{
if (timeOffsets.Length == 0)
return double.NaN;
double mean = timeOffsets.Average();
double squares = timeOffsets.Select(offset => Math.Pow(offset - mean, 2)).Sum();
return Math.Sqrt(squares / timeOffsets.Length);
}
protected override string DisplayValue(double value) => double.IsNaN(value) ? "(not available)" : value.ToString("N2");
protected override string DisplayValue(double? value) => value == null ? "(not available)" : value.Value.ToString(@"N2");
}
}