mirror of
https://github.com/ppy/osu.git
synced 2025-03-23 08:27:23 +08:00
Merge branch 'realtime-ready-button' into realtime-multiplayer-2
This commit is contained in:
commit
fdfe3c2b36
@ -0,0 +1,135 @@
|
||||
// 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.Linq;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Audio;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Beatmaps;
|
||||
using osu.Game.Online.Multiplayer;
|
||||
using osu.Game.Online.RealtimeMultiplayer;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Screens.Multi.RealtimeMultiplayer;
|
||||
using osu.Game.Tests.Resources;
|
||||
using osu.Game.Users;
|
||||
using osuTK;
|
||||
using osuTK.Input;
|
||||
|
||||
namespace osu.Game.Tests.Visual.RealtimeMultiplayer
|
||||
{
|
||||
public class TestSceneRealtimeReadyButton : RealtimeMultiplayerTestScene
|
||||
{
|
||||
private RealtimeReadyButton button;
|
||||
|
||||
private BeatmapManager beatmaps;
|
||||
private RulesetStore rulesets;
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(GameHost host, AudioManager audio)
|
||||
{
|
||||
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
|
||||
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, host, Beatmap.Default));
|
||||
beatmaps.Import(TestResources.GetTestBeatmapForImport(true)).Wait();
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public new void Setup() => Schedule(() =>
|
||||
{
|
||||
var beatmap = beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.All).First().Beatmaps.First();
|
||||
|
||||
Beatmap.Value = beatmaps.GetWorkingBeatmap(beatmap);
|
||||
|
||||
Child = button = new RealtimeReadyButton
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Size = new Vector2(200, 50),
|
||||
SelectedItem =
|
||||
{
|
||||
Value = new PlaylistItem
|
||||
{
|
||||
Beatmap = { Value = beatmap },
|
||||
Ruleset = { Value = beatmap.Ruleset }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Client.AddUser(API.LocalUser.Value);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void TestToggleStateWhenNotHost()
|
||||
{
|
||||
AddStep("add second user as host", () =>
|
||||
{
|
||||
Client.AddUser(new User { Id = 2, Username = "Another user" });
|
||||
Client.TransferHost(2);
|
||||
});
|
||||
|
||||
addClickButtonStep();
|
||||
AddAssert("user is ready", () => Client.Room?.Users[0].State == MultiplayerUserState.Ready);
|
||||
|
||||
addClickButtonStep();
|
||||
AddAssert("user is idle", () => Client.Room?.Users[0].State == MultiplayerUserState.Idle);
|
||||
}
|
||||
|
||||
[TestCase(true)]
|
||||
[TestCase(false)]
|
||||
public void TestToggleStateWhenHost(bool allReady)
|
||||
{
|
||||
AddStep("setup", () =>
|
||||
{
|
||||
Client.TransferHost(Client.Room?.Users[0].UserID ?? 0);
|
||||
|
||||
if (!allReady)
|
||||
Client.AddUser(new User { Id = 2, Username = "Another user" });
|
||||
});
|
||||
|
||||
addClickButtonStep();
|
||||
AddAssert("user is ready", () => Client.Room?.Users[0].State == MultiplayerUserState.Ready);
|
||||
|
||||
addClickButtonStep();
|
||||
AddAssert("match started", () => Client.Room?.Users[0].State == MultiplayerUserState.WaitingForLoad);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestBecomeHostWhileReady()
|
||||
{
|
||||
AddStep("add host", () =>
|
||||
{
|
||||
Client.AddUser(new User { Id = 2, Username = "Another user" });
|
||||
Client.TransferHost(2);
|
||||
});
|
||||
|
||||
addClickButtonStep();
|
||||
AddStep("make user host", () => Client.TransferHost(Client.Room?.Users[0].UserID ?? 0));
|
||||
|
||||
addClickButtonStep();
|
||||
AddAssert("match started", () => Client.Room?.Users[0].State == MultiplayerUserState.WaitingForLoad);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLoseHostWhileReady()
|
||||
{
|
||||
AddStep("setup", () =>
|
||||
{
|
||||
Client.TransferHost(Client.Room?.Users[0].UserID ?? 0);
|
||||
Client.AddUser(new User { Id = 2, Username = "Another user" });
|
||||
});
|
||||
|
||||
addClickButtonStep();
|
||||
AddStep("transfer host", () => Client.TransferHost(Client.Room?.Users[1].UserID ?? 0));
|
||||
|
||||
addClickButtonStep();
|
||||
AddAssert("match not started", () => Client.Room?.Users[0].State == MultiplayerUserState.Idle);
|
||||
}
|
||||
|
||||
private void addClickButtonStep() => AddStep("click button", () =>
|
||||
{
|
||||
InputManager.MoveMouseTo(button);
|
||||
InputManager.Click(MouseButton.Left);
|
||||
});
|
||||
}
|
||||
}
|
@ -79,7 +79,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
/// <param name="room">The API <see cref="Room"/>.</param>
|
||||
public async Task JoinRoom(Room room)
|
||||
{
|
||||
Debug.Assert(Room == null);
|
||||
if (Room != null)
|
||||
throw new InvalidOperationException("Cannot join a multiplayer room while already in one.");
|
||||
|
||||
Debug.Assert(room.RoomID.Value != null);
|
||||
|
||||
apiRoom = room;
|
||||
@ -162,6 +164,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
Task IMultiplayerClient.RoomStateChanged(MultiplayerRoomState state)
|
||||
{
|
||||
if (Room == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
Schedule(() =>
|
||||
{
|
||||
if (Room == null)
|
||||
@ -194,6 +199,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
async Task IMultiplayerClient.UserJoined(MultiplayerRoomUser user)
|
||||
{
|
||||
if (Room == null)
|
||||
return;
|
||||
|
||||
await PopulateUser(user);
|
||||
|
||||
Schedule(() =>
|
||||
@ -209,6 +217,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
Task IMultiplayerClient.UserLeft(MultiplayerRoomUser user)
|
||||
{
|
||||
if (Room == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
Schedule(() =>
|
||||
{
|
||||
if (Room == null)
|
||||
@ -225,6 +236,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
Task IMultiplayerClient.HostChanged(int userId)
|
||||
{
|
||||
if (Room == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
Schedule(() =>
|
||||
{
|
||||
if (Room == null)
|
||||
@ -251,6 +265,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
Task IMultiplayerClient.UserStateChanged(int userId, MultiplayerUserState state)
|
||||
{
|
||||
if (Room == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
Schedule(() =>
|
||||
{
|
||||
if (Room == null)
|
||||
@ -269,6 +286,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
Task IMultiplayerClient.LoadRequested()
|
||||
{
|
||||
if (Room == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
Schedule(() =>
|
||||
{
|
||||
if (Room == null)
|
||||
@ -282,15 +302,15 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
Task IMultiplayerClient.MatchStarted()
|
||||
{
|
||||
Debug.Assert(Room != null);
|
||||
var players = Room.Users.Where(u => u.State == MultiplayerUserState.Playing).Select(u => u.UserID).ToList();
|
||||
if (Room == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
Schedule(() =>
|
||||
{
|
||||
if (Room == null)
|
||||
return;
|
||||
|
||||
PlayingUsers.AddRange(players);
|
||||
PlayingUsers.AddRange(Room.Users.Where(u => u.State == MultiplayerUserState.Playing).Select(u => u.UserID));
|
||||
|
||||
MatchStarted?.Invoke();
|
||||
});
|
||||
@ -300,6 +320,9 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
|
||||
Task IMultiplayerClient.ResultsReady()
|
||||
{
|
||||
if (Room == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
Schedule(() =>
|
||||
{
|
||||
if (Room == null)
|
||||
|
@ -11,28 +11,22 @@ using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osu.Game.Online.Multiplayer;
|
||||
|
||||
namespace osu.Game.Screens.Multi.Match.Components
|
||||
namespace osu.Game.Screens.Multi.Components
|
||||
{
|
||||
public class ReadyButton : TriangleButton
|
||||
public abstract class ReadyButton : TriangleButton
|
||||
{
|
||||
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
|
||||
|
||||
[Resolved(typeof(Room), nameof(Room.EndDate))]
|
||||
private Bindable<DateTimeOffset> endDate { get; set; }
|
||||
public new readonly BindableBool Enabled = new BindableBool();
|
||||
|
||||
[Resolved]
|
||||
private IBindable<WorkingBeatmap> gameBeatmap { get; set; }
|
||||
protected IBindable<WorkingBeatmap> GameBeatmap { get; private set; }
|
||||
|
||||
[Resolved]
|
||||
private BeatmapManager beatmaps { get; set; }
|
||||
|
||||
private bool hasBeatmap;
|
||||
|
||||
public ReadyButton()
|
||||
{
|
||||
Text = "Start";
|
||||
}
|
||||
|
||||
private IBindable<WeakReference<BeatmapSetInfo>> managerUpdated;
|
||||
private IBindable<WeakReference<BeatmapSetInfo>> managerRemoved;
|
||||
|
||||
@ -45,10 +39,6 @@ namespace osu.Game.Screens.Multi.Match.Components
|
||||
managerRemoved.BindValueChanged(beatmapRemoved);
|
||||
|
||||
SelectedItem.BindValueChanged(item => updateSelectedItem(item.NewValue), true);
|
||||
|
||||
BackgroundColour = colours.Green;
|
||||
Triangles.ColourDark = colours.Green;
|
||||
Triangles.ColourLight = colours.GreenLight;
|
||||
}
|
||||
|
||||
private void updateSelectedItem(PlaylistItem item)
|
||||
@ -94,15 +84,13 @@ namespace osu.Game.Screens.Multi.Match.Components
|
||||
|
||||
private void updateEnabledState()
|
||||
{
|
||||
if (gameBeatmap.Value == null || SelectedItem.Value == null)
|
||||
if (GameBeatmap.Value == null || SelectedItem.Value == null)
|
||||
{
|
||||
Enabled.Value = false;
|
||||
base.Enabled.Value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
bool hasEnoughTime = DateTimeOffset.UtcNow.AddSeconds(30).AddMilliseconds(gameBeatmap.Value.Track.Length) < endDate.Value;
|
||||
|
||||
Enabled.Value = hasBeatmap && hasEnoughTime;
|
||||
base.Enabled.Value = hasBeatmap && Enabled.Value;
|
||||
}
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Online.Multiplayer;
|
||||
using osu.Game.Screens.Multi.Timeshift;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Screens.Multi.Match.Components
|
||||
@ -31,7 +32,7 @@ namespace osu.Game.Screens.Multi.Match.Components
|
||||
InternalChildren = new[]
|
||||
{
|
||||
background = new Box { RelativeSizeAxes = Axes.Both },
|
||||
new ReadyButton
|
||||
new TimeshiftReadyButton
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
|
@ -0,0 +1,123 @@
|
||||
// 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.Diagnostics;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Backgrounds;
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Online.Multiplayer;
|
||||
using osu.Game.Online.RealtimeMultiplayer;
|
||||
using osu.Game.Screens.Multi.Components;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Screens.Multi.RealtimeMultiplayer
|
||||
{
|
||||
public class RealtimeReadyButton : RealtimeRoomComposite
|
||||
{
|
||||
public Bindable<PlaylistItem> SelectedItem => button.SelectedItem;
|
||||
|
||||
[Resolved]
|
||||
private IAPIProvider api { get; set; }
|
||||
|
||||
[CanBeNull]
|
||||
private MultiplayerRoomUser localUser;
|
||||
|
||||
[Resolved]
|
||||
private OsuColour colours { get; set; }
|
||||
|
||||
private readonly ButtonWithTrianglesExposed button;
|
||||
|
||||
public RealtimeReadyButton()
|
||||
{
|
||||
InternalChild = button = new ButtonWithTrianglesExposed
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Size = Vector2.One,
|
||||
Enabled = { Value = true },
|
||||
Action = onClick
|
||||
};
|
||||
}
|
||||
|
||||
protected override void OnRoomChanged()
|
||||
{
|
||||
base.OnRoomChanged();
|
||||
|
||||
localUser = Room?.Users.Single(u => u.User?.Id == api.LocalUser.Value.Id);
|
||||
button.Enabled.Value = Client.Room?.State == MultiplayerRoomState.Open;
|
||||
updateState();
|
||||
}
|
||||
|
||||
private void updateState()
|
||||
{
|
||||
if (localUser == null)
|
||||
return;
|
||||
|
||||
Debug.Assert(Room != null);
|
||||
|
||||
switch (localUser.State)
|
||||
{
|
||||
case MultiplayerUserState.Idle:
|
||||
button.Text = "Ready";
|
||||
updateButtonColour(true);
|
||||
break;
|
||||
|
||||
case MultiplayerUserState.Ready:
|
||||
if (Room?.Host?.Equals(localUser) == true)
|
||||
{
|
||||
int countReady = Room.Users.Count(u => u.State == MultiplayerUserState.Ready);
|
||||
button.Text = $"Start match ({countReady} / {Room.Users.Count} ready)";
|
||||
updateButtonColour(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
button.Text = "Waiting for host...";
|
||||
updateButtonColour(false);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateButtonColour(bool green)
|
||||
{
|
||||
if (green)
|
||||
{
|
||||
button.BackgroundColour = colours.Green;
|
||||
button.Triangles.ColourDark = colours.Green;
|
||||
button.Triangles.ColourLight = colours.GreenLight;
|
||||
}
|
||||
else
|
||||
{
|
||||
button.BackgroundColour = colours.YellowDark;
|
||||
button.Triangles.ColourDark = colours.YellowDark;
|
||||
button.Triangles.ColourLight = colours.Yellow;
|
||||
}
|
||||
}
|
||||
|
||||
private void onClick()
|
||||
{
|
||||
if (localUser == null)
|
||||
return;
|
||||
|
||||
if (localUser.State == MultiplayerUserState.Idle)
|
||||
Client.ChangeState(MultiplayerUserState.Ready);
|
||||
else
|
||||
{
|
||||
if (Room?.Host?.Equals(localUser) == true)
|
||||
Client.StartMatch();
|
||||
else
|
||||
Client.ChangeState(MultiplayerUserState.Idle);
|
||||
}
|
||||
}
|
||||
|
||||
private class ButtonWithTrianglesExposed : ReadyButton
|
||||
{
|
||||
public new Triangles Triangles => base.Triangles;
|
||||
}
|
||||
}
|
||||
}
|
@ -51,7 +51,7 @@ namespace osu.Game.Screens.Multi.RealtimeMultiplayer
|
||||
var joinedRoom = JoinedRoom.Value;
|
||||
|
||||
base.PartRoom();
|
||||
multiplayerClient.LeaveRoom().Wait();
|
||||
multiplayerClient.LeaveRoom();
|
||||
|
||||
// Todo: This is not the way to do this. Basically when we're the only participant and the room closes, there's no way to know if this is actually the case.
|
||||
RemoveRoom(joinedRoom);
|
||||
|
38
osu.Game/Screens/Multi/Timeshift/TimeshiftReadyButton.cs
Normal file
38
osu.Game/Screens/Multi/Timeshift/TimeshiftReadyButton.cs
Normal file
@ -0,0 +1,38 @@
|
||||
// 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 osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Online.Multiplayer;
|
||||
using osu.Game.Screens.Multi.Components;
|
||||
|
||||
namespace osu.Game.Screens.Multi.Timeshift
|
||||
{
|
||||
public class TimeshiftReadyButton : ReadyButton
|
||||
{
|
||||
[Resolved(typeof(Room), nameof(Room.EndDate))]
|
||||
private Bindable<DateTimeOffset> endDate { get; set; }
|
||||
|
||||
public TimeshiftReadyButton()
|
||||
{
|
||||
Text = "Start";
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
BackgroundColour = colours.Green;
|
||||
Triangles.ColourDark = colours.Green;
|
||||
Triangles.ColourLight = colours.GreenLight;
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
Enabled.Value = DateTimeOffset.UtcNow.AddSeconds(30).AddMilliseconds(GameBeatmap.Value.Track.Length) < endDate.Value;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user