2017-02-07 12:59:30 +08:00
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-08-31 18:49:34 +08:00
|
|
|
|
2016-10-07 21:51:10 +08:00
|
|
|
using System;
|
2016-08-31 18:49:34 +08:00
|
|
|
using System.Collections.Generic;
|
2017-04-19 17:46:26 +08:00
|
|
|
using System.Linq;
|
2016-08-31 18:49:34 +08:00
|
|
|
using Newtonsoft.Json;
|
2017-05-21 08:26:39 +08:00
|
|
|
using osu.Framework.Configuration;
|
2017-04-19 17:46:26 +08:00
|
|
|
using osu.Framework.Lists;
|
2016-08-31 18:49:34 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Chat
|
|
|
|
{
|
|
|
|
public class Channel
|
|
|
|
{
|
|
|
|
[JsonProperty(@"name")]
|
|
|
|
public string Name;
|
|
|
|
|
|
|
|
[JsonProperty(@"description")]
|
|
|
|
public string Topic;
|
|
|
|
|
|
|
|
[JsonProperty(@"type")]
|
|
|
|
public string Type;
|
|
|
|
|
|
|
|
[JsonProperty(@"channel_id")]
|
|
|
|
public int Id;
|
|
|
|
|
2017-05-17 18:02:17 +08:00
|
|
|
public readonly SortedList<Message> Messages = new SortedList<Message>(Comparer<Message>.Default);
|
2016-08-31 18:49:34 +08:00
|
|
|
|
2017-05-26 14:38:52 +08:00
|
|
|
public Bindable<bool> Joined = new Bindable<bool>();
|
|
|
|
|
2017-05-11 22:51:26 +08:00
|
|
|
public bool ReadOnly => Name != "#lazer";
|
|
|
|
|
2017-02-20 20:07:44 +08:00
|
|
|
public const int MAX_HISTORY = 300;
|
2016-10-12 14:25:07 +08:00
|
|
|
|
2016-08-31 18:49:34 +08:00
|
|
|
[JsonConstructor]
|
|
|
|
public Channel()
|
|
|
|
{
|
|
|
|
}
|
2016-10-07 21:51:10 +08:00
|
|
|
|
2017-02-20 20:09:34 +08:00
|
|
|
public event Action<IEnumerable<Message>> NewMessagesArrived;
|
2016-10-07 21:51:10 +08:00
|
|
|
|
2017-05-15 12:26:35 +08:00
|
|
|
public void AddNewMessages(params Message[] messages)
|
2016-10-07 21:51:10 +08:00
|
|
|
{
|
2017-05-15 12:26:35 +08:00
|
|
|
messages = messages.Except(Messages).ToArray();
|
2017-04-19 17:46:26 +08:00
|
|
|
|
2016-10-07 21:51:10 +08:00
|
|
|
Messages.AddRange(messages);
|
2017-04-19 17:46:26 +08:00
|
|
|
|
2016-10-07 21:51:10 +08:00
|
|
|
purgeOldMessages();
|
|
|
|
|
|
|
|
NewMessagesArrived?.Invoke(messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void purgeOldMessages()
|
|
|
|
{
|
|
|
|
int messageCount = Messages.Count;
|
2016-10-12 14:25:07 +08:00
|
|
|
if (messageCount > MAX_HISTORY)
|
|
|
|
Messages.RemoveRange(0, messageCount - MAX_HISTORY);
|
2016-10-07 21:51:10 +08:00
|
|
|
}
|
2017-05-11 22:10:48 +08:00
|
|
|
|
|
|
|
public override string ToString() => Name;
|
2016-08-31 18:49:34 +08:00
|
|
|
}
|
|
|
|
}
|