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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2020-12-29 14:51:46 +08:00
|
|
|
using System;
|
2022-03-17 17:43:04 +08:00
|
|
|
using System.Diagnostics;
|
2020-12-19 01:59:11 +08:00
|
|
|
using System.Linq;
|
2022-03-17 17:43:04 +08:00
|
|
|
using JetBrains.Annotations;
|
2020-12-19 01:59:11 +08:00
|
|
|
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;
|
2022-03-17 18:05:28 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-08-27 17:57:18 +08:00
|
|
|
using osu.Framework.Threading;
|
2020-12-19 01:59:11 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2022-03-17 18:26:42 +08:00
|
|
|
using osu.Game.Online.Multiplayer.Countdown;
|
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
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class MatchStartControl : MultiplayerRoomComposite
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
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
|
|
|
|
2022-03-17 17:43:04 +08:00
|
|
|
[CanBeNull]
|
|
|
|
private IDisposable clickOperation;
|
2021-01-08 16:24:55 +08:00
|
|
|
|
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
|
|
|
|
2022-03-25 17:29:00 +08:00
|
|
|
private readonly MultiplayerReadyButton readyButton;
|
2022-03-24 13:28:38 +08:00
|
|
|
private readonly MultiplayerCountdownButton countdownButton;
|
2020-12-24 16:17:45 +08:00
|
|
|
private int countReady;
|
2021-08-27 17:57:18 +08:00
|
|
|
private ScheduledDelegate readySampleDelegate;
|
2022-03-17 17:43:04 +08:00
|
|
|
private IBindable<bool> operationInProgress;
|
2021-08-27 17:57:18 +08:00
|
|
|
|
2022-03-23 09:37:53 +08:00
|
|
|
public MatchStartControl()
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
2022-03-17 18:05:28 +08:00
|
|
|
InternalChild = new GridContainer
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2022-03-17 18:05:28 +08:00
|
|
|
ColumnDimensions = new[]
|
|
|
|
{
|
|
|
|
new Dimension(),
|
|
|
|
new Dimension(GridSizeMode.AutoSize)
|
|
|
|
},
|
|
|
|
Content = new[]
|
|
|
|
{
|
|
|
|
new Drawable[]
|
|
|
|
{
|
2022-03-25 17:29:00 +08:00
|
|
|
readyButton = new MultiplayerReadyButton
|
2022-03-17 18:05:28 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Size = Vector2.One,
|
|
|
|
Action = onReadyClick,
|
|
|
|
},
|
2022-03-24 13:28:38 +08:00
|
|
|
countdownButton = new MultiplayerCountdownButton
|
2022-03-17 18:05:28 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
Size = new Vector2(40, 1),
|
|
|
|
Alpha = 0,
|
|
|
|
Action = startCountdown,
|
2022-03-25 17:40:32 +08:00
|
|
|
CancelAction = cancelCountdown
|
2022-03-17 18:05:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 01:59:11 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-10 19:08:59 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-02-24 15:20:40 +08:00
|
|
|
CurrentPlaylistItem.BindValueChanged(_ => updateState());
|
2021-12-10 19:08:59 +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();
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:43:04 +08:00
|
|
|
protected override void OnRoomLoadRequested()
|
|
|
|
{
|
|
|
|
base.OnRoomLoadRequested();
|
|
|
|
endOperation();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onReadyClick()
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Debug.Assert(clickOperation == null);
|
|
|
|
clickOperation = ongoingOperationTracker.BeginOperation();
|
|
|
|
|
2022-09-01 17:53:35 +08:00
|
|
|
if (isReady() && Client.IsHost && !Room.ActiveCountdowns.Any(c => c is MatchStartCountdown))
|
2022-03-25 17:40:32 +08:00
|
|
|
startMatch();
|
|
|
|
else
|
2022-03-17 17:43:04 +08:00
|
|
|
toggleReady();
|
|
|
|
|
|
|
|
bool isReady() => Client.LocalUser?.State == MultiplayerUserState.Ready || Client.LocalUser?.State == MultiplayerUserState.Spectating;
|
|
|
|
|
2022-03-30 22:58:30 +08:00
|
|
|
void toggleReady() => Client.ToggleReady().FireAndForget(
|
|
|
|
onSuccess: endOperation,
|
|
|
|
onError: _ => endOperation());
|
2022-03-17 17:43:04 +08:00
|
|
|
|
2022-03-30 22:58:30 +08:00
|
|
|
void startMatch() => Client.StartMatch().FireAndForget(onSuccess: () =>
|
2022-03-17 17:43:04 +08:00
|
|
|
{
|
|
|
|
// gameplay is starting, the button will be unblocked on load requested.
|
2022-03-30 22:58:30 +08:00
|
|
|
}, onError: _ =>
|
|
|
|
{
|
|
|
|
// gameplay was not started due to an exception; unblock button.
|
|
|
|
endOperation();
|
2022-03-17 17:43:04 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:05:28 +08:00
|
|
|
private void startCountdown(TimeSpan duration)
|
|
|
|
{
|
2022-03-17 18:26:42 +08:00
|
|
|
Debug.Assert(clickOperation == null);
|
|
|
|
clickOperation = ongoingOperationTracker.BeginOperation();
|
|
|
|
|
2022-03-23 14:21:16 +08:00
|
|
|
Client.SendMatchRequest(new StartMatchCountdownRequest { Duration = duration }).ContinueWith(_ => endOperation());
|
2022-03-17 18:05:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-25 17:40:32 +08:00
|
|
|
private void cancelCountdown()
|
|
|
|
{
|
2022-09-01 17:53:35 +08:00
|
|
|
if (Client.Room == null)
|
|
|
|
return;
|
|
|
|
|
2022-03-25 17:40:32 +08:00
|
|
|
Debug.Assert(clickOperation == null);
|
|
|
|
clickOperation = ongoingOperationTracker.BeginOperation();
|
|
|
|
|
2022-09-01 17:53:35 +08:00
|
|
|
MultiplayerCountdown countdown = Client.Room.ActiveCountdowns.Single(c => c is MatchStartCountdown);
|
|
|
|
Client.SendMatchRequest(new StopCountdownRequest(countdown.ID)).ContinueWith(_ => endOperation());
|
2022-03-25 17:40:32 +08:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:43:04 +08:00
|
|
|
private void endOperation()
|
|
|
|
{
|
|
|
|
clickOperation?.Dispose();
|
|
|
|
clickOperation = null;
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
private void updateState()
|
|
|
|
{
|
2022-03-17 17:58:09 +08:00
|
|
|
if (Room == null)
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
2022-03-25 17:29:00 +08:00
|
|
|
readyButton.Enabled.Value = false;
|
|
|
|
countdownButton.Enabled.Value = false;
|
2022-03-17 17:58:09 +08:00
|
|
|
return;
|
2020-12-19 01:59:11 +08:00
|
|
|
}
|
2020-12-24 16:17:45 +08:00
|
|
|
|
2022-03-17 17:58:09 +08:00
|
|
|
var localUser = Client.LocalUser;
|
|
|
|
|
|
|
|
int newCountReady = Room.Users.Count(u => u.State == MultiplayerUserState.Ready);
|
|
|
|
int newCountTotal = Room.Users.Count(u => u.State != MultiplayerUserState.Spectating);
|
|
|
|
|
2022-03-25 17:29:00 +08:00
|
|
|
if (!Client.IsHost || Room.Settings.AutoStartEnabled)
|
2022-03-25 14:41:01 +08:00
|
|
|
countdownButton.Hide();
|
2022-03-17 18:26:42 +08:00
|
|
|
else
|
2022-03-17 18:05:28 +08:00
|
|
|
{
|
2022-03-17 18:26:42 +08:00
|
|
|
switch (localUser?.State)
|
|
|
|
{
|
|
|
|
default:
|
2022-03-25 14:41:01 +08:00
|
|
|
countdownButton.Hide();
|
2022-03-17 18:26:42 +08:00
|
|
|
break;
|
|
|
|
|
2022-03-25 17:29:00 +08:00
|
|
|
case MultiplayerUserState.Idle:
|
2022-03-17 18:26:42 +08:00
|
|
|
case MultiplayerUserState.Spectating:
|
|
|
|
case MultiplayerUserState.Ready:
|
2022-03-25 14:41:01 +08:00
|
|
|
countdownButton.Show();
|
2022-03-17 18:26:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-03-17 18:05:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-25 17:29:00 +08:00
|
|
|
readyButton.Enabled.Value = countdownButton.Enabled.Value =
|
2022-03-17 17:58:09 +08:00
|
|
|
Room.State == MultiplayerRoomState.Open
|
2022-02-24 15:20:40 +08:00
|
|
|
&& CurrentPlaylistItem.Value?.ID == Room.Settings.PlaylistItemId
|
2021-12-10 19:08:59 +08:00
|
|
|
&& !Room.Playlist.Single(i => i.ID == Room.Settings.PlaylistItemId).Expired
|
2021-11-10 17:42:46 +08:00
|
|
|
&& !operationInProgress.Value;
|
2020-12-29 03:59:38 +08:00
|
|
|
|
2022-03-25 17:29:00 +08:00
|
|
|
// When the local user is the host and spectating the match, the ready button should be enabled only if any users are ready.
|
2021-07-13 16:59:35 +08:00
|
|
|
if (localUser?.State == MultiplayerUserState.Spectating)
|
2022-09-01 17:53:35 +08:00
|
|
|
readyButton.Enabled.Value &= Client.IsHost && newCountReady > 0 && !Room.ActiveCountdowns.Any(c => c is MatchStartCountdown);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|