From 267d1ee7d442ef09bbdd5b7eb285c264d424ffb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20Sch=C3=BCrz?= Date: Tue, 3 Oct 2023 22:08:14 +0200 Subject: [PATCH] Handle cases when player cannot be invited. --- .../Multiplayer/IMultiplayerRoomServer.cs | 3 +- .../Multiplayer/OnlineMultiplayerClient.cs | 29 +++++++++++++++++-- .../Multiplayer/UserBlockedException.cs | 26 +++++++++++++++++ .../Multiplayer/UserBlocksPMsException.cs | 26 +++++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 osu.Game/Online/Multiplayer/UserBlockedException.cs create mode 100644 osu.Game/Online/Multiplayer/UserBlocksPMsException.cs diff --git a/osu.Game/Online/Multiplayer/IMultiplayerRoomServer.cs b/osu.Game/Online/Multiplayer/IMultiplayerRoomServer.cs index 64cd6df24d..e98570dc29 100644 --- a/osu.Game/Online/Multiplayer/IMultiplayerRoomServer.cs +++ b/osu.Game/Online/Multiplayer/IMultiplayerRoomServer.cs @@ -104,7 +104,8 @@ namespace osu.Game.Online.Multiplayer /// Invites a player to the current room. /// /// - /// + /// The user has blocked or has been blocked by the invited user. + /// The invited user does not accept private messages. Task InvitePlayer(int userId); } } diff --git a/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs b/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs index 5b5741ef1c..08e82f2ad3 100644 --- a/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs +++ b/osu.Game/Online/Multiplayer/OnlineMultiplayerClient.cs @@ -12,6 +12,7 @@ using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Game.Online.API; using osu.Game.Online.Rooms; +using osu.Game.Overlays.Notifications; namespace osu.Game.Online.Multiplayer { @@ -107,14 +108,36 @@ namespace osu.Game.Online.Multiplayer return connection.InvokeAsync(nameof(IMultiplayerServer.LeaveRoom)); } - public override Task InvitePlayer(int userId) + public override async Task InvitePlayer(int userId) { if (!IsConnected.Value) - return Task.CompletedTask; + return; Debug.Assert(connection != null); - return connection.InvokeAsync(nameof(IMultiplayerServer.InvitePlayer), userId); + try + { + await connection.InvokeAsync(nameof(IMultiplayerServer.InvitePlayer), userId).ConfigureAwait(false); + } + catch (HubException exception) + { + switch (exception.GetHubExceptionMessage()) + { + case UserBlockedException.MESSAGE: + PostNotification?.Invoke(new SimpleErrorNotification + { + Text = "User cannot be invited by someone they have blocked or are blocked by." + }); + break; + + case UserBlocksPMsException.MESSAGE: + PostNotification?.Invoke(new SimpleErrorNotification + { + Text = "User cannot be invited because they cannot receive private messages from people not on their friends list." + }); + break; + } + } } public override Task TransferHost(int userId) diff --git a/osu.Game/Online/Multiplayer/UserBlockedException.cs b/osu.Game/Online/Multiplayer/UserBlockedException.cs new file mode 100644 index 0000000000..363f878183 --- /dev/null +++ b/osu.Game/Online/Multiplayer/UserBlockedException.cs @@ -0,0 +1,26 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Runtime.Serialization; +using Microsoft.AspNetCore.SignalR; + +namespace osu.Game.Online.Multiplayer +{ + [Serializable] + public class UserBlockedException : HubException + { + public const string MESSAGE = "User cannot be invited by someone they have blocked or are blocked by."; + + public UserBlockedException() + : + base(MESSAGE) + { + } + + protected UserBlockedException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/osu.Game/Online/Multiplayer/UserBlocksPMsException.cs b/osu.Game/Online/Multiplayer/UserBlocksPMsException.cs new file mode 100644 index 0000000000..220a84cfe8 --- /dev/null +++ b/osu.Game/Online/Multiplayer/UserBlocksPMsException.cs @@ -0,0 +1,26 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Runtime.Serialization; +using Microsoft.AspNetCore.SignalR; + +namespace osu.Game.Online.Multiplayer +{ + [Serializable] + public class UserBlocksPMsException : HubException + { + public const string MESSAGE = "User cannot be invited because they have disabled private messages."; + + public UserBlocksPMsException() + : + base(MESSAGE) + { + } + + protected UserBlocksPMsException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +}