1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 10:12:54 +08:00

Add error message when creation fails

This commit is contained in:
smoogipoo 2018-12-26 18:38:58 +09:00
parent 052ab4763a
commit be9ba78d47
7 changed files with 109 additions and 16 deletions

View File

@ -76,7 +76,7 @@ namespace osu.Game.Tests.Visual
public readonly BindableCollection<Room> Rooms = new BindableCollection<Room>();
IBindableCollection<Room> IRoomManager.Rooms => Rooms;
public void CreateRoom(Room room) => Rooms.Add(room);
public void CreateRoom(Room room, Action<string> onError = null) => Rooms.Add(room);
public void JoinRoom(Room room) => RoomJoined?.Invoke(room);

View File

@ -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<TimeSpan> 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<Room> CreateRequested;
public const string FAILED_TEXT = "failed";
public Func<Room, bool> CreateRequested;
public event Action<Room> RoomJoined;
public IBindableCollection<Room> Rooms { get; } = null;
public void CreateRoom(Room room) => CreateRequested?.Invoke(room);
public void CreateRoom(Room room, Action<string> onError = null)
{
if (CreateRequested == null)
return;
if (!CreateRequested.Invoke(room))
onError?.Invoke(FAILED_TEXT);
}
public void JoinRoom(Room room) => throw new NotImplementedException();

View File

@ -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<Room>
public class CreateRoomRequest : APIRequest<APICreatedRoom>
{
private readonly Room room;

View File

@ -0,0 +1,14 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// 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; }
}
}

View File

@ -24,7 +24,8 @@ namespace osu.Game.Screens.Multi
/// Creates a new <see cref="Room"/>.
/// </summary>
/// <param name="room">The <see cref="Room"/> to create.</param>
void CreateRoom(Room room);
/// <param name="onError">An action to be invoked if an error occurred.</param>
void CreateRoom(Room room, Action<string> onError = null);
/// <summary>
/// Joins a <see cref="Room"/>.

View File

@ -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

View File

@ -42,13 +42,19 @@ namespace osu.Game.Screens.Multi
TimeBetweenPolls = 5000;
}
public void CreateRoom(Room room)
public void CreateRoom(Room room, Action<string> 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);
}