1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-31 00:02:56 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MatchStartControl.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

264 lines
9.1 KiB
C#
Raw Normal View History

// 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
using System;
2022-03-17 17:43:04 +08:00
using System.Diagnostics;
using System.Linq;
2022-03-17 17:43:04 +08:00
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2022-03-17 18:05:28 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Multiplayer.Countdown;
using osu.Game.Overlays;
2023-12-05 15:54:44 +08:00
using osu.Game.Overlays.Dialog;
using osuTK;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
{
2022-03-23 09:37:53 +08:00
public partial class MatchStartControl : MultiplayerRoomComposite
{
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
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }
private Sample sampleReady;
private Sample sampleReadyAll;
private Sample sampleUnready;
private readonly MultiplayerReadyButton readyButton;
private readonly MultiplayerCountdownButton countdownButton;
private int countReady;
private ScheduledDelegate readySampleDelegate;
2022-03-17 17:43:04 +08:00
private IBindable<bool> operationInProgress;
2022-03-23 09:37:53 +08:00
public MatchStartControl()
{
2022-03-17 18:05:28 +08:00
InternalChild = new GridContainer
{
RelativeSizeAxes = Axes.Both,
2022-03-17 18:05:28 +08:00
ColumnDimensions = new[]
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize)
},
Content = new[]
{
new Drawable[]
{
readyButton = new MultiplayerReadyButton
2022-03-17 18:05:28 +08:00
{
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
2023-12-01 17:47:40 +08:00
Action = onReadyButtonClick,
2022-03-17 18:05:28 +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
}
}
}
};
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
2020-12-29 15:20:43 +08:00
operationInProgress = ongoingOperationTracker.InProgress.GetBoundCopy();
operationInProgress.BindValueChanged(_ => updateState());
sampleReady = audio.Samples.Get(@"Multiplayer/player-ready");
sampleReadyAll = audio.Samples.Get(@"Multiplayer/player-ready-all");
sampleUnready = audio.Samples.Get(@"Multiplayer/player-unready");
}
protected override void LoadComplete()
{
base.LoadComplete();
CurrentPlaylistItem.BindValueChanged(_ => updateState());
}
protected override void OnRoomUpdated()
{
base.OnRoomUpdated();
updateState();
}
2022-03-17 17:43:04 +08:00
protected override void OnRoomLoadRequested()
{
base.OnRoomLoadRequested();
endOperation();
}
2023-12-01 17:47:40 +08:00
private void onReadyButtonClick()
2022-03-17 17:43:04 +08:00
{
if (Room == null)
return;
Debug.Assert(clickOperation == null);
clickOperation = ongoingOperationTracker.BeginOperation();
if (Client.IsHost)
{
if (Room.State == MultiplayerRoomState.Open)
{
if (isReady() && !Room.ActiveCountdowns.Any(c => c is MatchStartCountdown))
startMatch();
else
toggleReady();
}
else
{
if (dialogOverlay == null)
abortMatch();
else
dialogOverlay.Push(new ConfirmAbortDialog(abortMatch, endOperation));
}
}
else if (Room.State != MultiplayerRoomState.Closed)
2022-03-17 17:43:04 +08:00
toggleReady();
bool isReady() => Client.LocalUser?.State == MultiplayerUserState.Ready || Client.LocalUser?.State == MultiplayerUserState.Spectating;
void toggleReady() => Client.ToggleReady().FireAndForget(
onSuccess: endOperation,
onError: _ => endOperation());
2022-03-17 17:43:04 +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.
}, onError: _ =>
{
// gameplay was not started due to an exception; unblock button.
endOperation();
2022-03-17 17:43:04 +08:00
});
void abortMatch() => Client.AbortMatch().FireAndForget(endOperation, _ => endOperation());
2022-03-17 17:43:04 +08:00
}
2022-03-17 18:05:28 +08:00
private void startCountdown(TimeSpan duration)
{
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()
{
if (Client.Room == null)
return;
2022-03-25 17:40:32 +08:00
Debug.Assert(clickOperation == null);
clickOperation = ongoingOperationTracker.BeginOperation();
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;
}
private void updateState()
{
if (Room == null)
{
readyButton.Enabled.Value = false;
countdownButton.Enabled.Value = false;
return;
}
var localUser = Client.LocalUser;
int newCountReady = Room.Users.Count(u => u.State == MultiplayerUserState.Ready);
int newCountTotal = Room.Users.Count(u => u.State != MultiplayerUserState.Spectating);
if (!Client.IsHost || Room.Settings.AutoStartEnabled)
2022-03-25 14:41:01 +08:00
countdownButton.Hide();
else
2022-03-17 18:05:28 +08:00
{
switch (localUser?.State)
{
default:
2022-03-25 14:41:01 +08:00
countdownButton.Hide();
break;
case MultiplayerUserState.Idle:
case MultiplayerUserState.Spectating:
case MultiplayerUserState.Ready:
2022-03-25 14:41:01 +08:00
countdownButton.Show();
break;
}
2022-03-17 18:05:28 +08:00
}
readyButton.Enabled.Value = countdownButton.Enabled.Value =
Room.State != MultiplayerRoomState.Closed
&& CurrentPlaylistItem.Value?.ID == Room.Settings.PlaylistItemId
&& !Room.Playlist.Single(i => i.ID == Room.Settings.PlaylistItemId).Expired
&& !operationInProgress.Value;
2020-12-29 03:59:38 +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.
if (localUser?.State == MultiplayerUserState.Spectating)
readyButton.Enabled.Value &= Client.IsHost && newCountReady > 0 && !Room.ActiveCountdowns.Any(c => c is MatchStartCountdown);
2021-04-07 15:35:36 +08:00
// When the local user is not the host, the button should only be enabled when no match is in progress.
if (!Client.IsHost)
readyButton.Enabled.Value &= Room.State == MultiplayerRoomState.Open;
2023-12-04 10:00:11 +08:00
// At all times, the countdown button should only be enabled when no match is in progress.
countdownButton.Enabled.Value &= Room.State == MultiplayerRoomState.Open;
if (newCountReady == countReady)
return;
readySampleDelegate?.Cancel();
readySampleDelegate = Schedule(() =>
{
if (newCountReady > countReady)
{
if (newCountReady == newCountTotal)
sampleReadyAll?.Play();
else
sampleReady?.Play();
}
else if (newCountReady < countReady)
{
sampleUnready?.Play();
}
countReady = newCountReady;
});
}
2023-12-05 15:54:44 +08:00
public partial class ConfirmAbortDialog : DangerousActionDialog
{
public ConfirmAbortDialog(Action abortMatch, Action cancel)
{
HeaderText = "Are you sure you want to abort the match?";
DangerousAction = abortMatch;
CancelAction = cancel;
}
}
}
}