diff --git a/Projects/UOContent/Mobiles/Townfolk/Banker.cs b/Projects/UOContent/Mobiles/Townfolk/Banker.cs index 77c2c696a..b9f149463 100644 --- a/Projects/UOContent/Mobiles/Townfolk/Banker.cs +++ b/Projects/UOContent/Mobiles/Townfolk/Banker.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using ModernUO.Serialization; using Server.Accounting; +using Server.Collections; using Server.ContextMenus; using Server.Items; using Server.Network; @@ -63,10 +64,11 @@ public partial class Banker : BaseVendor return (int)Math.Clamp(balance, 0, int.MaxValue); } - public static int GetBalance(Mobile m, out List gold, out List checks) + public static int GetBalance(Mobile m, out List gold, out List checks) { long balance = 0; - gold = checks = new List(); + gold = null; + checks = null; if (AccountGold.Enabled && m.Account != null) { @@ -82,6 +84,8 @@ public partial class Banker : BaseVendor if (bank != null) { + gold = new List(); + foreach (var g in bank.FindItemsByType()) { balance += g.Amount; @@ -93,6 +97,8 @@ public partial class Banker : BaseVendor return int.MaxValue; } + checks = new List(); + foreach (var bc in bank.FindItemsByType()) { balance += bc.Worth; @@ -103,6 +109,52 @@ public partial class Banker : BaseVendor return (int)Math.Clamp(balance, 0, int.MaxValue); } + private static bool HasRequiredBalance(int requiredBalance, Mobile m, out PooledRefList gold, out PooledRefList checks) + { + Container bank = m.FindBankNoCreate(); + + if (bank == null) + { + gold = default; + checks = default; + + return false; + } + + long balance = 0; + + gold = PooledRefList.Create(); + foreach (var g in bank.FindItemsByType()) + { + balance += g.Amount; + gold.Add(g); + + if (balance >= requiredBalance) + { + checks = default; + return true; + } + } + + checks = PooledRefList.Create(); + + foreach (var bc in bank.FindItemsByType()) + { + balance += bc.Worth; + checks.Add(bc); + + if (balance >= requiredBalance) + { + return true; + } + } + + gold.Dispose(); + checks.Dispose(); + + return false; + } + public static bool Withdraw(Mobile from, int amount) { // If for whatever reason the TOL checks fail, we should still try old methods for withdrawing currency. @@ -111,30 +163,30 @@ public partial class Banker : BaseVendor return true; } - var balance = GetBalance(from, out var gold, out var checks); - - if (balance < amount) + if (!HasRequiredBalance(amount, from, out var gold, out var checks)) { return false; } for (var i = 0; amount > 0 && i < gold.Count; ++i) { - if (gold[i].Amount <= amount) + var g = gold[i]; + + if (g.Amount <= amount) { - amount -= gold[i].Amount; - gold[i].Delete(); + amount -= g.Amount; + g.Delete(); } else { - gold[i].Amount -= amount; + g.Amount -= amount; amount = 0; } } for (var i = 0; amount > 0 && i < checks.Count; ++i) { - var check = (BankCheck)checks[i]; + var check = checks[i]; if (check.Worth <= amount) { @@ -148,6 +200,9 @@ public partial class Banker : BaseVendor } } + gold.Dispose(); + checks.Dispose(); + return true; }