1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 13:37:25 +08:00
osu-lazer/osu.Game/Overlays/RankingsOverlay.cs

267 lines
9.4 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.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;
2020-02-05 17:09:32 +08:00
using osu.Game.Online.API.Requests.Responses;
using System.Linq;
using osuTK;
using osu.Game.Overlays.Direct;
2019-11-30 08:01:07 +08:00
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>();
2020-02-05 17:09:32 +08:00
private readonly Bindable<APISpotlight> spotlight = new Bindable<APISpotlight>();
2019-11-30 08:01:07 +08:00
private readonly BasicScrollContainer scrollFlow;
2020-02-05 17:14:24 +08:00
private readonly Container contentContainer;
2019-11-30 08:01:07 +08:00
private readonly DimmedLoadingLayer loading;
2020-02-04 02:05:11 +08:00
private readonly Box background;
2020-02-05 17:09:32 +08:00
private readonly RankingsOverlayHeader header;
2019-11-30 08:01:07 +08:00
2019-12-05 13:26:36 +08:00
private APIRequest lastRequest;
2019-11-30 08:01:07 +08:00
private CancellationTokenSource cancellationToken;
private GetSpotlightsRequest spotlightsRequest;
2019-11-30 08:01:07 +08:00
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private RulesetStore rulesets { get; set; }
2019-11-30 08:01:07 +08:00
public RankingsOverlay()
2020-01-24 17:24:35 +08:00
: base(OverlayColourScheme.Green)
2019-11-30 08:01:07 +08:00
{
Children = new Drawable[]
{
2020-02-04 02:05:11 +08:00
background = new Box
2019-11-30 08:01:07 +08:00
{
2020-02-04 02:05:11 +08:00
RelativeSizeAxes = Axes.Both
2019-11-30 08:01:07 +08:00
},
scrollFlow = new BasicScrollContainer
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
2020-02-05 17:09:32 +08:00
header = new RankingsOverlayHeader
2019-11-30 08:01:07 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
2020-02-04 01:53:30 +08:00
Depth = -float.MaxValue,
Country = { BindTarget = Country },
2020-02-04 01:53:30 +08:00
Current = { BindTarget = Scope },
2020-02-05 17:09:32 +08:00
Ruleset = { BindTarget = ruleset },
Spotlight = { BindTarget = spotlight }
2019-11-30 08:01:07 +08:00
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
2020-02-05 17:14:24 +08:00
contentContainer = 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(),
}
}
}
}
}
};
}
2020-02-04 02:05:11 +08:00
[BackgroundDependencyLoader]
private void load()
{
background.Colour = ColourProvider.Background5;
}
2019-11-30 08:01:07 +08:00
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(_ =>
{
spotlightsRequest?.Cancel();
// country filtering is only valid for performance scope.
if (Scope.Value != RankingsScope.Performance)
Country.Value = null;
2020-02-05 17:09:32 +08:00
if (Scope.Value == RankingsScope.Spotlights && !header.Spotlights.Any())
{
getSpotlights();
return;
}
Scheduler.AddOnce(loadNewContent);
}, true);
ruleset.BindValueChanged(_ => Scheduler.AddOnce(loadNewContent), true);
2020-02-05 17:09:32 +08:00
spotlight.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
}
2020-02-05 17:09:32 +08:00
private void getSpotlights()
{
loading.Show();
spotlightsRequest = new GetSpotlightsRequest();
spotlightsRequest.Success += response => header.Spotlights = response.Spotlights;
api.Queue(spotlightsRequest);
2020-02-05 17:09:32 +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
{
2020-02-05 17:14:24 +08:00
loadContent(null);
2019-12-05 13:26:36 +08:00
return;
}
2020-02-05 17:14:24 +08:00
request.Success += () => loadContent(createContentFromResponse(request));
request.Failure += _ => loadContent(null);
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);
2020-02-05 17:09:32 +08:00
case RankingsScope.Spotlights:
return new GetSpotlightRankingsRequest(ruleset.Value, header.Spotlight.Value.Id);
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
}
2020-02-05 17:14:24 +08:00
private Drawable createContentFromResponse(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);
2020-02-05 17:09:32 +08:00
case GetSpotlightRankingsRequest spotlightRequest:
header.SpotlightSelector.ShowInfo(spotlightRequest.Result.Spotlight, spotlightRequest.Result.BeatmapSets.Count);
return new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 20),
Children = new Drawable[]
{
new ScoresTable(1, spotlightRequest.Result.Users),
new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Spacing = new Vector2(10),
Children = spotlightRequest.Result.BeatmapSets.Select(b => new DirectGridPanel(b.ToBeatmapSet(rulesets))
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
}).ToList()
}
}
};
2019-12-05 13:26:36 +08:00
}
return null;
2019-11-30 08:01:07 +08:00
}
2020-02-05 17:14:24 +08:00
private void loadContent(Drawable content)
2019-11-30 08:01:07 +08:00
{
2019-12-05 13:26:36 +08:00
scrollFlow.ScrollToStart();
2020-02-05 17:14:24 +08:00
if (content == null)
2019-12-05 13:26:36 +08:00
{
2020-02-05 17:14:24 +08:00
contentContainer.Clear();
2019-12-05 13:26:36 +08:00
loading.Hide();
return;
}
2020-02-05 17:14:24 +08:00
LoadComponentAsync(content, t =>
2019-11-30 08:01:07 +08:00
{
loading.Hide();
2020-02-05 17:14:24 +08:00
contentContainer.Child = content;
2019-12-05 10:20:22 +08:00
}, (cancellationToken = new CancellationTokenSource()).Token);
2019-11-30 08:01:07 +08:00
}
}
}