1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 00:42:55 +08:00

Expand GetUser and GetUserScores requests by adding RulesetInfo… (#5621)

Expand GetUser and GetUserScores requests by adding RulesetInfo parameter
This commit is contained in:
Dean Herbert 2019-08-07 16:58:36 +09:00 committed by GitHub
commit 801f458684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 129 additions and 3 deletions

View File

@ -0,0 +1,109 @@
// 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 NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mania;
using osu.Game.Users;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Taiko;
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Tests.Visual.Online
{
[TestFixture]
public class TestSceneUserRequest : OsuTestScene
{
[Resolved]
private IAPIProvider api { get; set; }
private readonly Bindable<User> user = new Bindable<User>();
private GetUserRequest request;
private readonly DimmedLoadingLayer loading;
public TestSceneUserRequest()
{
Add(new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
new UserTestContainer
{
User = { BindTarget = user }
},
loading = new DimmedLoadingLayer
{
Alpha = 0
}
}
});
}
protected override void LoadComplete()
{
base.LoadComplete();
AddStep(@"local user", () => getUser());
AddStep(@"local user with taiko ruleset", () => getUser(ruleset: new TaikoRuleset().RulesetInfo));
AddStep(@"cookiezi", () => getUser(124493));
AddStep(@"cookiezi with mania ruleset", () => getUser(124493, new ManiaRuleset().RulesetInfo));
}
private void getUser(long? userId = null, RulesetInfo ruleset = null)
{
loading.Show();
request?.Cancel();
request = new GetUserRequest(userId, ruleset);
request.Success += user =>
{
this.user.Value = user;
loading.Hide();
};
api.Queue(request);
}
private class UserTestContainer : FillFlowContainer
{
public readonly Bindable<User> User = new Bindable<User>();
public UserTestContainer()
{
AutoSizeAxes = Axes.Both;
Direction = FillDirection.Vertical;
}
protected override void LoadComplete()
{
base.LoadComplete();
User.BindValueChanged(onUserUpdate, true);
}
private void onUserUpdate(ValueChangedEvent<User> user)
{
Clear();
AddRange(new Drawable[]
{
new SpriteText
{
Text = $@"Username: {user.NewValue?.Username}"
},
new SpriteText
{
Text = $@"RankedScore: {user.NewValue?.Statistics.RankedScore}"
},
});
}
}
}
}

View File

@ -2,18 +2,21 @@
// See the LICENCE file in the repository root for full licence text.
using osu.Game.Users;
using osu.Game.Rulesets;
namespace osu.Game.Online.API.Requests
{
public class GetUserRequest : APIRequest<User>
{
private readonly long? userId;
private readonly RulesetInfo ruleset;
public GetUserRequest(long? userId = null)
public GetUserRequest(long? userId = null, RulesetInfo ruleset = null)
{
this.userId = userId;
this.ruleset = ruleset;
}
protected override string Target => userId.HasValue ? $@"users/{userId}" : @"me";
protected override string Target => userId.HasValue ? $@"users/{userId}/{ruleset?.ShortName}" : $@"me/{ruleset?.ShortName}";
}
}

View File

@ -2,7 +2,9 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using osu.Framework.IO.Network;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Rulesets;
namespace osu.Game.Online.API.Requests
{
@ -10,12 +12,24 @@ namespace osu.Game.Online.API.Requests
{
private readonly long userId;
private readonly ScoreType type;
private readonly RulesetInfo ruleset;
public GetUserScoresRequest(long userId, ScoreType type, int page = 0, int itemsPerPage = 5)
public GetUserScoresRequest(long userId, ScoreType type, int page = 0, int itemsPerPage = 5, RulesetInfo ruleset = null)
: base(page, itemsPerPage)
{
this.userId = userId;
this.type = type;
this.ruleset = ruleset;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
if (ruleset != null)
req.AddParameter("mode", ruleset.ShortName);
return req;
}
protected override string Target => $@"users/{userId}/scores/{type.ToString().ToLowerInvariant()}";