1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 14:37:30 +08:00
osu-lazer/osu.Game/Online/Chat/Channel.cs

106 lines
3.2 KiB
C#
Raw Normal View History

// 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
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;
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;
public readonly SortedList<Message> Messages = new SortedList<Message>(Comparer<Message>.Default);
2016-08-31 18:49:34 +08:00
2017-08-21 16:43:26 +08:00
private readonly List<LocalEchoMessage> pendingMessages = new List<LocalEchoMessage>();
2017-05-26 14:38:52 +08:00
public Bindable<bool> Joined = new Bindable<bool>();
2017-09-06 19:26:01 +08:00
public bool ReadOnly => false;
2017-02-20 20:07:44 +08:00
public const int MAX_HISTORY = 300;
2016-08-31 18:49:34 +08:00
[JsonConstructor]
public Channel()
{
}
2017-02-20 20:09:34 +08:00
public event Action<IEnumerable<Message>> NewMessagesArrived;
2017-08-21 16:43:26 +08:00
public event Action<LocalEchoMessage, Message> PendingMessageResolved;
public event Action<Message> MessageRemoved;
public void AddLocalEcho(LocalEchoMessage message)
{
pendingMessages.Add(message);
Messages.Add(message);
NewMessagesArrived?.Invoke(new[] { message });
}
public void AddNewMessages(params Message[] messages)
{
messages = messages.Except(Messages).ToArray();
2017-04-19 17:46:26 +08:00
2017-08-21 17:51:14 +08:00
Messages.AddRange(messages);
2017-04-19 17:46:26 +08:00
purgeOldMessages();
NewMessagesArrived?.Invoke(messages);
}
private void purgeOldMessages()
{
2017-08-21 16:43:26 +08:00
// never purge local echos
int messageCount = Messages.Count - pendingMessages.Count;
if (messageCount > MAX_HISTORY)
Messages.RemoveRange(0, messageCount - MAX_HISTORY);
}
2017-08-21 16:43:26 +08:00
/// <summary>
/// Replace or remove a message from the channel.
/// </summary>
/// <param name="echo">The local echo message (client-side).</param>
/// <param name="final">The response message, or null if the message became invalid.</param>
public void ReplaceMessage(LocalEchoMessage echo, Message final)
{
if (!pendingMessages.Remove(echo))
2017-08-21 22:34:08 +08:00
throw new InvalidOperationException("Attempted to remove echo that wasn't present");
2017-08-21 16:43:26 +08:00
Messages.Remove(echo);
if (final == null)
{
MessageRemoved?.Invoke(echo);
return;
}
if (Messages.Contains(final))
{
// message already inserted, so let's throw away this update.
// we may want to handle this better in the future, but for the time being api requests are single-threaded so order is assumed.
MessageRemoved?.Invoke(echo);
return;
}
Messages.Add(final);
PendingMessageResolved?.Invoke(echo, final);
}
public override string ToString() => Name;
2016-08-31 18:49:34 +08:00
}
}