diff --git a/osu.Game/Online/Multiplayer/IMultiplayerClient.cs b/osu.Game/Online/Multiplayer/IMultiplayerClient.cs index f59ded93f8..ba9e8a237c 100644 --- a/osu.Game/Online/Multiplayer/IMultiplayerClient.cs +++ b/osu.Game/Online/Multiplayer/IMultiplayerClient.cs @@ -46,8 +46,9 @@ namespace osu.Game.Online.Multiplayer /// Signals that a user has been invited into a multiplayer room. /// /// Id of user that invited the player. - /// The room the user got invited to. - Task Invited(int invitedBy, MultiplayerRoom room); + /// Id of the room the user got invited to. + /// Password to join the room. + Task Invited(int invitedBy, long roomID, string password); /// /// Signal that the host of the room has changed. diff --git a/osu.Game/Online/Multiplayer/MultiplayerClient.cs b/osu.Game/Online/Multiplayer/MultiplayerClient.cs index 6e46a5a3b9..61c2fa495a 100644 --- a/osu.Game/Online/Multiplayer/MultiplayerClient.cs +++ b/osu.Game/Online/Multiplayer/MultiplayerClient.cs @@ -11,6 +11,7 @@ using Newtonsoft.Json; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Development; +using osu.Framework.Extensions; using osu.Framework.Graphics; using osu.Framework.Logging; using osu.Game.Database; @@ -30,6 +31,8 @@ namespace osu.Game.Online.Multiplayer { public Action? PostNotification { protected get; set; } + public Action? InviteAccepted { protected get; set; } + /// /// Invoked when any change occurs to the multiplayer room. /// @@ -442,20 +445,50 @@ namespace osu.Game.Online.Multiplayer return handleUserLeft(user, UserKicked); } - async Task IMultiplayerClient.Invited(int invitedBy, MultiplayerRoom room) + async Task IMultiplayerClient.Invited(int invitedBy, long roomID, string password) { - var user = await userLookupCache.GetUserAsync(invitedBy).ConfigureAwait(false); + var loadUserTask = userLookupCache.GetUserAsync(invitedBy); + var loadRoomTask = loadRoom(roomID); - if (user == null) return; + await Task.WhenAll(loadUserTask, loadRoomTask).ConfigureAwait(false); + + APIUser? apiUser = loadUserTask.GetResultSafely(); + Room? apiRoom = loadRoomTask.GetResultSafely(); + + if (apiUser == null || apiRoom == null) return; Scheduler.Add(() => { PostNotification?.Invoke( - new UserAvatarNotification(user, $"{user.Username} invited you to a multiplayer match:\"{room.Settings.Name}\"!") + new UserAvatarNotification(apiUser, $"{apiUser.Username} invited you to a multiplayer match:\"{apiRoom.Name}\"!") + { + Activated = () => + { + InviteAccepted?.Invoke(apiRoom, password); + return true; + } + } ); }); } + private Task loadRoom(long id) + { + return Task.Run(() => + { + var t = new TaskCompletionSource(); + var request = new GetRoomRequest(id); + + request.Success += room => t.TrySetResult(room); + + request.Failure += e => t.TrySetResult(null); + + API.Queue(request); + + return t.Task; + }); + } + private void addUserToAPIRoom(MultiplayerRoomUser user) { Debug.Assert(APIRoom != null); diff --git a/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs b/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs index 0e327bbc83..5b5741ef1c 100644 --- a/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs +++ b/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs @@ -50,7 +50,7 @@ namespace osu.Game.Online.Multiplayer connection.On(nameof(IMultiplayerClient.UserJoined), ((IMultiplayerClient)this).UserJoined); connection.On(nameof(IMultiplayerClient.UserLeft), ((IMultiplayerClient)this).UserLeft); connection.On(nameof(IMultiplayerClient.UserKicked), ((IMultiplayerClient)this).UserKicked); - connection.On(nameof(IMultiplayerClient.Invited), ((IMultiplayerClient)this).Invited); + connection.On(nameof(IMultiplayerClient.Invited), ((IMultiplayerClient)this).Invited); connection.On(nameof(IMultiplayerClient.HostChanged), ((IMultiplayerClient)this).HostChanged); connection.On(nameof(IMultiplayerClient.SettingsChanged), ((IMultiplayerClient)this).SettingsChanged); connection.On(nameof(IMultiplayerClient.UserStateChanged), ((IMultiplayerClient)this).UserStateChanged);