From 940d4a16bbbdedfc616f0a33e45e4e3d792d2100 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 26 Dec 2018 18:38:58 +0900 Subject: [PATCH] Add error message when creation fails --- .../Visual/TestCaseLoungeRoomsContainer.cs | 2 +- .../Visual/TestCaseMatchSettingsOverlay.cs | 49 +++++++++++++++++-- .../Online/API/Requests/CreateRoomRequest.cs | 3 +- .../API/Requests/Responses/APICreatedRoom.cs | 14 ++++++ osu.Game/Screens/Multi/IRoomManager.cs | 3 +- .../Match/Components/MatchSettingsOverlay.cs | 44 ++++++++++++++--- osu.Game/Screens/Multi/RoomManager.cs | 10 +++- 7 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 osu.Game/Online/API/Requests/Responses/APICreatedRoom.cs diff --git a/osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs b/osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs index dd7e8f7fac..e42781849d 100644 --- a/osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs +++ b/osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs @@ -76,7 +76,7 @@ namespace osu.Game.Tests.Visual public readonly BindableCollection Rooms = new BindableCollection(); IBindableCollection IRoomManager.Rooms => Rooms; - public void CreateRoom(Room room) => Rooms.Add(room); + public void CreateRoom(Room room, Action onError = null) => Rooms.Add(room); public void JoinRoom(Room room) => RoomJoined?.Invoke(room); diff --git a/osu.Game.Tests/Visual/TestCaseMatchSettingsOverlay.cs b/osu.Game.Tests/Visual/TestCaseMatchSettingsOverlay.cs index 3c41fd6920..a31b91cc2a 100644 --- a/osu.Game.Tests/Visual/TestCaseMatchSettingsOverlay.cs +++ b/osu.Game.Tests/Visual/TestCaseMatchSettingsOverlay.cs @@ -9,6 +9,7 @@ using osu.Framework.Configuration; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; +using osu.Game.Graphics.Sprites; using osu.Game.Graphics.UserInterface; using osu.Game.Online.Multiplayer; using osu.Game.Screens.Multi; @@ -77,7 +78,11 @@ namespace osu.Game.Tests.Visual settings.NameField.Current.Value = expected_name; settings.DurationField.Current.Value = expectedDuration; - roomManager.CreateRequested = r => createdRoom = r; + roomManager.CreateRequested = r => + { + createdRoom = r; + return true; + }; }); AddStep("create room", () => settings.ApplyButton.Action.Invoke()); @@ -85,13 +90,40 @@ namespace osu.Game.Tests.Visual AddAssert("has correct duration", () => createdRoom.Duration.Value == expectedDuration); } - private class TestRoomSettings : MatchSettingsOverlay + [Test] + public void TestCreationFailureDisplaysError() + { + bool fail; + + AddStep("setup", () => + { + fail = true; + roomManager.CreateRequested = _ => !fail; + }); + AddAssert("error not displayed", () => !settings.ErrorText.IsPresent); + + AddStep("create room", () => settings.ApplyButton.Action.Invoke()); + AddAssert("error displayed", () => settings.ErrorText.IsPresent); + AddAssert("error has correct text", () => settings.ErrorText.Text == TestRoomManager.FAILED_TEXT); + + AddStep("create room no fail", () => + { + fail = false; + settings.ApplyButton.Action.Invoke(); + }); + + AddUntilStep(() => !settings.ErrorText.IsPresent, "error not displayed"); + } + + private class TestRoomSettings : RoomSettingsOverlay { public new TriangleButton ApplyButton => base.ApplyButton; public new OsuTextBox NameField => base.NameField; public new OsuDropdown DurationField => base.DurationField; + public new OsuSpriteText ErrorText => base.ErrorText; + public TestRoomSettings(Room room) : base(room) { @@ -100,13 +132,22 @@ namespace osu.Game.Tests.Visual private class TestRoomManager : IRoomManager { - public Action CreateRequested; + public const string FAILED_TEXT = "failed"; + + public Func CreateRequested; public event Action RoomJoined; public IBindableCollection Rooms { get; } = null; - public void CreateRoom(Room room) => CreateRequested?.Invoke(room); + public void CreateRoom(Room room, Action onError = null) + { + if (CreateRequested == null) + return; + + if (!CreateRequested.Invoke(room)) + onError?.Invoke(FAILED_TEXT); + } public void JoinRoom(Room room) => throw new NotImplementedException(); diff --git a/osu.Game/Online/API/Requests/CreateRoomRequest.cs b/osu.Game/Online/API/Requests/CreateRoomRequest.cs index 7ce289b65a..fc401be5f1 100644 --- a/osu.Game/Online/API/Requests/CreateRoomRequest.cs +++ b/osu.Game/Online/API/Requests/CreateRoomRequest.cs @@ -4,11 +4,12 @@ using System.Net.Http; using Newtonsoft.Json; using osu.Framework.IO.Network; +using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.Multiplayer; namespace osu.Game.Online.API.Requests { - public class CreateRoomRequest : APIRequest + public class CreateRoomRequest : APIRequest { private readonly Room room; diff --git a/osu.Game/Online/API/Requests/Responses/APICreatedRoom.cs b/osu.Game/Online/API/Requests/Responses/APICreatedRoom.cs new file mode 100644 index 0000000000..ca0daa04b6 --- /dev/null +++ b/osu.Game/Online/API/Requests/Responses/APICreatedRoom.cs @@ -0,0 +1,14 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using Newtonsoft.Json; +using osu.Game.Online.Multiplayer; + +namespace osu.Game.Online.API.Requests.Responses +{ + public class APICreatedRoom : Room + { + [JsonProperty("error")] + public string Error { get; set; } + } +} diff --git a/osu.Game/Screens/Multi/IRoomManager.cs b/osu.Game/Screens/Multi/IRoomManager.cs index b94df93581..bb996f0b5c 100644 --- a/osu.Game/Screens/Multi/IRoomManager.cs +++ b/osu.Game/Screens/Multi/IRoomManager.cs @@ -24,7 +24,8 @@ namespace osu.Game.Screens.Multi /// Creates a new . /// /// The to create. - void CreateRoom(Room room); + /// An action to be invoked if an error occurred. + void CreateRoom(Room room, Action onError = null); /// /// Joins a . diff --git a/osu.Game/Screens/Multi/Match/Components/MatchSettingsOverlay.cs b/osu.Game/Screens/Multi/Match/Components/MatchSettingsOverlay.cs index 6de2bed938..d6228b106b 100644 --- a/osu.Game/Screens/Multi/Match/Components/MatchSettingsOverlay.cs +++ b/osu.Game/Screens/Multi/Match/Components/MatchSettingsOverlay.cs @@ -37,6 +37,8 @@ namespace osu.Game.Screens.Multi.Match.Components protected readonly TriangleButton ApplyButton; protected readonly OsuPasswordTextBox PasswordField; + protected readonly OsuSpriteText ErrorText; + private readonly Room room; [Resolved(CanBeNull = true)] @@ -200,14 +202,31 @@ namespace osu.Game.Screens.Multi.Match.Components RelativeSizeAxes = Axes.Both, Colour = OsuColour.FromHex(@"28242d").Darken(0.5f).Opacity(1f), }, - ApplyButton = new CreateRoomButton + new FillFlowContainer { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + Direction = FillDirection.Vertical, + Spacing = new Vector2(0, 20), Margin = new MarginPadding { Vertical = 20 }, - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - Size = new Vector2(230, 55), - Action = apply, - }, + Children = new Drawable[] + { + ApplyButton = new CreateRoomButton + { + Anchor = Anchor.BottomCentre, + Origin = Anchor.BottomCentre, + Size = new Vector2(230, 55), + Action = apply, + }, + ErrorText = new OsuSpriteText + { + Anchor = Anchor.BottomCentre, + Origin = Anchor.BottomCentre, + Alpha = 0, + Depth = 1 + } + } + } } } } @@ -229,6 +248,7 @@ namespace osu.Game.Screens.Multi.Match.Components private void load(OsuColour colours) { typeLabel.Colour = colours.Yellow; + ErrorText.Colour = colours.RedDark; MaxParticipantsField.ReadOnly = true; PasswordField.ReadOnly = true; @@ -258,6 +278,8 @@ namespace osu.Game.Screens.Multi.Match.Components private void apply() { + hideError(); + bindings.Name.Value = NameField.Text; bindings.Availability.Value = AvailabilityPicker.Current.Value; bindings.Type.Value = TypePicker.Current.Value; @@ -269,7 +291,15 @@ namespace osu.Game.Screens.Multi.Match.Components bindings.Duration.Value = DurationField.Current.Value; - manager?.CreateRoom(room); + manager?.CreateRoom(room, showError); + } + + private void hideError() => ErrorText.FadeOut(50); + + private void showError(string text) + { + ErrorText.Text = text; + ErrorText.FadeIn(50); } private class SettingsTextBox : OsuTextBox diff --git a/osu.Game/Screens/Multi/RoomManager.cs b/osu.Game/Screens/Multi/RoomManager.cs index 1947909438..08458bf4e9 100644 --- a/osu.Game/Screens/Multi/RoomManager.cs +++ b/osu.Game/Screens/Multi/RoomManager.cs @@ -48,13 +48,19 @@ namespace osu.Game.Screens.Multi PartRoom(); } - public void CreateRoom(Room room) + public void CreateRoom(Room room, Action onError = null) { room.Host.Value = api.LocalUser; var req = new CreateRoomRequest(room); req.Success += result => addRoom(room, result); - req.Failure += exception => Logger.Log($"Failed to create room: {exception}"); + req.Failure += exception => + { + if (req.Result != null) + onError?.Invoke(req.Result.Error); + else + Logger.Log($"Failed to create the room: {exception}", level: LogLevel.Important); + }; api.Queue(req); }