1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 15:33:05 +08:00

Add proper user data retrieval on connect.

This commit is contained in:
Dean Herbert 2016-12-01 16:44:24 +09:00
parent d7ab74363d
commit 6ccce88a0e
6 changed files with 44 additions and 6 deletions

View File

@ -8,6 +8,7 @@ using System.Diagnostics;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using osu.Framework; using osu.Framework;
using osu.Framework.Configuration;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Framework.Threading; using osu.Framework.Threading;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
@ -32,6 +33,8 @@ namespace osu.Game.Online.API
public string Password; public string Password;
public Bindable<User> LocalUser = new Bindable<User>();
public string Token public string Token
{ {
get { return authentication.Token?.ToString(); } get { return authentication.Token?.ToString(); }
@ -126,6 +129,17 @@ namespace osu.Game.Online.API
continue; continue;
} }
var userReq = new GetUserRequest();
userReq.Success += (u) => {
LocalUser.Value = u;
};
if (!handleRequest(userReq))
{
State = APIState.Failing;
continue;
}
//we're connected! //we're connected!
State = APIState.Online; State = APIState.Online;
failureCount = 0; failureCount = 0;

View File

@ -0,0 +1,18 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Online.API.Requests
{
public class GetUserRequest : APIRequest<User>
{
private int? userId;
public GetUserRequest(int? userId = null)
{
this.userId = userId;
}
protected override string Target => userId.HasValue ? $@"users/{userId}" : @"me";
}
}

View File

@ -10,6 +10,9 @@ namespace osu.Game.Online
[JsonProperty(@"username")] [JsonProperty(@"username")]
public string Name; public string Name;
[JsonProperty(@"id")]
public int Id;
[JsonProperty(@"colour")] [JsonProperty(@"colour")]
public string Colour; public string Colour;
} }

View File

@ -16,12 +16,14 @@ using osu.Game.Database;
using osu.Game.Graphics.Cursor; using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.Processing; using osu.Game.Graphics.Processing;
using osu.Game.IPC; using osu.Game.IPC;
using osu.Game.Online;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Online.API.Requests;
namespace osu.Game namespace osu.Game
{ {
public class OsuGameBase : BaseGame public class OsuGameBase : BaseGame, IOnlineComponent
{ {
internal OsuConfigManager Config; internal OsuConfigManager Config;
@ -43,7 +45,7 @@ namespace osu.Game
Dependencies.Cache(this); Dependencies.Cache(this);
Dependencies.Cache(Config); Dependencies.Cache(Config);
Dependencies.Cache(new BeatmapDatabase(Host.Storage, Host)); Dependencies.Cache(new BeatmapDatabase(Host.Storage, Host));
//this completely overrides the framework default. will need to change once we make a proper FontStore. //this completely overrides the framework default. will need to change once we make a proper FontStore.
Dependencies.Cache(Fonts = new FontStore { ScaleAdjust = 0.01f }, true); Dependencies.Cache(Fonts = new FontStore { ScaleAdjust = 0.01f }, true);
@ -74,12 +76,12 @@ namespace osu.Game
Token = Config.Get<string>(OsuConfig.Token) Token = Config.Get<string>(OsuConfig.Token)
}); });
API.OnStateChange += apiStateChanged; API.Register(this);
} }
private void apiStateChanged(APIState oldState, APIState newState) public void APIStateChanged(APIAccess api, APIState state)
{ {
switch (newState) switch (state)
{ {
case APIState.Online: case APIState.Online:
Config.Set(OsuConfig.Username, Config.Get<bool>(OsuConfig.SaveUsername) ? API.Username : string.Empty); Config.Set(OsuConfig.Username, Config.Get<bool>(OsuConfig.SaveUsername) ? API.Username : string.Empty);

View File

@ -46,7 +46,7 @@ namespace osu.Game.Overlays.Toolbar
break; break;
case APIState.Online: case APIState.Online:
Text = api.Username; Text = api.Username;
avatar.UserId = 2; avatar.UserId = api.LocalUser.Value.Id;
break; break;
} }
} }

View File

@ -70,6 +70,7 @@
<Compile Include="Modes\Score.cs" /> <Compile Include="Modes\Score.cs" />
<Compile Include="Modes\ScoreProcesssor.cs" /> <Compile Include="Modes\ScoreProcesssor.cs" />
<Compile Include="Online\API\IOnlineComponent.cs" /> <Compile Include="Online\API\IOnlineComponent.cs" />
<Compile Include="Online\API\Requests\GetUserRequest.cs" />
<Compile Include="Overlays\DragBar.cs" /> <Compile Include="Overlays\DragBar.cs" />
<Compile Include="Overlays\MusicController.cs" /> <Compile Include="Overlays\MusicController.cs" />
<Compile Include="Beatmaps\Beatmap.cs" /> <Compile Include="Beatmaps\Beatmap.cs" />