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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
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
|
|
|
using osu.Game.Rulesets;
|
|
|
|
|
2020-12-25 23:50:00 +08:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Components
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
|
|
|
public abstract class RoomManager : CompositeDrawable, IRoomManager
|
|
|
|
{
|
|
|
|
public event Action RoomsUpdated;
|
|
|
|
|
|
|
|
private readonly BindableList<Room> rooms = new BindableList<Room>();
|
|
|
|
|
2020-12-20 22:05:17 +08:00
|
|
|
public IBindable<bool> InitialRoomsReceived => initialRoomsReceived;
|
|
|
|
private readonly Bindable<bool> initialRoomsReceived = new Bindable<bool>();
|
2020-12-18 23:15:41 +08:00
|
|
|
|
|
|
|
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 RulesetStore rulesets { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private BeatmapManager beatmaps { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
|
|
protected RoomManager()
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
InternalChildren = CreatePollingComponents().Select(p =>
|
|
|
|
{
|
|
|
|
p.RoomsReceived = onRoomsReceived;
|
|
|
|
return p;
|
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
update(room, result);
|
|
|
|
addRoom(room);
|
|
|
|
|
|
|
|
RoomsUpdated?.Invoke();
|
|
|
|
onSuccess?.Invoke(room);
|
|
|
|
};
|
|
|
|
|
|
|
|
req.Failure += exception =>
|
|
|
|
{
|
2020-12-22 15:19:19 +08:00
|
|
|
onError?.Invoke(req.Result?.Error ?? exception.Message);
|
2020-12-18 23:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
api.Queue(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
private JoinRoomRequest currentJoinRoomRequest;
|
|
|
|
|
|
|
|
public virtual void JoinRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
|
|
|
|
{
|
|
|
|
currentJoinRoomRequest?.Cancel();
|
|
|
|
currentJoinRoomRequest = new JoinRoomRequest(room);
|
|
|
|
|
|
|
|
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 =>
|
|
|
|
{
|
|
|
|
if (!(exception is OperationCanceledException))
|
|
|
|
Logger.Log($"Failed to join room: {exception}", level: LogLevel.Important);
|
|
|
|
onError?.Invoke(exception.ToString());
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-12-20 17:05:43 +08:00
|
|
|
api.Queue(new PartRoomRequest(joinedRoom.Value));
|
|
|
|
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
|
|
|
|
|
|
|
private void onRoomsReceived(List<Room> received)
|
|
|
|
{
|
2020-12-20 22:05:17 +08:00
|
|
|
if (received == null)
|
|
|
|
{
|
2020-12-20 22:10:45 +08:00
|
|
|
ClearRooms();
|
2020-12-20 22:05:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:15:41 +08:00
|
|
|
// Remove past matches
|
|
|
|
foreach (var r in rooms.ToList())
|
|
|
|
{
|
|
|
|
if (received.All(e => e.RoomID.Value != r.RoomID.Value))
|
|
|
|
rooms.Remove(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < received.Count; i++)
|
|
|
|
{
|
|
|
|
var room = received[i];
|
|
|
|
|
|
|
|
Debug.Assert(room.RoomID.Value != null);
|
|
|
|
|
|
|
|
if (ignoredRooms.Contains(room.RoomID.Value.Value))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
room.Position.Value = i;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
update(room, room);
|
|
|
|
addRoom(room);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.Error(ex, $"Failed to update room: {room.Name.Value}.");
|
|
|
|
|
|
|
|
ignoredRooms.Add(room.RoomID.Value.Value);
|
|
|
|
rooms.Remove(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomsUpdated?.Invoke();
|
2020-12-20 22:05:17 +08:00
|
|
|
initialRoomsReceived.Value = true;
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
|
|
|
|
2020-12-19 01:02:04 +08:00
|
|
|
protected void RemoveRoom(Room room) => rooms.Remove(room);
|
|
|
|
|
2020-12-19 00:01:09 +08:00
|
|
|
protected void ClearRooms()
|
|
|
|
{
|
|
|
|
rooms.Clear();
|
2020-12-20 22:10:45 +08:00
|
|
|
initialRoomsReceived.Value = false;
|
2020-12-19 00:01:09 +08:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:15:41 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Updates a local <see cref="Room"/> with a remote copy.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="local">The local <see cref="Room"/> to update.</param>
|
|
|
|
/// <param name="remote">The remote <see cref="Room"/> to update with.</param>
|
|
|
|
private void update(Room local, Room remote)
|
|
|
|
{
|
|
|
|
foreach (var pi in remote.Playlist)
|
|
|
|
pi.MapObjects(beatmaps, rulesets);
|
|
|
|
|
|
|
|
local.CopyFrom(remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a <see cref="Room"/> to the list of available rooms.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="room">The <see cref="Room"/> to add.</param>
|
|
|
|
private void addRoom(Room room)
|
|
|
|
{
|
|
|
|
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == room.RoomID.Value);
|
|
|
|
if (existing == null)
|
|
|
|
rooms.Add(room);
|
|
|
|
else
|
|
|
|
existing.CopyFrom(room);
|
|
|
|
}
|
|
|
|
|
2020-12-20 16:36:23 +08:00
|
|
|
protected abstract IEnumerable<RoomPollingComponent> CreatePollingComponents();
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
|
|
|
}
|