From 990bd44ca27868a76f690cd47b49ba3fe4e512bb Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 8 Feb 2019 13:01:10 +0900 Subject: [PATCH] Combine roommanager and roompollingcomponent --- osu.Game/Screens/Multi/Multiplayer.cs | 14 ++- osu.Game/Screens/Multi/RoomManager.cs | 85 ++++++++++++++----- .../Screens/Multi/RoomPollingComponent.cs | 66 -------------- 3 files changed, 68 insertions(+), 97 deletions(-) delete mode 100644 osu.Game/Screens/Multi/RoomPollingComponent.cs diff --git a/osu.Game/Screens/Multi/Multiplayer.cs b/osu.Game/Screens/Multi/Multiplayer.cs index 6851bc2a2a..2a2927734b 100644 --- a/osu.Game/Screens/Multi/Multiplayer.cs +++ b/osu.Game/Screens/Multi/Multiplayer.cs @@ -48,7 +48,6 @@ namespace osu.Game.Screens.Multi private readonly OsuButton createButton; private readonly LoungeSubScreen loungeSubScreen; private readonly ScreenStack screenStack; - private readonly RoomPollingComponent pollingComponent; [Cached] private readonly Bindable filter = new Bindable(); @@ -104,7 +103,7 @@ namespace osu.Game.Screens.Multi }, }, }, - roomManager = new RoomManager + new Container { RelativeSizeAxes = Axes.Both, Padding = new MarginPadding { Top = Header.HEIGHT }, @@ -128,11 +127,10 @@ namespace osu.Game.Screens.Multi Name = { Value = $"{api.LocalUser}'s awesome room" } }), }, - pollingComponent = new RoomPollingComponent() + roomManager = new RoomManager() }); - pollingComponent.Filter.BindTo(filter); - pollingComponent.RoomsRetrieved += roomManager.UpdateRooms; + roomManager.Filter.BindTo(filter); screenStack.ScreenPushed += screenPushed; screenStack.ScreenExited += screenExited; @@ -166,8 +164,8 @@ namespace osu.Game.Screens.Multi private void updatePollingRate(bool idle) { - pollingComponent.TimeBetweenPolls = !this.IsCurrentScreen() || !(screenStack.CurrentScreen is LoungeSubScreen) ? 0 : (idle ? 120000 : 15000); - Logger.Log($"Polling adjusted to {pollingComponent.TimeBetweenPolls}"); + roomManager.TimeBetweenPolls = !this.IsCurrentScreen() || !(screenStack.CurrentScreen is LoungeSubScreen) ? 0 : (idle ? 120000 : 15000); + Logger.Log($"Polling adjusted to {roomManager.TimeBetweenPolls}"); } public void APIStateChanged(APIAccess api, APIState state) @@ -230,7 +228,7 @@ namespace osu.Game.Screens.Multi this.FadeOut(250); cancelLooping(); - pollingComponent.TimeBetweenPolls = 0; + roomManager.TimeBetweenPolls = 0; } private void cancelLooping() diff --git a/osu.Game/Screens/Multi/RoomManager.cs b/osu.Game/Screens/Multi/RoomManager.cs index ceee918586..554be0dfe2 100644 --- a/osu.Game/Screens/Multi/RoomManager.cs +++ b/osu.Game/Screens/Multi/RoomManager.cs @@ -2,27 +2,33 @@ // See the LICENCE file in the repository root for full licence text. using System; -using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using osu.Framework.Allocation; using osu.Framework.Configuration; -using osu.Framework.Graphics.Containers; 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 : Container, IRoomManager + public class RoomManager : PollingComponent, IRoomManager { public event Action RoomsUpdated; private readonly BindableList rooms = new BindableList(); public IBindableList Rooms => rooms; + /// + /// The to use when polling for s. + /// + public readonly Bindable Filter = new Bindable(); + public Bindable CurrentRoom { get; } = new Bindable(); private Room joinedRoom; @@ -36,6 +42,16 @@ namespace osu.Game.Screens.Multi [Resolved] private BeatmapManager beatmaps { get; set; } + public RoomManager() + { + Filter.BindValueChanged(_ => + { + if (IsLoaded) + PollImmediately(); + }); + } + + protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); @@ -46,17 +62,20 @@ namespace osu.Game.Screens.Multi { room.Host.Value = api.LocalUser; + addRoom(room); + joinRoom(room); + + RoomsUpdated?.Invoke(); + + onSuccess?.Invoke(room); + + return; + var req = new CreateRoomRequest(room); req.Success += result => { - update(room, result); - addRoom(room); - joinRoom(room); - RoomsUpdated?.Invoke(); - - onSuccess?.Invoke(room); }; req.Failure += exception => @@ -108,25 +127,45 @@ namespace osu.Game.Screens.Multi joinedRoom = null; } - public void UpdateRooms(List newRooms) + private GetRoomsRequest pollReq; + + protected override Task Poll() { - // Remove past matches - foreach (var r in rooms.ToList()) + if (!api.IsLoggedIn) + return base.Poll(); + + var tcs = new TaskCompletionSource(); + + pollReq?.Cancel(); + pollReq = new GetRoomsRequest(Filter.Value.PrimaryFilter); + + pollReq.Success += result => { - if (newRooms.All(e => e.RoomID.Value != r.RoomID.Value)) - rooms.Remove(r); - } + // Remove past matches + foreach (var r in rooms.ToList()) + { + if (result.All(e => e.RoomID.Value != r.RoomID.Value)) + rooms.Remove(r); + } - for (int i = 0; i < newRooms.Count; i++) - { - var r = newRooms[i]; - r.Position.Value = i; + for (int i = 0; i < result.Count; i++) + { + var r = result[i]; + r.Position.Value = i; - update(r, r); - addRoom(r); - } + update(r, r); + addRoom(r); + } - RoomsUpdated?.Invoke(); + RoomsUpdated?.Invoke(); + tcs.SetResult(true); + }; + + pollReq.Failure += _ => tcs.SetResult(false); + + api.Queue(pollReq); + + return tcs.Task; } /// diff --git a/osu.Game/Screens/Multi/RoomPollingComponent.cs b/osu.Game/Screens/Multi/RoomPollingComponent.cs deleted file mode 100644 index 53c90d4d61..0000000000 --- a/osu.Game/Screens/Multi/RoomPollingComponent.cs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using osu.Framework.Allocation; -using osu.Framework.Configuration; -using osu.Game.Online; -using osu.Game.Online.API; -using osu.Game.Online.API.Requests; -using osu.Game.Online.Multiplayer; -using osu.Game.Screens.Multi.Lounge.Components; - -namespace osu.Game.Screens.Multi -{ - public class RoomPollingComponent : PollingComponent - { - /// - /// Invoked when s have been retrieved from the API. - /// - public Action> RoomsRetrieved; - - /// - /// The to use when polling for s. - /// - public readonly Bindable Filter = new Bindable(); - - [Resolved] - private APIAccess api { get; set; } - - public RoomPollingComponent() - { - Filter.BindValueChanged(_ => - { - 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(Filter.Value.PrimaryFilter); - - pollReq.Success += result => - { - RoomsRetrieved?.Invoke(result); - tcs.SetResult(true); - }; - - pollReq.Failure += _ => tcs.SetResult(false); - - api.Queue(pollReq); - - return tcs.Task; - } - } -}