2022-03-23 09:37:53 +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;
|
|
|
|
using System.Linq;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Localisation;
|
2022-03-23 09:50:05 +08:00
|
|
|
using osu.Framework.Threading;
|
2022-03-23 09:37:53 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Backgrounds;
|
|
|
|
using osu.Game.Online.Multiplayer;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
|
|
|
|
{
|
|
|
|
public class ReadyButton : Components.ReadyButton
|
|
|
|
{
|
|
|
|
public new Triangles Triangles => base.Triangles;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private MultiplayerClient multiplayerClient { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
|
|
|
[CanBeNull]
|
|
|
|
private MultiplayerRoom room => multiplayerClient.Room;
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
multiplayerClient.RoomUpdated += () => Scheduler.AddOnce(onRoomUpdated);
|
|
|
|
onRoomUpdated();
|
|
|
|
}
|
|
|
|
|
2022-03-23 14:19:43 +08:00
|
|
|
private MultiplayerCountdown countdown;
|
|
|
|
private DateTimeOffset countdownReceivedTime;
|
2022-03-23 09:50:05 +08:00
|
|
|
private ScheduledDelegate countdownUpdateDelegate;
|
2022-03-23 09:37:53 +08:00
|
|
|
|
|
|
|
private void onRoomUpdated()
|
|
|
|
{
|
2022-03-23 14:19:43 +08:00
|
|
|
if (countdown == null && room?.Countdown != null)
|
|
|
|
countdownReceivedTime = DateTimeOffset.Now;
|
|
|
|
|
|
|
|
countdown = room?.Countdown;
|
2022-03-23 09:50:05 +08:00
|
|
|
|
|
|
|
if (room?.Countdown != null)
|
|
|
|
countdownUpdateDelegate ??= Scheduler.AddDelayed(updateButtonText, 1000, true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
countdownUpdateDelegate?.Cancel();
|
|
|
|
countdownUpdateDelegate = null;
|
|
|
|
}
|
2022-03-23 14:19:43 +08:00
|
|
|
|
|
|
|
updateButtonText();
|
|
|
|
updateButtonColour();
|
2022-03-23 09:37:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateButtonText()
|
|
|
|
{
|
|
|
|
if (room == null)
|
|
|
|
{
|
|
|
|
Text = "Ready";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var localUser = multiplayerClient.LocalUser;
|
|
|
|
|
|
|
|
int countReady = room.Users.Count(u => u.State == MultiplayerUserState.Ready);
|
|
|
|
int countTotal = room.Users.Count(u => u.State != MultiplayerUserState.Spectating);
|
|
|
|
string countText = $"({countReady} / {countTotal} ready)";
|
|
|
|
|
2022-03-23 14:19:43 +08:00
|
|
|
if (countdown != null)
|
2022-03-23 09:37:53 +08:00
|
|
|
{
|
2022-03-23 14:19:43 +08:00
|
|
|
TimeSpan timeElapsed = DateTimeOffset.Now - countdownReceivedTime;
|
|
|
|
TimeSpan countdownRemaining;
|
|
|
|
|
|
|
|
if (timeElapsed > countdown.TimeRemaining)
|
|
|
|
countdownRemaining = TimeSpan.Zero;
|
|
|
|
else
|
|
|
|
countdownRemaining = countdown.TimeRemaining - timeElapsed;
|
|
|
|
|
|
|
|
string countdownText = $"Starting in {countdownRemaining:mm\\:ss}";
|
2022-03-23 09:37:53 +08:00
|
|
|
|
|
|
|
switch (localUser?.State)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
Text = $"Ready ({countdownText.ToLowerInvariant()})";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiplayerUserState.Spectating:
|
|
|
|
case MultiplayerUserState.Ready:
|
|
|
|
Text = $"{countdownText} {countText}";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (localUser?.State)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
Text = "Ready";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiplayerUserState.Spectating:
|
|
|
|
case MultiplayerUserState.Ready:
|
|
|
|
Text = room.Host?.Equals(localUser) == true
|
|
|
|
? $"Start match {countText}"
|
|
|
|
: $"Waiting for host... {countText}";
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateButtonColour()
|
|
|
|
{
|
|
|
|
if (room == null)
|
|
|
|
{
|
|
|
|
setGreen();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var localUser = multiplayerClient.LocalUser;
|
|
|
|
|
|
|
|
switch (localUser?.State)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
setGreen();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiplayerUserState.Spectating:
|
|
|
|
case MultiplayerUserState.Ready:
|
|
|
|
if (room?.Host?.Equals(localUser) == true && room.Countdown == null)
|
|
|
|
setGreen();
|
|
|
|
else
|
|
|
|
setYellow();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setYellow()
|
|
|
|
{
|
|
|
|
BackgroundColour = colours.YellowDark;
|
|
|
|
Triangles.ColourDark = colours.YellowDark;
|
|
|
|
Triangles.ColourLight = colours.Yellow;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setGreen()
|
|
|
|
{
|
|
|
|
BackgroundColour = colours.Green;
|
|
|
|
Triangles.ColourDark = colours.Green;
|
|
|
|
Triangles.ColourLight = colours.GreenLight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
if (multiplayerClient != null)
|
|
|
|
multiplayerClient.RoomUpdated -= onRoomUpdated;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override LocalisableString TooltipText
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (room?.Countdown != null && multiplayerClient.IsHost && multiplayerClient.LocalUser?.State == MultiplayerUserState.Ready)
|
|
|
|
return "Cancel countdown";
|
|
|
|
|
|
|
|
return base.TooltipText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|