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 ability to view kudosu rankings #25388

Merged
merged 2 commits into from
Nov 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions osu.Game/Online/API/Requests/GetKudosuRankingsRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 osu.Framework.IO.Network;

namespace osu.Game.Online.API.Requests
{
public class GetKudosuRankingsRequest : APIRequest<GetKudosuRankingsResponse>
{
private readonly int page;

public GetKudosuRankingsRequest(int page = 1)
{
this.page = page;
}

protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();

req.AddParameter(@"page", page.ToString());

return req;
}

protected override string Target => @"rankings/kudosu";
}
}
15 changes: 15 additions & 0 deletions osu.Game/Online/API/Requests/GetKudosuRankingsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.Collections.Generic;
using Newtonsoft.Json;
using osu.Game.Online.API.Requests.Responses;

namespace osu.Game.Online.API.Requests
{
public class GetKudosuRankingsResponse
{
[JsonProperty("ranking")]
public List<APIUser> Users = null!;
}
}
13 changes: 4 additions & 9 deletions osu.Game/Online/API/Requests/Responses/APIUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,15 @@ public class APIUser : IEquatable<APIUser>, IUser
[JsonProperty(@"previous_usernames")]
public string[] PreviousUsernames;

private CountryCode? countryCode;
[JsonProperty(@"country_code")]
private string countryCodeString;

public CountryCode CountryCode
{
get => countryCode ??= (Enum.TryParse(country?.Code, out CountryCode result) ? result : default);
set => countryCode = value;
get => Enum.TryParse(countryCodeString, out CountryCode result) ? result : CountryCode.Unknown;
set => countryCodeString = value.ToString();
}

#pragma warning disable 649
[CanBeNull]
[JsonProperty(@"country")]
private Country country;
#pragma warning restore 649

public readonly Bindable<UserStatus> Status = new Bindable<UserStatus>();

public readonly Bindable<UserActivity> Activity = new Bindable<UserActivity>();
Expand Down
95 changes: 95 additions & 0 deletions osu.Game/Overlays/KudosuTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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.Collections.Generic;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Rankings.Tables;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Users;

namespace osu.Game.Overlays
{
public partial class KudosuTable : RankingsTable<APIUser>
{
public KudosuTable(int page, List<APIUser> users)
: base(page, users)
{
}

protected override Drawable CreateRowBackground(APIUser item)
{
var background = base.CreateRowBackground(item);

// see: https://github.com/ppy/osu-web/blob/9de00a0b874c56893d98261d558d78d76259d81b/resources/views/multiplayer/rooms/_rankings_table.blade.php#L23
if (!item.Active)
background.Alpha = 0.5f;

return background;
}

protected override Drawable[] CreateRowContent(int index, APIUser item)
{
var content = base.CreateRowContent(index, item);

// see: https://github.com/ppy/osu-web/blob/9de00a0b874c56893d98261d558d78d76259d81b/resources/views/multiplayer/rooms/_rankings_table.blade.php#L23
if (!item.Active)
{
foreach (var d in content)
d.Alpha = 0.5f;
}

return content;
}

protected override RankingsTableColumn[] CreateAdditionalHeaders()
{
const int min_width = 120;
return new[]
{
new RankingsTableColumn(RankingsStrings.KudosuTotal, Anchor.Centre, new Dimension(GridSizeMode.AutoSize, minSize: min_width), true),
new RankingsTableColumn(RankingsStrings.KudosuAvailable, Anchor.Centre, new Dimension(GridSizeMode.AutoSize, minSize: min_width)),
new RankingsTableColumn(RankingsStrings.KudosuUsed, Anchor.Centre, new Dimension(GridSizeMode.AutoSize, minSize: min_width)),
};
}

protected override Drawable[] CreateAdditionalContent(APIUser item)
{
int kudosuTotal = item.Kudosu.Total;
int kudosuAvailable = item.Kudosu.Available;
return new Drawable[]
{
new RowText
{
Text = kudosuTotal.ToLocalisableString(@"N0")
},
new ColouredRowText
{
Text = kudosuAvailable.ToLocalisableString(@"N0")
},
new ColouredRowText
{
Text = (kudosuTotal - kudosuAvailable).ToLocalisableString(@"N0")
},
};
}

protected override CountryCode GetCountryCode(APIUser item) => item.CountryCode;

protected override Drawable CreateFlagContent(APIUser item)
{
var username = new LinkFlowContainer(t => t.Font = OsuFont.GetFont(size: TEXT_SIZE, italics: true))
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
TextAnchor = Anchor.CentreLeft
};
username.AddUserLink(item);
return username;
}
}
}
5 changes: 4 additions & 1 deletion osu.Game/Overlays/Rankings/RankingsScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public enum RankingsScope
Score,

[LocalisableDescription(typeof(RankingsStrings), nameof(RankingsStrings.TypeCountry))]
Country
Country,

[LocalisableDescription(typeof(RankingsStrings), nameof(RankingsStrings.TypeKudosu))]
Kudosu,
}
}
9 changes: 9 additions & 0 deletions osu.Game/Overlays/RankingsOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ private APIRequest createScopedRequest()

case RankingsScope.Score:
return new GetUserRankingsRequest(ruleset.Value, UserRankingsType.Score);

case RankingsScope.Kudosu:
return new GetKudosuRankingsRequest();
}

return null;
Expand Down Expand Up @@ -166,6 +169,12 @@ private Drawable createTableFromResponse(APIRequest request)

return new CountriesTable(1, countryRequest.Response.Countries);
}

case GetKudosuRankingsRequest kudosuRequest:
if (kudosuRequest.Response == null)
return null;

return new KudosuTable(1, kudosuRequest.Response.Users);
}

return null;
Expand Down