// Copyright (c) ppy Pty Ltd . 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 { protected Bindable Ruleset => Header.Ruleset; protected Bindable Country => Header.Country; private APIRequest lastRequest; [Resolved] private IAPIProvider api { get; set; } public RankingsOverlay() : base(OverlayColourScheme.Green) { } protected override void LoadComplete() { base.LoadComplete(); Country.BindValueChanged(_ => { // if a country is requested, force performance scope. if (Country.Value != null) Header.Current.Value = RankingsScope.Performance; Scheduler.AddOnce(triggerTabChanged); }); Ruleset.BindValueChanged(_ => { if (Header.Current.Value == RankingsScope.Spotlights) return; Scheduler.AddOnce(triggerTabChanged); }); } 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); protected override RankingsOverlayHeader CreateHeader() => new RankingsOverlayHeader(); public void ShowCountry(Country requested) { if (requested == null) return; Show(); Country.Value = requested; } protected override void CreateDisplayToLoad(RankingsScope tab) { lastRequest?.Cancel(); if (Header.Current.Value == RankingsScope.Spotlights) { LoadDisplay(new SpotlightsLayout { Ruleset = { BindTarget = Ruleset } }); return; } var request = createScopedRequest(); lastRequest = request; if (request == null) { LoadDisplay(Empty()); return; } request.Success += () => Schedule(() => LoadDisplay(createTableFromResponse(request))); request.Failure += _ => Schedule(() => LoadDisplay(Empty())); api.Queue(request); } private APIRequest createScopedRequest() { switch (Header.Current.Value) { case RankingsScope.Performance: return new GetUserRankingsRequest(Ruleset.Value, country: Country.Value?.FlagName); case RankingsScope.Country: return new GetCountryRankingsRequest(Ruleset.Value); case RankingsScope.Score: return new GetUserRankingsRequest(Ruleset.Value, UserRankingsType.Score); } return null; } private Drawable createTableFromResponse(APIRequest request) { switch (request) { case GetUserRankingsRequest userRequest: switch (userRequest.Type) { case UserRankingsType.Performance: return new PerformanceTable(1, userRequest.Result.Users); case UserRankingsType.Score: return new ScoresTable(1, userRequest.Result.Users); } return null; case GetCountryRankingsRequest countryRequest: return new CountriesTable(1, countryRequest.Result.Countries); } return null; } protected override void Dispose(bool isDisposing) { lastRequest?.Cancel(); base.Dispose(isDisposing); } } }