Ragemp-roleplay-skript/Database/PlayerDatabase.cs

436 lines
17 KiB
C#

using System;
using MySql.Data.MySqlClient;
using GrandTheftMultiplayer.Server;
using GrandTheftMultiplayer.Shared;
using GrandTheftMultiplayer.Server.API;
using GrandTheftMultiplayer.Shared.Math;
using GrandTheftMultiplayer.Server.Elements;
using GrandTheftMultiplayer.Server.Constant;
using GrandTheftMultiplayer.Server.Managers;
namespace Roleplay
{
class PlayerDatabase
{
//Initialise conString, conn, cmd, reader
public static string myConnectionString = "SERVER=185.223.28.49;" + "DATABASE=gtanetwork;" + "UID=fivem;" + "PASSWORD=" + Main.databasepass + ";";
public static MySqlConnection connection;
public static MySqlCommand command;
public static MySqlDataReader reader;
public static class PlayerDatabaseFlags
{
public static string fraktion = "fraktion";
public static string skinid = "skinid";
public static string money = "money";
public static string moneyhand = "moneyhand";
public static string fraktionrang = "fraktionrang";
public static string perso = "personalausweis";
public static string handynummer = "handynummer";
public static string adminlevel = "adminlevel";
public static string autoschein = "autoschein";
public static string flugzeugschein = "flugzeugschein";
public static string bootschein = "bootschein";
public static string job = "job";
public static string joblevel = "joblevel";
public static string level = "level";
}
//SERVER SETTINGS
public static int startgeld = 25000;
//Check if the player is existiting
public static Boolean playerExists(Client player)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname = @nickname";
command.Parameters.AddWithValue("@nickname", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
string name = reader.GetString("nickname");
if(name == player.name)
{
//NAME equals the PLAYERNAME PLAYER is existing
connection.Close();
return true;
}
}
connection.Close();
return false;
}
public static bool isPlayerWhitelisted(Client player)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM whitelist WHERE socialclub = '" + player.socialClubName + "'";
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
string name = reader.GetString("socialclub");
if (name == player.socialClubName)
{
connection.Close();
return true;
}
}
connection.Close();
return false;
}
//inputs the player into the database!
public static void createPlayer(Client player, string pass,string skin)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "INSERT INTO `userdata`(`socialclub`, `password`, `nickname`, `skinid`,`moneyhand`, `money`, `fraktion`, `fraktionrang`, `personalausweis`, `spawnx`, `spawny`, `spawnz`, `handynummer`, `adminlevel`, `autoschein`, `flugzeugschein`, `bootschein`, `lastlogin`) VALUES (@socialclub,@password,@nickname,@skinid,@moneyhand,@money,0,0,0,@spawnx,@spawny,@spawnz,@handynummer,0,0,0,0,0)";
command.Parameters.AddWithValue("@socialclub", player.socialClubName);
command.Parameters.AddWithValue("@password", RAPI.MainAPI.CalculateMD5Hash(pass)); // SHA256 UPDATED
command.Parameters.AddWithValue("@nickname", player.name);
command.Parameters.AddWithValue("@skinid", skin);
command.Parameters.AddWithValue("@money", startgeld);
command.Parameters.AddWithValue("@moneyhand", 250);
command.Parameters.AddWithValue("@spawnx", Main.spawnposition.X);
command.Parameters.AddWithValue("@spawny", Main.spawnposition.Y);
command.Parameters.AddWithValue("@spawnz", Main.spawnposition.Z);
command.Parameters.AddWithValue("@handynummer", 0);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
API.shared.sleep(1000*5);
Main.changePWtoSHA256(player,pass);
}
//get the password of a player!
public static Boolean checkPassword(Client player, string pass)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
command.Parameters.AddWithValue("@password", RAPI.MainAPI.CalculateMD5Hash(pass));
connection.Open();
reader = command.ExecuteReader();
string salt = "";
while(reader.Read())
{
salt = reader.GetString("salt");
if(salt != "")
{
string hashedpass = API.shared.getHashSHA256(salt + pass);
if(hashedpass == reader.GetString("password")){
connection.Close();
return true;
}else
{
API.shared.consoleOutput("Player Failed Login! + " + player.socialClubName);
}
}
else
{
if (RAPI.MainAPI.CalculateMD5Hash(pass) == reader.GetString("password"))
{
//password is right
connection.Close();
return true;
}
else
{
API.shared.consoleOutput("Player Failed Login! + " + player.socialClubName);
}
}
}
connection.Close();
return false;
}
[Command("md5")]
public void genmd5(Client player, string pass)
{
player.sendChatMessage(RAPI.MainAPI.CalculateMD5Hash(pass));
}
//get the Skin of the player
public static string getSkin(Client player)
{
string skin = "";
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while(reader.Read())
{
skin = reader.GetString("skinid");
}
connection.Close();
return skin; //
}
//get the money of the player
public static int getMoney(Client player)
{
int money = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
money = reader.GetInt32("money");
}
connection.Close();
return money;
}
public static int getMoneyHand(Client player)
{
int money = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
money = reader.GetInt32("moneyhand");
}
connection.Close();
return money;
}
public static void setMoney(Client player, int money)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE userdata SET `money` = " + money + " WHERE `nickname` = @username;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public static void setMoneyHand(Client player, int moneyhand)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE userdata SET `moneyhand` = " + moneyhand + " WHERE `nickname` = @username;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public static Vector3 getSpawnPosition(Client player)
{
Vector3 vector = null;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
vector = new Vector3(reader.GetInt32("spawnx"), reader.GetInt32("spawny"), reader.GetInt32("spawnz"));
}
connection.Close();
return vector;
}
public static void setSpawnPosition(Client player)
{
Vector3 position = player.position;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE `userdata` SET `spawnx` = " + Convert.ToInt16(position.X) + ", `spawnz` = " + Convert.ToInt16(position.Z) + ", `spawny` = " + Convert.ToInt16(position.Y) + " WHERE nickname = '" + player.name + "'";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public static int getHandyNummer(Client player)
{
int handynummer = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
handynummer = reader.GetInt32("handynummer");
}
connection.Close();
return handynummer;
}
public static int getAdminLevel(Client player)
{
int AdminLevel = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
AdminLevel = reader.GetInt32("adminlevel");
}
connection.Close();
return AdminLevel;
}
public static Boolean hasSchein(Client player, String schein)
{
Boolean hasschein = false;
int dbschein = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
dbschein = reader.GetInt32(schein);
if (dbschein == 1)
{
hasschein = true;
}
else
{
hasschein = false;
}
}
connection.Close();
return hasschein;
}
public static void setDatabaseData(Client player, string flag, int value)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE `userdata` SET " + flag + " = " + value + " WHERE nickname = '" + player.name + "'";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public static void setDataBaseObject(Client player, string flag, object notstringvalue){
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE `userdata` SET " + flag + " = " + notstringvalue + " WHERE nickname = '" + player.name + "'";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public static void setDatabaseDataString(Client player, string flag, string value)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE `userdata` SET " + flag + " = '" + value + "' WHERE nickname = '" + player.name + "'";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
public static Boolean hasDatabaseData(Client player, string flag)
{
Boolean hasvalue = false;
int value = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
value = reader.GetInt32(flag);
if (value >= 1)
{
hasvalue = true;
}
else
{
hasvalue = false;
}
}
connection.Close();
return hasvalue;
}
public static int getDatabaseDataInt(Client player, string flag)
{
int value = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
value = reader.GetInt32(flag);
}
connection.Close();
return value;
}
public static string getDatabaseDataString(Client player, string flag)
{
string value = "";
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM userdata WHERE nickname=@username LIMIT 1;";
command.Parameters.AddWithValue("@username", player.name);
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
value = reader.GetString(flag);
}
connection.Close();
return value;
}
}
}