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

Add basic "time ago" display to leaderboards on beatmap overlay #16355

Merged
merged 3 commits into from
Jan 8, 2022
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
57 changes: 57 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneScoreboardTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. 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;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Timing;
using osu.Game.Overlays.BeatmapSet.Scores;

namespace osu.Game.Tests.Visual.Online
{
public class TestSceneScoreboardTime : OsuTestScene
{
private StopwatchClock stopwatch;

[Test]
public void TestVariousUnits()
{
AddStep("create various scoreboard times", () => Child = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Clock = new FramedClock(stopwatch = new StopwatchClock()), // prevent time from naturally elapsing.
Direction = FillDirection.Vertical,
ChildrenEnumerable = testCases.Select(dateTime => new ScoreboardTime(dateTime, 24).With(time => time.Anchor = time.Origin = Anchor.TopCentre))
});

AddStep("start stopwatch", () => stopwatch.Start());
}

private static IEnumerable<DateTimeOffset> testCases => new[]
{
DateTimeOffset.Now,
DateTimeOffset.Now.AddSeconds(-1),
DateTimeOffset.Now.AddSeconds(-25),
DateTimeOffset.Now.AddSeconds(-59),
DateTimeOffset.Now.AddMinutes(-1),
DateTimeOffset.Now.AddMinutes(-25),
DateTimeOffset.Now.AddMinutes(-59),
DateTimeOffset.Now.AddHours(-1),
DateTimeOffset.Now.AddHours(-13),
DateTimeOffset.Now.AddHours(-23),
DateTimeOffset.Now.AddDays(-1),
DateTimeOffset.Now.AddDays(-6),
DateTimeOffset.Now.AddDays(-16),
DateTimeOffset.Now.AddMonths(-1),
DateTimeOffset.Now.AddMonths(-11),
DateTimeOffset.Now.AddYears(-1),
DateTimeOffset.Now.AddYears(-5)
};
}
}
6 changes: 6 additions & 0 deletions osu.Game/Overlays/BeatmapSet/Scores/ScoreTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private TableColumn[] createHeaders(IReadOnlyList<ScoreInfo> scores)
if (showPerformancePoints)
columns.Add(new TableColumn(BeatmapsetsStrings.ShowScoreboardHeaderspp, Anchor.CentreLeft, new Dimension(GridSizeMode.Absolute, 30)));

columns.Add(new TableColumn(BeatmapsetsStrings.ShowScoreboardHeadersTime, Anchor.CentreLeft, new Dimension(GridSizeMode.AutoSize)));
columns.Add(new TableColumn(BeatmapsetsStrings.ShowScoreboardHeadersMods, Anchor.CentreLeft, new Dimension(GridSizeMode.AutoSize)));

return columns.ToArray();
Expand Down Expand Up @@ -202,6 +203,11 @@ private Drawable[] createContent(int index, ScoreInfo score)
});
}

content.Add(new ScoreboardTime(score.Date, text_size)
{
Margin = new MarginPadding { Right = 10 }
});

content.Add(new FillFlowContainer
{
Direction = FillDirection.Horizontal,
Expand Down
56 changes: 56 additions & 0 deletions osu.Game/Overlays/BeatmapSet/Scores/ScoreboardTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using Humanizer;
using osu.Game.Graphics;
using osu.Game.Resources.Localisation.Web;

namespace osu.Game.Overlays.BeatmapSet.Scores
{
public class ScoreboardTime : DrawableDate
{
public ScoreboardTime(DateTimeOffset date, float textSize = OsuFont.DEFAULT_FONT_SIZE, bool italic = true)
: base(date, textSize, italic)
{
}

protected override string Format()
{
var now = DateTime.Now;
var difference = now - Date;

// web uses momentjs's custom locales to format the date for the purposes of the scoreboard.
// this is intended to be a best-effort, more legible approximation of that.
// compare:
// * https://github.com/ppy/osu-web/blob/a8f5a68fb435cb19a4faa4c7c4bce08c4f096933/resources/assets/lib/scoreboard-time.tsx
// * https://momentjs.com/docs/#/customization/ (reference for the customisation format)

// TODO: support localisation (probably via `CommonStrings.CountHours()` etc.)
// requires pluralisable string support framework-side

if (difference.TotalHours < 1)
return CommonStrings.TimeNow.ToString();
if (difference.TotalDays < 1)
return "hr".ToQuantity((int)difference.TotalHours);

// this is where this gets more complicated because of how the calendar works.
// since there's no `TotalMonths` / `TotalYears`, we have to iteratively add months/years
// and test against cutoff dates to determine how many months/years to show.

if (Date > now.AddMonths(-1))
return difference.TotalDays < 2 ? "1dy" : $"{(int)difference.TotalDays}dys";

for (int months = 1; months <= 11; ++months)
{
if (Date > now.AddMonths(-(months + 1)))
return months == 1 ? "1mo" : $"{months}mos";
}

int years = 1;
while (Date <= now.AddYears(-(years + 1)))
years += 1;
return years == 1 ? "1yr" : $"{years}yrs";
}
}
}