示例#1
0
        /// <summary>
        /// Creates the world account if needed and sets it.
        /// </summary>
        public async Task EnsureWorldAccountExistsAsync()
        {
            //World account matches the current world, ignore.
            if (SEconomyPlugin.WorldAccount != null && SEconomyPlugin.WorldAccount.WorldID == Terraria.Main.worldID)
            {
                return;
            }

            if (Terraria.Main.worldID > 0)
            {
                int bankAccountK = 0;
                DatabaseObjects.BankAccount worldAccount = await(from i in AsyncConnection.Table <DatabaseObjects.BankAccount>()
                                                                 where (i.Flags & DatabaseObjects.BankAccountFlags.SystemAccount) == DatabaseObjects.BankAccountFlags.SystemAccount &&
                                                                 (i.Flags & DatabaseObjects.BankAccountFlags.PluginAccount) == 0 &&
                                                                 i.WorldID == Terraria.Main.worldID
                                                                 select i).FirstOrDefaultAsync();

                if (worldAccount == null)
                {
                    //world account does not exist for this world ID, create one
                    worldAccount = new DatabaseObjects.BankAccount();
                    worldAccount.UserAccountName = "SYSTEM";
                    worldAccount.WorldID         = Terraria.Main.worldID;
                    worldAccount.Description     = "World account for world " + Terraria.Main.worldName;
                    //This account is always enabled, locked to the world it's in and a system account (ie. can run into deficit) but not a plugin account
                    worldAccount.Flags = DatabaseObjects.BankAccountFlags.Enabled | DatabaseObjects.BankAccountFlags.LockedToWorld | DatabaseObjects.BankAccountFlags.SystemAccount;

                    bankAccountK = await AsyncConnection.InsertAsync(worldAccount);

                    worldAccount.BankAccountK = bankAccountK;
                }

                if (worldAccount != null && worldAccount.BankAccountK > 0)
                {
                    //Is this account listed as enabled?
                    bool accountEnabled = (worldAccount.Flags & DatabaseObjects.BankAccountFlags.Enabled) == DatabaseObjects.BankAccountFlags.Enabled;

                    if (!accountEnabled)
                    {
                        TShockAPI.Log.ConsoleError("The world account for world " + Terraria.Main.worldName + " is disabled.  Currency will not work for this game.");
                    }
                    else
                    {
                        SEconomyPlugin.WorldAccount = new Economy.BankAccount(worldAccount);
                    }
                }
                else
                {
                    Log.ConsoleError("There was an error loading the bank account for this world.  Currency will not work for this game.");
                }



                /*
                 * See how much easier this shit is?
                 *
                 *
                 * return AsyncConnection.Table<DatabaseObjects.BankAccount>().Where(i => (i.Flags & DatabaseObjects.BankAccountFlags.SystemAccount) == DatabaseObjects.BankAccountFlags.SystemAccount
                 *      && (i.Flags & DatabaseObjects.BankAccountFlags.PluginAccount) == 0
                 *      && i.WorldID == Terraria.Main.worldID).FirstOrDefaultAsync().ContinueWith((worldAccountResult) => {
                 *
                 *  if (worldAccountResult.Result == null) {
                 *      //world account does not exist for this world ID, create one
                 *      DatabaseObjects.BankAccount worldAccount = new DatabaseObjects.BankAccount();
                 *      worldAccount.UserAccountName = "SYSTEM";
                 *      worldAccount.WorldID = Terraria.Main.worldID;
                 *      worldAccount.Description = "World account for world " + Terraria.Main.worldName;
                 *
                 *      //This account is always enabled, locked to the world it's in and a system account (ie. can run into deficit) but not a plugin account
                 *      worldAccount.Flags = DatabaseObjects.BankAccountFlags.Enabled | DatabaseObjects.BankAccountFlags.LockedToWorld | DatabaseObjects.BankAccountFlags.SystemAccount;
                 *
                 *      AsyncConnection.InsertAsync(worldAccount).ContinueWith((newPrimaryKey) => {
                 *          int bankAccountK = newPrimaryKey.Result;
                 *
                 *          if (bankAccountK > 0) {
                 *
                 *              //Retrieve the new world account from the database
                 *              AsyncConnection.Table<DatabaseObjects.BankAccount>().Where(i => i.BankAccountK == bankAccountK).FirstOrDefaultAsync().ContinueWith((newWorldAccountResult) => {
                 *                  if (newWorldAccountResult.Result != null) {
                 *
                 *                      //override world account inserter with the new one.
                 *                      worldAccount = newWorldAccountResult.Result;
                 *
                 *                      //Is this account listed as enabled?
                 *                      bool accountEnabled = (worldAccount.Flags & DatabaseObjects.BankAccountFlags.Enabled) == DatabaseObjects.BankAccountFlags.Enabled;
                 *
                 *                      if (!accountEnabled) {
                 *                          TShockAPI.Log.ConsoleError("The world account for world " + Terraria.Main.worldName + " is disabled.  Currency will not work for this game.");
                 *                      }
                 *
                 *                      //Push it back to the main instance.
                 *                      SEconomyPlugin.WorldAccount = new Economy.BankAccount(worldAccount);
                 *                  }
                 *              });
                 *
                 *          } else {
                 *              Log.ConsoleError(string.Format("SEconomy: error: create world account for {0} failed.", Terraria.Main.worldName));
                 *          }
                 *      });
                 *
                 *  } else {
                 *      DatabaseObjects.BankAccount worldAccount = worldAccountResult.Result;
                 *
                 *      //Is this account listed as enabled?
                 *      bool accountEnabled = (worldAccount.Flags & DatabaseObjects.BankAccountFlags.Enabled) == DatabaseObjects.BankAccountFlags.Enabled;
                 *
                 *      if (!accountEnabled) {
                 *          TShockAPI.Log.ConsoleError("The world account for world " + Terraria.Main.worldName + " is disabled.  Currency will not work for this game.");
                 *      }
                 *
                 *      //Assign the world account to the running world.
                 *      SEconomyPlugin.WorldAccount = new Economy.BankAccount(worldAccount);
                 *  }
                 * });
                 *
                 * } else {
                 * return Task.Factory.StartNew(() => {
                 *  TShockAPI.Log.ConsoleError("SEconomy: EnsureBankAccountExists called but no world has been loaded yet.");
                 * });
                 */
            }
        }
        /// <summary>
        /// Creates the world account if needed and sets it.
        /// </summary>
        public async Task EnsureWorldAccountExistsAsync() {

            //World account matches the current world, ignore.
            if (SEconomyPlugin.WorldAccount != null && SEconomyPlugin.WorldAccount.WorldID == Terraria.Main.worldID) {
                return;
            }

            if (Terraria.Main.worldID > 0) {
                int bankAccountK = 0;
                DatabaseObjects.BankAccount worldAccount = await (from i in AsyncConnection.Table<DatabaseObjects.BankAccount>()
                                                                  where (i.Flags & DatabaseObjects.BankAccountFlags.SystemAccount) == DatabaseObjects.BankAccountFlags.SystemAccount
                                                                     && (i.Flags & DatabaseObjects.BankAccountFlags.PluginAccount) == 0
                                                                     && i.WorldID == Terraria.Main.worldID
                                                                  select i).FirstOrDefaultAsync();

                if (worldAccount == null) {
                    //world account does not exist for this world ID, create one
                    worldAccount = new DatabaseObjects.BankAccount();
                    worldAccount.UserAccountName = "SYSTEM";
                    worldAccount.WorldID = Terraria.Main.worldID;
                    worldAccount.Description = "World account for world " + Terraria.Main.worldName;
                    //This account is always enabled, locked to the world it's in and a system account (ie. can run into deficit) but not a plugin account
                    worldAccount.Flags = DatabaseObjects.BankAccountFlags.Enabled | DatabaseObjects.BankAccountFlags.LockedToWorld | DatabaseObjects.BankAccountFlags.SystemAccount;

                    bankAccountK = await AsyncConnection.InsertAsync(worldAccount);
                    worldAccount.BankAccountK = bankAccountK;
                }

                if (worldAccount != null && worldAccount.BankAccountK > 0) {
                    //Is this account listed as enabled?
                    bool accountEnabled = (worldAccount.Flags & DatabaseObjects.BankAccountFlags.Enabled) == DatabaseObjects.BankAccountFlags.Enabled;

                    if (!accountEnabled) {
                        TShockAPI.Log.ConsoleError("The world account for world " + Terraria.Main.worldName + " is disabled.  Currency will not work for this game.");
                    } else {
                        SEconomyPlugin.WorldAccount = new Economy.BankAccount(worldAccount);
                    }
                } else {
                    Log.ConsoleError("There was an error loading the bank account for this world.  Currency will not work for this game.");
                }



                /*
                 * See how much easier this shit is?
                 * 
                 * 
                return AsyncConnection.Table<DatabaseObjects.BankAccount>().Where(i => (i.Flags & DatabaseObjects.BankAccountFlags.SystemAccount) == DatabaseObjects.BankAccountFlags.SystemAccount
                        && (i.Flags & DatabaseObjects.BankAccountFlags.PluginAccount) == 0
                        && i.WorldID == Terraria.Main.worldID).FirstOrDefaultAsync().ContinueWith((worldAccountResult) => {

                    if (worldAccountResult.Result == null) {
                        //world account does not exist for this world ID, create one
                        DatabaseObjects.BankAccount worldAccount = new DatabaseObjects.BankAccount();
                        worldAccount.UserAccountName = "SYSTEM";
                        worldAccount.WorldID = Terraria.Main.worldID;
                        worldAccount.Description = "World account for world " + Terraria.Main.worldName;

                        //This account is always enabled, locked to the world it's in and a system account (ie. can run into deficit) but not a plugin account
                        worldAccount.Flags = DatabaseObjects.BankAccountFlags.Enabled | DatabaseObjects.BankAccountFlags.LockedToWorld | DatabaseObjects.BankAccountFlags.SystemAccount;

                        AsyncConnection.InsertAsync(worldAccount).ContinueWith((newPrimaryKey) => {
                            int bankAccountK = newPrimaryKey.Result;

                            if (bankAccountK > 0) {

                                //Retrieve the new world account from the database
                                AsyncConnection.Table<DatabaseObjects.BankAccount>().Where(i => i.BankAccountK == bankAccountK).FirstOrDefaultAsync().ContinueWith((newWorldAccountResult) => {
                                    if (newWorldAccountResult.Result != null) {

                                        //override world account inserter with the new one.
                                        worldAccount = newWorldAccountResult.Result;

                                        //Is this account listed as enabled?
                                        bool accountEnabled = (worldAccount.Flags & DatabaseObjects.BankAccountFlags.Enabled) == DatabaseObjects.BankAccountFlags.Enabled;

                                        if (!accountEnabled) {
                                            TShockAPI.Log.ConsoleError("The world account for world " + Terraria.Main.worldName + " is disabled.  Currency will not work for this game.");
                                        }

                                        //Push it back to the main instance.
                                        SEconomyPlugin.WorldAccount = new Economy.BankAccount(worldAccount);
                                    }
                                });

                            } else {
                                Log.ConsoleError(string.Format("SEconomy: error: create world account for {0} failed.", Terraria.Main.worldName));
                            }
                        });

                    } else {
                        DatabaseObjects.BankAccount worldAccount = worldAccountResult.Result;

                        //Is this account listed as enabled?
                        bool accountEnabled = (worldAccount.Flags & DatabaseObjects.BankAccountFlags.Enabled) == DatabaseObjects.BankAccountFlags.Enabled;

                        if (!accountEnabled) {
                            TShockAPI.Log.ConsoleError("The world account for world " + Terraria.Main.worldName + " is disabled.  Currency will not work for this game.");
                        }

                        //Assign the world account to the running world.
                        SEconomyPlugin.WorldAccount = new Economy.BankAccount(worldAccount);
                    }
                });

            } else {
                return Task.Factory.StartNew(() => { 
                    TShockAPI.Log.ConsoleError("SEconomy: EnsureBankAccountExists called but no world has been loaded yet.");
                });
            */
            }
        }