2020-12-18 23:15: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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2020-12-18 23:15:41 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2024-02-02 18:48:13 +08:00
|
|
|
using JetBrains.Annotations;
|
2020-12-18 23:15:41 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2021-12-10 16:22:24 +08:00
|
|
|
using osu.Framework.Development;
|
2020-12-18 23:15:41 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Online.API;
|
2020-12-25 12:38:11 +08:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-18 23:15:41 +08:00
|
|
|
|
2020-12-25 23:50:00 +08:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Components
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
2021-08-13 16:39:09 +08:00
|
|
|
public partial class RoomManager : Component, IRoomManager
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
2024-02-02 18:48:13 +08:00
|
|
|
[CanBeNull]
|
2020-12-18 23:15:41 +08:00
|
|
|
public event Action RoomsUpdated;
|
|
|
|
|
|
|
|
private readonly BindableList<Room> rooms = new BindableList<Room>();
|
|
|
|
|
|
|
|
public IBindableList<Room> Rooms => rooms;
|
|
|
|
|
2020-12-20 17:05:43 +08:00
|
|
|
protected IBindable<Room> JoinedRoom => joinedRoom;
|
|
|
|
private readonly Bindable<Room> joinedRoom = new Bindable<Room>();
|
2020-12-19 00:57:30 +08:00
|
|
|
|
2020-12-18 23:15:41 +08:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
public RoomManager()
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
PartRoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void CreateRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
|
|
|
|
{
|
|
|
|
room.Host.Value = api.LocalUser.Value;
|
|
|
|
|
|
|
|
var req = new CreateRoomRequest(room);
|
|
|
|
|
|
|
|
req.Success += result =>
|
|
|
|
{
|
2020-12-20 17:05:43 +08:00
|
|
|
joinedRoom.Value = room;
|
2020-12-18 23:15:41 +08:00
|
|
|
|
2021-08-16 12:04:06 +08:00
|
|
|
AddOrUpdateRoom(result);
|
|
|
|
room.CopyFrom(result); // Also copy back to the source model, since this is likely to have been stored elsewhere.
|
2020-12-18 23:15:41 +08:00
|
|
|
|
2021-08-17 08:40:25 +08:00
|
|
|
// The server may not contain all properties (such as password), so invoke success with the given room.
|
|
|
|
onSuccess?.Invoke(room);
|
2020-12-18 23:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
req.Failure += exception =>
|
|
|
|
{
|
2021-10-05 13:28:56 +08:00
|
|
|
onError?.Invoke(req.Response?.Error ?? exception.Message);
|
2020-12-18 23:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
api.Queue(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
private JoinRoomRequest currentJoinRoomRequest;
|
|
|
|
|
2021-07-10 15:08:12 +08:00
|
|
|
public virtual void JoinRoom(Room room, string password = null, Action<Room> onSuccess = null, Action<string> onError = null)
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
|
|
|
currentJoinRoomRequest?.Cancel();
|
2021-07-13 13:27:07 +08:00
|
|
|
currentJoinRoomRequest = new JoinRoomRequest(room, password);
|
2020-12-18 23:15:41 +08:00
|
|
|
|
|
|
|
currentJoinRoomRequest.Success += () =>
|
|
|
|
{
|
2020-12-20 17:05:43 +08:00
|
|
|
joinedRoom.Value = room;
|
2020-12-18 23:15:41 +08:00
|
|
|
onSuccess?.Invoke(room);
|
|
|
|
};
|
|
|
|
|
|
|
|
currentJoinRoomRequest.Failure += exception =>
|
|
|
|
{
|
2021-09-14 13:10:55 +08:00
|
|
|
if (exception is OperationCanceledException)
|
|
|
|
return;
|
|
|
|
|
|
|
|
onError?.Invoke(exception.Message);
|
2020-12-18 23:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
api.Queue(currentJoinRoomRequest);
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:02:04 +08:00
|
|
|
public virtual void PartRoom()
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
|
|
|
currentJoinRoomRequest?.Cancel();
|
|
|
|
|
2020-12-21 14:38:20 +08:00
|
|
|
if (JoinedRoom.Value == null)
|
2020-12-18 23:15:41 +08:00
|
|
|
return;
|
|
|
|
|
2023-11-21 13:41:03 +08:00
|
|
|
if (api.State.Value == APIState.Online)
|
|
|
|
api.Queue(new PartRoomRequest(joinedRoom.Value));
|
|
|
|
|
2020-12-20 17:05:43 +08:00
|
|
|
joinedRoom.Value = null;
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:29:40 +08:00
|
|
|
private readonly HashSet<long> ignoredRooms = new HashSet<long>();
|
2020-12-18 23:15:41 +08:00
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
public void AddOrUpdateRoom(Room room)
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
2021-12-10 16:22:24 +08:00
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
2021-08-13 16:39:09 +08:00
|
|
|
Debug.Assert(room.RoomID.Value != null);
|
|
|
|
|
|
|
|
if (ignoredRooms.Contains(room.RoomID.Value.Value))
|
2020-12-20 22:05:17 +08:00
|
|
|
return;
|
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
try
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
2021-08-16 12:04:06 +08:00
|
|
|
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == room.RoomID.Value);
|
|
|
|
if (existing == null)
|
|
|
|
rooms.Add(room);
|
|
|
|
else
|
|
|
|
existing.CopyFrom(room);
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
2021-08-13 16:39:09 +08:00
|
|
|
catch (Exception ex)
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
2021-08-13 16:39:09 +08:00
|
|
|
Logger.Error(ex, $"Failed to update room: {room.Name.Value}.");
|
2020-12-18 23:15:41 +08:00
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
ignoredRooms.Add(room.RoomID.Value.Value);
|
|
|
|
rooms.Remove(room);
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
notifyRoomsUpdated();
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
public void RemoveRoom(Room room)
|
|
|
|
{
|
2021-12-10 16:22:24 +08:00
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
rooms.Remove(room);
|
|
|
|
notifyRoomsUpdated();
|
|
|
|
}
|
2020-12-19 01:02:04 +08:00
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
public void ClearRooms()
|
2020-12-19 00:01:09 +08:00
|
|
|
{
|
2021-12-10 16:22:24 +08:00
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
|
|
|
|
2020-12-19 00:01:09 +08:00
|
|
|
rooms.Clear();
|
2021-08-13 16:39:09 +08:00
|
|
|
notifyRoomsUpdated();
|
2020-12-19 00:01:09 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 16:52:19 +08:00
|
|
|
private void notifyRoomsUpdated()
|
|
|
|
{
|
|
|
|
Scheduler.AddOnce(invokeRoomsUpdated);
|
|
|
|
|
|
|
|
void invokeRoomsUpdated() => RoomsUpdated?.Invoke();
|
|
|
|
}
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
|
|
|
}
|