1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:17:23 +08:00

Use response params in next page request

This commit is contained in:
smoogipoo 2020-07-28 21:40:11 +09:00
parent 9f6446d836
commit db91d1de50
4 changed files with 53 additions and 47 deletions

View File

@ -1,6 +1,7 @@
// 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 JetBrains.Annotations;
using osu.Framework.IO.Network;
using osu.Game.Extensions;
using osu.Game.Online.API;
@ -16,31 +17,31 @@ namespace osu.Game.Online.Multiplayer
private readonly int roomId;
private readonly int playlistItemId;
private readonly Cursor cursor;
private readonly MultiplayerScoresSort? sort;
private readonly IndexScoresParams indexParams;
public IndexPlaylistScoresRequest(int roomId, int playlistItemId, Cursor cursor = null, MultiplayerScoresSort? sort = null)
public IndexPlaylistScoresRequest(int roomId, int playlistItemId)
{
this.roomId = roomId;
this.playlistItemId = playlistItemId;
}
public IndexPlaylistScoresRequest(int roomId, int playlistItemId, [NotNull] Cursor cursor, [NotNull] IndexScoresParams indexParams)
: this(roomId, playlistItemId)
{
this.cursor = cursor;
this.sort = sort;
this.indexParams = indexParams;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.AddCursor(cursor);
switch (sort)
if (cursor != null)
{
case MultiplayerScoresSort.Ascending:
req.AddParameter("sort", "score_asc");
break;
req.AddCursor(cursor);
case MultiplayerScoresSort.Descending:
req.AddParameter("sort", "score_desc");
break;
foreach (var (key, value) in indexParams.Properties)
req.AddParameter(key, value.ToString());
}
return req;

View File

@ -0,0 +1,20 @@
// 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.Collections.Generic;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace osu.Game.Online.Multiplayer
{
/// <summary>
/// A collection of parameters which should be passed to the index endpoint to fetch the next page.
/// </summary>
public class IndexScoresParams
{
[UsedImplicitly]
[JsonExtensionData]
public IDictionary<string, JToken> Properties;
}
}

View File

@ -29,5 +29,11 @@ namespace osu.Game.Online.Multiplayer
/// </summary>
[JsonProperty("user_score")]
public MultiplayerScore UserScore { get; set; }
/// <summary>
/// The parameters to be used to fetch the next page.
/// </summary>
[JsonProperty("params")]
public IndexScoresParams Params { get; set; }
}
}

View File

@ -10,7 +10,6 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Multiplayer;
using osu.Game.Scoring;
using osu.Game.Screens.Ranking;
@ -23,8 +22,8 @@ namespace osu.Game.Screens.Multi.Ranking
private readonly PlaylistItem playlistItem;
private LoadingSpinner loadingLayer;
private Cursor higherScoresCursor;
private Cursor lowerScoresCursor;
private MultiplayerScores higherScores;
private MultiplayerScores lowerScores;
[Resolved]
private IAPIProvider api { get; set; }
@ -52,8 +51,8 @@ namespace osu.Game.Screens.Multi.Ranking
protected override APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback)
{
// This performs two requests:
// 1. A request to show the user's score.
// 2. If (1) fails, a request to index the room.
// 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);
@ -64,13 +63,13 @@ namespace osu.Game.Screens.Multi.Ranking
if (userScore.ScoresAround?.Higher != null)
{
allScores.AddRange(userScore.ScoresAround.Higher.Scores);
higherScoresCursor = userScore.ScoresAround.Higher.Cursor;
higherScores = userScore.ScoresAround.Higher;
}
if (userScore.ScoresAround?.Lower != null)
{
allScores.AddRange(userScore.ScoresAround.Lower.Scores);
lowerScoresCursor = userScore.ScoresAround.Lower.Cursor;
lowerScores = userScore.ScoresAround.Lower;
}
performSuccessCallback(scoresCallback, allScores);
@ -84,7 +83,7 @@ namespace osu.Game.Screens.Multi.Ranking
indexReq.Success += r =>
{
performSuccessCallback(scoresCallback, r.Scores);
lowerScoresCursor = r.Cursor;
lowerScores = r;
};
indexReq.Failure += __ => loadingLayer.Hide();
@ -99,39 +98,19 @@ namespace osu.Game.Screens.Multi.Ranking
{
Debug.Assert(direction == 1 || direction == -1);
Cursor cursor;
MultiplayerScoresSort sort;
MultiplayerScores pivot = direction == -1 ? higherScores : lowerScores;
switch (direction)
{
case -1:
cursor = higherScoresCursor;
sort = MultiplayerScoresSort.Ascending;
break;
default:
cursor = lowerScoresCursor;
sort = MultiplayerScoresSort.Descending;
break;
}
if (cursor == null)
if (pivot?.Cursor == null)
return null;
var indexReq = new IndexPlaylistScoresRequest(roomId, playlistItem.ID, cursor, sort);
var indexReq = new IndexPlaylistScoresRequest(roomId, playlistItem.ID, pivot.Cursor, pivot.Params);
indexReq.Success += r =>
{
switch (direction)
{
case -1:
higherScoresCursor = r.Cursor;
break;
default:
lowerScoresCursor = r.Cursor;
break;
}
if (direction == -1)
higherScores = r;
else
lowerScores = r;
performSuccessCallback(scoresCallback, r.Scores);
};