1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 14:07:24 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerRoomManager.cs

169 lines
6.1 KiB
C#
Raw Normal View History

2020-12-18 23:18:41 +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.
using System;
2020-12-20 22:10:45 +08:00
using System.Collections.Generic;
2020-12-18 23:18:41 +08:00
using System.Diagnostics;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ExceptionExtensions;
2020-12-18 23:18:41 +08:00
using osu.Framework.Logging;
using osu.Game.Online.Multiplayer;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms;
using osu.Game.Online.Rooms.RoomStatuses;
using osu.Game.Screens.OnlinePlay.Components;
2020-12-18 23:18:41 +08:00
namespace osu.Game.Screens.OnlinePlay.Multiplayer
2020-12-18 23:18:41 +08:00
{
2020-12-25 12:38:11 +08:00
public class MultiplayerRoomManager : RoomManager
2020-12-18 23:18:41 +08:00
{
[Resolved]
private StatefulMultiplayerClient multiplayerClient { get; set; }
public readonly Bindable<double> TimeBetweenListingPolls = new Bindable<double>();
public readonly Bindable<double> TimeBetweenSelectionPolls = new Bindable<double>();
private readonly IBindable<bool> isConnected = new Bindable<bool>();
private readonly Bindable<bool> allowPolling = new Bindable<bool>();
2020-12-19 01:02:04 +08:00
private ListingPollingComponent listingPollingComponent;
protected override void LoadComplete()
{
base.LoadComplete();
isConnected.BindTo(multiplayerClient.IsConnected);
isConnected.BindValueChanged(_ => Scheduler.AddOnce(updatePolling));
JoinedRoom.BindValueChanged(_ => Scheduler.AddOnce(updatePolling), true);
}
2020-12-18 23:18:41 +08:00
public override void CreateRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
2020-12-22 14:27:49 +08:00
=> base.CreateRoom(room, r => joinMultiplayerRoom(r, onSuccess, onError), onError);
2020-12-18 23:18:41 +08:00
public override void JoinRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
{
if (!multiplayerClient.IsConnected.Value)
{
onError?.Invoke("Not currently connected to the multiplayer server.");
return;
}
// 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.
if (room.Status.Value is RoomStatusEnded)
{
onError?.Invoke("Cannot join an ended room.");
return;
}
base.JoinRoom(room, r => joinMultiplayerRoom(r, onSuccess, onError), onError);
}
2020-12-18 23:18:41 +08:00
2020-12-19 01:02:04 +08:00
public override void PartRoom()
{
2020-12-21 14:38:20 +08:00
if (JoinedRoom.Value == null)
2020-12-19 01:02:04 +08:00
return;
var joinedRoom = JoinedRoom.Value;
2020-12-19 01:02:04 +08:00
base.PartRoom();
multiplayerClient.LeaveRoom();
2020-12-19 01:02:04 +08:00
// Todo: This is not the way to do this. Basically when we're the only participant and the room closes, there's no way to know if this is actually the case.
// This is delayed one frame because upon exiting the match subscreen, multiplayer updates the polling rate and messes with polling.
2020-12-20 23:39:31 +08:00
Schedule(() =>
{
RemoveRoom(joinedRoom);
listingPollingComponent.PollImmediately();
});
2020-12-19 01:02:04 +08:00
}
2020-12-22 14:27:49 +08:00
private void joinMultiplayerRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
2020-12-18 23:18:41 +08:00
{
Debug.Assert(room.RoomID.Value != null);
multiplayerClient.JoinRoom(room).ContinueWith(t =>
2020-12-18 23:18:41 +08:00
{
if (t.IsCompletedSuccessfully)
Schedule(() => onSuccess?.Invoke(room));
else if (t.IsFaulted)
{
const string message = "Failed to join multiplayer room.";
if (t.Exception != null)
Logger.Error(t.Exception, message);
PartRoom();
Schedule(() => onError?.Invoke(t.Exception?.AsSingular().Message ?? message));
}
});
2020-12-18 23:18:41 +08:00
}
private void updatePolling()
{
if (!isConnected.Value)
ClearRooms();
// Don't poll when not connected or when a room has been joined.
allowPolling.Value = isConnected.Value && JoinedRoom.Value == null;
}
2020-12-20 22:10:45 +08:00
protected override IEnumerable<RoomPollingComponent> CreatePollingComponents() => new RoomPollingComponent[]
2020-12-18 23:18:41 +08:00
{
2020-12-25 12:38:11 +08:00
listingPollingComponent = new MultiplayerListingPollingComponent
{
TimeBetweenPolls = { BindTarget = TimeBetweenListingPolls },
AllowPolling = { BindTarget = allowPolling }
},
new MultiplayerSelectionPollingComponent
{
TimeBetweenPolls = { BindTarget = TimeBetweenSelectionPolls },
AllowPolling = { BindTarget = allowPolling }
}
2020-12-18 23:18:41 +08:00
};
2020-12-25 12:38:11 +08:00
private class MultiplayerListingPollingComponent : ListingPollingComponent
{
public readonly IBindable<bool> AllowPolling = new Bindable<bool>();
protected override void LoadComplete()
{
base.LoadComplete();
AllowPolling.BindValueChanged(allowPolling =>
{
if (!allowPolling.NewValue)
return;
if (IsLoaded)
PollImmediately();
});
}
protected override Task Poll() => !AllowPolling.Value ? Task.CompletedTask : base.Poll();
}
private class MultiplayerSelectionPollingComponent : SelectionPollingComponent
{
public readonly IBindable<bool> AllowPolling = new Bindable<bool>();
protected override void LoadComplete()
{
base.LoadComplete();
AllowPolling.BindValueChanged(allowPolling =>
{
if (!allowPolling.NewValue)
return;
if (IsLoaded)
PollImmediately();
});
}
protected override Task Poll() => !AllowPolling.Value ? Task.CompletedTask : base.Poll();
}
2020-12-18 23:18:41 +08:00
}
}