void MultiplyExp(object sender, PendingTransactionEventArgs e) { // Should only work with system accounts as this will also make the sender pay more currency if (SEconomyPlugin.Instance != null && e.ToAccount != null && e.FromAccount != null && e.FromAccount.IsSystemAccount && (!e.Options.HasFlag(BankAccountTransferOptions.SuppressDefaultAnnounceMessages))) { // Find the player (note: could be troublesome if multiple login is enabled) var player = (from p in TShock.Players where p.IsLoggedIn && p.User.Name == e.ToAccount.UserAccountName && p.ContainsData(Contributor.DataKey) select p).FirstOrDefault(); if (player == null) { return; } Contributor con = player.GetData <Contributor>(Contributor.DataKey); // Get the tier, find the experience multiplier Tier tier = Tiers.Get(con.Tier); if (tier == null) { TShock.Log.ConsoleError($"CTRS: contributor {con.Accounts[0]} has an invalid tier ID! ({con.Tier})"); return; } if (tier.ExperienceMultiplier != 1f) { // Multiply the amount of currency gained by the experience multiplier e.Amount = new Money(Convert.ToInt64(e.Amount.Value * tier.ExperienceMultiplier)); } } }
public BankTransferEventArgs TransferBetween(IBankAccount FromAccount, IBankAccount ToAccount, Money Amount, BankAccountTransferOptions Options, string TransactionMessage, string JournalMessage) { BankTransferEventArgs args = new BankTransferEventArgs(); Guid profile = Guid.Empty; try { if (ToAccount != null && TransferMaySucceed(FromAccount, ToAccount, Amount, Options)) { PendingTransactionEventArgs pendingTransaction = new PendingTransactionEventArgs(FromAccount, ToAccount, Amount, Options, TransactionMessage, JournalMessage); if (BankTransactionPending != null) { BankTransactionPending(this, pendingTransaction); } if (pendingTransaction == null) { return(args); } args.Amount = pendingTransaction.Amount; args.SenderAccount = pendingTransaction.FromAccount; args.ReceiverAccount = pendingTransaction.ToAccount; args.TransferOptions = Options; args.TransferSucceeded = false; args.TransactionMessage = pendingTransaction.TransactionMessage; if (pendingTransaction.IsCancelled) { return(args); } //insert the source negative transaction ITransaction sourceTran = BeginSourceTransaction(FromAccount.BankAccountK, pendingTransaction.Amount, pendingTransaction.JournalLogMessage); if (sourceTran != null) { //insert the destination inverse transaction ITransaction destTran = FinishEndTransaction(sourceTran.BankAccountTransactionK, ToAccount, pendingTransaction.Amount, pendingTransaction.JournalLogMessage); if (destTran != null) { //perform the double-entry binding BindTransactions(ref sourceTran, ref destTran); args.TransactionID = sourceTran.BankAccountTransactionK; //update balances FromAccount.Balance += (Amount * (-1)); ToAccount.Balance += Amount; //transaction complete args.TransferSucceeded = true; } } } else { args.TransferSucceeded = false; TSPlayer from; if ((from = TShockAPI.TShock.Players.FirstOrDefault(i => i.Name == FromAccount.UserAccountName)) == null) { return(args); } /* * concept: ?????? * if the amount coming from "this" account is a negative then the "sender account" needs to know the transfer failed. * if the amount coming from "this" acount is a positive then the "reciever account" needs to know the transfer failed. */ if (ToAccount.IsSystemAccount == false && ToAccount.IsPluginAccount == false) { if (Amount < 0) { from.SendErrorMessage(SEconomyPlugin.Locale.StringOrDefault(83, "Invalid amount.")); } else { from.SendErrorMessage(SEconomyPlugin.Locale.StringOrDefault(84, "You need {0} more to make this payment."), ((Money)(FromAccount.Balance - Amount)).ToLongString()); } } } } catch (Exception ex) { args.Exception = ex; args.TransferSucceeded = false; } if (BankTransferCompleted != null) { BankTransferCompleted(this, args); } return(args); }
public BankTransferEventArgs TransferBetween(IBankAccount FromAccount, IBankAccount ToAccount, Money Amount, BankAccountTransferOptions Options, string TransactionMessage, string JournalMessage) { long accountCount = -1; PendingTransactionEventArgs pendingTransaction = new PendingTransactionEventArgs(FromAccount, ToAccount, Amount, Options, TransactionMessage, JournalMessage); ITransaction sourceTran, destTran; MySqlConnection conn = null; MySqlTransaction sqlTrans = null; BankTransferEventArgs args = new BankTransferEventArgs() { TransferSucceeded = false }; string accountVerifyQuery = @"select count(*) from `bank_account` where `bank_account_id` = @0;" ; Stopwatch sw = new Stopwatch(); if (SEconomyInstance.Configuration.EnableProfiler == true) { sw.Start(); } if (ToAccount == null || TransferMaySucceed(FromAccount, ToAccount, Amount, Options) == false) { return(args); } if ((conn = Connection) == null) { TShock.Log.ConsoleError(" seconomy mysql: Cannot connect to the SQL server"); return(args); } conn.Open(); if ((accountCount = Connection.QueryScalar <long>(accountVerifyQuery, FromAccount.BankAccountK)) != 1) { TShock.Log.ConsoleError(" seconomy mysql: Source account " + FromAccount.BankAccountK + " does not exist."); conn.Dispose(); return(args); } if ((accountCount = Connection.QueryScalar <long>(accountVerifyQuery, ToAccount.BankAccountK)) != 1) { TShock.Log.ConsoleError(" seconomy mysql: Source account " + FromAccount.BankAccountK + " does not exist."); conn.Dispose(); return(args); } if (BankTransactionPending != null) { BankTransactionPending(this, pendingTransaction); } if (pendingTransaction == null || pendingTransaction.IsCancelled == true) { return(args); } args.Amount = pendingTransaction.Amount; args.SenderAccount = pendingTransaction.FromAccount; args.ReceiverAccount = pendingTransaction.ToAccount; args.TransferOptions = Options; args.TransactionMessage = pendingTransaction.TransactionMessage; try { sqlTrans = conn.BeginTransaction(); if ((sourceTran = BeginSourceTransaction(sqlTrans, FromAccount.BankAccountK, pendingTransaction.Amount, pendingTransaction.JournalLogMessage)) == null) { throw new Exception("BeginSourceTransaction failed"); } if ((destTran = FinishEndTransaction(sqlTrans, ToAccount, pendingTransaction.Amount, pendingTransaction.JournalLogMessage)) == null) { throw new Exception("FinishEndTransaction failed"); } BindTransactions(sqlTrans, sourceTran.BankAccountTransactionK, destTran.BankAccountTransactionK); sqlTrans.Commit(); } catch (Exception ex) { if (conn != null && conn.State == ConnectionState.Open) { try { sqlTrans.Rollback(); } catch { TShock.Log.ConsoleError(" seconomy mysql: error in rollback:" + ex.ToString()); } } TShock.Log.ConsoleError(" seconomy mysql: database error in transfer:" + ex.ToString()); args.Exception = ex; return(args); } finally { if (conn != null) { conn.Dispose(); } } FromAccount.SyncBalance(); ToAccount.SyncBalance(); args.TransferSucceeded = true; if (BankTransferCompleted != null) { BankTransferCompleted(this, args); } if (SEconomyInstance.Configuration.EnableProfiler == true) { sw.Stop(); TShock.Log.ConsoleInfo("seconomy mysql: transfer took {0} ms", sw.ElapsedMilliseconds); } return(args); }
public BankTransferEventArgs TransferBetween(IBankAccount FromAccount, IBankAccount ToAccount, Money Amount, BankAccountTransferOptions Options, string TransactionMessage, string JournalMessage) { long accountCount = -1; PendingTransactionEventArgs pendingTransaction = new PendingTransactionEventArgs(FromAccount, ToAccount, Amount, Options, TransactionMessage, JournalMessage); ITransaction sourceTran, destTran; MySqlConnection conn = null; MySqlTransaction sqlTrans = null; BankTransferEventArgs args = new BankTransferEventArgs() { TransferSucceeded = false }; string accountVerifyQuery = @"select count(*) from `bank_account` where `bank_account_id` = @0;"; Stopwatch sw = new Stopwatch(); if (SEconomyInstance.Configuration.EnableProfiler == true) { sw.Start(); } if (ToAccount == null || TransferMaySucceed(FromAccount, ToAccount, Amount, Options) == false) { return args; } if ((conn = Connection) == null) { TShock.Log.ConsoleError(" seconomy mysql: Cannot connect to the SQL server"); return args; } conn.Open(); if ((accountCount = Connection.QueryScalar<long>(accountVerifyQuery, FromAccount.BankAccountK)) != 1) { TShock.Log.ConsoleError(" seconomy mysql: Source account " + FromAccount.BankAccountK + " does not exist."); conn.Dispose(); return args; } if ((accountCount = Connection.QueryScalar<long>(accountVerifyQuery, ToAccount.BankAccountK)) != 1) { TShock.Log.ConsoleError(" seconomy mysql: Source account " + FromAccount.BankAccountK + " does not exist."); conn.Dispose(); return args; } if (BankTransactionPending != null) { BankTransactionPending(this, pendingTransaction); } if (pendingTransaction == null || pendingTransaction.IsCancelled == true) { return args; } args.Amount = pendingTransaction.Amount; args.SenderAccount = pendingTransaction.FromAccount; args.ReceiverAccount = pendingTransaction.ToAccount; args.TransferOptions = Options; args.TransactionMessage = pendingTransaction.TransactionMessage; try { sqlTrans = conn.BeginTransaction(); if ((sourceTran = BeginSourceTransaction(sqlTrans, FromAccount.BankAccountK, pendingTransaction.Amount, pendingTransaction.JournalLogMessage)) == null) { throw new Exception("BeginSourceTransaction failed"); } if ((destTran = FinishEndTransaction(sqlTrans, ToAccount, pendingTransaction.Amount, pendingTransaction.JournalLogMessage)) == null) { throw new Exception("FinishEndTransaction failed"); } BindTransactions(sqlTrans, sourceTran.BankAccountTransactionK, destTran.BankAccountTransactionK); sqlTrans.Commit(); } catch (Exception ex) { if (conn != null && conn.State == ConnectionState.Open) { try { sqlTrans.Rollback(); } catch { TShock.Log.ConsoleError(" seconomy mysql: error in rollback:" + ex.ToString()); } } TShock.Log.ConsoleError(" seconomy mysql: database error in transfer:" + ex.ToString()); args.Exception = ex; return args; } finally { if (conn != null) { conn.Dispose(); } } FromAccount.SyncBalance(); ToAccount.SyncBalance(); args.TransferSucceeded = true; if (BankTransferCompleted != null) { BankTransferCompleted(this, args); } if (SEconomyInstance.Configuration.EnableProfiler == true) { sw.Stop(); TShock.Log.ConsoleInfo("seconomy mysql: transfer took {0} ms", sw.ElapsedMilliseconds); } return args; }
public BankTransferEventArgs TransferBetween(IBankAccount FromAccount, IBankAccount ToAccount, Money Amount, BankAccountTransferOptions Options, string TransactionMessage, string JournalMessage) { BankTransferEventArgs args = new BankTransferEventArgs(); Guid profile = Guid.Empty; try { if (ToAccount != null && TransferMaySucceed(FromAccount, ToAccount, Amount, Options)) { PendingTransactionEventArgs pendingTransaction = new PendingTransactionEventArgs(FromAccount, ToAccount, Amount, Options, TransactionMessage, JournalMessage); if (BankTransactionPending != null) { BankTransactionPending(this, pendingTransaction); } if (pendingTransaction == null) { return args; } args.Amount = pendingTransaction.Amount; args.SenderAccount = pendingTransaction.FromAccount; args.ReceiverAccount = pendingTransaction.ToAccount; args.TransferOptions = Options; args.TransferSucceeded = false; args.TransactionMessage = pendingTransaction.TransactionMessage; if (pendingTransaction.IsCancelled) { return args; } //insert the source negative transaction ITransaction sourceTran = BeginSourceTransaction(FromAccount.BankAccountK, pendingTransaction.Amount, pendingTransaction.JournalLogMessage); if (sourceTran != null) { //insert the destination inverse transaction ITransaction destTran = FinishEndTransaction(sourceTran.BankAccountTransactionK, ToAccount, pendingTransaction.Amount, pendingTransaction.JournalLogMessage); if (destTran != null) { //perform the double-entry binding BindTransactions(ref sourceTran, ref destTran); args.TransactionID = sourceTran.BankAccountTransactionK; //update balances FromAccount.Balance += (Amount * (-1)); ToAccount.Balance += Amount; //transaction complete args.TransferSucceeded = true; } } } else { args.TransferSucceeded = false; TSPlayer from; if ((from = TShockAPI.TShock.Players.FirstOrDefault(i => i.UserAccountName == FromAccount.UserAccountName)) == null) { return args; } /* * concept: ?????? * if the amount coming from "this" account is a negative then the "sender account" needs to know the transfer failed. * if the amount coming from "this" acount is a positive then the "reciever account" needs to know the transfer failed. */ if (ToAccount.IsSystemAccount == false && ToAccount.IsPluginAccount == false) { if (Amount < 0) { from.SendErrorMessage(SEconomyPlugin.Locale.StringOrDefault(83, "Invalid amount.")); } else { from.SendErrorMessage(SEconomyPlugin.Locale.StringOrDefault(84, "You need {0} more to make this payment."), ((Money)(FromAccount.Balance - Amount)).ToLongString()); } } } } catch (Exception ex) { args.Exception = ex; args.TransferSucceeded = false; } if (BankTransferCompleted != null) { BankTransferCompleted(this, args); } return args; }