2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-04-11 16:47:51 +08:00
|
|
|
|
using System;
|
2019-03-22 10:55:35 +08:00
|
|
|
|
using System.Threading;
|
2019-11-29 19:03:14 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2019-03-22 10:55:35 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-11-04 17:02:44 +08:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Users;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
2019-03-22 10:55:35 +08:00
|
|
|
|
public class DummyAPIAccess : Component, IAPIProvider
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
|
public Bindable<APIUser> LocalUser { get; } = new Bindable<APIUser>(new APIUser
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
Username = @"Dummy",
|
2019-03-13 11:56:47 +08:00
|
|
|
|
Id = 1001,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
|
public BindableList<APIUser> Friends { get; } = new BindableList<APIUser>();
|
2020-12-17 18:30:55 +08:00
|
|
|
|
|
2019-06-12 17:04:57 +08:00
|
|
|
|
public Bindable<UserActivity> Activity { get; } = new Bindable<UserActivity>();
|
|
|
|
|
|
2020-10-22 14:03:43 +08:00
|
|
|
|
public string AccessToken => "token";
|
|
|
|
|
|
2020-10-22 13:19:12 +08:00
|
|
|
|
public bool IsLoggedIn => State.Value == APIState.Online;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-03-13 11:56:47 +08:00
|
|
|
|
public string ProvidedUsername => LocalUser.Value.Username;
|
|
|
|
|
|
2020-12-24 17:11:40 +08:00
|
|
|
|
public string APIEndpointUrl => "http://localhost";
|
|
|
|
|
|
|
|
|
|
public string WebsiteRootUrl => "http://localhost";
|
2019-03-13 11:56:47 +08:00
|
|
|
|
|
2022-02-17 17:33:27 +08:00
|
|
|
|
public int APIVersion => int.Parse(DateTime.Now.ToString("yyyyMMdd"));
|
|
|
|
|
|
2021-10-04 14:40:24 +08:00
|
|
|
|
public Exception LastLoginError { get; private set; }
|
|
|
|
|
|
2020-04-11 16:47:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provide handling logic for an arbitrary API request.
|
2021-03-23 17:08:32 +08:00
|
|
|
|
/// Should return true is a request was handled. If null or false return, the request will be failed with a <see cref="NotSupportedException"/>.
|
2020-04-11 16:47:51 +08:00
|
|
|
|
/// </summary>
|
2021-03-23 17:08:32 +08:00
|
|
|
|
public Func<APIRequest, bool> HandleRequest;
|
2020-04-11 16:47:51 +08:00
|
|
|
|
|
2020-10-22 13:19:12 +08:00
|
|
|
|
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
|
2019-03-22 10:55:35 +08:00
|
|
|
|
|
2021-10-04 14:40:24 +08:00
|
|
|
|
private bool shouldFailNextLogin;
|
|
|
|
|
|
2020-10-22 13:19:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current connectivity state of the API.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IBindable<APIState> State => state;
|
2019-06-12 17:04:57 +08:00
|
|
|
|
|
|
|
|
|
public DummyAPIAccess()
|
|
|
|
|
{
|
|
|
|
|
LocalUser.BindValueChanged(u =>
|
|
|
|
|
{
|
|
|
|
|
u.OldValue?.Activity.UnbindFrom(Activity);
|
|
|
|
|
u.NewValue.Activity.BindTo(Activity);
|
|
|
|
|
}, true);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
public virtual void Queue(APIRequest request)
|
|
|
|
|
{
|
2022-05-30 15:32:44 +08:00
|
|
|
|
Schedule(() =>
|
2021-03-23 17:08:32 +08:00
|
|
|
|
{
|
2022-05-30 15:32:44 +08:00
|
|
|
|
if (HandleRequest?.Invoke(request) != true)
|
|
|
|
|
{
|
|
|
|
|
request.Fail(new InvalidOperationException($@"{nameof(DummyAPIAccess)} cannot process this request."));
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 20:35:35 +08:00
|
|
|
|
public void Perform(APIRequest request) => HandleRequest?.Invoke(request);
|
2019-11-29 19:03:14 +08:00
|
|
|
|
|
2020-04-13 20:35:35 +08:00
|
|
|
|
public Task PerformAsync(APIRequest request)
|
|
|
|
|
{
|
|
|
|
|
HandleRequest?.Invoke(request);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2019-11-29 19:03:14 +08:00
|
|
|
|
|
2019-03-13 11:56:47 +08:00
|
|
|
|
public void Login(string username, string password)
|
|
|
|
|
{
|
2021-10-04 14:40:24 +08:00
|
|
|
|
state.Value = APIState.Connecting;
|
|
|
|
|
|
|
|
|
|
if (shouldFailNextLogin)
|
|
|
|
|
{
|
|
|
|
|
LastLoginError = new APIException("Not powerful enough to login.", new ArgumentException(nameof(shouldFailNextLogin)));
|
|
|
|
|
|
|
|
|
|
state.Value = APIState.Offline;
|
|
|
|
|
shouldFailNextLogin = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LastLoginError = null;
|
2021-11-04 17:02:44 +08:00
|
|
|
|
LocalUser.Value = new APIUser
|
2019-03-13 11:56:47 +08:00
|
|
|
|
{
|
2019-03-22 10:55:35 +08:00
|
|
|
|
Username = username,
|
2019-03-13 18:01:42 +08:00
|
|
|
|
Id = 1001,
|
2019-03-13 11:56:47 +08:00
|
|
|
|
};
|
2019-03-22 10:55:35 +08:00
|
|
|
|
|
2020-10-22 13:19:12 +08:00
|
|
|
|
state.Value = APIState.Online;
|
2019-03-13 11:56:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Logout()
|
|
|
|
|
{
|
|
|
|
|
LocalUser.Value = new GuestUser();
|
2020-10-22 13:19:12 +08:00
|
|
|
|
state.Value = APIState.Offline;
|
2019-03-13 11:56:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 13:44:51 +08:00
|
|
|
|
public IHubClientConnector GetHubConnector(string clientName, string endpoint, bool preferMessagePack) => null;
|
2021-02-15 15:31:00 +08:00
|
|
|
|
|
2019-03-22 10:55:35 +08:00
|
|
|
|
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-10-22 13:19:12 +08:00
|
|
|
|
|
|
|
|
|
public void SetState(APIState newState) => state.Value = newState;
|
2020-12-18 14:16:36 +08:00
|
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
|
IBindable<APIUser> IAPIProvider.LocalUser => LocalUser;
|
|
|
|
|
IBindableList<APIUser> IAPIProvider.Friends => Friends;
|
2020-12-18 14:16:36 +08:00
|
|
|
|
IBindable<UserActivity> IAPIProvider.Activity => Activity;
|
2021-10-04 14:40:24 +08:00
|
|
|
|
|
|
|
|
|
public void FailNextLogin() => shouldFailNextLogin = true;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|