1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 16:27:26 +08:00

Merge pull request #11516 from frenzibyte/ready-button-clean-up

Replace ready button beatmap logic with using `MultiplayerBeatmapTracker` instead
This commit is contained in:
Dean Herbert 2021-02-05 15:09:25 +09:00 committed by GitHub
commit 2fc3a97f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 89 deletions

View File

@ -6,6 +6,7 @@ using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio; using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Framework.Testing; using osu.Framework.Testing;
@ -26,8 +27,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
public class TestSceneMultiplayerReadyButton : MultiplayerTestScene public class TestSceneMultiplayerReadyButton : MultiplayerTestScene
{ {
private MultiplayerReadyButton button; private MultiplayerReadyButton button;
private OnlinePlayBeatmapAvailablilityTracker beatmapTracker;
private BeatmapSetInfo importedSet; private BeatmapSetInfo importedSet;
private readonly Bindable<PlaylistItem> selectedItem = new Bindable<PlaylistItem>();
private BeatmapManager beatmaps; private BeatmapManager beatmaps;
private RulesetStore rulesets; private RulesetStore rulesets;
@ -39,6 +43,13 @@ namespace osu.Game.Tests.Visual.Multiplayer
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory)); Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, host, Beatmap.Default));
beatmaps.Import(TestResources.GetTestBeatmapForImport(true)).Wait(); beatmaps.Import(TestResources.GetTestBeatmapForImport(true)).Wait();
Add(beatmapTracker = new OnlinePlayBeatmapAvailablilityTracker
{
SelectedItem = { BindTarget = selectedItem }
});
Dependencies.Cache(beatmapTracker);
} }
[SetUp] [SetUp]
@ -46,20 +57,20 @@ namespace osu.Game.Tests.Visual.Multiplayer
{ {
importedSet = beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.All).First(); importedSet = beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.All).First();
Beatmap.Value = beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First()); Beatmap.Value = beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First());
selectedItem.Value = new PlaylistItem
{
Beatmap = { Value = Beatmap.Value.BeatmapInfo },
Ruleset = { Value = Beatmap.Value.BeatmapInfo.Ruleset },
};
Child = button = new MultiplayerReadyButton if (button != null)
Remove(button);
Add(button = new MultiplayerReadyButton
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(200, 50), Size = new Vector2(200, 50),
SelectedItem =
{
Value = new PlaylistItem
{
Beatmap = { Value = Beatmap.Value.BeatmapInfo },
Ruleset = { Value = Beatmap.Value.BeatmapInfo.Ruleset }
}
},
OnReadyClick = async () => OnReadyClick = async () =>
{ {
readyClickOperation = OngoingOperationTracker.BeginOperation(); readyClickOperation = OngoingOperationTracker.BeginOperation();
@ -73,7 +84,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
await Client.ToggleReady(); await Client.ToggleReady();
readyClickOperation.Dispose(); readyClickOperation.Dispose();
} }
}; });
}); });
[Test] [Test]

View File

@ -1,77 +1,29 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
namespace osu.Game.Screens.OnlinePlay.Components namespace osu.Game.Screens.OnlinePlay.Components
{ {
public abstract class ReadyButton : TriangleButton public abstract class ReadyButton : TriangleButton
{ {
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
public new readonly BindableBool Enabled = new BindableBool(); public new readonly BindableBool Enabled = new BindableBool();
[Resolved] private IBindable<BeatmapAvailability> availability;
protected IBindable<WorkingBeatmap> GameBeatmap { get; private set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
private bool hasBeatmap;
private IBindable<WeakReference<BeatmapSetInfo>> managerUpdated;
private IBindable<WeakReference<BeatmapSetInfo>> managerRemoved;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OnlinePlayBeatmapAvailablilityTracker beatmapTracker)
{ {
managerUpdated = beatmaps.ItemUpdated.GetBoundCopy(); availability = beatmapTracker.Availability.GetBoundCopy();
managerUpdated.BindValueChanged(beatmapUpdated);
managerRemoved = beatmaps.ItemRemoved.GetBoundCopy();
managerRemoved.BindValueChanged(beatmapRemoved);
SelectedItem.BindValueChanged(item => updateSelectedItem(item.NewValue), true); availability.BindValueChanged(_ => updateState());
Enabled.BindValueChanged(_ => updateState(), true);
} }
private void updateSelectedItem(PlaylistItem _) => Scheduler.AddOnce(updateBeatmapState); private void updateState() => base.Enabled.Value = availability.Value.State == DownloadState.LocallyAvailable && Enabled.Value;
private void beatmapUpdated(ValueChangedEvent<WeakReference<BeatmapSetInfo>> _) => Scheduler.AddOnce(updateBeatmapState);
private void beatmapRemoved(ValueChangedEvent<WeakReference<BeatmapSetInfo>> _) => Scheduler.AddOnce(updateBeatmapState);
private void updateBeatmapState()
{
int? beatmapId = SelectedItem.Value?.Beatmap.Value?.OnlineBeatmapID;
string checksum = SelectedItem.Value?.Beatmap.Value?.MD5Hash;
if (beatmapId == null || checksum == null)
return;
var databasedBeatmap = beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == beatmapId && b.MD5Hash == checksum);
hasBeatmap = databasedBeatmap?.BeatmapSet?.DeletePending == false;
}
protected override void Update()
{
base.Update();
updateEnabledState();
}
private void updateEnabledState()
{
if (GameBeatmap.Value == null || SelectedItem.Value == null)
{
base.Enabled.Value = false;
return;
}
base.Enabled.Value = hasBeatmap && Enabled.Value;
}
} }
} }

