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.
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
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;
|
2020-12-23 16:10:34 +08:00
|
|
|
using osu.Game.Extensions;
|
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;
|
|
|
|
using osu.Game.Online.RealtimeMultiplayer;
|
|
|
|
using osu.Game.Screens.Multi.Components;
|
|
|
|
using osuTK;
|
|
|
|
|
2020-12-20 22:49:06 +08:00
|
|
|
namespace osu.Game.Screens.Multi.RealtimeMultiplayer.Match
|
2020-12-19 01:59:11 +08:00
|
|
|
{
|
|
|
|
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; }
|
|
|
|
|
2020-12-24 16:17:45 +08:00
|
|
|
private SampleChannel sampleReadyCount;
|
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
private readonly ButtonWithTrianglesExposed button;
|
|
|
|
|
2020-12-24 16:17:45 +08:00
|
|
|
private int countReady;
|
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
public RealtimeReadyButton()
|
|
|
|
{
|
|
|
|
InternalChild = button = new ButtonWithTrianglesExposed
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Size = Vector2.One,
|
|
|
|
Enabled = { Value = true },
|
|
|
|
Action = onClick
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-24 16:17:45 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(AudioManager audio)
|
|
|
|
{
|
|
|
|
sampleReadyCount = audio.Samples.Get(@"SongSelect/select-difficulty");
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
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);
|
|
|
|
|
2020-12-24 16:17:45 +08:00
|
|
|
int newCountReady = Room.Users.Count(u => u.State == MultiplayerUserState.Ready);
|
|
|
|
|
|
|
|
string countText = $"({newCountReady} / {Room.Users.Count} ready)";
|
|
|
|
|
2020-12-19 01:59:11 +08:00
|
|
|
switch (localUser.State)
|
|
|
|
{
|
|
|
|
case MultiplayerUserState.Idle:
|
|
|
|
button.Text = "Ready";
|
|
|
|
updateButtonColour(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiplayerUserState.Ready:
|
|
|
|
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
|
|
|
|
|
|
|
if (newCountReady != countReady)
|
|
|
|
{
|
|
|
|
countReady = newCountReady;
|
|
|
|
Scheduler.AddOnce(playSound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void playSound()
|
|
|
|
{
|
2020-12-24 19:45:01 +08:00
|
|
|
if (sampleReadyCount == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sampleReadyCount.Frequency.Value = 0.77f + countReady * 0.06f;
|
|
|
|
sampleReadyCount.Play();
|
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 void onClick()
|
|
|
|
{
|
|
|
|
if (localUser == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (localUser.State == MultiplayerUserState.Idle)
|
2020-12-23 16:14:58 +08:00
|
|
|
Client.ChangeState(MultiplayerUserState.Ready).CatchUnobservedExceptions(true);
|
2020-12-19 01:59:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Room?.Host?.Equals(localUser) == true)
|
2020-12-23 16:14:58 +08:00
|
|
|
Client.StartMatch().CatchUnobservedExceptions(true);
|
2020-12-19 01:59:11 +08:00
|
|
|
else
|
2020-12-23 16:14:58 +08:00
|
|
|
Client.ChangeState(MultiplayerUserState.Idle).CatchUnobservedExceptions(true);
|
2020-12-19 01:59:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ButtonWithTrianglesExposed : ReadyButton
|
|
|
|
{
|
|
|
|
public new Triangles Triangles => base.Triangles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|