Exemplo n.º 1
0
        /// <summary> Adds a new PlayerInfo entry for a player who has never been online, by name. </summary>
        /// <returns> A newly-created PlayerInfo entry. </returns>
        public PlayerInfo AddUnrecognizedPlayer(string name, Rank startingRank, RankChangeType rankChangeType)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (startingRank == null)
            {
                throw new ArgumentNullException("startingRank");
            }
            using (PlayerDB.GetWriteLock()) {
                using (MySqlTransaction transaction = connection.BeginTransaction()) {
                    preInsertCommand.Transaction = transaction;
                    preInsertCommand.ExecuteNonQuery();
                    int id = (int)preInsertCommand.InsertId;

                    PlayerInfo info = new PlayerInfo(id, name, startingRank, rankChangeType, false);

                    MySqlCommand updateCmd = GetUpdateCommand(info);
                    updateCmd.Transaction = transaction;
                    updateCmd.ExecuteNonQuery();

                    transaction.Commit();

                    preInsertCommand.Transaction = null;
                    updateCmd.Transaction        = null;
                    return(info);
                }
            }
        }
Exemplo n.º 2
0
 /// <summary> Inserts all data from given playerInfo directly into the database. </summary>
 /// <param name="playerInfo"> Player record to import. </param>
 public void Import(PlayerInfo playerInfo)
 {
     if (playerInfo == null)
     {
         throw new ArgumentNullException("playerInfo");
     }
     using (PlayerDB.GetWriteLock()) {
         GetImportCommand(playerInfo).ExecuteNonQuery();
     }
 }
Exemplo n.º 3
0
 /// <summary> Removes a PlayerInfo entry from the database. </summary>
 /// <returns> True if the entry is successfully found and removed; otherwise false. </returns>
 public bool Remove(PlayerInfo playerInfo)
 {
     if (playerInfo == null)
     {
         throw new ArgumentNullException("playerInfo");
     }
     using (PlayerDB.GetWriteLock()) {
         MySqlCommand cmd          = GetDeleteCommand(playerInfo.ID);
         int          rowsAffected = cmd.ExecuteNonQuery();
         return(rowsAffected > 0);
     }
 }
Exemplo n.º 4
0
 /// <summary> Inserts all data from given PlayerInfo list directly into the database. </summary>
 /// <param name="playerInfos"> List of player record to import. </param>
 public void Import(IEnumerable <PlayerInfo> playerInfos)
 {
     if (playerInfos == null)
     {
         throw new ArgumentNullException("playerInfos");
     }
     using (PlayerDB.GetWriteLock()) {
         using (MySqlTransaction transaction = connection.BeginTransaction()) {
             importCommand.Transaction = transaction;
             foreach (PlayerInfo info in playerInfos)
             {
                 GetImportCommand(info).ExecuteNonQuery();
             }
             transaction.Commit();
             importCommand.Transaction = null;
         }
     }
 }
Exemplo n.º 5
0
 /// <summary> Saves the whole database. </summary>
 public void Save()
 {
     using (PlayerDB.GetWriteLock()) {
         var playersToUpdate = PlayerDB.List.Where(p => p.Changed);
         using (MySqlTransaction transaction = connection.BeginTransaction()) {
             MySqlCommand cmd = null;
             foreach (PlayerInfo info in playersToUpdate)
             {
                 lock (info.SyncRoot) {
                     info.Changed = false;
                     cmd          = GetUpdateCommand(info);
                 }
                 cmd.Transaction = transaction;
                 cmd.ExecuteNonQuery();
             }
             if (cmd != null)
             {
                 transaction.Commit();
                 cmd.Transaction = null;
             }
         }
     }
 }