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.
|
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
using System.Linq;
|
2020-12-18 23:15:41 +08:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2020-12-25 12:38:11 +08:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-25 23:50:00 +08:00
|
|
|
using osu.Game.Screens.OnlinePlay.Lounge.Components;
|
2020-12-18 23:15:41 +08:00
|
|
|
|
2020-12-25 23:50:00 +08:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Components
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A <see cref="RoomPollingComponent"/> that polls for the lounge listing.
|
|
|
|
/// </summary>
|
|
|
|
public class ListingPollingComponent : RoomPollingComponent
|
|
|
|
{
|
2021-08-16 12:09:04 +08:00
|
|
|
public IBindable<bool> InitialRoomsReceived => initialRoomsReceived;
|
|
|
|
private readonly Bindable<bool> initialRoomsReceived = new Bindable<bool>();
|
2021-08-13 16:39:09 +08:00
|
|
|
|
2020-12-18 23:15:41 +08:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<FilterCriteria> currentFilter { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private Bindable<Room> selectedRoom { get; set; }
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
currentFilter.BindValueChanged(_ =>
|
|
|
|
{
|
2021-08-13 16:39:09 +08:00
|
|
|
RoomManager.ClearRooms();
|
2021-08-16 12:09:04 +08:00
|
|
|
initialRoomsReceived.Value = false;
|
2021-08-13 16:39:09 +08:00
|
|
|
|
2020-12-18 23:15:41 +08:00
|
|
|
if (IsLoaded)
|
|
|
|
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.Value.Status, currentFilter.Value.Category);
|
|
|
|
|
|
|
|
pollReq.Success += result =>
|
|
|
|
{
|
2021-08-13 16:39:09 +08:00
|
|
|
foreach (var existing in RoomManager.Rooms.ToArray())
|
2020-12-18 23:15:41 +08:00
|
|
|
{
|
2021-08-13 16:39:09 +08:00
|
|
|
if (result.All(r => r.RoomID.Value != existing.RoomID.Value))
|
|
|
|
RoomManager.RemoveRoom(existing);
|
2020-12-18 23:15:41 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 16:39:09 +08:00
|
|
|
foreach (var incoming in result)
|
|
|
|
RoomManager.AddOrUpdateRoom(incoming);
|
|
|
|
|
2021-08-16 12:09:04 +08:00
|
|
|
initialRoomsReceived.Value = true;
|
2020-12-18 23:15:41 +08:00
|
|
|
tcs.SetResult(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
pollReq.Failure += _ => tcs.SetResult(false);
|
|
|
|
|
|
|
|
API.Queue(pollReq);
|
|
|
|
|
|
|
|
return tcs.Task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|