Skip to content

Commit

Permalink
Merge pull request #29187 from peppy/daily-challenge-pieces-not-alway…
Browse files Browse the repository at this point in the history
…s-present

Favour updating daily challenge statistics when they come on screen
  • Loading branch information
bdach authored Jul 29, 2024
2 parents 3a30ff7 + 7afcd72 commit 5032a14
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 30 deletions.
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)
{
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

0 comments on commit 5032a14

Please sign in to comment.