Fix db connciton issues

This commit is contained in:
SilicaAndPina 2020-10-19 11:25:41 +13:00
parent 43035bbf05
commit 4b6762984f

View file

@ -6,12 +6,13 @@ namespace Horse_Isle_Server
{ {
class Database class Database
{ {
public static string ConnectionString = "";
public static MySqlConnection db;
public static void OpenDatabase() public static void OpenDatabase()
{ {
db = new MySqlConnection("server=" + ConfigReader.DatabaseIP + ";user=" + ConfigReader.DatabaseUsername + ";password=" + ConfigReader.DatabasePassword+";database="+ConfigReader.DatabaseName); ConnectionString = "server=" + ConfigReader.DatabaseIP + ";user=" + ConfigReader.DatabaseUsername + ";password=" + ConfigReader.DatabasePassword + ";database=" + ConfigReader.DatabaseName;
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{
db.Open(); db.Open();
string UserTable = "CREATE TABLE Users(Id INT, Username TEXT(16),Email TEXT(128),Country TEXT(128),SecurityQuestion Text(128),SecurityAnswerHash TEXT(128),Age INT,PassHash TEXT(128), Salt TEXT(128),Gender TEXT(16), Admin TEXT(3), Moderator TEXT(3))"; string UserTable = "CREATE TABLE Users(Id INT, Username TEXT(16),Email TEXT(128),Country TEXT(128),SecurityQuestion Text(128),SecurityAnswerHash TEXT(128),Age INT,PassHash TEXT(128), Salt TEXT(128),Gender TEXT(16), Admin TEXT(3), Moderator TEXT(3))";
string ExtTable = "CREATE TABLE UserExt(Id INT, X INT, Y INT, Money INT, BankBalance BIGINT,ProfilePage Text(1028), CharId INT, ChatViolations INT)"; string ExtTable = "CREATE TABLE UserExt(Id INT, X INT, Y INT, Money INT, BankBalance BIGINT,ProfilePage Text(1028), CharId INT, ChatViolations INT)";
@ -26,8 +27,10 @@ namespace Horse_Isle_Server
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = UserTable; sqlCommand.CommandText = UserTable;
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
catch (Exception e) { catch (Exception e)
{
Logger.WarnPrint(e.Message); Logger.WarnPrint(e.Message);
}; };
@ -36,6 +39,7 @@ namespace Horse_Isle_Server
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = ExtTable; sqlCommand.CommandText = ExtTable;
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
catch (Exception e) catch (Exception e)
{ {
@ -48,6 +52,7 @@ namespace Horse_Isle_Server
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = MailTable; sqlCommand.CommandText = MailTable;
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
catch (Exception e) catch (Exception e)
{ {
@ -60,6 +65,7 @@ namespace Horse_Isle_Server
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = BuddyTable; sqlCommand.CommandText = BuddyTable;
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
catch (Exception e) catch (Exception e)
{ {
@ -72,6 +78,7 @@ namespace Horse_Isle_Server
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = DroppedTable; sqlCommand.CommandText = DroppedTable;
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
catch (Exception e) catch (Exception e)
{ {
@ -93,6 +100,7 @@ namespace Horse_Isle_Server
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
catch (Exception e) catch (Exception e)
{ {
@ -100,8 +108,12 @@ namespace Horse_Isle_Server
}; };
} }
}
public static void SetServerTime(int time, int day, int year) public static void SetServerTime(int time, int day, int year)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "UPDATE World SET Time=@time,Day=@day,Year=@year"; sqlCommand.CommandText = "UPDATE World SET Time=@time,Day=@day,Year=@year";
@ -110,50 +122,73 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@year", year); sqlCommand.Parameters.AddWithValue("@year", year);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
} }
public static int GetServerTime() public static int GetServerTime()
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT Time FROM World"; sqlCommand.CommandText = "SELECT Time FROM World";
int serverTime = Convert.ToInt32(sqlCommand.ExecuteScalar()); int serverTime = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return serverTime; return serverTime;
} }
}
public static int GetServerDay() public static int GetServerDay()
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT Day FROM World"; sqlCommand.CommandText = "SELECT Day FROM World";
int serverTime = Convert.ToInt32(sqlCommand.ExecuteScalar()); int serverTime = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return serverTime; return serverTime;
} }
}
public static int GetServerYear() public static int GetServerYear()
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT Year FROM World"; sqlCommand.CommandText = "SELECT Year FROM World";
int creationTime = Convert.ToInt32(sqlCommand.ExecuteScalar()); int creationTime = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return creationTime; return creationTime;
} }
}
public static string GetWorldWeather() public static string GetWorldWeather()
{ {
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT Weather FROM World"; sqlCommand.CommandText = "SELECT Weather FROM World";
string Weather = sqlCommand.ExecuteScalar().ToString(); string Weather = sqlCommand.ExecuteScalar().ToString();
sqlCommand.Dispose();
return Weather; return Weather;
} }
}
public static void SetWorldWeather(string Weather) public static void SetWorldWeather(string Weather)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "UPDATE World SET Weather=@weather"; sqlCommand.CommandText = "UPDATE World SET Weather=@weather";
sqlCommand.Parameters.AddWithValue("@weather", Weather); sqlCommand.Parameters.AddWithValue("@weather", Weather);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
} }
public static byte[] GetPasswordSalt(string username) public static byte[] GetPasswordSalt(string username)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(username)) if (CheckUserExist(username))
{ {
@ -162,6 +197,7 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@name", username); sqlCommand.Parameters.AddWithValue("@name", username);
sqlCommand.Prepare(); sqlCommand.Prepare();
string expectedHash = sqlCommand.ExecuteScalar().ToString(); string expectedHash = sqlCommand.ExecuteScalar().ToString();
sqlCommand.Dispose();
return Converters.StringToByteArray(expectedHash); return Converters.StringToByteArray(expectedHash);
} }
else else
@ -169,19 +205,26 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Username " + username + " not found in database."); throw new KeyNotFoundException("Username " + username + " not found in database.");
} }
} }
}
public static int CheckMailcount(int id) public static int CheckMailcount(int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT COUNT(1) FROM Mailbox WHERE IdTo=@id"; sqlCommand.CommandText = "SELECT COUNT(1) FROM Mailbox WHERE IdTo=@id";
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar()); Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return count; return count;
} }
}
public static void AddMail(int toId, string fromName, string subject, string message) public static void AddMail(int toId, string fromName, string subject, string message)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
@ -194,11 +237,14 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@time", epoch); sqlCommand.Parameters.AddWithValue("@time", epoch);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
} }
public static bool CheckUserExist(int id) public static bool CheckUserExist(int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT COUNT(1) FROM Users WHERE Id=@id"; sqlCommand.CommandText = "SELECT COUNT(1) FROM Users WHERE Id=@id";
@ -206,9 +252,14 @@ namespace Horse_Isle_Server
sqlCommand.Prepare(); sqlCommand.Prepare();
Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar()); Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return count >= 1; return count >= 1;
} }
}
public static bool CheckUserExist(string username) public static bool CheckUserExist(string username)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT COUNT(1) FROM Users WHERE Username=@name"; sqlCommand.CommandText = "SELECT COUNT(1) FROM Users WHERE Username=@name";
@ -216,9 +267,14 @@ namespace Horse_Isle_Server
sqlCommand.Prepare(); sqlCommand.Prepare();
Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar()); Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return count >= 1; return count >= 1;
} }
}
public static bool CheckUserExtExists(int id) public static bool CheckUserExtExists(int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT COUNT(1) FROM UserExt WHERE Id=@id"; sqlCommand.CommandText = "SELECT COUNT(1) FROM UserExt WHERE Id=@id";
@ -226,11 +282,16 @@ namespace Horse_Isle_Server
sqlCommand.Prepare(); sqlCommand.Prepare();
Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar()); Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return count >= 1; return count >= 1;
} }
}
public static bool CheckUserIsModerator(string username) public static bool CheckUserIsModerator(string username)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(username)) if (CheckUserExist(username))
{ {
@ -239,6 +300,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@name", username); sqlCommand.Parameters.AddWithValue("@name", username);
sqlCommand.Prepare(); sqlCommand.Prepare();
string modStr = sqlCommand.ExecuteScalar().ToString(); string modStr = sqlCommand.ExecuteScalar().ToString();
sqlCommand.Dispose();
return modStr == "YES"; return modStr == "YES";
} }
else else
@ -246,9 +309,12 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Username " + username + " not found in database."); throw new KeyNotFoundException("Username " + username + " not found in database.");
} }
} }
}
public static bool CheckUserIsAdmin(string username) public static bool CheckUserIsAdmin(string username)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(username)) if (CheckUserExist(username))
{ {
@ -257,6 +323,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@name", username); sqlCommand.Parameters.AddWithValue("@name", username);
sqlCommand.Prepare(); sqlCommand.Prepare();
string adminStr = sqlCommand.ExecuteScalar().ToString(); string adminStr = sqlCommand.ExecuteScalar().ToString();
sqlCommand.Dispose();
return adminStr == "YES"; return adminStr == "YES";
} }
else else
@ -264,8 +332,11 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Username " + username + " not found in database."); throw new KeyNotFoundException("Username " + username + " not found in database.");
} }
} }
}
public static int GetBuddyCount(int id) public static int GetBuddyCount(int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT COUNT(1) FROM BuddyList WHERE Id=@id OR IdFriend=@id AND Pending=false"; sqlCommand.CommandText = "SELECT COUNT(1) FROM BuddyList WHERE Id=@id OR IdFriend=@id AND Pending=false";
@ -273,10 +344,15 @@ namespace Horse_Isle_Server
sqlCommand.Prepare(); sqlCommand.Prepare();
Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar()); Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return count; return count;
} }
}
public static int[] GetBuddyList(int id) public static int[] GetBuddyList(int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (GetBuddyCount(id) <= 0) if (GetBuddyCount(id) <= 0)
return new int[0]; // user is forever alone. return new int[0]; // user is forever alone.
@ -289,7 +365,7 @@ namespace Horse_Isle_Server
sqlCommand.Prepare(); sqlCommand.Prepare();
MySqlDataReader dataReader = sqlCommand.ExecuteReader(); MySqlDataReader dataReader = sqlCommand.ExecuteReader();
while(dataReader.Read()) while (dataReader.Read())
{ {
int adder = dataReader.GetInt32(0); int adder = dataReader.GetInt32(0);
int friend = dataReader.GetInt32(1); int friend = dataReader.GetInt32(1);
@ -299,10 +375,14 @@ namespace Horse_Isle_Server
buddyList.Add(adder); buddyList.Add(adder);
} }
sqlCommand.Dispose();
return buddyList.ToArray(); return buddyList.ToArray();
} }
}
public static bool IsPendingBuddyRequestExist(int id, int friendId) public static bool IsPendingBuddyRequestExist(int id, int friendId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT COUNT(1) FROM BuddyList WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id) AND Pending=true"; sqlCommand.CommandText = "SELECT COUNT(1) FROM BuddyList WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id) AND Pending=true";
@ -311,10 +391,14 @@ namespace Horse_Isle_Server
sqlCommand.Prepare(); sqlCommand.Prepare();
Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar()); Int32 count = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return count >= 1; return count >= 1;
} }
}
public static void RemoveBuddy(int id, int friendId) public static void RemoveBuddy(int id, int friendId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "DELETE FROM BuddyList WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id)"; sqlCommand.CommandText = "DELETE FROM BuddyList WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id)";
@ -322,8 +406,12 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@friendId", friendId); sqlCommand.Parameters.AddWithValue("@friendId", friendId);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
} }
public static void AcceptBuddyRequest(int id, int friendId) public static void AcceptBuddyRequest(int id, int friendId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "UPDATE BuddyList SET Pending=false WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id)"; sqlCommand.CommandText = "UPDATE BuddyList SET Pending=false WHERE (Id=@id AND IdFriend=@friendId) OR (Id=@friendid AND IdFriend=@Id)";
@ -331,8 +419,12 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@friendId", friendId); sqlCommand.Parameters.AddWithValue("@friendId", friendId);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
} }
public static void AddPendingBuddyRequest(int id, int friendId) public static void AddPendingBuddyRequest(int id, int friendId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "INSERT INTO BuddyList VALUES(@id,@friendId,true)"; sqlCommand.CommandText = "INSERT INTO BuddyList VALUES(@id,@friendId,true)";
@ -340,8 +432,13 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@friendId", friendId); sqlCommand.Parameters.AddWithValue("@friendId", friendId);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
} }
public static void CreateUserExt(int id) public static void CreateUserExt(int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExtExists(id)) // user allready exists! if (CheckUserExtExists(id)) // user allready exists!
throw new Exception("Userid " + id + " Allready in userext."); throw new Exception("Userid " + id + " Allready in userext.");
@ -353,9 +450,14 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@y", Map.NewUserStartY); sqlCommand.Parameters.AddWithValue("@y", Map.NewUserStartY);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
} }
public static int GetUserid(string username) public static int GetUserid(string username)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(username)) if (CheckUserExist(username))
{ {
@ -364,6 +466,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@name", username); sqlCommand.Parameters.AddWithValue("@name", username);
sqlCommand.Prepare(); sqlCommand.Prepare();
int userId = Convert.ToInt32(sqlCommand.ExecuteScalar()); int userId = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return userId; return userId;
} }
else else
@ -371,8 +475,11 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Username " + username + " not found in database."); throw new KeyNotFoundException("Username " + username + " not found in database.");
} }
} }
}
public static int GetPlayerCharId(int userId) public static int GetPlayerCharId(int userId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExtExists(userId)) if (CheckUserExtExists(userId))
{ {
@ -381,6 +488,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", userId); sqlCommand.Parameters.AddWithValue("@id", userId);
sqlCommand.Prepare(); sqlCommand.Prepare();
int CharId = Convert.ToInt32(sqlCommand.ExecuteScalar()); int CharId = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return CharId; return CharId;
} }
else else
@ -388,8 +497,11 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + userId + " not found in database."); throw new KeyNotFoundException("Id " + userId + " not found in database.");
} }
} }
}
public static void SetPlayerCharId(int charid, int id) public static void SetPlayerCharId(int charid, int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -399,14 +511,19 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
else else
{ {
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static int GetPlayerX(int userId) public static int GetPlayerX(int userId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExtExists(userId)) if (CheckUserExtExists(userId))
{ {
@ -415,6 +532,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", userId); sqlCommand.Parameters.AddWithValue("@id", userId);
sqlCommand.Prepare(); sqlCommand.Prepare();
int X = Convert.ToInt32(sqlCommand.ExecuteScalar()); int X = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return X; return X;
} }
else else
@ -422,8 +541,11 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + userId + " not found in database."); throw new KeyNotFoundException("Id " + userId + " not found in database.");
} }
} }
}
public static void SetPlayerX(int x, int id) public static void SetPlayerX(int x, int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -433,14 +555,19 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
else else
{ {
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static int GetPlayerY(int userId) public static int GetPlayerY(int userId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExtExists(userId)) if (CheckUserExtExists(userId))
{ {
@ -449,6 +576,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", userId); sqlCommand.Parameters.AddWithValue("@id", userId);
sqlCommand.Prepare(); sqlCommand.Prepare();
int Y = Convert.ToInt32(sqlCommand.ExecuteScalar()); int Y = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return Y; return Y;
} }
else else
@ -456,8 +585,11 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + userId + " not found in database."); throw new KeyNotFoundException("Id " + userId + " not found in database.");
} }
} }
}
public static int GetChatViolations(int userId) public static int GetChatViolations(int userId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExtExists(userId)) if (CheckUserExtExists(userId))
{ {
@ -466,6 +598,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", userId); sqlCommand.Parameters.AddWithValue("@id", userId);
sqlCommand.Prepare(); sqlCommand.Prepare();
int violations = Convert.ToInt32(sqlCommand.ExecuteScalar()); int violations = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return violations; return violations;
} }
else else
@ -473,9 +607,12 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + userId + " not found in database."); throw new KeyNotFoundException("Id " + userId + " not found in database.");
} }
} }
}
public static void SetChatViolations(int violations, int id) public static void SetChatViolations(int violations, int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -485,13 +622,18 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
else else
{ {
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static void SetPlayerY(int y, int id) public static void SetPlayerY(int y, int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -501,14 +643,19 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
else else
{ {
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static void SetPlayerMoney(int money, int id) public static void SetPlayerMoney(int money, int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -518,13 +665,18 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
else else
{ {
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static int GetPlayerMoney(int userId) public static int GetPlayerMoney(int userId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExtExists(userId)) if (CheckUserExtExists(userId))
{ {
@ -533,6 +685,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", userId); sqlCommand.Parameters.AddWithValue("@id", userId);
sqlCommand.Prepare(); sqlCommand.Prepare();
int Money = Convert.ToInt32(sqlCommand.ExecuteScalar()); int Money = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return Money; return Money;
} }
else else
@ -540,8 +694,11 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + userId + " not found in database."); throw new KeyNotFoundException("Id " + userId + " not found in database.");
} }
} }
}
public static int GetPlayerBankMoney(int userId) public static int GetPlayerBankMoney(int userId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExtExists(userId)) if (CheckUserExtExists(userId))
{ {
@ -550,6 +707,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", userId); sqlCommand.Parameters.AddWithValue("@id", userId);
sqlCommand.Prepare(); sqlCommand.Prepare();
int BankMoney = Convert.ToInt32(sqlCommand.ExecuteScalar()); int BankMoney = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return BankMoney; return BankMoney;
} }
else else
@ -557,8 +716,11 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + userId + " not found in database."); throw new KeyNotFoundException("Id " + userId + " not found in database.");
} }
} }
}
public static void SetPlayerBankMoney(int bankMoney, int id) public static void SetPlayerBankMoney(int bankMoney, int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -568,14 +730,19 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
else else
{ {
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static void SetPlayerProfile(string profilePage, int id) public static void SetPlayerProfile(string profilePage, int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -585,14 +752,19 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery(); sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
} }
else else
{ {
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static string GetPlayerProfile(int id) public static string GetPlayerProfile(int id)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
if (CheckUserExist(id)) if (CheckUserExist(id))
{ {
@ -601,6 +773,8 @@ namespace Horse_Isle_Server
sqlCommand.Parameters.AddWithValue("@id", id); sqlCommand.Parameters.AddWithValue("@id", id);
sqlCommand.Prepare(); sqlCommand.Prepare();
string profilePage = sqlCommand.ExecuteScalar().ToString(); string profilePage = sqlCommand.ExecuteScalar().ToString();
sqlCommand.Dispose();
return profilePage; return profilePage;
} }
else else
@ -608,17 +782,22 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + id + " not found in database."); throw new KeyNotFoundException("Id " + id + " not found in database.");
} }
} }
}
public static string GetUsername(int userId) public static string GetUsername(int userId)
{ {
if(CheckUserExist(userId)) using (MySqlConnection db = new MySqlConnection(ConnectionString))
{
if (CheckUserExist(userId))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT Username FROM Users WHERE Id=@id"; sqlCommand.CommandText = "SELECT Username FROM Users WHERE Id=@id";
sqlCommand.Parameters.AddWithValue("@id", userId); sqlCommand.Parameters.AddWithValue("@id", userId);
sqlCommand.Prepare(); sqlCommand.Prepare();
string username = sqlCommand.ExecuteScalar().ToString(); string username = sqlCommand.ExecuteScalar().ToString();
sqlCommand.Dispose();
return username; return username;
} }
else else
@ -626,15 +805,20 @@ namespace Horse_Isle_Server
throw new KeyNotFoundException("Id " + userId + " not found in database."); throw new KeyNotFoundException("Id " + userId + " not found in database.");
} }
} }
}
public static byte[] GetPasswordHash(string username) public static byte[] GetPasswordHash(string username)
{ {
if(CheckUserExist(username)) using (MySqlConnection db = new MySqlConnection(ConnectionString))
{
if (CheckUserExist(username))
{ {
MySqlCommand sqlCommand = db.CreateCommand(); MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT PassHash FROM Users WHERE Username=@name"; sqlCommand.CommandText = "SELECT PassHash FROM Users WHERE Username=@name";
sqlCommand.Parameters.AddWithValue("@name", username); sqlCommand.Parameters.AddWithValue("@name", username);
sqlCommand.Prepare(); sqlCommand.Prepare();
string expectedHash = sqlCommand.ExecuteScalar().ToString(); string expectedHash = sqlCommand.ExecuteScalar().ToString();
sqlCommand.Dispose();
return Converters.StringToByteArray(expectedHash); return Converters.StringToByteArray(expectedHash);
} }
else else
@ -643,5 +827,6 @@ namespace Horse_Isle_Server
} }
} }
} }
}
} }