osu/osu.Game/Screens/Multi/RoomManager.cs

197 lines
5.4 KiB
C#
Raw Normal View History

// 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-20 11:58:34 +00:00
using System;
2018-12-12 10:04:11 +00:00
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
2018-12-12 10:04:11 +00:00
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Multiplayer;
2018-12-12 10:04:11 +00:00
using osu.Game.Rulesets;
using osu.Game.Screens.Multi.Lounge.Components;
namespace osu.Game.Screens.Multi
{
public class RoomManager : PollingComponent, IRoomManager
{
2018-12-27 16:45:19 +00:00
public event Action RoomsUpdated;
2019-01-07 09:50:27 +00:00
private readonly BindableList<Room> rooms = new BindableList<Room>();
public IBindableList<Room> Rooms => rooms;
private Room joinedRoom;
2019-02-08 04:02:17 +00:00
[Resolved]
2019-02-08 05:57:51 +00:00
private Bindable<Room> currentRoom { get; set; }
[Resolved]
private Bindable<FilterCriteria> currentFilter { get; set; }
2019-02-08 04:02:17 +00:00
[Resolved]
private APIAccess api { get; set; }
2018-12-12 10:04:11 +00:00
[Resolved]
private RulesetStore rulesets { get; set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
2019-02-08 05:57:51 +00:00
[BackgroundDependencyLoader]
private void load()
{
2019-02-08 05:57:51 +00:00
currentFilter.BindValueChanged(_ =>
{
if (IsLoaded)
PollImmediately();
});
}
2018-12-26 11:12:51 +00:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
PartRoom();
}
public void CreateRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
{
room.Host.Value = api.LocalUser;
addRoom(room);
joinRoom(room);
RoomsUpdated?.Invoke();
onSuccess?.Invoke(room);
return;
2018-12-12 10:04:11 +00:00
var req = new CreateRoomRequest(room);
2018-12-26 11:32:01 +00:00
req.Success += result =>
{
2018-12-27 16:45:19 +00:00
2018-12-26 11:32:01 +00:00
};
2018-12-26 09:38:58 +00: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 11:58:34 +00:00
2018-12-12 10:04:11 +00:00
api.Queue(req);
}
2018-12-20 11:58:34 +00:00
private JoinRoomRequest currentJoinRoomRequest;
public void JoinRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
2018-12-20 11:58:34 +00:00
{
currentJoinRoomRequest?.Cancel();
currentJoinRoomRequest = null;
currentJoinRoomRequest = new JoinRoomRequest(room, api.LocalUser.Value);
currentJoinRoomRequest.Success += () =>
{
joinRoom(room);
onSuccess?.Invoke(room);
2018-12-20 11:58:34 +00:00
};
currentJoinRoomRequest.Failure += exception =>
{
Logger.Log($"Failed to join room: {exception}", level: LogLevel.Important);
onError?.Invoke(exception.ToString());
};
2018-12-20 11:58:34 +00:00
api.Queue(currentJoinRoomRequest);
}
private void joinRoom(Room room)
{
2019-02-08 05:57:51 +00:00
currentRoom.Value = room;
joinedRoom = room;
}
2018-12-20 11:58:34 +00:00
public void PartRoom()
{
if (joinedRoom == null)
2018-12-20 11:58:34 +00:00
return;
api.Queue(new PartRoomRequest(joinedRoom, api.LocalUser.Value));
joinedRoom = null;
2018-12-20 11:58:34 +00:00
}
private GetRoomsRequest pollReq;
protected override Task Poll()
{
if (!api.IsLoggedIn)
return base.Poll();
var tcs = new TaskCompletionSource<bool>();
2018-12-27 16:45:19 +00:00
pollReq?.Cancel();
2019-02-08 05:57:51 +00:00
pollReq = new GetRoomsRequest(currentFilter.Value.PrimaryFilter);
pollReq.Success += result =>
{
// Remove past matches
foreach (var r in rooms.ToList())
{
if (result.All(e => e.RoomID.Value != r.RoomID.Value))
rooms.Remove(r);
}
for (int i = 0; i < result.Count; i++)
{
var r = result[i];
r.Position.Value = i;
update(r, r);
addRoom(r);
}
RoomsUpdated?.Invoke();
tcs.SetResult(true);
};
pollReq.Failure += _ => tcs.SetResult(false);
2018-12-12 10:04:11 +00:00
api.Queue(pollReq);
return tcs.Task;
2018-12-12 10:04:11 +00:00
}
2018-12-26 11:32:01 +00: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 10:04:11 +00:00
{
2018-12-26 11:32:01 +00:00
foreach (var pi in remote.Playlist)
pi.MapObjects(beatmaps, rulesets);
2018-12-26 13:14:49 +00:00
2018-12-27 04:30:36 +00:00
local.CopyFrom(remote);
2018-12-12 10:04:11 +00:00
}
2018-12-26 11:32:01 +00: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-26 11:32:01 +00:00
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == room.RoomID.Value);
if (existing == null)
rooms.Add(room);
else
existing.CopyFrom(room);
}
}
}