예제 #1
0
        public async void SEconomyTransferAsync(Journal.IBankAccount From, Journal.IBankAccount To, Money Amount, string TxMessage, JsValue completedCallback)
        {
            BankTransferEventArgs result = null;

            if (JistPlugin.Instance == null ||
                SEconomyPlugin.Instance == null ||
                From == null ||
                To == null)
            {
                return;
            }

            result = await From.TransferToAsync(To, Amount,
                                                Journal.BankAccountTransferOptions.AnnounceToSender,
                                                TxMessage, TxMessage);

            if (result == null)
            {
                result = new BankTransferEventArgs()
                {
                    TransferSucceeded = false
                };
            }

            Jist.JistPlugin.Instance.CallFunction(completedCallback, null, result);
        }
예제 #2
0
        async Task SetBalanceToAsync(Money money)
        {
            await account.SyncBalanceAsync();

            BankTransferEventArgs args = await account.OwningJournal.TransferBetweenAsync(parent.WorldAccount, account, money - account.Balance, BankAccountTransferOptions.AnnounceToReceiver | BankAccountTransferOptions.AnnounceToSender, "Balance Adjustment", "Balance Adjustment from bank manager.");

            if (args.TransferSucceeded == true)
            {
                this.MainThreadInvoke(() => this.Hide());
                return;
            }

            MessageBox.Show("Transfer failed.", "Transfer failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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;
		}
예제 #6
0
        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;
        }
예제 #7
0
        async void BankTransferCompleted(object sender, BankTransferEventArgs e)
        {
            // Null check the instance - Thanks Wolfje
            if (SEconomyPlugin.Instance == null)
            {
                return;
            }

            // The world account does not have a rank
            if (e.ReceiverAccount == null ||
                !e.ReceiverAccount.IsAccountEnabled ||
                e.ReceiverAccount.IsSystemAccount ||
                SEconomyPlugin.Instance.WorldAccount == null)
            {
                return;
            }

            // Stop chain transfers
            if (e.TransactionMessage.StartsWith("AutoRank"))
            {
                return;
            }

            TSPlayer ply = TShock.Players.FirstOrDefault(p => p != null && p.Active && p.IsLoggedIn &&
                                                         p.User.Name == e.ReceiverAccount.UserAccountName);

            if (ply == null)
            {
                return;
            }

            var rank = ply.GetRank();

            if (rank != null)
            {
                var ranks = rank.FindNextRanks(e.ReceiverAccount.Balance);
                if (ranks != null && ranks.Count > 0)
                {
                    //Money cost = 0L;
                    //foreach (Rank rk in ranks)
                    //{
                    //	cost += rk.Cost();
                    //}

                    //Money balance = e.ReceiverAccount.Balance;
                    //var task = await e.ReceiverAccount.TransferToAsync(SEconomyPlugin.Instance.WorldAccount, cost,
                    //	BankAccountTransferOptions.SuppressDefaultAnnounceMessages, "",
                    //	String.Format("{0} paid {1} to rank up with AutoRank.", ply.Name, cost.ToString()));
                    //if (!task.TransferSucceeded)
                    //{
                    //	if (task.Exception != null)
                    //	{
                    //		Log.ConsoleError("SEconomy Exception: {0}\nCheck logs for details.", task.Exception.Message);
                    //		Log.Error(task.Exception.ToString());
                    //	}

                    //	// Returning the money; This transaction may fail, but I see no other way.
                    //	await SEconomyPlugin.Instance.WorldAccount.TransferToAsync(e.ReceiverAccount,
                    //		balance - e.ReceiverAccount.Balance, BankAccountTransferOptions.SuppressDefaultAnnounceMessages,
                    //		"", "");
                    //	ply.SendErrorMessage(
                    //		"Your transaction could not be completed. Start a new transaction to retry.");
                    //}
                    //else

                    await ply.RankUpAsync(ranks);

                    //Task.Factory.StartNew(await args.ReceiverAccount.TransferToAsync(SEconomyPlugin.Instance.WorldAccount, cost,
                    //	BankAccountTransferOptions.None, String.Empty,
                    //	String.Format("{0} paid {1} to rank up with AutoRank.", ply.Name,
                    //	cost.ToString())) }); .ContinueWith((task) =>
                    //		{
                    //			if (!task.Result.TransferSucceeded)
                    //			{
                    //				ply.SendErrorMessage(
                    //					"Your transaction could not be completed. Start a new transaction to retry.");
                    //				return;
                    //			}
                    //			ply.RankUpAsync(ranks);
                    //		});
                }
            }
        }