Exemplo n.º 1
0
        public async Task SquashJournalAsync()
        {
            int  bankAccountCount = BankAccounts.Count();
            bool responsibleForTurningBackupsBackOn = false;

            Console.WriteLine(SEconomyPlugin.Locale.StringOrDefault(81, "seconomy xml: beginning Squash"));

            if (SEconomyInstance.RunningJournal.BackupsEnabled == true)
            {
                SEconomyInstance.RunningJournal.BackupsEnabled = false;
                responsibleForTurningBackupsBackOn             = true;
            }

            for (int i = 0; i < bankAccountCount; i++)
            {
                IBankAccount account = BankAccounts.ElementAtOrDefault(i);
                if (account == null)
                {
                    continue;
                }

                // Add the squished summary
                ITransaction squash = new XmlTransaction(account)
                {
                    Amount             = account.Transactions.Sum(x => x.Amount),
                    Flags              = BankAccountTransactionFlags.FundsAvailable | BankAccountTransactionFlags.Squashed,
                    TransactionDateUtc = DateTime.UtcNow,
                    Message            = "Transaction squash"
                };

                account.Transactions.Clear();
                account.AddTransaction(squash);
            }

            //abandon the old journal and assign the squashed one
            Console.WriteLine(SEconomyPlugin.Locale.StringOrDefault(82, "re-syncing online accounts."));

            foreach (TSPlayer player in TShockAPI.TShock.Players)
            {
                IBankAccount account = null;
                if (player == null ||
                    SEconomyPlugin.Instance == null ||
                    (account = SEconomyPlugin.Instance.GetBankAccount(player)) == null)
                {
                    return;
                }
                Console.WriteLine("re-syncing {0}", player.Name);
                await account.SyncBalanceAsync();
            }

            await SaveJournalAsync();

            if (responsibleForTurningBackupsBackOn)
            {
                /*
                 * the backups could already have been disabled by something else.
                 * We don't want to be the ones turning it back on
                 */
                SEconomyInstance.RunningJournal.BackupsEnabled = true;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Compresses all transactions into one line.  Doing this is going to remove all transaction history but you gain space and processing speed
        /// </summary>
        public static void SquashJournal()
        {
            int       bankAccountCount = BankAccounts.Count();
            int       tranCount        = Transactions.Count();
            XDocument newJournal       = NewJournal();

            bool responsibleForTurningBackupsBackOn = false;

            Console.WriteLine("seconomy xml: beginning Squash");

            if (SEconomyPlugin.BackupCanRun == true)
            {
                SEconomyPlugin.BackupCanRun        = false;
                responsibleForTurningBackupsBackOn = true;
            }

            for (int i = 0; i < bankAccountCount; i++)
            {
                XBankAccount account = BankAccounts.ElementAtOrDefault(i);
                if (account != null)
                {
                    //update account balance
                    account.SyncBalance();

                    string line = string.Format("\r [squash] {0:p} {1}", (double)i / (double)bankAccountCount, account.UserAccountName);
                    SEconomyPlugin.FillWithSpaces(ref line);

                    Console.Write(line);

                    //copy the bank account from the old journal into the new one
                    newJournal.Element("Journal").Element("BankAccounts").Add((XElement)account);

                    //Add the squished summary
                    XTransaction transSummary = new XTransaction(account.Balance);
                    transSummary.BankAccountTransactionK = RandomString(13);
                    transSummary.BankAccountFK           = account.BankAccountK;
                    transSummary.Flags              = BankAccountTransactionFlags.FundsAvailable | BankAccountTransactionFlags.Squashed;
                    transSummary.Message            = "Transaction squash";
                    transSummary.TransactionDateUtc = DateTime.UtcNow;

                    newJournal.Element("Journal").Element("Transactions").Add((XElement)transSummary);
                }
            }

            //abandon the old journal and assign the squashed one
            XmlJournal = newJournal;

            Console.WriteLine("re-syncing online accounts.");

            foreach (XBankAccount account in BankAccounts)
            {
                XBankAccount runtimeAccount = GetBankAccount(account.BankAccountK);
                if (runtimeAccount != null && runtimeAccount.Owner != null)
                {
                    Console.WriteLine("re-syncing {0}", runtimeAccount.Owner.TSPlayer.Name);
                    runtimeAccount.SyncBalance();
                }
            }

            SaveXml(Configuration.JournalPath);

            //the backups could already have been disabled by something else.  We don't want to be the ones turning it back on
            if (responsibleForTurningBackupsBackOn)
            {
                SEconomyPlugin.BackupCanRun = true;
            }
        }