/// <summary> /// Recreates all BankAccounts using the configured Database. /// </summary> public void Load() { Debug.Print("BankAccountManager.Load!"); CurrencyManager = new CurrencyManager(BankingPlugin.DataDirectory); playerAccountMaps.Clear(); if (!string.IsNullOrWhiteSpace(Config.Instance.ScriptPath)) { LoadScripts(Path.Combine(BankingPlugin.DataDirectory, Config.Instance.ScriptPath)); } var cfg = Config.Instance.Database; //Database = new SqliteDatabase(Config.Instance.Database.ConnectionString); //Database = DatabaseFactory.LoadOrCreateDatabase("mysql", "Server=localhost;Database=db_banking;Uid=xxx;Pwd=xxx;"); //Database = DatabaseFactory.LoadOrCreateDatabase("redis", "localhost:6379"); Database = DatabaseFactory.LoadOrCreateDatabase(cfg.DatabaseType, cfg.ConnectionString); var accounts = Database.Load(); foreach (var acc in accounts) { PlayerBankAccountMap playerAccounts = null; if (!playerAccountMaps.TryGetValue(acc.OwnerName, out playerAccounts)) { playerAccounts = new PlayerBankAccountMap(acc.OwnerName); playerAccountMaps.Add(acc.OwnerName, playerAccounts); } playerAccounts.Add(acc.Name, acc); } EnsureBankAccountsExist(TSPlayer.Server.Name); foreach (var player in TShock.Players) { if (player?.IsLoggedIn == true) // && player?.Active == true) { EnsureBankAccountsExist(player.Name); } } }
/// <summary> /// Ensures that a player has BankAccounts for each configured Currency. /// </summary> /// <param name="playerName"></param> internal void EnsureBankAccountsExist(string playerName) { if (!playerAccountMaps.TryGetValue(playerName, out var playerAccountMap)) { Debug.Print($"Creating bank account map for user {playerName}..."); playerAccountMap = new PlayerBankAccountMap(playerName); playerAccountMaps.Add(playerName, playerAccountMap); } var currencyNames = CurrencyManager.Select(v => v.InternalName); playerAccountMap.EnsureBankAccountNamesExist(currencyNames); //lets set/reset a default mapping from currency's to bank accounts for reward purposes. Anything subscribed to EnsuringPlayerAccounts event //will have a chance to change the mapping again. foreach (var name in currencyNames) { playerAccountMap.SetAccountNameOverride(name, name); } }