From b9b45493e6f3f546f61c5d26034584d83265da03 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Wed, 17 May 2017 18:02:17 +0800 Subject: [PATCH 1/8] Use generic IComparable together with IEqutable for Message. --- osu.Game/Online/Chat/Channel.cs | 2 +- osu.Game/Online/Chat/Message.cs | 14 +++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/osu.Game/Online/Chat/Channel.cs b/osu.Game/Online/Chat/Channel.cs index 2925c3ccb4..93fd0a8956 100644 --- a/osu.Game/Online/Chat/Channel.cs +++ b/osu.Game/Online/Chat/Channel.cs @@ -23,7 +23,7 @@ namespace osu.Game.Online.Chat [JsonProperty(@"channel_id")] public int Id; - public readonly SortedList Messages = new SortedList((m1, m2) => m1.Id.CompareTo(m2.Id)); + public readonly SortedList Messages = new SortedList(Comparer.Default); //internal bool Joined; diff --git a/osu.Game/Online/Chat/Message.cs b/osu.Game/Online/Chat/Message.cs index bf53a68910..8c0cb043c4 100644 --- a/osu.Game/Online/Chat/Message.cs +++ b/osu.Game/Online/Chat/Message.cs @@ -8,7 +8,7 @@ using osu.Game.Users; namespace osu.Game.Online.Chat { - public class Message + public class Message : IComparable, IEquatable { [JsonProperty(@"message_id")] public readonly long Id; @@ -42,17 +42,9 @@ namespace osu.Game.Online.Chat Id = id; } - public override bool Equals(object obj) - { - var objMessage = obj as Message; + public int CompareTo(Message other) => Id.CompareTo(other.Id); - return Id == objMessage?.Id; - } - - public override int GetHashCode() - { - return Id.GetHashCode(); - } + public bool Equals(Message other) => Id == other.Id; } public enum TargetType From 6c9505fa3a053cc4fc607f366a30d1b94fe3600f Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Wed, 17 May 2017 18:11:38 +0800 Subject: [PATCH 2/8] Handle possible nulls. --- osu.Game/Online/Chat/Message.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Online/Chat/Message.cs b/osu.Game/Online/Chat/Message.cs index 8c0cb043c4..ed76ef248e 100644 --- a/osu.Game/Online/Chat/Message.cs +++ b/osu.Game/Online/Chat/Message.cs @@ -44,7 +44,7 @@ namespace osu.Game.Online.Chat public int CompareTo(Message other) => Id.CompareTo(other.Id); - public bool Equals(Message other) => Id == other.Id; + public bool Equals(Message other) => Id == other?.Id; } public enum TargetType From b5d7211cd6471d58c9bcf6b78d49c9f5ce4e9a77 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Thu, 18 May 2017 01:30:24 +0800 Subject: [PATCH 3/8] Expire placeholder text. --- osu.Game/Overlays/ChatOverlay.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/osu.Game/Overlays/ChatOverlay.cs b/osu.Game/Overlays/ChatOverlay.cs index 2836be22ae..c722d46ace 100644 --- a/osu.Game/Overlays/ChatOverlay.cs +++ b/osu.Game/Overlays/ChatOverlay.cs @@ -229,6 +229,7 @@ namespace osu.Game.Overlays Scheduler.Add(delegate { loading.FadeOut(100); + loading.Expire(); addChannel(channels.Find(c => c.Name == @"#lazer")); addChannel(channels.Find(c => c.Name == @"#osu")); From e2b1fcc0887f2ff46c87058261d5c47a1e95c96c Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Thu, 18 May 2017 01:43:11 +0800 Subject: [PATCH 4/8] Use string.Join in GetMessagesRequest. --- osu.Game/Online/API/Requests/GetMessagesRequest.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/osu.Game/Online/API/Requests/GetMessagesRequest.cs b/osu.Game/Online/API/Requests/GetMessagesRequest.cs index cf52f9ccd3..858015e29b 100644 --- a/osu.Game/Online/API/Requests/GetMessagesRequest.cs +++ b/osu.Game/Online/API/Requests/GetMessagesRequest.cs @@ -2,6 +2,7 @@ // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System.Collections.Generic; +using System.Linq; using osu.Framework.IO.Network; using osu.Game.Online.Chat; @@ -20,10 +21,7 @@ namespace osu.Game.Online.API.Requests protected override WebRequest CreateWebRequest() { - string channelString = string.Empty; - foreach (Channel c in channels) - channelString += c.Id + ","; - channelString = channelString.TrimEnd(','); + string channelString = string.Join(",", channels.Select(x => x.Id)); var req = base.CreateWebRequest(); req.AddParameter(@"channels", channelString); From 23e2d3ef07b2fd99749245c06ad94bd8d4763699 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Thu, 18 May 2017 02:30:17 +0800 Subject: [PATCH 5/8] Use GroupBy in ChatOverlay. --- osu.Game/Overlays/ChatOverlay.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/osu.Game/Overlays/ChatOverlay.cs b/osu.Game/Overlays/ChatOverlay.cs index c722d46ace..686a1d513a 100644 --- a/osu.Game/Overlays/ChatOverlay.cs +++ b/osu.Game/Overlays/ChatOverlay.cs @@ -321,11 +321,8 @@ namespace osu.Game.Overlays fetchReq = new GetMessagesRequest(careChannels, lastMessageId); fetchReq.Success += delegate (List messages) { - var ids = messages.Where(m => m.TargetType == TargetType.Channel).Select(m => m.TargetId).Distinct(); - - //batch messages per channel. - foreach (var id in ids) - careChannels.Find(c => c.Id == id)?.AddNewMessages(messages.Where(m => m.TargetId == id).ToArray()); + foreach (var group in messages.Where(m => m.TargetType == TargetType.Channel).GroupBy(m => m.TargetId)) + careChannels.Find(c => c.Id == group.Key)?.AddNewMessages(group.ToArray()); lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId; From 88f8619e9a655f8ab29d14d3697f29793dcec58c Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Thu, 18 May 2017 02:46:12 +0800 Subject: [PATCH 6/8] More missed exceptions. --- osu.Game.Rulesets.Mania/Timing/ControlPointContainer.cs | 2 +- osu.Game.Rulesets.Mania/UI/ManiaHitRenderer.cs | 2 +- osu.Game/Online/API/APIAccess.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/osu.Game.Rulesets.Mania/Timing/ControlPointContainer.cs b/osu.Game.Rulesets.Mania/Timing/ControlPointContainer.cs index 6c39ba40f9..2ff97047c0 100644 --- a/osu.Game.Rulesets.Mania/Timing/ControlPointContainer.cs +++ b/osu.Game.Rulesets.Mania/Timing/ControlPointContainer.cs @@ -51,7 +51,7 @@ namespace osu.Game.Rulesets.Mania.Timing var controlPoint = drawableControlPoints.LastOrDefault(t => t.CanContain(drawable)) ?? drawableControlPoints.FirstOrDefault(); if (controlPoint == null) - throw new Exception("Could not find suitable timing section to add object to."); + throw new InvalidOperationException("Could not find suitable timing section to add object to."); controlPoint.Add(drawable); } diff --git a/osu.Game.Rulesets.Mania/UI/ManiaHitRenderer.cs b/osu.Game.Rulesets.Mania/UI/ManiaHitRenderer.cs index 986aefb2bd..dccc18fa8b 100644 --- a/osu.Game.Rulesets.Mania/UI/ManiaHitRenderer.cs +++ b/osu.Game.Rulesets.Mania/UI/ManiaHitRenderer.cs @@ -34,7 +34,7 @@ namespace osu.Game.Rulesets.Mania.UI ControlPoint firstTimingChange = Beatmap.TimingInfo.ControlPoints.FirstOrDefault(t => t.TimingChange); if (firstTimingChange == null) - throw new Exception("The Beatmap contains no timing points!"); + throw new InvalidOperationException("The Beatmap contains no timing points!"); // Generate the timing points, making non-timing changes use the previous timing change var timingChanges = Beatmap.TimingInfo.ControlPoints.Select(c => diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 2f344e0bdf..7e3bb44465 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -288,7 +288,7 @@ namespace osu.Game.Online.API { APIRequest req; while (oldQueue.TryDequeue(out req)) - req.Fail(new Exception(@"Disconnected from server")); + req.Fail(new WebException(@"Disconnected from server")); } } From ffbab6bfeb55212cb1177b46b168621e551a3996 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Thu, 18 May 2017 03:26:07 +0800 Subject: [PATCH 7/8] Tidy up DrawableChannel. --- osu.Game/Overlays/Chat/DrawableChannel.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/osu.Game/Overlays/Chat/DrawableChannel.cs b/osu.Game/Overlays/Chat/DrawableChannel.cs index 355aeed134..50c849f00e 100644 --- a/osu.Game/Overlays/Chat/DrawableChannel.cs +++ b/osu.Game/Overlays/Chat/DrawableChannel.cs @@ -13,7 +13,7 @@ namespace osu.Game.Overlays.Chat public class DrawableChannel : Container { public readonly Channel Channel; - private readonly FillFlowContainer flow; + private readonly FillFlowContainer flow; private readonly ScrollContainer scroll; public DrawableChannel(Channel channel) @@ -29,7 +29,7 @@ namespace osu.Game.Overlays.Chat RelativeSizeAxes = Axes.Both, Children = new Drawable[] { - flow = new FillFlowContainer + flow = new FillFlowContainer { Direction = FillDirection.Vertical, RelativeSizeAxes = Axes.X, @@ -63,19 +63,18 @@ namespace osu.Game.Overlays.Chat var displayMessages = newMessages.Skip(Math.Max(0, newMessages.Count() - Channel.MAX_HISTORY)); + //up to last Channel.MAX_HISTORY messages + flow.Add(displayMessages.Select(m => new ChatLine(m))); + if (scroll.IsScrolledToEnd(10) || !flow.Children.Any()) scrollToEnd(); - //up to last Channel.MAX_HISTORY messages - foreach (Message m in displayMessages) - { - var d = new ChatLine(m); - flow.Add(d); - } + var staleMessages = flow.Children.Where(c => c.LifetimeEnd == double.MaxValue).ToArray(); + int count = staleMessages.Length - Channel.MAX_HISTORY; - while (flow.Children.Count(c => c.LifetimeEnd == double.MaxValue) > Channel.MAX_HISTORY) + for (int i = 0; i < count; i++) { - var d = flow.Children.First(c => c.LifetimeEnd == double.MaxValue); + var d = staleMessages[i]; if (!scroll.IsScrolledToEnd(10)) scroll.OffsetScrollPosition(-d.DrawHeight); d.Expire(); From cd065b8ff31ce97931cd6e560bf27763290fdccc Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Thu, 18 May 2017 05:27:20 +0800 Subject: [PATCH 8/8] Add back GetHashCode. --- osu.Game/Online/Chat/Message.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/osu.Game/Online/Chat/Message.cs b/osu.Game/Online/Chat/Message.cs index ed76ef248e..4c7e099647 100644 --- a/osu.Game/Online/Chat/Message.cs +++ b/osu.Game/Online/Chat/Message.cs @@ -45,6 +45,8 @@ namespace osu.Game.Online.Chat public int CompareTo(Message other) => Id.CompareTo(other.Id); public bool Equals(Message other) => Id == other?.Id; + + public override int GetHashCode() => Id.GetHashCode(); } public enum TargetType