View File

@ -3,13 +3,11 @@
using System; using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay.Playlists; using osu.Game.Screens.OnlinePlay.Playlists;
using osuTK; using osuTK;
@ -20,7 +18,6 @@ namespace osu.Game.Screens.OnlinePlay.Match.Components
public const float HEIGHT = 50; public const float HEIGHT = 50;
public Action OnStart; public Action OnStart;
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
private readonly Drawable background; private readonly Drawable background;
@ -37,7 +34,6 @@ namespace osu.Game.Screens.OnlinePlay.Match.Components
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(600, 50), Size = new Vector2(600, 50),
SelectedItem = { BindTarget = SelectedItem },
Action = () => OnStart?.Invoke() Action = () => OnStart?.Invoke()
} }
}; };

View File

@ -51,17 +51,18 @@ namespace osu.Game.Screens.OnlinePlay.Match
protected RoomSubScreen() protected RoomSubScreen()
{ {
BeatmapAvailablilityTracker = new OnlinePlayBeatmapAvailablilityTracker AddInternal(BeatmapAvailablilityTracker = new OnlinePlayBeatmapAvailablilityTracker
{ {
SelectedItem = { BindTarget = SelectedItem }, SelectedItem = { BindTarget = SelectedItem }
}; });
} }
protected override void ClearInternal(bool disposeChildren = true) =>
throw new InvalidOperationException($"{nameof(RoomSubScreen)}'s children should not be cleared as it will remove required components");
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(AudioManager audio) private void load(AudioManager audio)
{ {
AddInternal(BeatmapAvailablilityTracker);
sampleStart = audio.Samples.Get(@"SongSelect/confirm-selection"); sampleStart = audio.Samples.Get(@"SongSelect/confirm-selection");
} }

View File

@ -3,13 +3,11 @@
using System; using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Online.Rooms;
using osuTK; using osuTK;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
@ -18,8 +16,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
{ {
public const float HEIGHT = 50; public const float HEIGHT = 50;
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
public Action OnReadyClick public Action OnReadyClick
{ {
set => readyButton.OnReadyClick = value; set => readyButton.OnReadyClick = value;
@ -41,7 +37,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(600, 50), Size = new Vector2(600, 50),
SelectedItem = { BindTarget = SelectedItem }
} }
}; };
} }

View File

@ -13,7 +13,6 @@ using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds; using osu.Game.Graphics.Backgrounds;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.Multiplayer; using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay.Components; using osu.Game.Screens.OnlinePlay.Components;
using osuTK; using osuTK;
@ -21,8 +20,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
{ {
public class MultiplayerReadyButton : MultiplayerRoomComposite public class MultiplayerReadyButton : MultiplayerRoomComposite
{ {
public Bindable<PlaylistItem> SelectedItem => button.SelectedItem;
public Action OnReadyClick public Action OnReadyClick
{ {
set => button.Action = value; set => button.Action = value;

View File

@ -61,7 +61,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
InternalChildren = new Drawable[] AddRangeInternal(new Drawable[]
{ {
mainContent = new GridContainer mainContent = new GridContainer
{ {
@ -219,7 +219,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
{ {
new MultiplayerMatchFooter new MultiplayerMatchFooter
{ {
SelectedItem = { BindTarget = SelectedItem },
OnReadyClick = onReadyClick OnReadyClick = onReadyClick
} }
} }
@ -247,7 +246,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
State = { Value = client.Room == null ? Visibility.Visible : Visibility.Hidden } State = { Value = client.Room == null ? Visibility.Visible : Visibility.Hidden }
} }
}; });
if (client.Room == null) if (client.Room == null)
{ {

View File

@ -4,6 +4,7 @@
using System; using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay.Components; using osu.Game.Screens.OnlinePlay.Components;
@ -15,6 +16,9 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
[Resolved(typeof(Room), nameof(Room.EndDate))] [Resolved(typeof(Room), nameof(Room.EndDate))]
private Bindable<DateTimeOffset?> endDate { get; set; } private Bindable<DateTimeOffset?> endDate { get; set; }
[Resolved]
private IBindable<WorkingBeatmap> gameBeatmap { get; set; }
public PlaylistsReadyButton() public PlaylistsReadyButton()
{ {
Text = "Start"; Text = "Start";
@ -32,7 +36,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
{ {
base.Update(); base.Update();
Enabled.Value = endDate.Value != null && DateTimeOffset.UtcNow.AddSeconds(30).AddMilliseconds(GameBeatmap.Value.Track.Length) < endDate.Value; Enabled.Value = endDate.Value != null && DateTimeOffset.UtcNow.AddSeconds(30).AddMilliseconds(gameBeatmap.Value.Track.Length) < endDate.Value;
} }
} }
} }

View File

@ -43,7 +43,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
InternalChildren = new Drawable[] AddRangeInternal(new Drawable[]
{ {
mainContent = new GridContainer mainContent = new GridContainer
{ {
@ -174,7 +174,6 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
new Footer new Footer
{ {
OnStart = onStart, OnStart = onStart,
SelectedItem = { BindTarget = SelectedItem }
} }
} }
}, },
@ -190,7 +189,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
EditPlaylist = () => this.Push(new PlaylistsSongSelect()), EditPlaylist = () => this.Push(new PlaylistsSongSelect()),
State = { Value = roomId.Value == null ? Visibility.Visible : Visibility.Hidden } State = { Value = roomId.Value == null ? Visibility.Visible : Visibility.Hidden }
} }
}; });
if (roomId.Value == null) if (roomId.Value == null)
{ {