osu/osu.Game/Overlays/RankingsOverlay.cs

179 lines
5.1 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; }
[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.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
}
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.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:
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
}
}