Skip to content
Merged
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
109 changes: 74 additions & 35 deletions osu.Game/Online/Chat/StandAloneChatDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,16 @@ private void processChatCommands(string[] parts)
}
}
// special-case player invites
else if (parts[2].StartsWith('#') && int.TryParse(parts[2].AsSpan(1), out numericParam) && parts[1] == @"invite")
else if (parts[1] == @"invite")
{
inviteUserToRoom(numericParam);
if (parts[2].StartsWith('#') && int.TryParse(parts[2].AsSpan(1), out numericParam))
{
inviteIdToRoom(numericParam);
}
else
{
inviteUserToRoom(parts[2]);
}
}
else
{
Expand Down Expand Up @@ -693,38 +700,6 @@ private void postMessage(TextBox sender, bool newText)
}
else
{
async Task<APIUser?> queryUsername(string username)
{
string patchedUsername = username.Replace('_', ' ');

// check if user is already in the lobby
var matchingUser = Client.Room?.Users.FirstOrDefault(u => string.Equals(u.User?.Username, patchedUsername, StringComparison.OrdinalIgnoreCase));

if (matchingUser != null)
{
return matchingUser.User;
}

// try to resolve the username
userReq?.Cancel();
var tcs = new TaskCompletionSource<APIUser?>();
userReq = new GetUserRequest(patchedUsername);
userReq.Success += u =>
{
Logger.Log($@"[{nameof(StandAloneChatDisplay)}] Successfully resolved user ${u}");
tcs.TrySetResult(u);
};
userReq.Failure += e =>
{
Logger.Log($@"[{nameof(StandAloneChatDisplay)}] Could not resolve user {patchedUsername}");
tcs.TrySetResult(null);
};

API.Queue(userReq);

return await tcs.Task.ConfigureAwait(false);
}

void printRefsList() => EnqueueBotMessage($@"Match referees: {string.Join(", ", multiplayerRefereeTracker.Referees)}");

switch (parts.Length)
Expand Down Expand Up @@ -773,7 +748,39 @@ private void postMessage(TextBox sender, bool newText)
TextBox.Text = string.Empty;
}

private void inviteUserToRoom(int userId)
private async Task<APIUser?> queryUsername(string username)
{
string patchedUsername = username.Replace('_', ' ');

// check if user is already in the lobby
var matchingUser = Client.Room?.Users.FirstOrDefault(u => string.Equals(u.User?.Username, patchedUsername, StringComparison.OrdinalIgnoreCase));

if (matchingUser != null)
{
return matchingUser.User;
}

// try to resolve the username
userReq?.Cancel();
var tcs = new TaskCompletionSource<APIUser?>();
userReq = new GetUserRequest(patchedUsername);
userReq.Success += u =>
{
Logger.Log($@"[{nameof(StandAloneChatDisplay)}] Successfully resolved user ${u}");
tcs.TrySetResult(u);
};
userReq.Failure += e =>
{
Logger.Log($@"[{nameof(StandAloneChatDisplay)}] Could not resolve user {patchedUsername}");
tcs.TrySetResult(null);
};

API.Queue(userReq);

return await tcs.Task.ConfigureAwait(false);
}

private void inviteIdToRoom(int userId)
{
var matchingUser = Client.Room?.Users.FirstOrDefault(u => u.UserID == userId);

Expand All @@ -792,6 +799,38 @@ private void inviteUserToRoom(int userId)
Client.InvitePlayer(userId);
}

private void inviteUserToRoom(string username)
{
string patchedUsername = username.Replace('_', ' ');

var matchingUser = Client.Room?.Users.FirstOrDefault(u => string.Equals(u.User?.Username, patchedUsername, StringComparison.OrdinalIgnoreCase));

if (matchingUser != null)
{
EnqueueBotMessage($@"User {matchingUser.User?.Username ?? $@"ID {matchingUser.UserID}"} is already in the room!");
return;
}

queryUsername(username).ContinueWith(t =>
{
APIUser? user = t.GetResultSafely();

if (user == null)
{
EnqueueBotMessage($@"Failed to find user {username}");
return;
}

// It's possible the user isn't truly offline, so send the invite anyway. Warn the user in chat though.
string msg = metadataClient.GetPresence(user.Id) == null
? $@"User {username} may be offline, attempting to send an invite anyway."
: $@"Sent an invite to user {username}";
EnqueueBotMessage(msg);

Client.InvitePlayer(user.Id);
});
}

private async Task postLatestResults(long roomID, PlaylistItem? playlistItem)
{
if (playlistItem == null) return;
Expand Down