diff --git a/LuskiServer/Classes/AppConfig.cs b/LuskiServer/Classes/AppConfig.cs index aebde24..013dbef 100644 --- a/LuskiServer/Classes/AppConfig.cs +++ b/LuskiServer/Classes/AppConfig.cs @@ -8,6 +8,12 @@ public class AppConfig [JsonPropertyName("address")] public string Address { get; set; } = "127.0.0.1"; [JsonInclude] + [JsonPropertyName("data_id")] + public byte DataId { get; set; } = 0; + [JsonInclude] + [JsonPropertyName("server_epoch")] + public long ServerEpoch { get; set; } = Luski.Info.GetTimestampFromEpoch(0); + [JsonInclude] [JsonPropertyName("database")] public string Database { get; set; } = "Some database name"; [JsonInclude] diff --git a/LuskiServer/Classes/EXT.cs b/LuskiServer/Classes/EXT.cs index 6bb4e14..1ef47bf 100644 --- a/LuskiServer/Classes/EXT.cs +++ b/LuskiServer/Classes/EXT.cs @@ -44,7 +44,7 @@ public static class EXT if (LogInDB) { Tables.Logs.Insert( - Logs.ID.CreateParameter(Luski.Snowflake.GenerateSnowflake(WorkerId.Log).ID), + Logs.ID.CreateParameter(Luski.Snowflake.GenerateSnowflake(Luski.Config.ServerEpoch) .ID), Logs.Type.CreateParameter(LogType.Error), Logs.Message.CreateParameter(Error.ToString())); } diff --git a/LuskiServer/Classes/Luski.cs b/LuskiServer/Classes/Luski.cs index b8e0cdb..7ccfc80 100644 --- a/LuskiServer/Classes/Luski.cs +++ b/LuskiServer/Classes/Luski.cs @@ -78,15 +78,12 @@ public static class Luski } } - public static readonly DateTime Epoch = new(2023, 1, 1, 0, 0, 0, 0); - - public static long Timestamp + // public static readonly DateTime ServerEpoch = new(2023, 1, 1, 0, 0, 0, 0); + + public static long GetTimestampFromEpoch(long Epoch) { - get - { - double ts = Math.Round(DateTime.Now.Subtract(Epoch).TotalMilliseconds, 0); - return long.Parse(ts.ToString().Replace(".", string.Empty)); - } + double ts = Math.Round(DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalMilliseconds - Epoch, 0); + return long.Parse(ts.ToString().Replace(".", string.Empty)); } } @@ -95,23 +92,21 @@ public static class Luski public Snowflake(long ID) { this.ID = ID; - Increment = (ushort)((ID << 52) >> 52); - Worker_ID = (ushort)((ID << 47) >> 59); - Server_ID = (ushort)((ID << 42) >> 59); - Timestamp = ID >> 22; + Increment = (ushort)((ID << 52) >>> 52); + Data_ID = (byte)((ID << 44) >>> 56); + Timestamp = ID >> 20; } - public static Snowflake GenerateSnowflake(WorkerId workerID) + public static Snowflake GenerateSnowflake(long Epoch) { i++; - if (i > 4096) i = 0; - return new Snowflake((((((Info.Timestamp << 5) | (ushort)0) << 5) | (ushort)workerID) << 12) | i); + if (i > 0b_1111_1111_1111) i = 0; + return new Snowflake((((Info.GetTimestampFromEpoch(Epoch) << 8) | Config.DataId) << 12) | i); } public long ID { get; } public long Timestamp { get; } - public ushort Worker_ID { get; } - public ushort Server_ID { get; } + public byte Data_ID { get; } public ushort Increment { get; } private static ushort i = 0; diff --git a/LuskiServer/Controllers/v1/CreateAccountController.cs b/LuskiServer/Controllers/v1/CreateAccountController.cs index 2a6c32b..d838470 100644 --- a/LuskiServer/Controllers/v1/CreateAccountController.cs +++ b/LuskiServer/Controllers/v1/CreateAccountController.cs @@ -71,7 +71,7 @@ public class CreateAccountController : ControllerBase { int num = new Random().Next(1000, 1000000000); int num2 = new Random().Next(1000, 1000000000); - Luski.Snowflake id = Luski.Snowflake.GenerateSnowflake(WorkerId.CreateAccount); + Luski.Snowflake id = Luski.Snowflake.GenerateSnowflake(Luski.Config.ServerEpoch); byte[] ID = Encoding.UTF8.GetBytes(id.ID.ToString()); byte[] Timestamp = Encoding.UTF8.GetBytes(DateTime.UtcNow.ToString()); byte[] Number = Encoding.UTF8.GetBytes(num.ToString()); diff --git a/LuskiServer/Program.cs b/LuskiServer/Program.cs index c80bd94..469a0ca 100644 --- a/LuskiServer/Program.cs +++ b/LuskiServer/Program.cs @@ -12,6 +12,7 @@ using Swashbuckle.AspNetCore.SwaggerGen; [assembly: Microsoft.AspNetCore.Mvc.ApiController] Luski.Config = Luski.GetSettings("/etc/luskiserver/app.json", AppConfigContext.Default.AppConfig, true); +Console.WriteLine($"Server starting with starting epoch of {DateTime.UnixEpoch.AddMilliseconds(Luski.Config.ServerEpoch).ToLocalTime()}"); Luski.Database = new Database(Luski.Config.Address, Luski.Config.Database, Luski.Config.Username,