1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 00:02:54 +08:00

Combine roommanager and roompollingcomponent

This commit is contained in:
smoogipoo 2019-02-08 13:01:10 +09:00
parent 17fdfc15d9
commit 990bd44ca2
3 changed files with 68 additions and 97 deletions

View File

@ -48,7 +48,6 @@ namespace osu.Game.Screens.Multi
private readonly OsuButton createButton; private readonly OsuButton createButton;
private readonly LoungeSubScreen loungeSubScreen; private readonly LoungeSubScreen loungeSubScreen;
private readonly ScreenStack screenStack; private readonly ScreenStack screenStack;
private readonly RoomPollingComponent pollingComponent;
[Cached] [Cached]
private readonly Bindable<FilterCriteria> filter = new Bindable<FilterCriteria>(); private readonly Bindable<FilterCriteria> filter = new Bindable<FilterCriteria>();
@ -104,7 +103,7 @@ namespace osu.Game.Screens.Multi
}, },
}, },
}, },
roomManager = new RoomManager new Container
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = Header.HEIGHT }, Padding = new MarginPadding { Top = Header.HEIGHT },
@ -128,11 +127,10 @@ namespace osu.Game.Screens.Multi
Name = { Value = $"{api.LocalUser}'s awesome room" } Name = { Value = $"{api.LocalUser}'s awesome room" }
}), }),
}, },
pollingComponent = new RoomPollingComponent() roomManager = new RoomManager()
}); });
pollingComponent.Filter.BindTo(filter); roomManager.Filter.BindTo(filter);
pollingComponent.RoomsRetrieved += roomManager.UpdateRooms;
screenStack.ScreenPushed += screenPushed; screenStack.ScreenPushed += screenPushed;
screenStack.ScreenExited += screenExited; screenStack.ScreenExited += screenExited;
@ -166,8 +164,8 @@ namespace osu.Game.Screens.Multi
private void updatePollingRate(bool idle) private void updatePollingRate(bool idle)
{ {
pollingComponent.TimeBetweenPolls = !this.IsCurrentScreen() || !(screenStack.CurrentScreen is LoungeSubScreen) ? 0 : (idle ? 120000 : 15000); roomManager.TimeBetweenPolls = !this.IsCurrentScreen() || !(screenStack.CurrentScreen is LoungeSubScreen) ? 0 : (idle ? 120000 : 15000);
Logger.Log($"Polling adjusted to {pollingComponent.TimeBetweenPolls}"); Logger.Log($"Polling adjusted to {roomManager.TimeBetweenPolls}");
} }
public void APIStateChanged(APIAccess api, APIState state) public void APIStateChanged(APIAccess api, APIState state)
@ -230,7 +228,7 @@ namespace osu.Game.Screens.Multi
this.FadeOut(250); this.FadeOut(250);
cancelLooping(); cancelLooping();
pollingComponent.TimeBetweenPolls = 0; roomManager.TimeBetweenPolls = 0;
} }
private void cancelLooping() private void cancelLooping()

View File

@ -2,27 +2,33 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Online;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Online.Multiplayer; using osu.Game.Online.Multiplayer;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Screens.Multi.Lounge.Components;
namespace osu.Game.Screens.Multi namespace osu.Game.Screens.Multi
{ {
public class RoomManager : Container, IRoomManager public class RoomManager : PollingComponent, IRoomManager
{ {
public event Action RoomsUpdated; public event Action RoomsUpdated;
private readonly BindableList<Room> rooms = new BindableList<Room>(); private readonly BindableList<Room> rooms = new BindableList<Room>();
public IBindableList<Room> Rooms => rooms; public IBindableList<Room> Rooms => rooms;
/// <summary>
/// The <see cref="FilterCriteria"/> to use when polling for <see cref="Room"/>s.
/// </summary>
public readonly Bindable<FilterCriteria> Filter = new Bindable<FilterCriteria>();
public Bindable<Room> CurrentRoom { get; } = new Bindable<Room>(); public Bindable<Room> CurrentRoom { get; } = new Bindable<Room>();
private Room joinedRoom; private Room joinedRoom;
@ -36,6 +42,16 @@ namespace osu.Game.Screens.Multi
[Resolved] [Resolved]
private BeatmapManager beatmaps { get; set; } private BeatmapManager beatmaps { get; set; }
public RoomManager()
{
Filter.BindValueChanged(_ =>
{
if (IsLoaded)
PollImmediately();
});
}
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
base.Dispose(isDisposing); base.Dispose(isDisposing);
@ -46,17 +62,20 @@ namespace osu.Game.Screens.Multi
{ {
room.Host.Value = api.LocalUser; room.Host.Value = api.LocalUser;
addRoom(room);
joinRoom(room);
RoomsUpdated?.Invoke();
onSuccess?.Invoke(room);
return;
var req = new CreateRoomRequest(room); var req = new CreateRoomRequest(room);
req.Success += result => req.Success += result =>
{ {
update(room, result);
addRoom(room);
joinRoom(room);
RoomsUpdated?.Invoke();
onSuccess?.Invoke(room);
}; };
req.Failure += exception => req.Failure += exception =>
@ -108,25 +127,45 @@ namespace osu.Game.Screens.Multi
joinedRoom = null; joinedRoom = null;
} }
public void UpdateRooms(List<Room> newRooms) private GetRoomsRequest pollReq;
protected override Task Poll()
{ {
// Remove past matches if (!api.IsLoggedIn)
foreach (var r in rooms.ToList()) return base.Poll();
var tcs = new TaskCompletionSource<bool>();
pollReq?.Cancel();
pollReq = new GetRoomsRequest(Filter.Value.PrimaryFilter);
pollReq.Success += result =>
{ {
if (newRooms.All(e => e.RoomID.Value != r.RoomID.Value)) // Remove past matches
rooms.Remove(r); 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++) for (int i = 0; i < result.Count; i++)
{ {
var r = newRooms[i]; var r = result[i];
r.Position.Value = i; r.Position.Value = i;
update(r, r); update(r, r);
addRoom(r); addRoom(r);
} }
RoomsUpdated?.Invoke(); RoomsUpdated?.Invoke();
tcs.SetResult(true);
};
pollReq.Failure += _ => tcs.SetResult(false);
api.Queue(pollReq);
return tcs.Task;
} }
/// <summary> /// <summary>

View File

@ -1,66 +0,0 @@
// 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;
}
}
}