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 System.Threading;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
using osu.Game.Overlays.Rankings.Tables;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
2021-01-18 08:13:38 +00:00
|
|
|
|
public class RankingsOverlay : OnlineOverlay<RankingsOverlayHeader>
|
2019-11-30 00:01:07 +00:00
|
|
|
|
{
|
2020-09-03 07:27:31 +00:00
|
|
|
|
protected Bindable<Country> Country => Header.Country;
|
2020-02-18 18:26:25 +00:00
|
|
|
|
|
2020-09-03 07:27:31 +00:00
|
|
|
|
protected Bindable<RankingsScope> Scope => Header.Current;
|
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
|
|
|
|
private CancellationTokenSource cancellationToken;
|
|
|
|
|
|
|
|
|
|
[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
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 18:05:11 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-30 00:01:07 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
2020-02-18 18:26:25 +00:00
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2020-09-03 07:27:31 +00:00
|
|
|
|
Header.Ruleset.BindTo(ruleset);
|
2020-02-21 09:54:48 +00:00
|
|
|
|
|
2019-12-05 06:53:25 +00:00
|
|
|
|
Country.BindValueChanged(_ =>
|
2019-12-05 05:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
// if a country is requested, force performance scope.
|
2019-12-05 06:53:25 +00:00
|
|
|
|
if (Country.Value != null)
|
|
|
|
|
Scope.Value = RankingsScope.Performance;
|
2019-12-05 05:07:39 +00:00
|
|
|
|
|
2020-02-10 23:35:23 +00:00
|
|
|
|
Scheduler.AddOnce(loadNewContent);
|
2020-02-18 18:26:25 +00:00
|
|
|
|
});
|
2019-12-11 10:13:04 +00:00
|
|
|
|
|
2019-12-05 06:53:25 +00:00
|
|
|
|
Scope.BindValueChanged(_ =>
|
2019-12-05 05:07:39 +00:00
|
|
|
|
{
|
|
|
|
|
// country filtering is only valid for performance scope.
|
2019-12-05 06:53:25 +00:00
|
|
|
|
if (Scope.Value != RankingsScope.Performance)
|
|
|
|
|
Country.Value = null;
|
2019-12-05 05:07:39 +00:00
|
|
|
|
|
|
|
|
|
Scheduler.AddOnce(loadNewContent);
|
2020-02-18 18:26:25 +00:00
|
|
|
|
});
|
2019-12-05 05:07:39 +00:00
|
|
|
|
|
2020-02-10 23:35:23 +00:00
|
|
|
|
ruleset.BindValueChanged(_ =>
|
|
|
|
|
{
|
|
|
|
|
if (Scope.Value == RankingsScope.Spotlights)
|
|
|
|
|
return;
|
2019-12-05 05:07:39 +00:00
|
|
|
|
|
2020-02-10 23:35:23 +00:00
|
|
|
|
Scheduler.AddOnce(loadNewContent);
|
2020-02-18 18:26:25 +00:00
|
|
|
|
});
|
2020-02-21 09:54:48 +00:00
|
|
|
|
|
|
|
|
|
Scheduler.AddOnce(loadNewContent);
|
2019-11-30 00:01:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
|
2019-12-05 06:53:25 +00:00
|
|
|
|
Country.Value = requested;
|
2019-11-30 00:01:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 19:57:15 +00:00
|
|
|
|
public void ShowSpotlights()
|
|
|
|
|
{
|
|
|
|
|
Scope.Value = RankingsScope.Spotlights;
|
|
|
|
|
Show();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 05:07:39 +00:00
|
|
|
|
private void loadNewContent()
|
2019-11-30 00:01:07 +00:00
|
|
|
|
{
|
2021-01-18 07:48:12 +00:00
|
|
|
|
Loading.Show();
|
2019-11-30 00:01:07 +00:00
|
|
|
|
|
|
|
|
|
cancellationToken?.Cancel();
|
2019-12-05 05:26:36 +00:00
|
|
|
|
lastRequest?.Cancel();
|
2019-11-30 00:01:07 +00:00
|
|
|
|
|
2020-02-10 23:35:23 +00:00
|
|
|
|
if (Scope.Value == RankingsScope.Spotlights)
|
|
|
|
|
{
|
|
|
|
|
loadContent(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
|
|
|
|
{
|
2020-02-05 09:14:24 +00:00
|
|
|
|
loadContent(null);
|
2019-12-05 05:26:36 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:26:25 +00:00
|
|
|
|
request.Success += () => Schedule(() => loadContent(createTableFromResponse(request)));
|
|
|
|
|
request.Failure += _ => Schedule(() => loadContent(null));
|
2020-02-10 23:35:23 +00:00
|
|
|
|
|
2019-12-05 05:26:36 +00:00
|
|
|
|
api.Queue(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private APIRequest createScopedRequest()
|
|
|
|
|
{
|
2019-12-05 06:53:25 +00:00
|
|
|
|
switch (Scope.Value)
|
2019-12-05 05:26:36 +00:00
|
|
|
|
{
|
2019-11-30 00:01:07 +00:00
|
|
|
|
case RankingsScope.Performance:
|
2019-12-05 06:53:25 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 23:35:23 +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);
|
2020-02-05 20:06:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 09:14:24 +00:00
|
|
|
|
private void loadContent(Drawable content)
|
2019-11-30 00:01:07 +00:00
|
|
|
|
{
|
2021-01-18 07:48:12 +00:00
|
|
|
|
ScrollFlow.ScrollToStart();
|
2019-12-05 05:26:36 +00:00
|
|
|
|
|
2020-02-05 09:14:24 +00:00
|
|
|
|
if (content == null)
|
2019-12-05 05:26:36 +00:00
|
|
|
|
{
|
2021-01-18 07:48:12 +00:00
|
|
|
|
Clear();
|
|
|
|
|
Loading.Hide();
|
2019-12-05 05:26:36 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 23:35:23 +00:00
|
|
|
|
LoadComponentAsync(content, loaded =>
|
2019-11-30 00:01:07 +00:00
|
|
|
|
{
|
2021-01-18 07:48:12 +00:00
|
|
|
|
Loading.Hide();
|
|
|
|
|
Child = loaded;
|
2019-12-05 02:20:22 +00:00
|
|
|
|
}, (cancellationToken = new CancellationTokenSource()).Token);
|
2019-11-30 00:01:07 +00:00
|
|
|
|
}
|
2020-02-18 18:26:25 +00:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
lastRequest?.Cancel();
|
|
|
|
|
cancellationToken?.Cancel();
|
|
|
|
|
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
}
|
2019-11-30 00:01:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|