2020-12-19 01:59:11 +08:00
|
|
|
// 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.
|
|
|
|
|
2020-12-29 14:51:46 +08:00
|
|
|
using System;
|
2020-12-19 01:59:11 +08:00
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
2020-12-24 16:17:45 +08:00
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Audio.Sample;
|
2020-12-19 01:59:11 +08:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
2021-08-27 17:57:18 +08:00
|
|
|
using osu.Framework.Threading;
|
2020-12-19 01:59:11 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Backgrounds;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.Multiplayer;
|
2020-12-25 23:50:00 +08:00
|
|
|
using osu.Game.Screens.OnlinePlay.Components;
|
2020-12-19 01:59:11 +08:00
|
|
|
using osuTK;
|
|
|
|
|
2020-12-25 23:50:00 +08:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
2020-12-25 12:38:11 +08:00
|
|
|
public class MultiplayerReadyButton : MultiplayerRoomComposite
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
2020-12-30 23:29:36 +08:00
|
|
|
public Action OnReadyClick
|
2020-12-29 14:51:46 +08:00
|
|
|
{
|
|
|
|
set => button.Action = value;
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
2020-12-29 03:59:38 +08:00
|
|
|
[Resolved]
|
2020-12-29 15:20:43 +08:00
|
|
|
private OngoingOperationTracker ongoingOperationTracker { get; set; }
|
2020-12-29 03:59:38 +08:00
|
|
|
|
2021-01-08 16:24:55 +08:00
|
|
|
private IBindable<bool> operationInProgress;
|
|
|
|
|
2021-08-26 14:29:22 +08:00
|
|
|
private Sample sampleReady;
|
|
|
|
private Sample sampleReadyAll;
|
|
|
|
private Sample sampleUnready;
|
2020-12-24 16:17:45 +08:00
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
private readonly ButtonWithTrianglesExposed button;
|
|
|
|
|
2020-12-24 16:17:45 +08:00
|
|
|
private int countReady;
|
|
|
|
|
2021-08-27 17:57:18 +08:00
|
|
|
private ScheduledDelegate readySampleDelegate;
|
|
|
|
|
2020-12-25 12:38:11 +08:00
|
|
|
public MultiplayerReadyButton()
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
|
|
|
InternalChild = button = new ButtonWithTrianglesExposed
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Size = Vector2.One,
|
|
|
|
Enabled = { Value = true },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-24 16:17:45 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(AudioManager audio)
|
|
|
|
{
|
2020-12-29 15:20:43 +08:00
|
|
|
operationInProgress = ongoingOperationTracker.InProgress.GetBoundCopy();
|
|
|
|
operationInProgress.BindValueChanged(_ => updateState());
|
2021-08-26 14:29:22 +08:00
|
|
|
|
|
|
|
sampleReady = audio.Samples.Get(@"Multiplayer/player-ready");
|
|
|
|
sampleReadyAll = audio.Samples.Get(@"Multiplayer/player-ready-all");
|
|
|
|
sampleUnready = audio.Samples.Get(@"Multiplayer/player-unready");
|
2020-12-24 16:17:45 +08:00
|
|
|
}
|
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
protected override void OnRoomUpdated()
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
2020-12-26 09:48:55 +08:00
|
|
|
base.OnRoomUpdated();
|
2020-12-19 01:59:11 +08:00
|
|
|
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateState()
|
|
|
|
{
|
2020-12-29 14:46:22 +08:00
|
|
|
var localUser = Client.LocalUser;
|
|
|
|
|
2021-07-13 16:59:35 +08:00
|
|
|
int newCountReady = Room?.Users.Count(u => u.State == MultiplayerUserState.Ready) ?? 0;
|
|
|
|
int newCountTotal = Room?.Users.Count(u => u.State != MultiplayerUserState.Spectating) ?? 0;
|
2020-12-19 01:59:11 +08:00
|
|
|
|
2021-07-13 16:59:35 +08:00
|
|
|
switch (localUser?.State)
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
2021-07-13 16:59:35 +08:00
|
|
|
default:
|
2020-12-19 01:59:11 +08:00
|
|
|
button.Text = "Ready";
|
|
|
|
updateButtonColour(true);
|
|
|
|
break;
|
|
|
|
|
2021-04-07 15:35:36 +08:00
|
|
|
case MultiplayerUserState.Spectating:
|
2020-12-19 01:59:11 +08:00
|
|
|
case MultiplayerUserState.Ready:
|
2021-07-13 16:59:35 +08:00
|
|
|
string countText = $"({newCountReady} / {newCountTotal} ready)";
|
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
if (Room?.Host?.Equals(localUser) == true)
|
|
|
|
{
|
2020-12-24 16:17:45 +08:00
|
|
|
button.Text = $"Start match {countText}";
|
2020-12-20 17:49:39 +08:00
|
|
|
updateButtonColour(true);
|
2020-12-19 01:59:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-24 16:17:45 +08:00
|
|
|
button.Text = $"Waiting for host... {countText}";
|
2020-12-19 01:59:11 +08:00
|
|
|
updateButtonColour(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2020-12-24 16:17:45 +08:00
|
|
|
|
2021-07-30 16:58:50 +08:00
|
|
|
bool enableButton = Room?.State == MultiplayerRoomState.Open && !operationInProgress.Value;
|
2020-12-29 03:59:38 +08:00
|
|
|
|
2021-04-08 15:30:48 +08:00
|
|
|
// When the local user is the host and spectating the match, the "start match" state should be enabled if any users are ready.
|
2021-07-13 16:59:35 +08:00
|
|
|
if (localUser?.State == MultiplayerUserState.Spectating)
|
2021-04-08 15:30:48 +08:00
|
|
|
enableButton &= Room?.Host?.Equals(localUser) == true && newCountReady > 0;
|
|
|
|
|
|
|
|
button.Enabled.Value = enableButton;
|
2021-04-07 15:35:36 +08:00
|
|
|
|
2021-08-26 14:29:22 +08:00
|
|
|
if (newCountReady == countReady)
|
|
|
|
return;
|
|
|
|
|
2021-08-27 17:57:18 +08:00
|
|
|
readySampleDelegate?.Cancel();
|
|
|
|
readySampleDelegate = Schedule(() =>
|
2021-08-26 14:29:22 +08:00
|
|
|
{
|
2021-08-27 17:57:18 +08:00
|
|
|
if (newCountReady > countReady)
|
|
|
|
{
|
|
|
|
if (newCountReady == newCountTotal)
|
|
|
|
sampleReadyAll?.Play();
|
|
|
|
else
|
|
|
|
sampleReady?.Play();
|
|
|
|
}
|
|
|
|
else if (newCountReady < countReady)
|
2021-08-26 14:29:22 +08:00
|
|
|
{
|
|
|
|
sampleUnready?.Play();
|
|
|
|
}
|
2020-12-24 19:45:01 +08:00
|
|
|
|
2021-08-27 17:57:18 +08:00
|
|
|
countReady = newCountReady;
|
|
|
|
});
|
2020-12-19 01:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 class ButtonWithTrianglesExposed : ReadyButton
|
|
|
|
{
|
|
|
|
public new Triangles Triangles => base.Triangles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|