2020-12-19 00:15:41 +09: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.
|
|
|
|
|
2022-06-17 16:37:17 +09:00
|
|
|
#nullable disable
|
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
using System.Linq;
|
2020-12-19 00:15:41 +09:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2020-12-25 13:38:11 +09:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-25 16:50:00 +01:00
|
|
|
using osu.Game.Screens.OnlinePlay.Lounge.Components;
|
2020-12-19 00:15:41 +09:00
|
|
|
|
2020-12-25 16:50:00 +01:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Components
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A <see cref="RoomPollingComponent"/> that polls for the lounge listing.
|
|
|
|
/// </summary>
|
|
|
|
public partial class ListingPollingComponent : RoomPollingComponent
|
|
|
|
{
|
2021-08-16 13:09:04 +09:00
|
|
|
public IBindable<bool> InitialRoomsReceived => initialRoomsReceived;
|
|
|
|
private readonly Bindable<bool> initialRoomsReceived = new Bindable<bool>();
|
2021-08-13 17:39:09 +09:00
|
|
|
|
2021-08-17 09:36:43 +09:00
|
|
|
public readonly Bindable<FilterCriteria> Filter = new Bindable<FilterCriteria>();
|
2020-12-19 00:15:41 +09:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2021-08-17 09:36:43 +09:00
|
|
|
Filter.BindValueChanged(_ =>
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2021-08-13 17:39:09 +09:00
|
|
|
RoomManager.ClearRooms();
|
2021-08-16 13:09:04 +09:00
|
|
|
initialRoomsReceived.Value = false;
|
2021-08-13 17:39:09 +09:00
|
|
|
|
2020-12-19 00:15:41 +09:00
|
|
|
if (IsLoaded)
|
|
|
|
PollImmediately();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-28 13:43:53 +09:00
|
|
|
private GetRoomsRequest lastPollRequest;
|
2020-12-19 00:15:41 +09:00
|
|
|
|
|
|
|
protected override Task Poll()
|
|
|
|
{
|
|
|
|
if (!API.IsLoggedIn)
|
|
|
|
return base.Poll();
|
|
|
|
|
2021-08-17 09:36:43 +09:00
|
|
|
if (Filter.Value == null)
|
|
|
|
return base.Poll();
|
|
|
|
|
2020-12-19 00:15:41 +09:00
|
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
|
2022-01-28 13:43:53 +09:00
|
|
|
lastPollRequest?.Cancel();
|
2020-12-19 00:15:41 +09:00
|
|
|
|
2022-01-28 13:43:53 +09:00
|
|
|
var req = new GetRoomsRequest(Filter.Value.Status, Filter.Value.Category);
|
|
|
|
|
|
|
|
req.Success += result =>
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2021-08-13 17:39:09 +09:00
|
|
|
foreach (var existing in RoomManager.Rooms.ToArray())
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2021-08-13 17:39:09 +09:00
|
|
|
if (result.All(r => r.RoomID.Value != existing.RoomID.Value))
|
|
|
|
RoomManager.RemoveRoom(existing);
|
2020-12-19 00:15:41 +09:00
|
|
|
}
|
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
foreach (var incoming in result)
|
2021-09-14 18:20:28 +09:00
|
|
|
{
|
2021-09-15 17:03:26 +09:00
|
|
|
incoming.RemoveExpiredPlaylistItems();
|
2021-08-13 17:39:09 +09:00
|
|
|
RoomManager.AddOrUpdateRoom(incoming);
|
2021-09-14 18:20:28 +09:00
|
|
|
}
|
2021-08-13 17:39:09 +09:00
|
|
|
|
2021-08-16 13:09:04 +09:00
|
|
|
initialRoomsReceived.Value = true;
|
2020-12-19 00:15:41 +09:00
|
|
|
tcs.SetResult(true);
|
|
|
|
};
|
|
|
|
|
2022-01-28 13:43:53 +09:00
|
|
|
req.Failure += _ => tcs.SetResult(false);
|
2020-12-19 00:15:41 +09:00
|
|
|
|
2022-01-28 13:43:53 +09:00
|
|
|
API.Queue(req);
|
2020-12-19 00:15:41 +09:00
|
|
|
|
2022-01-28 13:43:53 +09:00
|
|
|
lastPollRequest = req;
|
2020-12-19 00:15:41 +09:00
|
|
|
return tcs.Task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|