From 56a818254e1082a8fc65f3508984e456217d7628 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 8 Feb 2023 04:32:12 +0100 Subject: [PATCH] Backported SharpId class --- SharpChat/ChatChannel.cs | 3 +-- SharpChat/ChatContext.cs | 4 +--- SharpChat/Database.cs | 15 +-------------- SharpChat/SharpId.cs | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 19 deletions(-) create mode 100644 SharpChat/SharpId.cs diff --git a/SharpChat/ChatChannel.cs b/SharpChat/ChatChannel.cs index 0570e40..34b7840 100644 --- a/SharpChat/ChatChannel.cs +++ b/SharpChat/ChatChannel.cs @@ -17,8 +17,7 @@ namespace SharpChat { public string TargetName => Name; - public ChatChannel() { - } + public ChatChannel() { } public ChatChannel(string name) { Name = name; diff --git a/SharpChat/ChatContext.cs b/SharpChat/ChatContext.cs index 894bf45..dea69ef 100644 --- a/SharpChat/ChatContext.cs +++ b/SharpChat/ChatContext.cs @@ -4,15 +4,13 @@ using System; using System.Collections.Generic; namespace SharpChat { - public class ChatContext : IDisposable, IPacketTarget { + public class ChatContext : IDisposable { public bool IsDisposed { get; private set; } public ChannelManager Channels { get; } public UserManager Users { get; } public ChatEventManager Events { get; } - public string TargetName => "@broadcast"; - public ChatContext() { Users = new(this); Channels = new(this); diff --git a/SharpChat/Database.cs b/SharpChat/Database.cs index cce4c27..28942ce 100644 --- a/SharpChat/Database.cs +++ b/SharpChat/Database.cs @@ -107,22 +107,9 @@ namespace SharpChat { return null; } - private const long ID_EPOCH = 1588377600000; - private static int IdCounter = 0; - - public static long GenerateId() { - if(IdCounter > 200) - IdCounter = 0; - - long id = 0; - id |= (DateTimeOffset.Now.ToUnixTimeMilliseconds() - ID_EPOCH) << 8; - id |= (ushort)(++IdCounter); - return id; - } - public static void LogEvent(IChatEvent evt) { if(evt.SequenceId < 1) - evt.SequenceId = GenerateId(); + evt.SequenceId = SharpId.Next(); RunCommand( "INSERT INTO `sqc_events` (`event_id`, `event_created`, `event_type`, `event_target`, `event_flags`, `event_data`" diff --git a/SharpChat/SharpId.cs b/SharpChat/SharpId.cs new file mode 100644 index 0000000..ad79736 --- /dev/null +++ b/SharpChat/SharpId.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading; + +namespace SharpChat { + public static class SharpId { + private const long EPOCH = 1588377600000; + private static int Counter = 0; + + public static long Next() + => ((DateTimeOffset.Now.ToUnixTimeMilliseconds() - EPOCH) << 8) + | (ushort)(Interlocked.Increment(ref Counter) & 0xFFFF); + } + +}