2021-11-11 20:09:48 +08:00
|
|
|
|
// 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;
|
2023-11-16 09:00:35 +08:00
|
|
|
|
using System.Diagnostics;
|
2021-11-11 20:09:48 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Scoring
|
|
|
|
|
{
|
|
|
|
|
public static class HitEventExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2024-02-06 01:57:55 +08:00
|
|
|
|
/// Calculates the "unstable rate" for a sequence of <see cref="HitEvent"/>s.
|
2021-11-11 20:09:48 +08:00
|
|
|
|
/// </summary>
|
2024-02-06 01:57:55 +08:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Uses <a href="https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm">Welford's online algorithm</a>.
|
|
|
|
|
/// </remarks>
|
2021-11-11 20:09:48 +08:00
|
|
|
|
/// <returns>
|
|
|
|
|
/// A non-null <see langword="double"/> value if unstable rate could be calculated,
|
|
|
|
|
/// and <see langword="null"/> if unstable rate cannot be calculated due to <paramref name="hitEvents"/> being empty.
|
|
|
|
|
/// </returns>
|
|
|
|
|
public static double? CalculateUnstableRate(this IEnumerable<HitEvent> hitEvents)
|
|
|
|
|
{
|
2023-11-22 07:19:04 +08:00
|
|
|
|
Debug.Assert(hitEvents.All(ev => ev.GameplayRate != null));
|
2023-11-16 09:00:35 +08:00
|
|
|
|
|
2024-02-03 05:19:04 +08:00
|
|
|
|
int count = 0;
|
2024-02-06 01:56:20 +08:00
|
|
|
|
double mean = 0;
|
|
|
|
|
double sumOfSquares = 0;
|
2024-02-03 03:28:47 +08:00
|
|
|
|
|
|
|
|
|
foreach (var e in hitEvents)
|
|
|
|
|
{
|
|
|
|
|
if (!affectsUnstableRate(e))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-02-03 05:19:04 +08:00
|
|
|
|
count++;
|
2024-02-03 03:28:47 +08:00
|
|
|
|
|
2024-02-03 05:19:04 +08:00
|
|
|
|
// Division by gameplay rate is to account for TimeOffset scaling with gameplay rate.
|
|
|
|
|
double currentValue = e.TimeOffset / e.GameplayRate!.Value;
|
2024-02-06 01:56:20 +08:00
|
|
|
|
double nextMean = mean + (currentValue - mean) / count;
|
|
|
|
|
sumOfSquares += (currentValue - mean) * (currentValue - nextMean);
|
|
|
|
|
mean = nextMean;
|
2024-02-03 03:28:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-03 05:19:04 +08:00
|
|
|
|
if (count == 0)
|
2024-02-03 03:28:47 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2024-02-06 01:56:20 +08:00
|
|
|
|
return 10.0 * Math.Sqrt(sumOfSquares / count);
|
2021-11-11 20:09:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 18:14:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates the average hit offset/error for a sequence of <see cref="HitEvent"/>s, where negative numbers mean the user hit too early on average.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// A non-null <see langword="double"/> value if unstable rate could be calculated,
|
|
|
|
|
/// and <see langword="null"/> if unstable rate cannot be calculated due to <paramref name="hitEvents"/> being empty.
|
|
|
|
|
/// </returns>
|
2022-03-01 15:02:48 +08:00
|
|
|
|
public static double? CalculateAverageHitError(this IEnumerable<HitEvent> hitEvents)
|
|
|
|
|
{
|
|
|
|
|
double[] timeOffsets = hitEvents.Where(affectsUnstableRate).Select(ev => ev.TimeOffset).ToArray();
|
|
|
|
|
|
|
|
|
|
if (timeOffsets.Length == 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return timeOffsets.Average();
|
|
|
|
|
}
|
2022-02-28 18:11:06 +08:00
|
|
|
|
|
2021-11-11 20:09:48 +08:00
|
|
|
|
private static bool affectsUnstableRate(HitEvent e) => !(e.HitObject.HitWindows is HitWindows.EmptyHitWindows) && e.Result.IsHit();
|
|
|
|
|
}
|
|
|
|
|
}
|