-
Notifications
You must be signed in to change notification settings - Fork 7
/
MultiplayerLoungeSubScreen.cs
127 lines (103 loc) · 4.27 KB
/
MultiplayerLoungeSubScreen.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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.
#nullable disable
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Framework.Screens;
using osu.Framework.Graphics;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Configuration;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay.Components;
using osu.Game.Screens.OnlinePlay.Lounge;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
using osu.Game.Screens.OnlinePlay.Match;
namespace osu.Game.Screens.OnlinePlay.Multiplayer
{
public partial class MultiplayerLoungeSubScreen : LoungeSubScreen
{
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private MultiplayerClient client { get; set; }
private Dropdown<RoomPermissionsFilter> roomAccessTypeDropdown;
public override void OnResuming(ScreenTransitionEvent e)
{
base.OnResuming(e);
// Upon having left a room, we don't know whether we were the only participant, and whether the room is now closed as a result of leaving it.
// To work around this, temporarily remove the room and trigger an immediate listing poll.
if (e.Last is MultiplayerMatchSubScreen match)
{
RoomManager?.RemoveRoom(match.Room);
ListingPollingComponent.PollImmediately();
}
}
protected override IEnumerable<Drawable> CreateFilterControls()
{
roomAccessTypeDropdown = new SlimEnumDropdown<RoomPermissionsFilter>
{
RelativeSizeAxes = Axes.None,
Current = Config.GetBindable<RoomPermissionsFilter>(OsuSetting.MultiplayerRoomFilter),
Width = 160,
};
roomAccessTypeDropdown.Current.BindValueChanged(_ => UpdateFilter());
return base.CreateFilterControls().Append(roomAccessTypeDropdown);
}
protected override FilterCriteria CreateFilterCriteria()
{
var criteria = base.CreateFilterCriteria();
criteria.Category = @"realtime";
criteria.Permissions = roomAccessTypeDropdown.Current.Value;
return criteria;
}
protected override OsuButton CreateNewRoomButton() => new CreateMultiplayerMatchButton();
protected override Room CreateNewRoom() => new Room
{
Name = { Value = $"{api.LocalUser}'s awesome room" },
Type = { Value = MatchType.HeadToHead },
};
protected override RoomSubScreen CreateRoomSubScreen(Room room) => new MultiplayerMatchSubScreen(room);
protected override ListingPollingComponent CreatePollingComponent() => new MultiplayerListingPollingComponent();
protected override void OpenNewRoom(Room room)
{
if (client?.IsConnected.Value != true)
{
Logger.Log("Not currently connected to the multiplayer server.", LoggingTarget.Runtime, LogLevel.Important);
return;
}
base.OpenNewRoom(room);
}
private partial class MultiplayerListingPollingComponent : ListingPollingComponent
{
[Resolved]
private MultiplayerClient client { get; set; }
private readonly IBindable<bool> isConnected = new Bindable<bool>();
[BackgroundDependencyLoader]
private void load()
{
isConnected.BindTo(client.IsConnected);
isConnected.BindValueChanged(_ => Scheduler.AddOnce(poll), true);
}
private void poll()
{
if (isConnected.Value && IsLoaded)
PollImmediately();
}
protected override Task Poll()
{
if (!isConnected.Value)
return Task.CompletedTask;
if (client.Room != null)
return Task.CompletedTask;
return base.Poll();
}
}
}
}