osu/osu.Game/Overlays/RankingsOverlay.cs

155 lines
4.5 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.
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 class RankingsOverlay : TabbableOnlineOverlay<RankingsOverlayHeader, RankingsScope>
2019-11-30 00:01:07 +00:00
{
protected Bindable<Country> 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; }
2021-01-18 07:48:12 +00:00
[Resolved]
private Bindable<RulesetInfo> ruleset { get; set; }
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);
2020-02-21 09:54:48 +00:00
Country.BindValueChanged(_ =>
{
// if a country is requested, force performance scope.
if (Country.Value != null)
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
}
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.Value = null;
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();
2019-11-30 00:01:07 +00:00
public void ShowCountry(Country requested)
{
if (requested == null)
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:
return new GetUserRankingsRequest(ruleset.Value, country: Country.Value?.FlagName);
2019-11-30 00:01:07 +00:00
case RankingsScope.Country:
2019-12-05 05:26:36 +00:00
return new GetCountryRankingsRequest(ruleset.Value);
2019-11-30 00:01:07 +00:00
case RankingsScope.Score:
2019-12-05 05:26:36 +00:00
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:
switch (userRequest.Type)
{
case UserRankingsType.Performance:
return new PerformanceTable(1, userRequest.Result.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.Result.Users);
}
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:
return new CountriesTable(1, countryRequest.Result.Countries);
}
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
}
}