osu/osu.Game/Screens/Ranking/Expanded/Statistics/PerformanceStatistic.cs

57 lines
1.8 KiB
C#
Raw Normal View History

// 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.
2020-10-10 17:58:06 +00:00
using System;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Scoring;
namespace osu.Game.Screens.Ranking.Expanded.Statistics
{
public class PerformanceStatistic : CounterStatistic
{
private readonly ScoreInfo score;
private readonly Bindable<int> performance = new Bindable<int>();
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
public PerformanceStatistic(ScoreInfo score)
: base("PP", 0)
{
this.score = score;
}
[BackgroundDependencyLoader]
private void load(ScorePerformanceManager performanceManager)
{
2020-09-28 17:05:22 +00:00
if (score.PP.HasValue)
{
2020-10-10 17:58:06 +00:00
performance.Value = (int)Math.Round(score.PP.Value, MidpointRounding.AwayFromZero);
2020-09-28 17:05:22 +00:00
}
else
{
performanceManager.CalculatePerformanceAsync(score, cancellationTokenSource.Token)
.ContinueWith(t => Schedule(() =>
{
if (t.Result.HasValue)
2020-10-10 17:58:06 +00:00
performance.Value = (int)Math.Round(t.Result.Value, MidpointRounding.AwayFromZero);
}), cancellationTokenSource.Token);
2020-09-28 17:05:22 +00:00
}
}
public override void Appear()
{
base.Appear();
Counter.Current.BindTo(performance);
}
protected override void Dispose(bool isDisposing)
{
2020-09-28 17:05:22 +00:00
cancellationTokenSource?.Cancel();
base.Dispose(isDisposing);
}
}
}