2019-01-24 08:43:03 +00: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 09:19:50 +00:00
|
|
|
|
|
2020-04-11 08:47:51 +00:00
|
|
|
|
using System;
|
2019-03-22 02:55:35 +00:00
|
|
|
|
using System.Threading;
|
2019-11-29 11:03:14 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-02-21 10:04:31 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2019-03-22 02:55:35 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Users;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
2019-03-22 02:55:35 +00:00
|
|
|
|
public class DummyAPIAccess : Component, IAPIProvider
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
public Bindable<User> LocalUser { get; } = new Bindable<User>(new User
|
|
|
|
|
{
|
|
|
|
|
Username = @"Dummy",
|
2019-03-13 03:56:47 +00:00
|
|
|
|
Id = 1001,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-12-18 06:25:12 +00:00
|
|
|
|
public BindableList<User> Friends { get; } = new BindableList<User>();
|
2020-12-17 10:30:55 +00:00
|
|
|
|
|
2019-06-12 09:04:57 +00:00
|
|
|
|
public Bindable<UserActivity> Activity { get; } = new Bindable<UserActivity>();
|
|
|
|
|
|
2020-10-22 06:03:43 +00:00
|
|
|
|
public string AccessToken => "token";
|
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
|
public bool IsLoggedIn => State.Value == APIState.Online;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-03-13 03:56:47 +00:00
|
|
|
|
public string ProvidedUsername => LocalUser.Value.Username;
|
|
|
|
|
|
2020-12-24 09:11:40 +00:00
|
|
|
|
public string APIEndpointUrl => "http://localhost";
|
|
|
|
|
|
|
|
|
|
public string WebsiteRootUrl => "http://localhost";
|
2019-03-13 03:56:47 +00:00
|
|
|
|
|
2020-04-11 08:47:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provide handling logic for an arbitrary API request.
|
2021-03-23 09:08:32 +00: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 08:47:51 +00:00
|
|
|
|
/// </summary>
|
2021-03-23 09:08:32 +00:00
|
|
|
|
public Func<APIRequest, bool> HandleRequest;
|
2020-04-11 08:47:51 +00:00
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
|
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
|
2019-03-22 02:55:35 +00:00
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current connectivity state of the API.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IBindable<APIState> State => state;
|
2019-06-12 09:04:57 +00:00
|
|
|
|
|
|
|
|
|
public DummyAPIAccess()
|
|
|
|
|
{
|
|
|
|
|
LocalUser.BindValueChanged(u =>
|
|
|
|
|
{
|
|
|
|
|
u.OldValue?.Activity.UnbindFrom(Activity);
|
|
|
|
|
u.NewValue.Activity.BindTo(Activity);
|
|
|
|
|
}, true);
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
public virtual void Queue(APIRequest request)
|
|
|
|
|
{
|
2021-03-23 09:08:32 +00:00
|
|
|
|
if (HandleRequest?.Invoke(request) != true)
|
|
|
|
|
{
|
2021-03-23 08:31:28 +00:00
|
|
|
|
// this will fail due to not receiving an APIAccess, and trigger a failure on the request.
|
|
|
|
|
// this is intended - any request in testing that needs non-failures should use HandleRequest.
|
|
|
|
|
request.Perform(this);
|
2021-03-23 09:08:32 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 12:35:35 +00:00
|
|
|
|
public void Perform(APIRequest request) => HandleRequest?.Invoke(request);
|
2019-11-29 11:03:14 +00:00
|
|
|
|
|
2020-04-13 12:35:35 +00:00
|
|
|
|
public Task PerformAsync(APIRequest request)
|
|
|
|
|
{
|
|
|
|
|
HandleRequest?.Invoke(request);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2019-11-29 11:03:14 +00:00
|
|
|
|
|
2019-03-13 03:56:47 +00:00
|
|
|
|
public void Login(string username, string password)
|
|
|
|
|
{
|
|
|
|
|
LocalUser.Value = new User
|
|
|
|
|
{
|
2019-03-22 02:55:35 +00:00
|
|
|
|
Username = username,
|
2019-03-13 10:01:42 +00:00
|
|
|
|
Id = 1001,
|
2019-03-13 03:56:47 +00:00
|
|
|
|
};
|
2019-03-22 02:55:35 +00:00
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
|
state.Value = APIState.Online;
|
2019-03-13 03:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Logout()
|
|
|
|
|
{
|
|
|
|
|
LocalUser.Value = new GuestUser();
|
2020-10-22 05:19:12 +00:00
|
|
|
|
state.Value = APIState.Offline;
|
2019-03-13 03:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 07:31:00 +00:00
|
|
|
|
public IHubClientConnector GetHubConnector(string clientName, string endpoint) => null;
|
|
|
|
|
|
2019-03-22 02:55:35 +00:00
|
|
|
|
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-10-22 05:19:12 +00:00
|
|
|
|
|
|
|
|
|
public void SetState(APIState newState) => state.Value = newState;
|
2020-12-18 06:16:36 +00:00
|
|
|
|
|
|
|
|
|
IBindable<User> IAPIProvider.LocalUser => LocalUser;
|
|
|
|
|
IBindableList<User> IAPIProvider.Friends => Friends;
|
|
|
|
|
IBindable<UserActivity> IAPIProvider.Activity => Activity;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|