2018-12-11 18:07:40 +08:00
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
2018-12-20 19:58:34 +08:00
|
|
|
using System;
|
2018-12-12 18:04:11 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2018-12-11 18:07:40 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Configuration;
|
2018-12-12 18:04:11 +08:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Online;
|
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-19 17:02:05 +08:00
|
|
|
using osu.Game.Screens.Multi.Lounge.Components;
|
2018-12-11 18:07:40 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Multi
|
|
|
|
{
|
2018-12-12 18:04:11 +08:00
|
|
|
public class RoomManager : PollingComponent
|
2018-12-11 18:07:40 +08:00
|
|
|
{
|
2018-12-22 11:45:36 +08:00
|
|
|
public event Action<Room> OpenRequested;
|
2018-12-20 19:58:34 +08:00
|
|
|
|
2018-12-11 18:07:40 +08:00
|
|
|
public IBindableCollection<Room> Rooms => rooms;
|
|
|
|
private readonly BindableCollection<Room> rooms = new BindableCollection<Room>();
|
|
|
|
|
2018-12-20 19:58:34 +08:00
|
|
|
private Room currentRoom;
|
2018-12-11 18:07:40 +08:00
|
|
|
|
2018-12-19 17:02:05 +08:00
|
|
|
private FilterCriteria currentFilter = new FilterCriteria();
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
public RoomManager()
|
|
|
|
{
|
|
|
|
TimeBetweenPolls = 5000;
|
|
|
|
}
|
|
|
|
|
2018-12-11 18:07:40 +08:00
|
|
|
public void CreateRoom(Room room)
|
|
|
|
{
|
|
|
|
room.Host.Value = api.LocalUser;
|
|
|
|
|
2018-12-12 18:04:11 +08:00
|
|
|
var req = new CreateRoomRequest(room);
|
|
|
|
req.Success += result => addRoom(room, result);
|
|
|
|
req.Failure += exception => Logger.Log($"Failed to create room: {exception}");
|
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;
|
|
|
|
|
|
|
|
public void JoinRoom(Room room)
|
|
|
|
{
|
|
|
|
currentJoinRoomRequest?.Cancel();
|
|
|
|
currentJoinRoomRequest = null;
|
|
|
|
|
|
|
|
currentJoinRoomRequest = new JoinRoomRequest(room, api.LocalUser.Value);
|
|
|
|
currentJoinRoomRequest.Success += () =>
|
|
|
|
{
|
|
|
|
currentRoom = room;
|
|
|
|
OpenRequested?.Invoke(room);
|
|
|
|
};
|
|
|
|
|
|
|
|
currentJoinRoomRequest.Failure += exception => Logger.Log($"Failed to join room: {exception}");
|
|
|
|
|
|
|
|
api.Queue(currentJoinRoomRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PartRoom()
|
|
|
|
{
|
|
|
|
if (currentRoom == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
api.Queue(new PartRoomRequest(currentRoom, api.LocalUser.Value));
|
|
|
|
currentRoom = null;
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:02:05 +08:00
|
|
|
public void Filter(FilterCriteria criteria)
|
|
|
|
{
|
|
|
|
currentFilter = criteria;
|
|
|
|
PollImmediately();
|
|
|
|
}
|
|
|
|
|
2018-12-12 18:04:11 +08:00
|
|
|
protected override Task Poll()
|
|
|
|
{
|
|
|
|
if (!api.IsLoggedIn)
|
|
|
|
return base.Poll();
|
|
|
|
|
|
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
|
2018-12-19 17:02:05 +08:00
|
|
|
var pollReq = new GetRoomsRequest(currentFilter.PrimaryFilter);
|
2018-12-12 18:04:11 +08:00
|
|
|
|
|
|
|
pollReq.Success += result =>
|
|
|
|
{
|
2018-12-19 17:02:05 +08:00
|
|
|
// 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
|
2018-12-12 18:04:11 +08:00
|
|
|
foreach (var r in result)
|
|
|
|
{
|
2018-12-17 10:04:38 +08:00
|
|
|
processPlaylist(r);
|
2018-12-12 18:04:11 +08:00
|
|
|
|
|
|
|
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == r.RoomID.Value);
|
|
|
|
if (existing == null)
|
|
|
|
rooms.Add(r);
|
|
|
|
else
|
|
|
|
existing.CopyFrom(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
tcs.SetResult(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
pollReq.Failure += _ => tcs.SetResult(false);
|
2018-12-11 18:07:40 +08:00
|
|
|
|
2018-12-12 18:04:11 +08:00
|
|
|
api.Queue(pollReq);
|
|
|
|
|
|
|
|
return tcs.Task;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addRoom(Room local, Room remote)
|
|
|
|
{
|
2018-12-17 10:04:38 +08:00
|
|
|
processPlaylist(remote);
|
|
|
|
|
2018-12-12 18:04:11 +08:00
|
|
|
local.CopyFrom(remote);
|
|
|
|
|
|
|
|
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == local.RoomID.Value);
|
|
|
|
if (existing != null)
|
|
|
|
rooms.Remove(existing);
|
|
|
|
rooms.Add(local);
|
|
|
|
}
|
|
|
|
|
2018-12-17 10:04:38 +08:00
|
|
|
private void processPlaylist(Room room)
|
|
|
|
{
|
|
|
|
foreach (var pi in room.Playlist)
|
|
|
|
pi.MapObjects(beatmaps, rulesets);
|
|
|
|
}
|
2018-12-11 18:07:40 +08:00
|
|
|
}
|
|
|
|
}
|