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

177 lines
5.0 KiB
C#

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
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;
using osu.Game.Rulesets;
using osu.Game.Screens.Multi.Lounge.Components;
namespace osu.Game.Screens.Multi
{
public class RoomManager : PollingComponent, IRoomManager
{
private readonly BindableCollection<Room> rooms = new BindableCollection<Room>();
public IBindableCollection<Room> Rooms => rooms;
private Room currentRoom;
private FilterCriteria currentFilter = new FilterCriteria();
[Resolved]
private APIAccess api { get; set; }
[Resolved]
private RulesetStore rulesets { get; set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
public RoomManager()
{
TimeBetweenPolls = 5000;
}
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;
var req = new CreateRoomRequest(room);
req.Success += result =>
{
update(room, result);
addRoom(room);
onSuccess?.Invoke(room);
};
req.Failure += exception =>
{
if (req.Result != null)
onError?.Invoke(req.Result.Error);
else
Logger.Log($"Failed to create the room: {exception}", level: LogLevel.Important);
};
api.Queue(req);
}
private JoinRoomRequest currentJoinRoomRequest;
public void JoinRoom(Room room, Action<Room> onSuccess = null, Action<string> onError = null)
{
currentJoinRoomRequest?.Cancel();
currentJoinRoomRequest = null;
currentJoinRoomRequest = new JoinRoomRequest(room, api.LocalUser.Value);
currentJoinRoomRequest.Success += () =>
{
currentRoom = room;
onSuccess?.Invoke(room);
};
currentJoinRoomRequest.Failure += exception =>
{
Logger.Log($"Failed to join room: {exception}", level: LogLevel.Important);
onError?.Invoke(exception.ToString());
};
api.Queue(currentJoinRoomRequest);
}
public void PartRoom()
{
if (currentRoom == null)
return;
api.Queue(new PartRoomRequest(currentRoom, api.LocalUser.Value));
currentRoom = null;
}
public void Filter(FilterCriteria criteria)
{
currentFilter = criteria;
PollImmediately();
}
private GetRoomsRequest pollReq;
protected override Task Poll()
{
if (!api.IsLoggedIn)
return base.Poll();
var tcs = new TaskCompletionSource<bool>();
pollReq?.Cancel();
pollReq = new GetRoomsRequest(currentFilter.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);
}
// Add new matches, or update existing
foreach (var r in result)
{
update(r, r);
addRoom(r);
}
tcs.SetResult(true);
};
pollReq.Failure += _ => tcs.SetResult(false);
api.Queue(pollReq);
return tcs.Task;
}
/// <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);
}
}
}