// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Threading.Tasks; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Game.Online.Multiplayer; using osu.Game.Screens.Multi.Lounge.Components; namespace osu.Game.Screens.Multi.Components { /// /// A that polls for the lounge listing. /// public class ListingPollingComponent : RoomPollingComponent { [Resolved] private Bindable currentFilter { get; set; } [Resolved] private Bindable selectedRoom { get; set; } [BackgroundDependencyLoader] private void load() { currentFilter.BindValueChanged(_ => { NotifyRoomsReceived(null); if (IsLoaded) PollImmediately(); }); } private GetRoomsRequest pollReq; protected override Task Poll() { if (!API.IsLoggedIn) return base.Poll(); var tcs = new TaskCompletionSource(); pollReq?.Cancel(); pollReq = new GetRoomsRequest(currentFilter.Value.Status, currentFilter.Value.Category); pollReq.Success += result => { for (int i = 0; i < result.Count; i++) { if (result[i].RoomID.Value == selectedRoom.Value?.RoomID.Value) { // The listing request always has less information than the opened room, so don't include it. result[i] = selectedRoom.Value; break; } } NotifyRoomsReceived(result); tcs.SetResult(true); }; pollReq.Failure += _ => tcs.SetResult(false); API.Queue(pollReq); return tcs.Task; } } }