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;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Scoring
|
|
|
|
{
|
|
|
|
public static class HitEventExtensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Calculates the "unstable rate" for a sequence of <see cref="HitEvent"/>s.
|
|
|
|
/// </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>
|
|
|
|
public static double? CalculateUnstableRate(this IEnumerable<HitEvent> hitEvents)
|
|
|
|
{
|
|
|
|
double[] timeOffsets = hitEvents.Where(affectsUnstableRate).Select(ev => ev.TimeOffset).ToArray();
|
|
|
|
return 10 * standardDeviation(timeOffsets);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
private static double? standardDeviation(double[] timeOffsets)
|
|
|
|
{
|
|
|
|
if (timeOffsets.Length == 0)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
double mean = timeOffsets.Average();
|
|
|
|
double squares = timeOffsets.Select(offset => Math.Pow(offset - mean, 2)).Sum();
|
|
|
|
return Math.Sqrt(squares / timeOffsets.Length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|