1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 03:52:57 +08:00
osu-lazer/osu.Game/Online/API/DummyAPIAccess.cs

131 lines
4.1 KiB
C#
Raw Normal View History

// 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
2022-06-17 15:37:17 +08:00
#nullable disable
2020-04-11 16:47:51 +08:00
using System;
using System.Threading;
using System.Threading.Tasks;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Online.API.Requests.Responses;
2018-04-13 17:19:50 +08:00
using osu.Game.Users;
namespace osu.Game.Online.API
{
public class DummyAPIAccess : Component, IAPIProvider
2018-04-13 17:19:50 +08:00
{
public Bindable<APIUser> LocalUser { get; } = new Bindable<APIUser>(new APIUser
2018-04-13 17:19:50 +08:00
{
Username = @"Dummy",
Id = 1001,
2018-04-13 17:19:50 +08:00
});
public BindableList<APIUser> Friends { get; } = new BindableList<APIUser>();
2020-12-17 18:30:55 +08:00
public Bindable<UserActivity> Activity { get; } = new Bindable<UserActivity>();
public string AccessToken => "token";
public bool IsLoggedIn => State.Value == APIState.Online;
2018-04-13 17:19:50 +08:00
public string ProvidedUsername => LocalUser.Value.Username;
public string APIEndpointUrl => "http://localhost";
public string WebsiteRootUrl => "http://localhost";
2022-02-17 17:33:27 +08:00
public int APIVersion => int.Parse(DateTime.Now.ToString("yyyyMMdd"));
public Exception LastLoginError { get; private set; }
2020-04-11 16:47:51 +08:00
/// <summary>
/// Provide handling logic for an arbitrary API request.
/// 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>
public Func<APIRequest, bool> HandleRequest;
2020-04-11 16:47:51 +08:00
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
private bool shouldFailNextLogin;
/// <summary>
/// The current connectivity state of the API.
/// </summary>
public IBindable<APIState> State => state;
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)
{
Schedule(() =>
{
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);
2020-04-13 20:35:35 +08:00
public Task PerformAsync(APIRequest request)
{
HandleRequest?.Invoke(request);
return Task.CompletedTask;
}
public void Login(string username, string password)
{
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;
LocalUser.Value = new APIUser
{
Username = username,
Id = 1001,
};
state.Value = APIState.Online;
}
public void Logout()
{
LocalUser.Value = new GuestUser();
state.Value = APIState.Offline;
}
public IHubClientConnector GetHubConnector(string clientName, string endpoint, bool preferMessagePack) => null;
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
{
Thread.Sleep(200);
return null;
}
public void SetState(APIState newState) => state.Value = newState;
IBindable<APIUser> IAPIProvider.LocalUser => LocalUser;
IBindableList<APIUser> IAPIProvider.Friends => Friends;
IBindable<UserActivity> IAPIProvider.Activity => Activity;
public void FailNextLogin() => shouldFailNextLogin = true;
2018-04-13 17:19:50 +08:00
}
}