From dcd4b4450d4fa41d165e4714f22f6078ce80b4e2 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 May 2017 13:26:35 +0900 Subject: [PATCH] Add error message in chat when attempting to use commands --- osu.Game/Online/Chat/Channel.cs | 4 +- osu.Game/Online/Chat/ErrorMessage.cs | 25 ++++++++++ osu.Game/Online/Chat/Message.cs | 5 ++ osu.Game/Overlays/ChatOverlay.cs | 69 +++++++++++++++------------- osu.Game/osu.Game.csproj | 1 + 5 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 osu.Game/Online/Chat/ErrorMessage.cs diff --git a/osu.Game/Online/Chat/Channel.cs b/osu.Game/Online/Chat/Channel.cs index d159f482a9..2925c3ccb4 100644 --- a/osu.Game/Online/Chat/Channel.cs +++ b/osu.Game/Online/Chat/Channel.cs @@ -38,9 +38,9 @@ namespace osu.Game.Online.Chat public event Action> NewMessagesArrived; - public void AddNewMessages(IEnumerable messages) + public void AddNewMessages(params Message[] messages) { - messages = messages.Except(Messages).ToList(); + messages = messages.Except(Messages).ToArray(); Messages.AddRange(messages); diff --git a/osu.Game/Online/Chat/ErrorMessage.cs b/osu.Game/Online/Chat/ErrorMessage.cs new file mode 100644 index 0000000000..a410e9044a --- /dev/null +++ b/osu.Game/Online/Chat/ErrorMessage.cs @@ -0,0 +1,25 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using osu.Game.Users; + +namespace osu.Game.Online.Chat +{ + public class ErrorMessage : Message + { + private static int errorId = -1; + + public ErrorMessage(string message) : base(errorId--) + { + Timestamp = DateTime.Now; + Content = message; + + Sender = new User + { + Username = @"system", + Colour = @"ff0000", + }; + } + } +} \ No newline at end of file diff --git a/osu.Game/Online/Chat/Message.cs b/osu.Game/Online/Chat/Message.cs index 372e43be38..bf53a68910 100644 --- a/osu.Game/Online/Chat/Message.cs +++ b/osu.Game/Online/Chat/Message.cs @@ -37,6 +37,11 @@ namespace osu.Game.Online.Chat { } + public Message(long id) + { + Id = id; + } + public override bool Equals(object obj) { var objMessage = obj as Message; diff --git a/osu.Game/Overlays/ChatOverlay.cs b/osu.Game/Overlays/ChatOverlay.cs index 7c5651b5ff..d43d0b22aa 100644 --- a/osu.Game/Overlays/ChatOverlay.cs +++ b/osu.Game/Overlays/ChatOverlay.cs @@ -306,7 +306,7 @@ namespace osu.Game.Overlays //batch messages per channel. foreach (var id in ids) - careChannels.Find(c => c.Id == id)?.AddNewMessages(messages.Where(m => m.TargetId == id)); + careChannels.Find(c => c.Id == id)?.AddNewMessages(messages.Where(m => m.TargetId == id).ToArray()); lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId; @@ -326,38 +326,45 @@ namespace osu.Game.Overlays { var postText = textbox.Text; - if (!string.IsNullOrEmpty(postText) && api.LocalUser.Value != null) + if (string.IsNullOrEmpty(postText) || api.LocalUser.Value == null) return; + + if (currentChannel == null) return; + + if (postText[0] == '/') { - if (currentChannel == null) return; - - var message = new Message - { - Sender = api.LocalUser.Value, - Timestamp = DateTimeOffset.Now, - TargetType = TargetType.Channel, //TODO: read this from currentChannel - TargetId = currentChannel.Id, - Content = postText - }; - - textbox.ReadOnly = true; - var req = new PostMessageRequest(message); - - req.Failure += e => - { - textbox.FlashColour(Color4.Red, 1000); - textbox.ReadOnly = false; - }; - - req.Success += m => - { - currentChannel.AddNewMessages(new[] { m }); - - textbox.ReadOnly = false; - textbox.Text = string.Empty; - }; - - api.Queue(req); + // TODO: handle commands + currentChannel.AddNewMessages(new ErrorMessage("Chat commands are not supported yet!")); + textbox.Text = string.Empty; + return; } + + var message = new Message + { + Sender = api.LocalUser.Value, + Timestamp = DateTimeOffset.Now, + TargetType = TargetType.Channel, //TODO: read this from currentChannel + TargetId = currentChannel.Id, + Content = postText + }; + + textbox.ReadOnly = true; + var req = new PostMessageRequest(message); + + req.Failure += e => + { + textbox.FlashColour(Color4.Red, 1000); + textbox.ReadOnly = false; + }; + + req.Success += m => + { + currentChannel.AddNewMessages(m); + + textbox.ReadOnly = false; + textbox.Text = string.Empty; + }; + + api.Queue(req); } } } diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 1f866f0e17..919b2bb732 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -75,6 +75,7 @@ +