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

Favour updating daily challenge statistics when they come on screen #29187

Merged
merged 4 commits into from
Jul 29, 2024
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 @@ -6,6 +6,7 @@
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Rooms;
Expand All @@ -20,11 +21,11 @@ public partial class TestSceneDailyChallengeScoreBreakdown : OsuTestScene
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Plum);

[Test]
public void TestBasicAppearance()
{
DailyChallengeScoreBreakdown breakdown = null!;
private DailyChallengeScoreBreakdown breakdown = null!;

[SetUpSteps]
public void SetUpSteps()
{
AddStep("create content", () => Children = new Drawable[]
{
new Box
Expand All @@ -50,7 +51,14 @@ public void TestBasicAppearance()
breakdown.Height = height;
});

AddToggleStep("toggle visible", v => breakdown.Alpha = v ? 1 : 0);

AddStep("set initial data", () => breakdown.SetInitialCounts([1, 4, 9, 16, 25, 36, 49, 36, 25, 16, 9, 4, 1]));
}

[Test]
public void TestBasicAppearance()
{
AddStep("add new score", () =>
{
var ev = new NewScoreEvent(1, new APIUser
Expand All @@ -65,5 +73,24 @@ public void TestBasicAppearance()
AddStep("set user score", () => breakdown.UserBestScore.Value = new MultiplayerScore { TotalScore = RNG.Next(1_000_000) });
AddStep("unset user score", () => breakdown.UserBestScore.Value = null);
}

[Test]
public void TestMassAdd()
{
AddStep("add 1000 scores at once", () =>
{
for (int i = 0; i < 1000; i++)
{
var ev = new NewScoreEvent(1, new APIUser
{
Id = 2,
Username = "peppy",
CoverUrl = "https://osu.ppy.sh/images/headers/profile-covers/c3.jpg",
}, RNG.Next(1_000_000), null);

breakdown.AddNewScore(ev);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public void TestBasicAppearance()
if (ring.IsNotNull())
ring.Height = height;
});
AddToggleStep("toggle visible", v => ring.Alpha = v ? 1 : 0);

AddStep("just started", () =>
{
room.Value.StartDate.Value = DateTimeOffset.Now.AddMinutes(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void TestBasicAppearance()
if (totals.IsNotNull())
totals.Height = height;
});
AddToggleStep("toggle visible", v => totals.Alpha = v ? 1 : 0);

AddStep("set counts", () => totals.SetInitialCounts(totalPassCount: 9650, cumulativeTotalScore: 10_000_000_000));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public override void Add(Drawable drawable)
{
drawable.RelativeSizeAxes = Axes.Both;
drawable.Size = Vector2.One;
drawable.AlwaysPresent = true;
drawable.Alpha = 0;

base.Add(drawable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
Expand All @@ -27,9 +28,6 @@ public partial class DailyChallengeScoreBreakdown : CompositeDrawable

private FillFlowContainer<Bar> barsContainer = null!;

// we're always present so that we can update while hidden, but we don't want tooltips to be displayed, therefore directly use alpha comparison here.
public override bool PropagatePositionalInputSubTree => base.PropagatePositionalInputSubTree && Alpha > 0;

private const int bin_count = MultiplayerPlaylistItemStats.TOTAL_SCORE_DISTRIBUTION_BINS;
private long[] bins = new long[bin_count];

Expand Down Expand Up @@ -70,34 +68,61 @@ protected override void LoadComplete()
});
}

private readonly Queue<NewScoreEvent> newScores = new Queue<NewScoreEvent>();

public void AddNewScore(NewScoreEvent newScoreEvent)
{
int targetBin = (int)Math.Clamp(Math.Floor((float)newScoreEvent.TotalScore / 100000), 0, bin_count - 1);
bins[targetBin] += 1;
updateCounts();
newScores.Enqueue(newScoreEvent);

var text = new OsuSpriteText
// ensure things don't get too out-of-hand.
if (newScores.Count > 25)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think this can likely be increased, but keeping it in sync with event feed for now.

{
Text = newScoreEvent.TotalScore.ToString(@"N0"),
Anchor = Anchor.TopCentre,
Origin = Anchor.BottomCentre,
Font = OsuFont.Default.With(size: 30),
RelativePositionAxes = Axes.X,
X = (targetBin + 0.5f) / bin_count - 0.5f,
Alpha = 0,
};
AddInternal(text);
bins[getTargetBin(newScores.Dequeue())] += 1;
Scheduler.AddOnce(updateCounts);
}
}

private double lastScoreDisplay;

protected override void Update()
{
base.Update();

Scheduler.AddDelayed(() =>
if (Time.Current - lastScoreDisplay > 150 && newScores.TryDequeue(out var newScore))
{
float startY = ToLocalSpace(barsContainer[targetBin].CircularBar.ScreenSpaceDrawQuad.TopLeft).Y;
text.FadeInFromZero()
.ScaleTo(new Vector2(0.8f), 500, Easing.OutElasticHalf)
.MoveToY(startY)
.MoveToOffset(new Vector2(0, -50), 2500, Easing.OutQuint)
.FadeOut(2500, Easing.OutQuint)
.Expire();
}, 150);
if (lastScoreDisplay < Time.Current)
lastScoreDisplay = Time.Current;

int targetBin = getTargetBin(newScore);
bins[targetBin] += 1;

updateCounts();

var text = new OsuSpriteText
{
Text = newScore.TotalScore.ToString(@"N0"),
Anchor = Anchor.TopCentre,
Origin = Anchor.BottomCentre,
Font = OsuFont.Default.With(size: 30),
RelativePositionAxes = Axes.X,
X = (targetBin + 0.5f) / bin_count - 0.5f,
Alpha = 0,
};
AddInternal(text);

Scheduler.AddDelayed(() =>
{
float startY = ToLocalSpace(barsContainer[targetBin].CircularBar.ScreenSpaceDrawQuad.TopLeft).Y;
text.FadeInFromZero()
.ScaleTo(new Vector2(0.8f), 500, Easing.OutElasticHalf)
.MoveToY(startY)
.MoveToOffset(new Vector2(0, -50), 2500, Easing.OutQuint)
.FadeOut(2500, Easing.OutQuint)
.Expire();
}, 150);

lastScoreDisplay = Time.Current;
}
}

public void SetInitialCounts(long[] counts)
Expand All @@ -109,6 +134,9 @@ public void SetInitialCounts(long[] counts)
updateCounts();
}

private static int getTargetBin(NewScoreEvent score) =>
(int)Math.Clamp(Math.Floor((float)score.TotalScore / 100000), 0, bin_count - 1);

private void updateCounts()
{
long max = Math.Max(bins.Max(), 1);
Expand Down
Loading