Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add !mp start #50

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion osu.Game/Online/Chat/StandAloneChatDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,26 @@ private void postMessage(TextBox sender, bool newText)
break;

case @"timer":
chatTimerHandler?.SetTimer(TimeSpan.FromSeconds(numericParam), Time.Current, Channel.Value);
chatTimerHandler?.SetTimer(TimeSpan.FromSeconds(numericParam), Time.Current);
break;

case @"start":
// we intentionally do this check both in startMatch and here
if (!Client.IsHost)
{
Logger.Log(@"Tried to start match when user is not host of the room. Cancelling!", LoggingTarget.Runtime, LogLevel.Important);
return;
}

chatTimerHandler?.SetTimer(TimeSpan.FromSeconds(numericParam), Time.Current, messagePrefix: @"Match starts in", onTimerComplete: startMatch);
break;
}
}
else
{
switch (parts[1])
{
// i don't think this belongs here in the first place... whatever
// ReSharper disable once StringLiteralTypo
case @"aborttimer":
abortTimer();
Expand Down Expand Up @@ -511,6 +523,11 @@ private void postMessage(TextBox sender, bool newText)
Client.AbortMatch().FireAndForget();
break;

// start immediately
case @"start":
startMatch();
break;

// ReSharper disable once StringLiteralTypo
case @"aborttimer":
abortTimer();
Expand All @@ -524,6 +541,25 @@ private void postMessage(TextBox sender, bool newText)
TextBox.Text = string.Empty;
}

private void startMatch()
{
if (!Client.IsHost)
{
Logger.Log(@"Tried to start match when user is not host of the room. Cancelling!", LoggingTarget.Runtime, LogLevel.Important);
return;
}

// no one is ready, server won't allow starting the map
if (Client.Room?.Users.All(u => u.State != MultiplayerUserState.Ready) ?? false)
{
Logger.Log(@"Tried to start match when no player is ready. Cancelling!", LoggingTarget.Runtime, LogLevel.Important);
botMessageQueue.Enqueue(new Tuple<string, Channel>(@"No player ready, cannot start match.", Channel.Value));
return;
}

Client.StartMatch().FireAndForget();
}

private void abortTimer()
{
chatTimerHandler.Abort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,21 @@ public class Pool

public partial class ChatTimerHandler : Component
{
protected readonly MultiplayerCountdown MultiplayerChatTimerCountdown = new MatchStartCountdown { TimeRemaining = TimeSpan.Zero };
protected double CountdownChangeTime;
private readonly MultiplayerCountdown multiplayerChatTimerCountdown = new MatchStartCountdown { TimeRemaining = TimeSpan.Zero };
private double countdownChangeTime;
private string countdownMessagePrefix;

private TimeSpan countdownTimeRemaining
{
get
{
double timeElapsed = Time.Current - CountdownChangeTime;
double timeElapsed = Time.Current - countdownChangeTime;
TimeSpan remaining;

if (timeElapsed > MultiplayerChatTimerCountdown.TimeRemaining.TotalMilliseconds)
if (timeElapsed > multiplayerChatTimerCountdown.TimeRemaining.TotalMilliseconds)
remaining = TimeSpan.Zero;
else
remaining = MultiplayerChatTimerCountdown.TimeRemaining - TimeSpan.FromMilliseconds(timeElapsed);
remaining = multiplayerChatTimerCountdown.TimeRemaining - TimeSpan.FromMilliseconds(timeElapsed);

return remaining;
}
Expand All @@ -95,13 +96,12 @@ private TimeSpan countdownTimeRemaining
[Resolved]
protected MultiplayerClient Client { get; private set; }

[Resolved]
protected ChannelManager ChannelManager { get; private set; }

protected Channel TargetChannel;

[CanBeNull]
public event Action<string> OnChatMessageDue;

[CanBeNull]
public event Action OnTimerComplete;

[BackgroundDependencyLoader]
private void load()
{
Expand All @@ -113,21 +113,30 @@ private void load()
if (countdownUpdateDelegate == null)
return;

Logger.Log($@"Timer scheduled delegate called, room state is {Client.Room?.State}");
Logger.Log($@"Room state updated: {Client.Room?.State}. Aborting timer.");
countdownUpdateDelegate?.Cancel();
countdownUpdateDelegate = null;
OnChatMessageDue?.Invoke(@"Countdown aborted (game started)");
};
}

public void SetTimer(TimeSpan duration, double startTime, Channel targetChannel)
public void SetTimer(TimeSpan duration, double startTime, string messagePrefix = @"Countdown ends in", Action onTimerComplete = null)
{
// OnChatMessageDue = null;
MultiplayerChatTimerCountdown.TimeRemaining = duration;
CountdownChangeTime = startTime;
TargetChannel = targetChannel;
Logger.Log($@"Starting new timer ({startTime}, {duration}, prefix: '{messagePrefix}', completeAction: {onTimerComplete?.Method.Name ?? @"null"})");

countdownUpdateDelegate?.Cancel();
multiplayerChatTimerCountdown.TimeRemaining = duration;
countdownChangeTime = startTime;

if (countdownUpdateDelegate != null)
{
Logger.Log(@"Aborting existing timer");
countdownUpdateDelegate.Cancel();
countdownUpdateDelegate = null;
OnChatMessageDue?.Invoke(@"Countdown aborted");
}

OnTimerComplete = onTimerComplete;
countdownMessagePrefix = messagePrefix;
countdownUpdateDelegate = Scheduler.Add(sendTimerMessage);
}

Expand All @@ -151,12 +160,18 @@ private void processTimerEvent()
private void sendTimerMessage()
{
int secondsRemaining = (int)Math.Round(countdownTimeRemaining.TotalSeconds);
string message = secondsRemaining == 0 ? @"Countdown finished" : $@"Countdown ends in {secondsRemaining} seconds";
string message = secondsRemaining <= 0 ? @"Countdown finished" : $@"{countdownMessagePrefix} {secondsRemaining} seconds";
OnChatMessageDue?.Invoke(message);
Logger.Log($@"Sent timer message, {secondsRemaining} seconds remaining on timer.");

if (secondsRemaining <= 0) return;
if (secondsRemaining <= 0)
{
countdownUpdateDelegate = null;
OnTimerComplete?.Invoke();
return;
}

Logger.Log($@"Sent timer message, {secondsRemaining} seconds remaining on timer. ");
Logger.Log($@"Scheduling {nameof(processTimerEvent)} in 100ms.");
countdownUpdateDelegate = Scheduler.AddDelayed(processTimerEvent, 100);
}

Expand Down Expand Up @@ -483,6 +498,7 @@ public override void OnEntering(ScreenTransitionEvent e)
public override bool OnExiting(ScreenExitEvent e)
{
chatTimerHandler.OnChatMessageDue -= chatDisplay.EnqueueBotMessage;
chatTimerHandler.Abort();

// room has not been created yet or we're offline; exit immediately.
if (client.Room == null || !IsConnected)
Expand Down