sharp-chat/SharpChat/EventStorage/VirtualEventStorage.cs

39 lines
1.2 KiB
C#
Raw Normal View History

using SharpChat.Events;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SharpChat.EventStorage {
public class VirtualEventStorage : IEventStorage {
private readonly Dictionary<long, IChatEvent> Events = new();
public void AddEvent(IChatEvent evt) {
if(evt == null)
throw new ArgumentNullException(nameof(evt));
Events.Add(evt.SequenceId, evt);
}
public IChatEvent GetEvent(long seqId) {
return Events.TryGetValue(seqId, out IChatEvent evt) ? evt : null;
}
public void RemoveEvent(IChatEvent evt) {
if(evt == null)
throw new ArgumentNullException(nameof(evt));
Events.Remove(evt.SequenceId);
}
public IEnumerable<IChatEvent> GetTargetEventLog(string target, int amount = 20, int offset = 0) {
IEnumerable<IChatEvent> subset = Events.Values.Where(ev => ev.TargetName == target);
int start = subset.Count() - offset - amount;
if(start < 0) {
amount += start;
start = 0;
}
return subset.Skip(start).Take(amount).ToArray();
}
}
}