From adf06a75d5b7e25e29d180cc59f3345be0abe137 Mon Sep 17 00:00:00 2001 From: raphaeIl Date: Thu, 30 May 2024 02:53:43 +0800 Subject: [PATCH] Proper Echelon Handling --- .../ModelExtensions/AccountExtensions.cs | 11 +++++++++++ SCHALE.Common/Database/dbs.cs | 2 ++ .../Controllers/Api/ProtocolHandlers/Echelon.cs | 16 +++++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/SCHALE.Common/Database/ModelExtensions/AccountExtensions.cs b/SCHALE.Common/Database/ModelExtensions/AccountExtensions.cs index 1272911..450c252 100644 --- a/SCHALE.Common/Database/ModelExtensions/AccountExtensions.cs +++ b/SCHALE.Common/Database/ModelExtensions/AccountExtensions.cs @@ -65,5 +65,16 @@ return [.. gears]; } + + public static List AddEchelons(this AccountDB account, SCHALEContext context, params EchelonDB[] echelons) + { + foreach (var echelon in echelons) + { + echelon.AccountServerId = account.ServerId; + context.Echelons.Add(echelon); + } + + return [.. echelons]; + } } } diff --git a/SCHALE.Common/Database/dbs.cs b/SCHALE.Common/Database/dbs.cs index e3f8e43..ac2e5a9 100644 --- a/SCHALE.Common/Database/dbs.cs +++ b/SCHALE.Common/Database/dbs.cs @@ -1194,7 +1194,9 @@ namespace SCHALE.Common.Database [JsonIgnore] public virtual AccountDB Account { get; set; } + [JsonIgnore] public long AccountServerId { get; set; } + public EchelonType EchelonType { get; set; } public long EchelonNumber { get; set; } public EchelonExtensionType ExtensionType { get; set; } diff --git a/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs b/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs index 8a286e1..cbdbe6e 100644 --- a/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs +++ b/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs @@ -31,9 +31,19 @@ namespace SCHALE.GameServer.Controllers.Api.ProtocolHandlers [ProtocolHandler(Protocol.Echelon_Save)] public ResponsePacket SaveHandler(EchelonSaveRequest req) { - var db = req.EchelonDB; - - context.Echelons.Add(db); + var account = sessionKeyService.GetAccount(req.SessionKey); + + var newEchelon = req.EchelonDB; + var existingEchelon = context.Echelons.FirstOrDefault(e => e.AccountServerId == newEchelon.AccountServerId && e.EchelonType == newEchelon.EchelonType && + e.EchelonNumber == newEchelon.EchelonNumber && e.ExtensionType == newEchelon.ExtensionType); + + if (existingEchelon != null) + { + context.Echelons.Remove(existingEchelon); + context.SaveChanges(); + } + + account.AddEchelons(context, [newEchelon]); context.SaveChanges(); return new EchelonSaveResponse() { EchelonDB = req.EchelonDB, };