1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-28 04:23:06 +08:00
osu-lazer/osu.Game/Overlays/RankingsOverlay.cs

215 lines
7.0 KiB
C#
Raw Normal View History

2019-11-30 08:01:07 +08: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.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Game.Overlays.Rankings;
using osu.Game.Users;
using osu.Game.Rulesets;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using System.Threading;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays.Rankings.Tables;
namespace osu.Game.Overlays
{
public class RankingsOverlay : FullscreenOverlay
{
protected readonly Bindable<Country> Country = new Bindable<Country>();
protected readonly Bindable<RankingsScope> Scope = new Bindable<RankingsScope>();
2019-11-30 08:01:07 +08:00
private readonly Bindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
private readonly BasicScrollContainer scrollFlow;
private readonly Box background;
2019-12-05 13:26:36 +08:00
private readonly Container tableContainer;
2019-11-30 08:01:07 +08:00
private readonly DimmedLoadingLayer loading;
2019-12-05 13:26:36 +08:00
private APIRequest lastRequest;
2019-11-30 08:01:07 +08:00
private CancellationTokenSource cancellationToken;
[Resolved]
private IAPIProvider api { get; set; }
public RankingsOverlay()
{
Children = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
scrollFlow = new BasicScrollContainer
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new RankingsHeader
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Country = { BindTarget = Country },
Scope = { BindTarget = Scope },
2019-11-30 08:01:07 +08:00
Ruleset = { BindTarget = ruleset }
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
2019-12-05 13:26:36 +08:00
tableContainer = new Container
2019-11-30 08:01:07 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Margin = new MarginPadding { Vertical = 10 }
},
loading = new DimmedLoadingLayer(),
}
}
}
}
}
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colour)
{
Waves.FirstWaveColour = colour.Green;
Waves.SecondWaveColour = colour.GreenLight;
Waves.ThirdWaveColour = colour.GreenDark;
Waves.FourthWaveColour = colour.GreenDarker;
background.Colour = OsuColour.Gray(0.1f);
}
protected override void LoadComplete()
{
Country.BindValueChanged(_ =>
{
// if a country is requested, force performance scope.
if (Country.Value != null)
Scope.Value = RankingsScope.Performance;
Scheduler.AddOnce(loadNewContent);
}, true);
2019-12-11 18:13:04 +08:00
Scope.BindValueChanged(_ =>
{
// country filtering is only valid for performance scope.
if (Scope.Value != RankingsScope.Performance)
Country.Value = null;
Scheduler.AddOnce(loadNewContent);
}, true);
ruleset.BindValueChanged(_ => Scheduler.AddOnce(loadNewContent), true);
2019-11-30 08:01:07 +08:00
base.LoadComplete();
}
public void ShowCountry(Country requested)
{
if (requested == null)
return;
Show();
Country.Value = requested;
2019-11-30 08:01:07 +08:00
}
private void loadNewContent()
2019-11-30 08:01:07 +08:00
{
loading.Show();
cancellationToken?.Cancel();
2019-12-05 13:26:36 +08:00
lastRequest?.Cancel();
2019-11-30 08:01:07 +08:00
2019-12-05 13:26:36 +08:00
var request = createScopedRequest();
lastRequest = request;
if (request == null)
2019-11-30 08:01:07 +08:00
{
2019-12-05 13:26:36 +08:00
loadTable(null);
return;
}
request.Success += () => loadTable(createTableFromResponse(request));
request.Failure += _ => loadTable(null);
2019-11-30 08:01:07 +08:00
2019-12-05 13:26:36 +08:00
api.Queue(request);
}
private APIRequest createScopedRequest()
{
switch (Scope.Value)
2019-12-05 13:26:36 +08:00
{
2019-11-30 08:01:07 +08:00
case RankingsScope.Performance:
return new GetUserRankingsRequest(ruleset.Value, country: Country.Value?.FlagName);
2019-11-30 08:01:07 +08:00
case RankingsScope.Country:
2019-12-05 13:26:36 +08:00
return new GetCountryRankingsRequest(ruleset.Value);
2019-11-30 08:01:07 +08:00
case RankingsScope.Score:
2019-12-05 13:26:36 +08:00
return new GetUserRankingsRequest(ruleset.Value, UserRankingsType.Score);
2019-11-30 08:01:07 +08:00
}
2019-12-05 13:26:36 +08:00
return null;
2019-11-30 08:01:07 +08:00
}
2019-12-05 13:26:36 +08:00
private Drawable createTableFromResponse(APIRequest request)
2019-11-30 08:01:07 +08:00
{
2019-12-05 13:26:36 +08:00
switch (request)
2019-11-30 08:01:07 +08:00
{
2019-12-05 13:26:36 +08:00
case GetUserRankingsRequest userRequest:
switch (userRequest.Type)
{
case UserRankingsType.Performance:
return new PerformanceTable(1, userRequest.Result.Users);
2019-11-30 08:01:07 +08:00
2019-12-05 13:26:36 +08:00
case UserRankingsType.Score:
return new ScoresTable(1, userRequest.Result.Users);
}
2019-11-30 08:01:07 +08:00
2019-12-05 13:26:36 +08:00
return null;
2019-11-30 08:01:07 +08:00
2019-12-05 13:26:36 +08:00
case GetCountryRankingsRequest countryRequest:
return new CountriesTable(1, countryRequest.Result.Countries);
}
return null;
2019-11-30 08:01:07 +08:00
}
private void loadTable(Drawable table)
{
2019-12-05 13:26:36 +08:00
scrollFlow.ScrollToStart();
if (table == null)
{
tableContainer.Clear();
loading.Hide();
return;
}
2019-12-05 10:20:22 +08:00
LoadComponentAsync(table, t =>
2019-11-30 08:01:07 +08:00
{
loading.Hide();
2019-12-05 13:26:36 +08:00
tableContainer.Child = table;
2019-12-05 10:20:22 +08:00
}, (cancellationToken = new CancellationTokenSource()).Token);
2019-11-30 08:01:07 +08:00
}
}
}