private void SyncDatabase(List <AuthConfig> authConfigs) { if (authConfigs.Count() == 0) { return; } // Add new accounts and update existing accounts. foreach (var authConfig in authConfigs) { if (string.IsNullOrEmpty(authConfig.Username) || string.IsNullOrEmpty(authConfig.Password)) { continue; } var existing = _context.Account.FirstOrDefault(x => x.Username == authConfig.Username && x.AuthType == authConfig.AuthType); if (existing == null) { try { Account newAcc = new Account(authConfig); _context.Account.Add(newAcc); _context.SaveChanges(); } catch (Exception) { Logic.Logging.Logger.Write($"Error while saving data into {ACCOUNT_DB_NAME}, please delete {ACCOUNT_DB_NAME} and restart bot to have it fully work in order"); } } else { // Update credentials in database using values from json. existing.Username = authConfig.Username; existing.Password = authConfig.Password; _context.SaveChanges(); } } // Remove accounts that are not in the auth.json but in the database. List <Account> accountsToRemove = new List <Account>(); foreach (var item in _context.Account) { var existing = authConfigs.FirstOrDefault(x => x.Username == item.Username && x.AuthType == item.AuthType); if (existing == null) { accountsToRemove.Add(item); } } foreach (var item in accountsToRemove) { _context.Account.Remove(item); } _context.SaveChanges(); }
private void LoadDataFromDB() { if (_context.Account.Count() > 0) { foreach (var item in _context.Account.OrderBy(p => p.Id)) { item.IsRunning = 0; UpdateLocalAccount(item); } _context.SaveChanges(); } }
private void MigrateLiteDbToSqLite() { // Delete all accounts _context.Account.RemoveRange(_context.Account); _context.SaveChanges(); using (var liteDb = new LiteDatabase(ACCOUNT_DB_NAME)) { var liteDbAccounts = liteDb.GetCollection <BotAccount>("accounts"); foreach (var liteDbAccount in liteDbAccounts.FindAll()) { if (string.IsNullOrEmpty(liteDbAccount.Username) || string.IsNullOrEmpty(liteDbAccount.Password)) { continue; } Account newAccount = new Account(); newAccount.AuthType = liteDbAccount.AuthType; newAccount.Username = liteDbAccount.Username; newAccount.Password = liteDbAccount.Password; newAccount.RuntimeTotal = liteDbAccount.RuntimeTotal; newAccount.LastRuntimeUpdatedAt = liteDbAccount.LastRuntimeUpdatedAt.ToUnixTime(); if (liteDbAccount.ReleaseBlockTime > DateTime.Now) { newAccount.ReleaseBlockTime = liteDbAccount.ReleaseBlockTime.ToUnixTime(); } newAccount.Nickname = liteDbAccount.Nickname; newAccount.LoggedTime = liteDbAccount.LoggedTime.ToUnixTime(); newAccount.Level = liteDbAccount.Level; newAccount.LastLogin = liteDbAccount.LastLogin; newAccount.LastLoginTimestamp = liteDbAccount.LastLoginTimestamp; newAccount.Stardust = liteDbAccount.Stardust; newAccount.CurrentXp = liteDbAccount.CurrentXp; newAccount.NextLevelXp = liteDbAccount.NextLevelXp; newAccount.PrevLevelXp = liteDbAccount.PrevLevelXp; _context.Account.Add(newAccount); _context.SaveChanges(); } } }