public static Account CreateAccount(string username, string nickname, int playerId, byte[] salt, byte[] hash) { using (DatabaseContainer dc = new DatabaseContainer()) { if (dc.Accounts.FirstOrDefault(a => a.Username == username) != null) { throw new ArgumentException($"Username: {username} is already taken"); } Player player = dc.Players.FirstOrDefault(p => p.Id == playerId) ?? CreatePlayer(nickname); Account account = new Account() { Username = username, Player = player, Salt = Encoding.ASCII.GetString(salt), Hash = Encoding.ASCII.GetString(hash) }; player.Account = account; dc.Accounts.Add(account); dc.SaveChanges(); return account; } }
public static void SaveChanges() { using (DatabaseContainer dc = new DatabaseContainer()) { dc.SaveChanges(); } }
public static Account CreateAccount(string username, string nickname, int playerId, byte[] salt, byte[] hash) { using (DatabaseContainer dc = new DatabaseContainer()) { if (dc.Accounts.FirstOrDefault(a => a.Username == username) != null) { throw new ArgumentException($"Username: {username} is already taken"); } Player player = dc.Players.FirstOrDefault(p => p.Id == playerId) ?? CreatePlayer(nickname); Account account = new Account() { Username = username, Player = player, Salt = Convert.ToBase64String(salt), Hash = Convert.ToBase64String(hash) }; player.Account = account; dc.Accounts.Add(account); dc.Entry(player).State = EntityState.Modified; // Avoid making duplicate players. dc.SaveChanges(); return account; } }
/// <summary> /// Adds gold to the player. /// </summary> /// <param name="observable"></param> /// <param name="data"></param> private void AddGold(Observable observable, object data) { Gold += 10; using (DatabaseContainer dc = new DatabaseContainer()) { dc.Entry(this).State = EntityState.Modified; dc.SaveChanges(); } }
public static Player CreatePlayer(string nickname) { using (DatabaseContainer dc = new DatabaseContainer()) { Player player = new Player() { Nickname = nickname }; // TODO: dynamic account. dc.Players.Add(player); dc.SaveChanges(); return player; } }
private void DestroyEvent(Observable observable, object data) { if (CurrentCharacter.HighScore > this.HighScore) { this.HighScore = CurrentCharacter.HighScore; } using (DatabaseContainer dc = new DatabaseContainer()) { dc.Entry(this).State = EntityState.Modified; dc.SaveChanges(); } }