2020-05-26 17:12:19 +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 System;
|
|
|
|
using System.Collections.Generic;
|
2020-07-22 19:24:55 +08:00
|
|
|
using System.Diagnostics;
|
2020-05-26 17:12:19 +08:00
|
|
|
using System.Linq;
|
2020-06-09 17:53:55 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2020-05-26 17:12:19 +08:00
|
|
|
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;
|
|
|
|
|
2020-06-09 17:53:55 +08:00
|
|
|
private LoadingSpinner loadingLayer;
|
2020-07-28 20:40:11 +08:00
|
|
|
private MultiplayerScores higherScores;
|
|
|
|
private MultiplayerScores lowerScores;
|
2020-07-22 18:40:00 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
2020-06-09 17:53:55 +08:00
|
|
|
|
2020-05-26 17:12:19 +08:00
|
|
|
public TimeshiftResultsScreen(ScoreInfo score, int roomId, PlaylistItem playlistItem, bool allowRetry = true)
|
|
|
|
: base(score, allowRetry)
|
|
|
|
{
|
|
|
|
this.roomId = roomId;
|
|
|
|
this.playlistItem = playlistItem;
|
|
|
|
}
|
|
|
|
|
2020-06-09 17:53:55 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
AddInternal(loadingLayer = new LoadingLayer
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
X = -10,
|
|
|
|
State = { Value = Score == null ? Visibility.Visible : Visibility.Hidden },
|
|
|
|
Padding = new MarginPadding { Bottom = TwoLayerButton.SIZE_EXTENDED.Y }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:12:19 +08:00
|
|
|
protected override APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback)
|
|
|
|
{
|
2020-07-22 18:40:00 +08:00
|
|
|
// This performs two requests:
|
2020-07-28 20:40:11 +08:00
|
|
|
// 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.
|
2020-07-22 18:40:00 +08:00
|
|
|
|
|
|
|
var userScoreReq = new ShowPlaylistUserScoreRequest(roomId, playlistItem.ID, api.LocalUser.Value.Id);
|
2020-06-09 17:53:55 +08:00
|
|
|
|
2020-07-22 18:40:00 +08:00
|
|
|
userScoreReq.Success += userScore =>
|
2020-06-09 17:53:55 +08:00
|
|
|
{
|
2020-07-22 18:40:00 +08:00
|
|
|
var allScores = new List<MultiplayerScore> { userScore };
|
|
|
|
|
|
|
|
if (userScore.ScoresAround?.Higher != null)
|
|
|
|
{
|
|
|
|
allScores.AddRange(userScore.ScoresAround.Higher.Scores);
|
2020-07-28 20:40:11 +08:00
|
|
|
higherScores = userScore.ScoresAround.Higher;
|
2020-07-22 18:40:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (userScore.ScoresAround?.Lower != null)
|
|
|
|
{
|
|
|
|
allScores.AddRange(userScore.ScoresAround.Lower.Scores);
|
2020-07-28 20:40:11 +08:00
|
|
|
lowerScores = userScore.ScoresAround.Lower;
|
2020-07-22 18:40:00 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 19:24:55 +08:00
|
|
|
performSuccessCallback(scoresCallback, allScores);
|
2020-07-22 18:40:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
userScoreReq.Failure += _ =>
|
|
|
|
{
|
|
|
|
// Fallback to a normal index.
|
|
|
|
var indexReq = new IndexPlaylistScoresRequest(roomId, playlistItem.ID);
|
2020-07-22 19:24:55 +08:00
|
|
|
|
|
|
|
indexReq.Success += r =>
|
|
|
|
{
|
|
|
|
performSuccessCallback(scoresCallback, r.Scores);
|
2020-07-28 20:40:11 +08:00
|
|
|
lowerScores = r;
|
2020-07-22 19:24:55 +08:00
|
|
|
};
|
|
|
|
|
2020-07-22 18:40:00 +08:00
|
|
|
indexReq.Failure += __ => loadingLayer.Hide();
|
2020-07-22 19:24:55 +08:00
|
|
|
|
2020-07-22 18:40:00 +08:00
|
|
|
api.Queue(indexReq);
|
2020-06-09 17:53:55 +08:00
|
|
|
};
|
|
|
|
|
2020-07-22 18:40:00 +08:00
|
|
|
return userScoreReq;
|
2020-07-22 19:24:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override APIRequest FetchNextPage(int direction, Action<IEnumerable<ScoreInfo>> scoresCallback)
|
|
|
|
{
|
|
|
|
Debug.Assert(direction == 1 || direction == -1);
|
|
|
|
|
2020-07-28 20:40:11 +08:00
|
|
|
MultiplayerScores pivot = direction == -1 ? higherScores : lowerScores;
|
2020-07-22 18:40:00 +08:00
|
|
|
|
2020-07-28 20:40:11 +08:00
|
|
|
if (pivot?.Cursor == null)
|
2020-07-22 19:24:55 +08:00
|
|
|
return null;
|
|
|
|
|
2020-07-28 20:40:11 +08:00
|
|
|
var indexReq = new IndexPlaylistScoresRequest(roomId, playlistItem.ID, pivot.Cursor, pivot.Params);
|
2020-07-22 19:24:55 +08:00
|
|
|
|
|
|
|
indexReq.Success += r =>
|
|
|
|
{
|
2020-07-28 20:40:11 +08:00
|
|
|
if (direction == -1)
|
|
|
|
higherScores = r;
|
|
|
|
else
|
|
|
|
lowerScores = r;
|
2020-07-22 18:40:00 +08:00
|
|
|
|
2020-07-22 19:24:55 +08:00
|
|
|
performSuccessCallback(scoresCallback, r.Scores);
|
|
|
|
};
|
|
|
|
|
|
|
|
indexReq.Failure += _ => loadingLayer.Hide();
|
2020-07-22 18:40:00 +08:00
|
|
|
|
2020-07-22 19:24:55 +08:00
|
|
|
return indexReq;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void performSuccessCallback(Action<IEnumerable<ScoreInfo>> callback, List<MultiplayerScore> scores)
|
|
|
|
{
|
|
|
|
var scoreInfos = new List<ScoreInfo>(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();
|
|
|
|
});
|
2020-07-22 18:40:00 +08:00
|
|
|
}
|
2020-07-22 19:24:55 +08:00
|
|
|
|
|
|
|
// Invoke callback to add the scores. Exclude the user's current score which was added previously.
|
|
|
|
callback?.Invoke(scoreInfos.Where(s => s.ID != Score?.OnlineScoreID));
|
|
|
|
|
|
|
|
loadingLayer.Hide();
|
2020-05-26 17:12:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|