// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Graphics.UserInterface; using osu.Game.Online.API; using osu.Game.Online.Multiplayer; using osu.Game.Scoring; using osu.Game.Screens.Ranking; namespace osu.Game.Screens.Multi.Ranking { public class TimeshiftResultsScreen : ResultsScreen { private readonly int roomId; private readonly PlaylistItem playlistItem; private LoadingSpinner leftLoadingLayer; private LoadingSpinner centreLoadingLayer; private LoadingSpinner rightLoadingLayer; private MultiplayerScores higherScores; private MultiplayerScores lowerScores; [Resolved] private IAPIProvider api { get; set; } public TimeshiftResultsScreen(ScoreInfo score, int roomId, PlaylistItem playlistItem, bool allowRetry = true) : base(score, allowRetry) { this.roomId = roomId; this.playlistItem = playlistItem; } [BackgroundDependencyLoader] private void load() { AddInternal(new Container { RelativeSizeAxes = Axes.Both, Padding = new MarginPadding { Bottom = TwoLayerButton.SIZE_EXTENDED.Y }, Children = new Drawable[] { leftLoadingLayer = new PanelListLoadingSpinner(ScorePanelList) { Anchor = Anchor.CentreLeft, Origin = Anchor.Centre, }, centreLoadingLayer = new PanelListLoadingSpinner(ScorePanelList) { Anchor = Anchor.Centre, Origin = Anchor.Centre, State = { Value = Score == null ? Visibility.Visible : Visibility.Hidden }, }, rightLoadingLayer = new PanelListLoadingSpinner(ScorePanelList) { Anchor = Anchor.CentreRight, Origin = Anchor.Centre, }, } }); } protected override APIRequest FetchScores(Action> scoresCallback) { // This performs two requests: // 1. A request to show the user's score (and scores around). // 2. If that fails, a request to index the room starting from the highest score. var userScoreReq = new ShowPlaylistUserScoreRequest(roomId, playlistItem.ID, api.LocalUser.Value.Id); userScoreReq.Success += userScore => { var allScores = new List { userScore }; if (userScore.ScoresAround?.Higher != null) { allScores.AddRange(userScore.ScoresAround.Higher.Scores); higherScores = userScore.ScoresAround.Higher; } if (userScore.ScoresAround?.Lower != null) { allScores.AddRange(userScore.ScoresAround.Lower.Scores); lowerScores = userScore.ScoresAround.Lower; } performSuccessCallback(scoresCallback, allScores); }; // On failure, fallback to a normal index. userScoreReq.Failure += _ => api.Queue(createIndexRequest(scoresCallback)); return userScoreReq; } protected override APIRequest FetchNextPage(int direction, Action> scoresCallback) { Debug.Assert(direction == 1 || direction == -1); MultiplayerScores pivot = direction == -1 ? higherScores : lowerScores; if (pivot?.Cursor == null) return null; if (pivot == higherScores) leftLoadingLayer.Show(); else rightLoadingLayer.Show(); return createIndexRequest(scoresCallback, pivot); } /// /// Creates a with an optional score pivot. /// /// Does not queue the request. /// The callback to perform with the resulting scores. /// An optional score pivot to retrieve scores around. Can be null to retrieve scores from the highest score. /// The indexing . private APIRequest createIndexRequest(Action> scoresCallback, [CanBeNull] MultiplayerScores pivot = null) { var indexReq = pivot != null ? new IndexPlaylistScoresRequest(roomId, playlistItem.ID, pivot.Cursor, pivot.Params) : new IndexPlaylistScoresRequest(roomId, playlistItem.ID); indexReq.Success += r => { if (pivot == lowerScores) lowerScores = r; else higherScores = r; performSuccessCallback(scoresCallback, r.Scores, r); }; indexReq.Failure += _ => hideLoadingSpinners(pivot); return indexReq; } /// /// Transforms returned into s, ensure the is put into a sane state, and invokes a given success callback. /// /// The callback to invoke with the final s. /// The s that were retrieved from s. /// An optional pivot around which the scores were retrieved. private void performSuccessCallback([NotNull] Action> callback, [NotNull] List scores, [CanBeNull] MultiplayerScores pivot = null) { var scoreInfos = new List(scores.Select(s => s.CreateScoreInfo(playlistItem))); // Select a score if we don't already have one selected. // Note: This is done before the callback so that the panel list centres on the selected score before panels are added (eliminating initial scroll). if (SelectedScore.Value == null) { Schedule(() => { // Prefer selecting the local user's score, or otherwise default to the first visible score. SelectedScore.Value = scoreInfos.FirstOrDefault(s => s.User.Id == api.LocalUser.Value.Id) ?? scoreInfos.FirstOrDefault(); }); } // Invoke callback to add the scores. Exclude the user's current score which was added previously. callback.Invoke(scoreInfos.Where(s => s.OnlineScoreID != Score?.OnlineScoreID)); hideLoadingSpinners(pivot); } private void hideLoadingSpinners([CanBeNull] MultiplayerScores pivot = null) { centreLoadingLayer.Hide(); if (pivot == lowerScores) rightLoadingLayer.Hide(); else if (pivot == higherScores) leftLoadingLayer.Hide(); } private class PanelListLoadingSpinner : LoadingSpinner { private readonly ScorePanelList list; /// /// Creates a new . /// /// The list to track. /// Whether the spinner should have a surrounding black box for visibility. public PanelListLoadingSpinner(ScorePanelList list, bool withBox = true) : base(withBox) { this.list = list; } protected override void Update() { base.Update(); float panelOffset = list.DrawWidth / 2 - ScorePanel.EXPANDED_WIDTH; if ((Anchor & Anchor.x0) > 0) X = (float)(panelOffset - list.Current); else if ((Anchor & Anchor.x2) > 0) X = (float)(list.ScrollableExtent - list.Current - panelOffset); } } } }