General improvements to chat querying and logic organisation.

This commit is contained in:
Dean Herbert
2016-10-07 22:51:10 +09:00
parent 965cd6539f
commit 3067c890ce
6 changed files with 173 additions and 65 deletions

View File

@ -1,8 +1,14 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json;
using osu.Framework.Configuration;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
namespace osu.Game.Online.Chat
{
@ -28,5 +34,26 @@ namespace osu.Game.Online.Chat
public Channel()
{
}
public event Action<Message[]> NewMessagesArrived;
public void AddNewMessages(params Message[] messages)
{
Messages.AddRange(messages);
purgeOldMessages();
NewMessagesArrived?.Invoke(messages);
}
private void purgeOldMessages()
{
const int max_history = 50;
int messageCount = Messages.Count;
if (messageCount > 50)
{
Messages.RemoveRange(0, messageCount - max_history);
}
}
}
}

View File

@ -0,0 +1,78 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using osu.Game.Online.Chat.Display.osu.Online.Social;
using OpenTK;
namespace osu.Game.Online.Chat.Display
{
public class ChannelDisplay : FlowContainer
{
private readonly Channel channel;
private FlowContainer flow;
public ChannelDisplay(Channel channel)
{
this.channel = channel;
newMessages(channel.Messages);
channel.NewMessagesArrived += newMessages;
RelativeSizeAxes = Axes.X;
Direction = FlowDirection.VerticalOnly;
Add(new SpriteText
{
Text = channel.Name
});
Add(new ScrollContainer
{
RelativeSizeAxes = Axes.X,
Size = new Vector2(1, 200),
Children = new Drawable[]
{
flow = new FlowContainer
{
Direction = FlowDirection.VerticalOnly,
RelativeSizeAxes = Axes.X,
LayoutEasing = EasingTypes.Out,
Padding = new Vector2(1, 1)
}
}
});
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
channel.NewMessagesArrived -= newMessages;
}
public override void Load()
{
base.Load();
newMessages(channel.Messages);
}
private void newMessages(IEnumerable<Message> newMessages)
{
if (!IsLoaded) return;
var displayMessages = newMessages.Skip(Math.Max(0, newMessages.Count() - 20));
//up to last 20 messages
foreach (Message m in displayMessages)
flow.Add(new ChatLine(m));
while (flow.Children.Count() > 20)
flow.Remove(flow.Children.First());
}
}
}

View File

@ -1,6 +1,8 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Drawables;
@ -14,11 +16,11 @@ namespace osu.Game.Online.Chat.Display
{
public class ChatLine : AutoSizeContainer
{
private readonly Message msg;
public readonly Message Message;
public ChatLine(Message msg)
public ChatLine(Message message)
{
this.msg = msg;
this.Message = message;
}
public override void Load()
@ -27,34 +29,27 @@ namespace osu.Game.Online.Chat.Display
RelativeSizeAxes = Axes.X;
Add(new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Aqua,
Alpha = 0.2f
});
Add(new SpriteText
{
Text = msg.Timestamp.ToLocalTime().ToLongTimeString(),
Text = Message.Timestamp.ToLocalTime().ToLongTimeString(),
Colour = new Color4(128, 128, 128, 255)
});
Add(new SpriteText
{
Text = msg.User.Name,
Text = Message.User.Name,
Origin = Anchor.TopRight,
RelativePositionAxes = Axes.X,
Position = new Vector2(0.14f,0),
Position = new Vector2(0.2f,0),
});
Add(new SpriteText
{
Text = msg.Content,
Text = Message.Content,
RelativePositionAxes = Axes.X,
Position = new Vector2(0.15f, 0),
Position = new Vector2(0.22f, 0),
RelativeSizeAxes = Axes.X,
Size = new Vector2(0.85f, 1),
Size = new Vector2(0.78f, 1),
});
}
}

View File

@ -12,10 +12,10 @@ namespace osu.Game.Online.Chat
public long Id;
[JsonProperty(@"user_id")]
public string UserId;
public int UserId;
[JsonProperty(@"channel_id")]
public string ChannelId;
public int ChannelId;
[JsonProperty(@"timestamp")]
public DateTime Timestamp;