osu/osu.Game/Overlays/RankingsOverlay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

181 lines
5.2 KiB
C#
Raw Normal View History

2019-11-30 00:01:07 +00:00
// 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.
2022-06-17 07:37:17 +00:00
#nullable disable
2019-11-30 00:01:07 +00:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Overlays.Rankings;
using osu.Game.Users;
using osu.Game.Rulesets;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays.Rankings.Tables;
namespace osu.Game.Overlays
{
public partial class RankingsOverlay : TabbableOnlineOverlay<RankingsOverlayHeader, RankingsScope>
2019-11-30 00:01:07 +00:00
{
2022-07-18 05:40:34 +00:00
protected Bindable<CountryCode> Country => Header.Country;
2020-02-18 18:26:25 +00:00
2019-12-05 05:26:36 +00:00
private APIRequest lastRequest;
2019-11-30 00:01:07 +00:00
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private IBindable<RulesetInfo> parentRuleset { get; set; }
[Cached]
private readonly Bindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
2019-11-30 00:01:07 +00:00
public RankingsOverlay()
2021-01-18 07:48:12 +00:00
: base(OverlayColourScheme.Green)
2019-11-30 00:01:07 +00:00
{
}
protected override void LoadComplete()
{
2020-02-18 18:26:25 +00:00
base.LoadComplete();
Header.Ruleset.BindTo(ruleset);
Country.BindValueChanged(_ =>
{
// if a country is requested, force performance scope.
if (!Country.IsDefault)
2021-02-09 09:42:15 +00:00
Header.Current.Value = RankingsScope.Performance;
2021-02-09 09:42:15 +00:00
Scheduler.AddOnce(triggerTabChanged);
2020-02-18 18:26:25 +00:00
});
ruleset.BindValueChanged(_ =>
{
2021-02-09 09:42:15 +00:00
if (Header.Current.Value == RankingsScope.Spotlights)
return;
2021-02-09 09:42:15 +00:00
Scheduler.AddOnce(triggerTabChanged);
2020-02-18 18:26:25 +00:00
});
2019-11-30 00:01:07 +00:00
}
private bool requiresRulesetUpdate = true;
protected override void PopIn()
{
if (requiresRulesetUpdate)
{
ruleset.Value = parentRuleset.Value;
requiresRulesetUpdate = false;
}
base.PopIn();
}
2021-02-09 09:42:15 +00:00
protected override void OnTabChanged(RankingsScope tab)
{
// country filtering is only valid for performance scope.
if (Header.Current.Value != RankingsScope.Performance)
Country.SetDefault();
2021-02-09 09:42:15 +00:00
Scheduler.AddOnce(triggerTabChanged);
}
private void triggerTabChanged() => base.OnTabChanged(Header.Current.Value);
2021-01-18 07:48:12 +00:00
protected override RankingsOverlayHeader CreateHeader() => new RankingsOverlayHeader();
2022-07-18 05:40:34 +00:00
public void ShowCountry(CountryCode requested)
2019-11-30 00:01:07 +00:00
{
if (requested == default)
2019-11-30 00:01:07 +00:00
return;
Show();
Country.Value = requested;
2019-11-30 00:01:07 +00:00
}
protected override void CreateDisplayToLoad(RankingsScope tab)
2019-11-30 00:01:07 +00:00
{
2019-12-05 05:26:36 +00:00
lastRequest?.Cancel();
2019-11-30 00:01:07 +00:00
2021-02-09 09:42:15 +00:00
if (Header.Current.Value == RankingsScope.Spotlights)
{
LoadDisplay(new SpotlightsLayout
{
Ruleset = { BindTarget = ruleset }
});
return;
}
2019-12-05 05:26:36 +00:00
var request = createScopedRequest();
lastRequest = request;
if (request == null)
2019-11-30 00:01:07 +00:00
{
LoadDisplay(Empty());
2019-12-05 05:26:36 +00:00
return;
}
request.Success += () => Schedule(() => LoadDisplay(createTableFromResponse(request)));
request.Failure += _ => Schedule(() => LoadDisplay(Empty()));
2019-12-05 05:26:36 +00:00
api.Queue(request);
}
private APIRequest createScopedRequest()
{
2021-02-09 09:42:15 +00:00
switch (Header.Current.Value)
2019-12-05 05:26:36 +00:00
{
2019-11-30 00:01:07 +00:00
case RankingsScope.Performance:
2022-07-18 05:40:34 +00:00
return new GetUserRankingsRequest(ruleset.Value, countryCode: Country.Value);
2019-11-30 00:01:07 +00:00
case RankingsScope.Country:
return new GetCountryRankingsRequest(ruleset.Value);
2019-11-30 00:01:07 +00:00
case RankingsScope.Score:
return new GetUserRankingsRequest(ruleset.Value, UserRankingsType.Score);
2019-11-30 00:01:07 +00:00
}
2019-12-05 05:26:36 +00:00
return null;
2019-11-30 00:01:07 +00:00
}
private Drawable createTableFromResponse(APIRequest request)
2019-11-30 00:01:07 +00:00
{
2019-12-05 05:26:36 +00:00
switch (request)
2019-11-30 00:01:07 +00:00
{
2019-12-05 05:26:36 +00:00
case GetUserRankingsRequest userRequest:
2021-10-05 08:59:38 +00:00
if (userRequest.Response == null)
return null;
2019-12-05 05:26:36 +00:00
switch (userRequest.Type)
{
case UserRankingsType.Performance:
return new PerformanceTable(1, userRequest.Response.Users);
2019-11-30 00:01:07 +00:00
2019-12-05 05:26:36 +00:00
case UserRankingsType.Score:
return new ScoresTable(1, userRequest.Response.Users);
2019-12-05 05:26:36 +00:00
}
2019-11-30 00:01:07 +00:00
2019-12-05 05:26:36 +00:00
return null;
2019-11-30 00:01:07 +00:00
2019-12-05 05:26:36 +00:00
case GetCountryRankingsRequest countryRequest:
2021-10-05 08:59:38 +00:00
{
if (countryRequest.Response == null)
return null;
return new CountriesTable(1, countryRequest.Response.Countries);
2021-10-05 08:59:38 +00:00
}
}
return null;
}
2020-02-18 18:26:25 +00:00
protected override void Dispose(bool isDisposing)
{
lastRequest?.Cancel();
base.Dispose(isDisposing);
}
2019-11-30 00:01:07 +00:00
}
}