Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
add profile validation and async saving
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryImMouse committed Jul 18, 2024
1 parent 630e6cf commit 3ec9c98
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions Content.Server/_NF/Bank/BankSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Database;
using Content.Server.Preferences.Managers;
using Content.Server.GameTicking;
Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -71,15 +77,29 @@ private void OnBankAccountChanged(EntityUid mobUid, BankAccountComponent bank, r
new HashSet<ProtoId<TraitPrototype>>(profile.TraitPreferences), // Frontier Merge
new Dictionary<string, RoleLoadout>(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
/// <summary>
/// Attempts to remove money from a character's bank account. This should always be used instead of attempting to modify the bankaccountcomponent directly
/// </summary>
Expand Down

0 comments on commit 3ec9c98

Please sign in to comment.