/// <summary> /// Gets the userID, password, and screenName by the username /// </summary> /// <param name="uname"></param> /// <returns>User object with the user data</returns> internal static User GetUserLoginInfo(string uname) { User userInfo = new User(); try { using (MySqlIO MySql = new MySqlIO()) { using (MySqlCommand cmd = MySql.CreateCommand()) { cmd.CommandText = @"select ID, Password, ScreenName from Users where Username = @uname;"; cmd.Parameters.AddWithValue("@uname", uname); MySqlDataReader rs = cmd.ExecuteReader(); if (rs.Read()) { userInfo.ID = rs["ID"].ThGetInt(); userInfo.Password = rs["Password"].ThGetTrimedString(); userInfo.ScreenName = rs["ScreenName"].ThGetTrimedString(); } } } } catch (Exception) { throw; } return(userInfo); }
/// <summary> /// Adds new user to the DB if the username does not exist /// </summary> /// <param name="uname"></param> /// <param name="pwd"></param> /// <param name="name"></param> /// <returns>if username already exists - returns -1, otherwise returns the new user ID</returns> internal static int AddNewUser(string uname, string pwd, string name) { int userID = 0; try { using (MySqlIO MySql = new MySqlIO()) { using (MySqlCommand cmd = MySql.CreateCommand()) { cmd.CommandText = "AddUser"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@uname", uname); cmd.Parameters["@uname"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@pwd", pwd.ThGetSHA256_hash()); cmd.Parameters["@pwd"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@sname", name); cmd.Parameters["@sname"].Direction = ParameterDirection.Input; var res = cmd.ExecuteScalar(); userID = res.ThGetInt(); } } } catch (Exception) { throw; } return(userID); }
/// <summary> /// Get all the records of game history for the player by userID /// </summary> /// <param name="userID"></param> /// <returns>DataTable of user games history</returns> internal static DataTable GetGamesHistory(int userID) { DataTable dt = new DataTable(); try { using (MySqlIO MySql = new MySqlIO()) { using (MySqlCommand cmd = MySql.CreateCommand()) { cmd.CommandText = @"select date_Format(Games_History.CreationTime, '%d/%m/%Y %H:%i') as CreationTime, Games.Name as GameName from Games_History inner join Games on Games.ID = Games_History.GameID where PlayerID = @userID order by Games_History.CreationTime desc;"; cmd.Parameters.AddWithValue("@userID", userID); using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd)) { dataAdapter.Fill(dt); } } } } catch (Exception) { throw; } return(dt); }
/// <summary> /// Get ID and Name of all Games /// </summary> /// <returns>DataTable with all games details</returns> internal static DataTable GetGames() { DataTable dt = new DataTable(); try { using (MySqlIO MySql = new MySqlIO()) { using (MySqlCommand cmd = MySql.CreateCommand()) { cmd.CommandText = @"select ID, Name from Games;"; using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd)) { dataAdapter.Fill(dt); } } } } catch (Exception) { throw; } return(dt); }
/// <summary> /// Add Record into the games_history table /// </summary> /// <param name="gameID"></param> /// <param name="playerID"></param> internal static void AddGameHistory(int gameID, int playerID) { try { using (MySqlIO MySql = new MySqlIO()) { using (MySqlCommand cmd = MySql.CreateCommand()) { cmd.CommandText = @"insert into games_history (GameID,PlayerID) Values (@gameId,@playerId);"; cmd.Parameters.AddWithValue("@gameId", gameID); cmd.Parameters.AddWithValue("@playerId", playerID); cmd.ExecuteNonQuery(); } } } catch (Exception) { throw; } }