2020-12-20 14:26:31 +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.
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
2021-02-01 08:57:32 +00:00
|
|
|
using System.Collections.Generic;
|
2020-12-20 14:26:31 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
2021-02-09 05:02:32 +00:00
|
|
|
using osu.Framework.Allocation;
|
2020-12-20 14:26:31 +00:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Game.Online.API;
|
2021-01-03 01:32:50 +00:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-20 14:26:31 +00:00
|
|
|
|
2020-12-25 04:38:11 +00:00
|
|
|
namespace osu.Game.Online.Multiplayer
|
2020-12-20 14:26:31 +00:00
|
|
|
{
|
2020-12-25 04:38:11 +00:00
|
|
|
public class MultiplayerClient : StatefulMultiplayerClient
|
2020-12-20 14:26:31 +00:00
|
|
|
{
|
2021-02-09 05:02:32 +00:00
|
|
|
private readonly string endpoint;
|
|
|
|
private HubClientConnector? connector;
|
2020-12-20 14:26:31 +00:00
|
|
|
|
2021-02-09 05:02:32 +00:00
|
|
|
public override IBindable<bool> IsConnected { get; } = new BindableBool();
|
2020-12-20 14:26:31 +00:00
|
|
|
|
2021-02-09 05:02:32 +00:00
|
|
|
private HubConnection? connection => connector?.CurrentConnection;
|
2020-12-24 08:58:38 +00:00
|
|
|
|
2020-12-25 04:38:11 +00:00
|
|
|
public MultiplayerClient(EndpointConfiguration endpoints)
|
2020-12-24 08:58:38 +00:00
|
|
|
{
|
2021-02-09 05:02:32 +00:00
|
|
|
endpoint = endpoints.MultiplayerEndpointUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(IAPIProvider api)
|
|
|
|
{
|
|
|
|
connector = new HubClientConnector(nameof(MultiplayerClient), endpoint, api)
|
2020-12-20 14:26:31 +00:00
|
|
|
{
|
2021-02-11 07:49:16 +00:00
|
|
|
ConfigureConnection = connection =>
|
2020-12-20 14:26:31 +00:00
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
// this is kind of SILLY
|
|
|
|
// https://github.com/dotnet/aspnetcore/issues/15198
|
2021-02-09 05:02:32 +00:00
|
|
|
connection.On<MultiplayerRoomState>(nameof(IMultiplayerClient.RoomStateChanged), ((IMultiplayerClient)this).RoomStateChanged);
|
|
|
|
connection.On<MultiplayerRoomUser>(nameof(IMultiplayerClient.UserJoined), ((IMultiplayerClient)this).UserJoined);
|
|
|
|
connection.On<MultiplayerRoomUser>(nameof(IMultiplayerClient.UserLeft), ((IMultiplayerClient)this).UserLeft);
|
|
|
|
connection.On<int>(nameof(IMultiplayerClient.HostChanged), ((IMultiplayerClient)this).HostChanged);
|
|
|
|
connection.On<MultiplayerRoomSettings>(nameof(IMultiplayerClient.SettingsChanged), ((IMultiplayerClient)this).SettingsChanged);
|
|
|
|
connection.On<int, MultiplayerUserState>(nameof(IMultiplayerClient.UserStateChanged), ((IMultiplayerClient)this).UserStateChanged);
|
|
|
|
connection.On(nameof(IMultiplayerClient.LoadRequested), ((IMultiplayerClient)this).LoadRequested);
|
|
|
|
connection.On(nameof(IMultiplayerClient.MatchStarted), ((IMultiplayerClient)this).MatchStarted);
|
|
|
|
connection.On(nameof(IMultiplayerClient.ResultsReady), ((IMultiplayerClient)this).ResultsReady);
|
|
|
|
connection.On<int, IEnumerable<APIMod>>(nameof(IMultiplayerClient.UserModsChanged), ((IMultiplayerClient)this).UserModsChanged);
|
2021-02-10 23:14:22 +00:00
|
|
|
connection.On<int, BeatmapAvailability>(nameof(IMultiplayerClient.UserBeatmapAvailabilityChanged), ((IMultiplayerClient)this).UserBeatmapAvailabilityChanged);
|
2021-02-08 23:01:52 +00:00
|
|
|
},
|
|
|
|
};
|
2021-02-09 05:02:32 +00:00
|
|
|
|
|
|
|
IsConnected.BindTo(connector.IsConnected);
|
2020-12-20 14:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override Task<MultiplayerRoom> JoinRoom(long roomId)
|
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2020-12-23 16:01:01 +00:00
|
|
|
return Task.FromCanceled<MultiplayerRoom>(new CancellationToken(true));
|
2020-12-20 14:26:31 +00:00
|
|
|
|
|
|
|
return connection.InvokeAsync<MultiplayerRoom>(nameof(IMultiplayerServer.JoinRoom), roomId);
|
|
|
|
}
|
|
|
|
|
2021-01-25 11:41:51 +00:00
|
|
|
protected override Task LeaveRoomInternal()
|
2020-12-20 14:26:31 +00:00
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2021-01-25 11:41:51 +00:00
|
|
|
return Task.FromCanceled(new CancellationToken(true));
|
2020-12-20 14:26:31 +00:00
|
|
|
|
2021-01-25 11:41:51 +00:00
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.LeaveRoom));
|
2020-12-20 14:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override Task TransferHost(int userId)
|
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2020-12-20 14:26:31 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.TransferHost), userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task ChangeSettings(MultiplayerRoomSettings settings)
|
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2020-12-20 14:26:31 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeSettings), settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task ChangeState(MultiplayerUserState newState)
|
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2020-12-20 14:26:31 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeState), newState);
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:32:50 +00:00
|
|
|
public override Task ChangeBeatmapAvailability(BeatmapAvailability newBeatmapAvailability)
|
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2021-01-03 01:32:50 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeBeatmapAvailability), newBeatmapAvailability);
|
|
|
|
}
|
|
|
|
|
2021-02-01 08:57:32 +00:00
|
|
|
public override Task ChangeUserMods(IEnumerable<APIMod> newMods)
|
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2021-02-01 08:57:32 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeUserMods), newMods);
|
|
|
|
}
|
|
|
|
|
2020-12-20 14:26:31 +00:00
|
|
|
public override Task StartMatch()
|
|
|
|
{
|
2021-02-08 23:01:52 +00:00
|
|
|
if (!IsConnected.Value)
|
2020-12-20 14:26:31 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
return connection.InvokeAsync(nameof(IMultiplayerServer.StartMatch));
|
|
|
|
}
|
2021-02-09 05:02:32 +00:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
connector?.Dispose();
|
|
|
|
}
|
2020-12-20 14:26:31 +00:00
|
|
|
}
|
|
|
|
}
|