Split polling logic from RoomManager, now a container

This commit is contained in:
smoogipoo 2019-02-05 15:38:19 +09:00
parent e33277bd76
commit 37c1f5a824
6 changed files with 125 additions and 73 deletions

View File

@ -4,7 +4,6 @@
using System;
using osu.Framework.Configuration;
using osu.Game.Online.Multiplayer;
using osu.Game.Screens.Multi.Lounge.Components;
namespace osu.Game.Screens.Multi
{
@ -40,11 +39,5 @@ namespace osu.Game.Screens.Multi
/// Parts the currently-joined <see cref="Room"/>.
/// </summary>
void PartRoom();
/// <summary>
/// Queries for <see cref="Room"/>s matching a new <see cref="FilterCriteria"/>.
/// </summary>
/// <param name="criteria">The <see cref="FilterCriteria"/> to match.</param>
void Filter(FilterCriteria criteria);
}
}

View File

@ -2,6 +2,8 @@
// See the LICENCE file in the repository root for full licence text.
using System.ComponentModel;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Game.Graphics;
using osu.Game.Overlays.SearchableList;
using osuTK.Graphics;
@ -15,18 +17,39 @@ namespace osu.Game.Screens.Multi.Lounge.Components
protected override float ContentHorizontalPadding => base.ContentHorizontalPadding + OsuScreen.HORIZONTAL_OVERFLOW_PADDING;
[Resolved(CanBeNull = true)]
private Bindable<FilterCriteria> filter { get; set; }
public FilterControl()
{
DisplayStyleControl.Hide();
}
public FilterCriteria CreateCriteria() => new FilterCriteria
[BackgroundDependencyLoader]
private void load()
{
if (filter == null)
filter = new Bindable<FilterCriteria>();
}
protected override void LoadComplete()
{
base.LoadComplete();
Search.Current.BindValueChanged(_ => updateFilter());
Tabs.Current.BindValueChanged(_ => updateFilter(), true);
}
private void updateFilter()
{
filter.Value = new FilterCriteria
{
SearchString = Search.Current.Value ?? string.Empty,
PrimaryFilter = Tabs.Current,
SecondaryFilter = DisplayStyleControl.Dropdown.Current
};
}
}
public enum PrimaryFilter
{

View File

@ -21,7 +21,6 @@ namespace osu.Game.Screens.Multi.Lounge
protected readonly FilterControl Filter;
private readonly Container content;
private readonly RoomsContainer rooms;
private readonly Action<Screen> pushGameplayScreen;
private readonly ProcessingOverlay processingOverlay;
@ -30,6 +29,7 @@ namespace osu.Game.Screens.Multi.Lounge
this.pushGameplayScreen = pushGameplayScreen;
RoomInspector inspector;
RoomsContainer rooms;
InternalChildren = new Drawable[]
{
@ -73,8 +73,6 @@ namespace osu.Game.Screens.Multi.Lounge
inspector.Room.BindTo(rooms.SelectedRoom);
Filter.Search.Current.ValueChanged += s => filterRooms();
Filter.Tabs.Current.ValueChanged += t => filterRooms();
Filter.Search.Exit += this.Exit;
}
@ -113,12 +111,6 @@ namespace osu.Game.Screens.Multi.Lounge
Filter.Search.HoldFocus = false;
}
private void filterRooms()
{
rooms.Filter(Filter.CreateCriteria());
Manager?.Filter(Filter.CreateCriteria());
}
private void joinRequested(Room room)
{
processingOverlay.Show();

View File

@ -20,6 +20,7 @@ using osu.Game.Overlays;
using osu.Game.Overlays.BeatmapSet.Buttons;
using osu.Game.Screens.Menu;
using osu.Game.Screens.Multi.Lounge;
using osu.Game.Screens.Multi.Lounge.Components;
using osu.Game.Screens.Multi.Match;
using osuTK;
@ -47,6 +48,10 @@ 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<FilterCriteria> filter = new Bindable<FilterCriteria>();
[Cached(Type = typeof(IRoomManager))]
private RoomManager roomManager;
@ -99,7 +104,7 @@ namespace osu.Game.Screens.Multi
},
},
},
new Container
roomManager = new RoomManager
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = Header.HEIGHT },
@ -123,9 +128,12 @@ namespace osu.Game.Screens.Multi
Name = { Value = $"{api.LocalUser}'s awesome room" }
}),
},
roomManager = new RoomManager()
pollingComponent = new RoomPollingComponent()
});
pollingComponent.Filter.BindTo(filter);
pollingComponent.RoomsRetrieved += roomManager.UpdateRooms;
screenStack.ScreenPushed += screenPushed;
screenStack.ScreenExited += screenExited;
}
@ -149,8 +157,8 @@ namespace osu.Game.Screens.Multi
private void updatePollingRate(bool idle)
{
roomManager.TimeBetweenPolls = !this.IsCurrentScreen() || !(screenStack.CurrentScreen is LoungeSubScreen) ? 0 : (idle ? 120000 : 15000);
Logger.Log($"Polling adjusted to {roomManager.TimeBetweenPolls}");
pollingComponent.TimeBetweenPolls = !this.IsCurrentScreen() || !(screenStack.CurrentScreen is LoungeSubScreen) ? 0 : (idle ? 120000 : 15000);
Logger.Log($"Polling adjusted to {pollingComponent.TimeBetweenPolls}");
}
public void APIStateChanged(APIAccess api, APIState state)
@ -213,7 +221,7 @@ namespace osu.Game.Screens.Multi
this.FadeOut(250);
cancelLooping();
roomManager.TimeBetweenPolls = 0;
pollingComponent.TimeBetweenPolls = 0;
}
private void cancelLooping()

View File

@ -2,22 +2,21 @@
// 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 : PollingComponent, IRoomManager
public class RoomManager : Container, IRoomManager
{
public event Action RoomsUpdated;
@ -26,8 +25,6 @@ namespace osu.Game.Screens.Multi
private Room currentRoom;
private FilterCriteria currentFilter = new FilterCriteria();
[Resolved]
private APIAccess api { get; set; }
@ -102,36 +99,18 @@ namespace osu.Game.Screens.Multi
currentRoom = null;
}
public void Filter(FilterCriteria criteria)
{
currentFilter = criteria;
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.PrimaryFilter);
pollReq.Success += result =>
public void UpdateRooms(List<Room> newRooms)
{
// Remove past matches
foreach (var r in rooms.ToList())
{
if (result.All(e => e.RoomID.Value != r.RoomID.Value))
if (newRooms.All(e => e.RoomID.Value != r.RoomID.Value))
rooms.Remove(r);
}
for (int i = 0; i < result.Count; i++)
for (int i = 0; i < newRooms.Count; i++)
{
var r = result[i];
var r = newRooms[i];
r.Position = i;
update(r, r);
@ -139,15 +118,6 @@ namespace osu.Game.Screens.Multi
}
RoomsUpdated?.Invoke();
tcs.SetResult(true);
};
pollReq.Failure += _ => tcs.SetResult(false);
api.Queue(pollReq);
return tcs.Task;
}
/// <summary>

View File

@ -0,0 +1,66 @@
// 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.
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
{
/// <summary>
/// Invoked when <see cref="Room"/>s have been retrieved from the API.
/// </summary>
public Action<List<Room>> RoomsRetrieved;
/// <summary>
/// The <see cref="FilterCriteria"/> to use when polling for <see cref="Room"/>s.
/// </summary>
public readonly Bindable<FilterCriteria> Filter = new Bindable<FilterCriteria>();
[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<bool>();
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;
}
}
}