2019-01-24 16:43:03 +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.
|
2018-12-11 18:07:40 +08:00
|
|
|
|
2018-12-20 19:58:34 +08:00
|
|
|
using System;
|
2019-02-05 14:38:19 +08:00
|
|
|
using System.Collections.Generic;
|
2018-12-12 18:04:11 +08:00
|
|
|
using System.Linq;
|
2018-12-11 18:07:40 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Configuration;
|
2019-02-05 14:38:19 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-12-12 18:04:11 +08:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Beatmaps;
|
2018-12-11 18:07:40 +08:00
|
|
|
using osu.Game.Online.API;
|
2018-12-22 11:50:37 +08:00
|
|
|
using osu.Game.Online.API.Requests;
|
2018-12-11 18:07:40 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2018-12-12 18:04:11 +08:00
|
|
|
using osu.Game.Rulesets;
|
2018-12-11 18:07:40 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Multi
|
|
|
|
{
|
2019-02-05 14:38:19 +08:00
|
|
|
public class RoomManager : Container, IRoomManager
|
2018-12-11 18:07:40 +08:00
|
|
|
{
|
2018-12-28 00:45:19 +08:00
|
|
|
public event Action RoomsUpdated;
|
|
|
|
|
2019-01-07 17:50:27 +08:00
|
|
|
private readonly BindableList<Room> rooms = new BindableList<Room>();
|
|
|
|
public IBindableList<Room> Rooms => rooms;
|
2018-12-11 18:07:40 +08:00
|
|
|
|
2019-02-05 14:56:18 +08:00
|
|
|
public readonly Bindable<Room> CurrentRoom = new Bindable<Room>();
|
2018-12-11 18:07:40 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private APIAccess api { get; set; }
|
|
|
|
|
2018-12-12 18:04:11 +08:00
|
|
|
[Resolved]
|
|
|
|
private RulesetStore rulesets { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private BeatmapManager beatmaps { get; set; }
|
|
|
|
|
2018-12-26 19:12:51 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
PartRoom();
|
|
|
|
}
|
|
|
|
|
2018-12-26 20:10:31 +08:00
|
|
|
public void CreateRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
|
2018-12-11 18:07:40 +08:00
|
|
|
{
|
|
|
|
room.Host.Value = api.LocalUser;
|
|
|
|
|
2018-12-12 18:04:11 +08:00
|
|
|
var req = new CreateRoomRequest(room);
|
2018-12-26 20:32:12 +08:00
|
|
|
|
2018-12-26 19:32:01 +08:00
|
|
|
req.Success += result =>
|
|
|
|
{
|
|
|
|
update(room, result);
|
|
|
|
addRoom(room);
|
2018-12-26 19:45:56 +08:00
|
|
|
|
2018-12-28 00:45:19 +08:00
|
|
|
RoomsUpdated?.Invoke();
|
|
|
|
|
2018-12-26 20:10:31 +08:00
|
|
|
onSuccess?.Invoke(room);
|
2018-12-26 19:32:01 +08:00
|
|
|
};
|
|
|
|
|
2018-12-26 17:38:58 +08:00
|
|
|
req.Failure += exception =>
|
|
|
|
{
|
|
|
|
if (req.Result != null)
|
|
|
|
onError?.Invoke(req.Result.Error);
|
|
|
|
else
|
|
|
|
Logger.Log($"Failed to create the room: {exception}", level: LogLevel.Important);
|
|
|
|
};
|
2018-12-20 19:58:34 +08:00
|
|
|
|
2018-12-12 18:04:11 +08:00
|
|
|
api.Queue(req);
|
|
|
|
}
|
|
|
|
|
2018-12-20 19:58:34 +08:00
|
|
|
private JoinRoomRequest currentJoinRoomRequest;
|
|
|
|
|
2018-12-26 20:10:31 +08:00
|
|
|
public void JoinRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
|
2018-12-20 19:58:34 +08:00
|
|
|
{
|
|
|
|
currentJoinRoomRequest?.Cancel();
|
|
|
|
currentJoinRoomRequest = null;
|
|
|
|
|
|
|
|
currentJoinRoomRequest = new JoinRoomRequest(room, api.LocalUser.Value);
|
|
|
|
currentJoinRoomRequest.Success += () =>
|
|
|
|
{
|
2019-02-05 14:56:18 +08:00
|
|
|
CurrentRoom.Value = room;
|
2018-12-26 20:10:31 +08:00
|
|
|
onSuccess?.Invoke(room);
|
2018-12-20 19:58:34 +08:00
|
|
|
};
|
|
|
|
|
2018-12-26 20:10:31 +08:00
|
|
|
currentJoinRoomRequest.Failure += exception =>
|
|
|
|
{
|
|
|
|
Logger.Log($"Failed to join room: {exception}", level: LogLevel.Important);
|
|
|
|
onError?.Invoke(exception.ToString());
|
|
|
|
};
|
2018-12-20 19:58:34 +08:00
|
|
|
|
|
|
|
api.Queue(currentJoinRoomRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PartRoom()
|
|
|
|
{
|
2019-02-05 14:56:18 +08:00
|
|
|
if (CurrentRoom.Value == null)
|
2018-12-20 19:58:34 +08:00
|
|
|
return;
|
|
|
|
|
2019-02-05 14:56:18 +08:00
|
|
|
api.Queue(new PartRoomRequest(CurrentRoom.Value, api.LocalUser.Value));
|
|
|
|
CurrentRoom.Value = null;
|
2018-12-20 19:58:34 +08:00
|
|
|
}
|
|
|
|
|
2019-02-05 14:38:19 +08:00
|
|
|
public void UpdateRooms(List<Room> newRooms)
|
2018-12-19 17:02:05 +08:00
|
|
|
{
|
2019-02-05 14:38:19 +08:00
|
|
|
// Remove past matches
|
|
|
|
foreach (var r in rooms.ToList())
|
2018-12-12 18:04:11 +08:00
|
|
|
{
|
2019-02-05 14:38:19 +08:00
|
|
|
if (newRooms.All(e => e.RoomID.Value != r.RoomID.Value))
|
|
|
|
rooms.Remove(r);
|
|
|
|
}
|
2018-12-28 00:45:19 +08:00
|
|
|
|
2019-02-05 14:38:19 +08:00
|
|
|
for (int i = 0; i < newRooms.Count; i++)
|
|
|
|
{
|
|
|
|
var r = newRooms[i];
|
2019-02-05 14:56:18 +08:00
|
|
|
r.Position.Value = i;
|
2018-12-11 18:07:40 +08:00
|
|
|
|
2019-02-05 14:38:19 +08:00
|
|
|
update(r, r);
|
|
|
|
addRoom(r);
|
|
|
|
}
|
2018-12-12 18:04:11 +08:00
|
|
|
|
2019-02-05 14:38:19 +08:00
|
|
|
RoomsUpdated?.Invoke();
|
2018-12-12 18:04:11 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 19:32:01 +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)
|
2018-12-12 18:04:11 +08:00
|
|
|
{
|
2018-12-26 19:32:01 +08:00
|
|
|
foreach (var pi in remote.Playlist)
|
|
|
|
pi.MapObjects(beatmaps, rulesets);
|
2018-12-26 21:14:49 +08:00
|
|
|
|
2018-12-27 12:30:36 +08:00
|
|
|
local.CopyFrom(remote);
|
2018-12-12 18:04:11 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 19:32:01 +08:00
|
|
|
/// <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)
|
2018-12-17 10:04:38 +08:00
|
|
|
{
|
2018-12-26 19:32:01 +08:00
|
|
|
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == room.RoomID.Value);
|
|
|
|
if (existing == null)
|
|
|
|
rooms.Add(room);
|
|
|
|
else
|
|
|
|
existing.CopyFrom(room);
|
2018-12-17 10:04:38 +08:00
|
|
|
}
|
2018-12-11 18:07:40 +08:00
|
|
|
}
|
|
|
|
}
|