Skip to content

Commit

Permalink
Merge pull request #50 from ILW8/mp-start
Browse files Browse the repository at this point in the history
add !mp start
  • Loading branch information
ILW8 authored May 9, 2024
2 parents bf87c42 + 18126ac commit b582ec3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 21 deletions.
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

0 comments on commit b582ec3

Please sign in to comment.