2020-12-18 15:18:41 +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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-12-18 15:18:41 +00:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using osu.Framework.Allocation;
|
2020-12-23 16:08:28 +00:00
|
|
|
using osu.Framework.Extensions.ExceptionExtensions;
|
2020-12-18 15:18:41 +00:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Online.Multiplayer;
|
2020-12-25 04:38:11 +00:00
|
|
|
using osu.Game.Online.Rooms;
|
|
|
|
using osu.Game.Online.Rooms.RoomStatuses;
|
2020-12-25 15:50:00 +00:00
|
|
|
using osu.Game.Screens.OnlinePlay.Components;
|
2020-12-18 15:18:41 +00:00
|
|
|
|
2020-12-25 15:50:00 +00:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer
|
2020-12-18 15:18:41 +00:00
|
|
|
{
|
2020-12-25 04:38:11 +00:00
|
|
|
public partial class MultiplayerRoomManager : RoomManager
|
2020-12-18 15:18:41 +00:00
|
|
|
{
|
|
|
|
[Resolved]
|
2021-05-20 06:39:45 +00:00
|
|
|
private MultiplayerClient multiplayerClient { get; set; }
|
2020-12-18 15:18:41 +00:00
|
|
|
|
|
|
|
public override void CreateRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
|
2021-08-17 00:40:25 +00:00
|
|
|
=> base.CreateRoom(room, r => joinMultiplayerRoom(r, r.Password.Value, onSuccess, onError), onError);
|
2020-12-18 15:18:41 +00:00
|
|
|
|
2021-07-10 07:08:12 +00:00
|
|
|
public override void JoinRoom(Room room, string password = null, Action<Room> onSuccess = null, Action<string> onError = null)
|
2020-12-23 02:52:10 +00:00
|
|
|
{
|
2020-12-23 07:17:55 +00:00
|
|
|
if (!multiplayerClient.IsConnected.Value)
|
|
|
|
{
|
|
|
|
onError?.Invoke("Not currently connected to the multiplayer server.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-23 04:57:48 +00:00
|
|
|
// this is done here as a pre-check to avoid clicking on already closed rooms in the lounge from triggering a server join.
|
|
|
|
// should probably be done at a higher level, but due to the current structure of things this is the easiest place for now.
|
2020-12-23 02:52:10 +00:00
|
|
|
if (room.Status.Value is RoomStatusEnded)
|
|
|
|
{
|
|
|
|
onError?.Invoke("Cannot join an ended room.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-10 07:08:12 +00:00
|
|
|
base.JoinRoom(room, password, r => joinMultiplayerRoom(r, password, onSuccess, onError), onError);
|
2020-12-23 02:52:10 +00:00
|
|
|
}
|
2020-12-18 15:18:41 +00:00
|
|
|
|
2020-12-18 17:02:04 +00:00
|
|
|
public override void PartRoom()
|
|
|
|
{
|
2020-12-21 06:38:20 +00:00
|
|
|
if (JoinedRoom.Value == null)
|
2020-12-18 17:02:04 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
base.PartRoom();
|
2021-01-29 07:32:28 +00:00
|
|
|
multiplayerClient.LeaveRoom();
|
2020-12-18 17:02:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 07:08:12 +00:00
|
|
|
private void joinMultiplayerRoom(Room room, string password, Action<Room> onSuccess = null, Action<string> onError = null)
|
2020-12-18 15:18:41 +00:00
|
|
|
{
|
|
|
|
Debug.Assert(room.RoomID.Value != null);
|
|
|
|
|
2021-07-10 07:08:12 +00:00
|
|
|
multiplayerClient.JoinRoom(room, password).ContinueWith(t =>
|
2020-12-18 15:18:41 +00:00
|
|
|
{
|
2020-12-23 07:56:51 +00:00
|
|
|
if (t.IsCompletedSuccessfully)
|
|
|
|
Schedule(() => onSuccess?.Invoke(room));
|
2021-01-30 22:39:01 +00:00
|
|
|
else if (t.IsFaulted)
|
2020-12-23 07:56:51 +00:00
|
|
|
{
|
2020-12-23 16:08:28 +00:00
|
|
|
const string message = "Failed to join multiplayer room.";
|
|
|
|
|
2020-12-23 07:56:51 +00:00
|
|
|
if (t.Exception != null)
|
2020-12-23 16:08:28 +00:00
|
|
|
Logger.Error(t.Exception, message);
|
2020-12-23 07:56:51 +00:00
|
|
|
|
|
|
|
PartRoom();
|
2020-12-23 16:08:28 +00:00
|
|
|
Schedule(() => onError?.Invoke(t.Exception?.AsSingular().Message ?? message));
|
2020-12-23 07:56:51 +00:00
|
|
|
}
|
|
|
|
});
|
2020-12-18 15:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|