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

225 lines
7.6 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.
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 osuTK;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
{
2022-03-23 09:37:53 +08:00
public 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
private Sample sampleReady;
private Sample sampleReadyAll;
private Sample sampleUnready;
2022-03-17 18:05:28 +08:00
private readonly BindableBool enabled = new BindableBool();
private readonly CountdownButton 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[]
{
new ReadyButton
{
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
Action = onReadyClick,
Enabled = { BindTarget = enabled },
},
countdownButton = new CountdownButton
{
RelativeSizeAxes = Axes.Y,
Size = new Vector2(40, 1),
Alpha = 0,
Action = startCountdown,
Enabled = { BindTarget = enabled }
}
}
}
};
}
[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();
}
private void onReadyClick()
{
if (Room == null)
return;
Debug.Assert(clickOperation == null);
clickOperation = ongoingOperationTracker.BeginOperation();
// Ensure the current user becomes ready before being able to do anything else (start match, stop countdown, unready).
if (!isReady() || !Client.IsHost)
{
toggleReady();
return;
}
// Local user is the room host and is in a ready state.
// The only action they can take is to stop a countdown if one's currently running.
if (Room.Countdown != null)
{
stopCountdown();
return;
}
2022-03-17 17:43:04 +08:00
// And if a countdown isn't running, start the match.
startMatch();
bool isReady() => Client.LocalUser?.State == MultiplayerUserState.Ready || Client.LocalUser?.State == MultiplayerUserState.Spectating;
void toggleReady() => Client.ToggleReady().ContinueWith(_ => endOperation());
void stopCountdown() => Client.SendMatchRequest(new StopCountdownRequest()).ContinueWith(_ => endOperation());
2022-03-17 17:43:04 +08:00
void startMatch() => Client.StartMatch().ContinueWith(t =>
{
// accessing Exception here silences any potential errors from the antecedent task
if (t.Exception != null)
{
// gameplay was not started due to an exception; unblock button.
endOperation();
}
// gameplay is starting, the button will be unblocked on load requested.
});
}
2022-03-17 18:05:28 +08:00
private void startCountdown(TimeSpan duration)
{
Debug.Assert(clickOperation == null);
clickOperation = ongoingOperationTracker.BeginOperation();
2022-03-18 20:05:19 +08:00
Client.SendMatchRequest(new StartMatchCountdownRequest { Delay = duration }).ContinueWith(_ => endOperation());
2022-03-17 18:05:28 +08:00
}
2022-03-17 17:43:04 +08:00
private void endOperation()
{
clickOperation?.Dispose();
clickOperation = null;
}
private void updateState()
{
if (Room == null)
{
2022-03-17 18:05:28 +08:00
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 (Room.Countdown != null)
countdownButton.Alpha = 0;
else
2022-03-17 18:05:28 +08:00
{
switch (localUser?.State)
{
default:
countdownButton.Alpha = 0;
break;
case MultiplayerUserState.Spectating:
case MultiplayerUserState.Ready:
countdownButton.Alpha = Room.Host?.Equals(localUser) == true ? 1 : 0;
break;
}
2022-03-17 18:05:28 +08:00
}
enabled.Value =
Room.State == MultiplayerRoomState.Open
&& 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
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.
if (localUser?.State == MultiplayerUserState.Spectating)
2022-03-17 18:05:28 +08:00
enabled.Value &= Room.Host?.Equals(localUser) == true && newCountReady > 0;
2021-04-07 15:35:36 +08:00
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;
});
}
}
}