Exemple #1
0
        /// <summary>
        /// Register a new player
        /// Returns null if it fails
        /// </summary>
        /// <param name="username">Username of the player</param>
        /// <param name="password">Password of the player</param>
        /// <returns>The player or null depending on how it's result</returns>
        public static KeyValuePair <DBProfile_Login, string> RegisterPlayer(string username, string password)
        {
            string hash = DBPasswordHash.GetHashString(password);

            if (!PlayerExist(username))
            {
                string sql = String.Format("INSERT INTO [dbo].[{0}] (Username,Password) VALUES('{1}', '{2}'); "
                                           , tableName
                                           , username
                                           , hash);

                var sqlResult = DBEndPoint.ExecuteSQL(sql);
                if (sqlResult.Key)
                {
                    var player = GetPlayer(username, hash);
                    RegisterPlayerInOtherTables(player.id);
                    return(new KeyValuePair <DBProfile_Login, string>(player, "Succesfully generated"));
                }
                else
                {
                    return(new KeyValuePair <DBProfile_Login, string>(null, "Internal issue"));
                }
            }
            else
            {
                return(new KeyValuePair <DBProfile_Login, string>(null, "A player with that name already exist"));
            }
        }
        /// <summary>
        /// Generates and executes the sql to increment a certain row and updating kills and deaths
        /// </summary>
        /// <param name="id">ID of the player, gotten from the LoginTable</param>
        /// <param name="rowNameToIncrement"></param>
        /// <param name="kills"></param>
        /// <param name="deaths"></param>
        static void Increment(int id, string rowNameToIncrement, int kills, int deaths)
        {
            string sql = String.Format("UPDATE {0} SET Losses = Losses + 1, Kills = Kills + {1}, Deaths = Deaths + {2} WHERE Id = {3}",
                                       tableName,
                                       kills,
                                       deaths,
                                       id);

            DBEndPoint.ExecuteSQL(sql);
        }
        /// <summary>
        /// Creates an row for a new player on the PlayerAchievements table, with the id matching the id of LoginTable
        /// </summary>
        /// <param name="id">Id of the player on LoginTable</param>
        public static void RegisterNewPlayer(int id)
        {
            string sql = String.Format("INSERT INTO [dbo].[{0}] (Id,Wins,Losses,Kills,Deaths) VALUES({1},0,0,0,0); "
                                       , tableName
                                       , id);

            var sqlResult = DBEndPoint.ExecuteSQL(sql);

            if (!sqlResult.Key)
            {
                throw new Exception("Register player failed in DBPlayerAchievements with msg " + sqlResult.Value);
            }
        }