From 3ec9c9809456431b92852772f1aa4aeb58894214 Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Thu, 18 Jul 2024 05:43:19 +0500 Subject: [PATCH] add profile validation and async saving --- Content.Server/_NF/Bank/BankSystem.cs | 28 +++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Content.Server/_NF/Bank/BankSystem.cs b/Content.Server/_NF/Bank/BankSystem.cs index d2ad4d25248..54ab026d0bf 100644 --- a/Content.Server/_NF/Bank/BankSystem.cs +++ b/Content.Server/_NF/Bank/BankSystem.cs @@ -1,4 +1,5 @@ using System.Threading; +using System.Threading.Tasks; using Content.Server.Database; using Content.Server.Preferences.Managers; using Content.Server.GameTicking; @@ -8,7 +9,6 @@ using Robust.Shared.Network; using Content.Shared._NF.Bank.Events; using Robust.Server.Player; -using Content.Server.Cargo.Components; using Content.Shared.Preferences.Loadouts; using Robust.Shared.Prototypes; using Content.Shared.Roles; @@ -22,6 +22,9 @@ public sealed partial class BankSystem : EntitySystem [Dependency] private readonly IServerPreferencesManager _prefsManager = default!; [Dependency] private readonly IServerDbManager _dbManager = default!; [Dependency] private readonly IPlayerManager _playerManager = default!; + + [Dependency] private readonly IDependencyCollection _dependencies = default!; // Corvax-Frontier-Changes + private ISawmill _log = default!; public override void Initialize() @@ -39,6 +42,9 @@ public override void Initialize() // TODO: stop it from running 5 times every time private void OnBankAccountChanged(EntityUid mobUid, BankAccountComponent bank, ref ComponentGetState args) { + if (args.Player is null) + return; + var user = args.Player?.UserId; if (user == null || args.Player?.AttachedEntity != mobUid) @@ -71,15 +77,29 @@ private void OnBankAccountChanged(EntityUid mobUid, BankAccountComponent bank, r new HashSet>(profile.TraitPreferences), // Frontier Merge new Dictionary(profile.Loadouts)); + newProfile.EnsureValid(args.Player, _dependencies, []); + args.State = new BankAccountComponentState { Balance = bank.Balance, }; - - _dbManager.SaveCharacterSlotAsync((NetUserId) user, newProfile, index); + // idk if it works as i want + Task.Run(() => SaveCharSlot((NetUserId) user, profile, index)).Wait(300); // Corvax-Frontier-Changes _log.Info($"Character {profile.Name} saved"); } - + // Corvax-Frontier-Changes-Start + private async Task SaveCharSlot(NetUserId userId, HumanoidCharacterProfile profile, int index) + { + try + { + await _dbManager.SaveCharacterSlotAsync(userId, profile, index); + } + catch (Exception ex) + { + _log.Error($"Caught exception while saving bank account to database: {ex.Message}. Stack Trace: {ex.StackTrace}"); + } + } + // Corvax-Frontier-Changes-End /// /// Attempts to remove money from a character's bank account. This should always be used instead of attempting to modify the bankaccountcomponent directly ///