public bool SellEmote(EmoteInfo aEmoteInfo, float aAmount, string aUsername) { //Update the user amount for that emote int ID = GetUserID(aUsername); string queryEmoteCost = "SELECT CurrentValue FROM emoteinfo WHERE EmoteName = '" + aEmoteInfo.GetName() + "'"; float emoteCost = -1; string queryCheckIfEmoteInDB = "SELECT COUNT(EmoteName) FROM emoteinfo WHERE EmoteName = '" + aEmoteInfo.GetName() + "'"; MySqlCommand cmdCost = new MySqlCommand(queryEmoteCost, m_Connection); MySqlCommand cmdCheck = new MySqlCommand(queryCheckIfEmoteInDB, m_Connection); if (m_Connection.Ping() == false) { m_Connection.Open(); } if (Convert.ToInt32(cmdCheck.ExecuteScalar()) == 0) { Console.WriteLine("Tried looking for an emote, but it does not exist in the database"); return(false); } emoteCost = Convert.ToSingle(cmdCost.ExecuteScalar()); float userMoney = GetMoney(aUsername); //Check if user is even in the database for moneywise if (userMoney == -1) { return(false); } float totalSellValue; totalSellValue = aAmount * aEmoteInfo.GetValue(); //Check for error if (totalSellValue <= 0) { Console.WriteLine("Error: sell value is less than 0, something went wrong"); Console.WriteLine("Emote Value: " + aEmoteInfo.GetValue()); return(false); } //Check if they have enough in the database string queryCheckSellAmount = "SELECT Amount FROM emotecount WHERE EmoteName = '" + aEmoteInfo.GetName() + "' AND DatabaseUserID = " + ID; MySqlCommand cmdGetAmountOfEmote = new MySqlCommand(queryCheckSellAmount, m_Connection); if (m_Connection.Ping() == false) { m_Connection.Open(); } float emoteAmount = Convert.ToSingle(cmdGetAmountOfEmote.ExecuteScalar()); if (emoteAmount < aAmount) { Console.WriteLine("Not enough emotes to sell."); this.CloseConnection(); return(false); } //Update the amount of that emote that was bought aEmoteInfo.SetAmountSold(aEmoteInfo.GetAmountSold() + aAmount); userMoney += totalSellValue; SetMoney(aUsername, userMoney); //Effect market value float valueDecrease; valueDecrease = aEmoteInfo.GetValue() / totalSellValue; aEmoteInfo.SetValue(aEmoteInfo.GetValue() - valueDecrease); if (m_Connection.Ping() == false) { m_Connection.Open(); } string queryUpdateUserAmount = "UPDATE emotecount SET Amount = Amount - " + aAmount + " WHERE EmoteName = '" + aEmoteInfo.GetName() + "' AND DatabaseUserID = " + ID; MySqlCommand cmdUpdateUserEmoteData = new MySqlCommand(queryUpdateUserAmount, m_Connection); cmdUpdateUserEmoteData.ExecuteNonQuery(); this.CloseConnection(); return(true); }
public bool BuyEmote(EmoteInfo aEmoteInfo, float aAmount, string aUsername) { string queryEmoteCost = "SELECT CurrentValue FROM emoteinfo WHERE EmoteName = '" + aEmoteInfo.GetName() + "'"; float emoteCost = -1; string queryCheckIfEmoteInDB = "SELECT COUNT(EmoteName) FROM emoteinfo WHERE EmoteName = '" + aEmoteInfo.GetName() + "'"; MySqlCommand cmdCost = new MySqlCommand(queryEmoteCost, m_Connection); MySqlCommand cmdCheck = new MySqlCommand(queryCheckIfEmoteInDB, m_Connection); if (m_Connection.Ping() == false) { m_Connection.Open(); } if (Convert.ToInt32(cmdCheck.ExecuteScalar()) == 0) { Console.WriteLine("Tried looking for an emote, but it does not exist in the database"); return(false); } emoteCost = Convert.ToSingle(cmdCost.ExecuteScalar()); float userMoney = GetMoney(aUsername); //Check if user is even in the database for moneywise if (userMoney == -1) { return(false); } float totalCost; totalCost = aAmount * aEmoteInfo.GetValue(); //Check for error if (totalCost <= 0) { Console.WriteLine("Error: total cost is less than 0, something went wrong"); Console.WriteLine("Emote Value: " + aEmoteInfo.GetValue()); return(false); } //Check if user has enough money to buy that amount if (totalCost > userMoney) { Console.WriteLine("User does not have enough money to buy the amount they want"); Console.WriteLine("Total Cost: " + totalCost + " UserMoney: " + userMoney); return(false); } //Update the amount of that emote that was bought aEmoteInfo.SetAmountBought(aEmoteInfo.GetAmountBought() + aAmount); userMoney -= totalCost; userMoney *= -1; SetMoney(aUsername, userMoney); //Effect market value float valueIncrease; valueIncrease = aEmoteInfo.GetValue() / totalCost; aEmoteInfo.SetValue(aEmoteInfo.GetValue() + valueIncrease); //Update the user amount for that emote int ID = GetUserID(aUsername); //Make a query to check if we need to put that emote into the db for that user string queryCheckIfInEmoteCount = "SELECT COUNT(*) FROM emotecount WHERE EmoteName = '" + aEmoteInfo.GetName() + "' AND DatabaseUserID = " + ID; MySqlCommand cmdCheckEmoteCount = new MySqlCommand(queryCheckIfInEmoteCount, m_Connection); if (m_Connection.Ping() == false) { m_Connection.Open(); } if (Convert.ToInt32(cmdCheckEmoteCount.ExecuteScalar()) == 0) { /* * string queryUpdateEmoteCount = "INSERT INTO emotecount VALUES ('" + aEmoteInfo.GetName() + "', " + ID + ", 0)"; * MySqlCommand cmdUpdateEmoteCount = new MySqlCommand(queryUpdateEmoteCount, m_Connection); * * cmdUpdateEmoteCount.ExecuteNonQuery(); */ this.AddToEmoteCount(aUsername, aEmoteInfo.GetName()); } if (m_Connection.Ping() == false) { m_Connection.Open(); } string updateUserAmount = "UPDATE emotecount SET Amount = Amount + " + aAmount + " WHERE EmoteName = '" + aEmoteInfo.GetName() + "' AND DatabaseUserID = " + ID; MySqlCommand cmdUpdateUserEmoteData = new MySqlCommand(updateUserAmount, m_Connection); cmdUpdateUserEmoteData.ExecuteNonQuery(); this.CloseConnection(); return(true); }