Ragemp-roleplay-skript/Haussystem/Haussystem.cs
2019-04-16 13:59:07 +02:00

615 lines
28 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrandTheftMultiplayer.Server.API;
using GrandTheftMultiplayer.Server.Constant;
using GrandTheftMultiplayer.Server.Elements;
using GrandTheftMultiplayer.Server.Managers;
using GrandTheftMultiplayer.Shared;
using GrandTheftMultiplayer.Shared.Math;
using MySql.Data.MySqlClient;
namespace Roleplay.Haussystem
{
class Haussystem : Script
{
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 const int HAUS_CALLBACK_GUEST = 68743186;
public const int HAUS_CALLBACK_OWNER = 687431826;
public const int HAUS_CALLBACK_KAUFEN = 687431825;
public const int HAUS_CALLBACK_OUTFIT = 687431824;
public static MySqlDataReader reader;
public Haussystem()
{
API.onEntityEnterColShape += onColHandler;
API.onClientEventTrigger += onMain;
loadHauser();
createOutfitsaverMarker();
}
public void createOutfitsaverMarker()
{
RAPI.Marker.createMarker(0, new Vector3(259.6595, -1003.877, -99.00), 1, 255, 0, 0, "saveOutfit", 0, 185, "", 0);
}
public void saveOutfit()
{
}
public Dictionary<Client, ColShape> clientColShape = new Dictionary<Client, ColShape>();
private void onColHandler(ColShape colshape, NetHandle entity)
{
if (entity != null && colshape != null)
{
Client player = API.getPlayerFromHandle(entity);
if (colshape.hasData("house") == true && colshape.getData("house") == true && player != null)
{
if (clientColShape.ContainsKey(player) == false)
{
clientColShape.Add(player, colshape);
}
else
{
clientColShape.Set(player, colshape);
}
if (colshape.getData("owner") == "Niemandem")
{
API.sendNotificationToPlayer(player, "Dieses Haus gehoert keinem. Du kannst es fuer ~g~$~y~" + colshape.getData("Price") + "~w~ Kaufen!");
openKaufenEnterMenu(player, colshape);
}
else
{
if (colshape.getData("locked") == 1) { API.sendNotificationToPlayer(player, "Dieses Haus gehoert: " + colshape.getData("owner") + ". Es ist abgeschlossen!"); }
else { API.sendNotificationToPlayer(player, "Dieses Haus gehoert: " + colshape.getData("owner") + ". Es ist aufgeschlossen!"); }
if (colshape.getData("owner") == player.name)
{
openOwnerEnterMenu(player, colshape);
}
else
{
if (colshape.getData("locked") == 0)
{
openGuestEnterMenu(player, colshape);
}
}
}
}
else if (colshape.hasData("saveOutfit") == true && colshape.getData("saveOutfit") == true)
{
if (entity != null && player != null)
{
object[] argumentList = new object[13];
argumentList[0] = HAUS_CALLBACK_OUTFIT;
argumentList[1] = "Outfits";
argumentList[2] = "Optionen:";
argumentList[3] = false;
int itemlength = 3;
argumentList[4] = itemlength;
argumentList[5] = "~r~Verlassen";
argumentList[6] = "~g~Outfit speichern";
argumentList[7] = "~b~Outfit laden";
API.triggerClientEvent(player, "menu_handler_create_menu", argumentList);
}
}
}
}
public void openGuestEnterMenu(Client player, ColShape col)
{
object[] argumentList = new object[11];
argumentList[0] = HAUS_CALLBACK_GUEST;
argumentList[1] = "Haus von " + col.getData("owner");
argumentList[2] = "Optionen:";
argumentList[3] = false;
int itemlength = 2;
argumentList[4] = itemlength;
argumentList[5] = "Verlassen";
argumentList[6] = "Betreten";
for (var i = 0; i < itemlength; i++)
{
argumentList[7 + i] = "";
}
API.triggerClientEvent(player, "menu_handler_create_menu", argumentList);
}
public void openOwnerEnterMenu(Client player, ColShape col)
{
object[] argumentList = new object[13];
argumentList[0] = HAUS_CALLBACK_OWNER;
argumentList[1] = "Haus von " + col.getData("owner");
argumentList[2] = "Optionen:";
argumentList[3] = false;
int itemlength = 4;
argumentList[4] = itemlength;
argumentList[5] = "Schliessen";
argumentList[6] = "Betreten";
argumentList[7] = "Auf/Abschliessen";
argumentList[8] = "Verkaufen";
for (var i = 0; i < itemlength; i++)
{
argumentList[9 + i] = "";
}
API.triggerClientEvent(player, "menu_handler_create_menu", argumentList);
}
public void openKaufenEnterMenu(Client player, ColShape col)
{
int price = (int) col.getData("Price");
object[] argumentList = new object[11];
argumentList[0] = HAUS_CALLBACK_KAUFEN;
argumentList[1] = "Haus kaufen";
argumentList[2] = "Optionen:";
argumentList[3] = false;
int itemlength = 2;
argumentList[4] = itemlength;
argumentList[5] = "Schliessen";
argumentList[6] = "Kaufen";
argumentList[8] = "Preis: ~b~" + price;
API.triggerClientEvent(player, "menu_handler_create_menu", argumentList);
}
private void onMain(Client sender, string eventName, object[] arguments)
{
if (eventName == "menu_handler_select_item")
{
int callback = (int)arguments[0];
if (callback == HAUS_CALLBACK_OWNER)
{
int index = (int)arguments[1];
ColShape col = clientColShape[sender];
if (index == 0)
{
API.triggerClientEvent(sender, "menu_handler_close_menu");
}
else if (index == 1)
{
ClientAPI.loadingScreen(sender,5);
int InteriorX = col.getData("Interior_X");
int InteriorY = col.getData("Interior_Y");
int InteriorZ = col.getData("Interior_Z");
int InteriorRot = col.getData("Interior_Rot");
int InteriorDim = col.getData("ID");
API.sendNativeToPlayer(sender, Hash.DO_SCREEN_FADE_OUT, 2000);
API.setEntityData(sender, "HOUSE:OUT:X", sender.position.X);
API.setEntityData(sender, "HOUSE:OUT:Y", sender.position.Y);
API.setEntityData(sender, "HOUSE:OUT:Z", sender.position.Z);
API.setEntityData(sender, "HOUSE:IN", true);
API.setEntityPosition(sender, new Vector3(InteriorX, InteriorY, InteriorZ));
API.setEntityRotation(sender, new Vector3(0, 0, InteriorRot));
API.sendNativeToPlayer(sender, Hash.DO_SCREEN_FADE_IN, 2000);
API.setEntityDimension(sender, InteriorDim);
API.sendChatMessageToPlayer(sender, "~r~Benutze /out um das Haus wieder zu verlassen!");
}
else if(index == 2)
{
if(col.getData("locked") == 0)
{
col.setData("locked", 1);
API.sendNotificationToPlayer(sender, "Du hast dein Haus abgeschlossen!", true);
} else
{
col.setData("locked", 0);
API.sendNotificationToPlayer(sender, "Du hast dein Haus aufgeschlossen!", true);
}
}
else if(index == 3)
{
int price = (int)col.getData("Price");
object[] argumentList = new object[11];
argumentList[0] = HAUS_CALLBACK_KAUFEN + 48;
argumentList[1] = "Haus verkaufen";
argumentList[2] = "Optionen:";
argumentList[3] = false;
int itemlength = 2;
argumentList[4] = itemlength;
argumentList[5] = "Schliessen";
argumentList[6] = "Verkaufen";
argumentList[8] = "Verkaufspreis: ~b~" + price * 0.5;
API.triggerClientEvent(sender, "menu_handler_create_menu", argumentList);
}
}
else if(callback == HAUS_CALLBACK_GUEST)
{
int index = (int)arguments[1];
ColShape col = clientColShape[sender];
if (index == 0)
{
API.triggerClientEvent(sender, "menu_handler_close_menu");
}
else if (index == 1)
{
ClientAPI.loadingScreen(sender,5);
int InteriorX = col.getData("Interior_X");
int InteriorY = col.getData("Interior_Y");
int InteriorZ = col.getData("Interior_Z");
int InteriorRot = col.getData("Interior_Rot");
int InteriorDim = col.getData("ID");
API.sendNativeToPlayer(sender, Hash.DO_SCREEN_FADE_OUT, 2000);
API.setEntityData(sender, "HOUSE:OUT:X", sender.position.X);
API.setEntityData(sender, "HOUSE:OUT:Y", sender.position.Y);
API.setEntityData(sender, "HOUSE:OUT:Z", sender.position.Z);
API.setEntityData(sender, "HOUSE:IN", true);
API.setEntityPosition(sender, new Vector3(InteriorX, InteriorY, InteriorZ));
API.setEntityRotation(sender, new Vector3(0, 0, InteriorRot));
API.sendNativeToPlayer(sender, Hash.DO_SCREEN_FADE_IN, 2000);
API.setEntityDimension(sender, InteriorDim);
API.sendChatMessageToPlayer(sender, "~r~Benutze /out um das Haus wieder zu verlassen!");
}
}
else if(callback == HAUS_CALLBACK_KAUFEN)
{
int index = (int)arguments[1];
ColShape col = clientColShape[sender];
if(index == 0)
{
API.triggerClientEvent(sender, "menu_handler_close_menu");
}
else if(index == 1)
{
if (hasHouse(sender) == false)
{
int price = col.getData("Price");
if (Environment.MONEY.Money.hasBarMoney(sender, price))
{
Environment.MONEY.Money.giveBarMoney(sender, -price, API);
API.sendNotificationToPlayer(sender, "~g~Du hast dir das Haus gekauft! Bei dem naechstem Restart gehoert es dir!");
API.consoleOutput(sender.name + " Bought House!");
col.setData("owner", sender.name);
executeHouseQueryString("Owner", sender.name, col.getData("ID"));
}
else
{
API.sendNotificationToPlayer(sender, "~r~Du hast nicht genug Bargeld dabei!");
}
}
else
{
API.sendNotificationToPlayer(sender, "~r~Du hast schon ein Haus!");
}
}
} else if(callback == HAUS_CALLBACK_OUTFIT)
{
int index = (int)arguments[1];
if(index == 0)
{
API.triggerClientEvent(sender, "menu_handler_close_menu");
}
else if(index == 1)
{
API.sendNotificationToPlayer(sender, "Outfit gespeichert! ID: " + nextOutfitSlot(sender));
saveOutfit(sender, nextOutfitSlot(sender));
}
else if(index == 2)
{
loadOutfitMenu(sender);
}
} else if(callback == CALLBACK_LOADOUTFITS){
int index = (int)arguments[1];
if (index == 0)
{
//VERLASSEN
}
else
{
int selectedOutfitID = index - 1;
loadOutfit(sender, selectedOutfitID);
API.sendNotificationToPlayer(sender, "Outfit geladen!");
}
} else if(callback == HAUS_CALLBACK_KAUFEN + 48)
{
int index = (int)arguments[1];
if (index == 1)
{
if(clientColShape.ContainsKey(sender) == true)
{
ColShape col = clientColShape[sender];
int price = col.getData("Price");
int calcprice = Convert.ToInt32((price * 0.5));
executeHouseQueryString("Owner", "Niemandem", col.getData("ID"));
col.setData("owner", "Niemandem");
Environment.MONEY.Money.giveBarMoney(sender, calcprice, API, "Haus verkauf! ~g~+ $" + calcprice + "");
}
}
}
}
}
public static void loadOutfit(Client player, int ID)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM outfits WHERE nickname = '" + player.name + "' AND ID = " + ID;
connection.Open();
reader = command.ExecuteReader();
int torso = 0;
int undershirt = 0;
int top = 0;
int topcolor = 0;
int legs = 0;
int legscolor = 0;
int feet = 0;
int feetcolor = 0;
int mask = 0;
int maskcolor = 0;
while (reader.Read())
{
torso = reader.GetInt32("TORSO");
undershirt = reader.GetInt32("UNDERSHIRT");
top = reader.GetInt32("TOP");
topcolor = reader.GetInt32("TOP_COLOR");
legs = reader.GetInt32("LEGS");
legscolor = reader.GetInt32("LEGS_COLOR");
feet = reader.GetInt32("FEET");
feetcolor = reader.GetInt32("FEET_COLOR");
mask = reader.GetInt32("MASK");
maskcolor = reader.GetInt32("MASK_COLOR");
}
API.shared.setPlayerClothes(player,3,torso,0);
API.shared.setPlayerClothes(player, 8, undershirt, 0);
API.shared.setPlayerClothes(player, 11, top, topcolor);
API.shared.setPlayerClothes(player, 4, legs, legscolor);
API.shared.setPlayerClothes(player, 6, feet, feetcolor);
API.shared.setPlayerClothes(player, 1, mask, maskcolor);
API.shared.consoleOutput("Loaded Outfit!");
}
public int nextOutfitSlot(Client player)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM outfits WHERE nickname = '" + player.name + "'";
connection.Open();
reader = command.ExecuteReader();
DataTable results = new DataTable();
results.Load(reader);
int slot = 0;
foreach (DataRow dr in results.Rows)
{
slot = slot + 1;
}
connection.Close();
return slot;
}
public void saveOutfit(Client player, int nextid)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "INSERT INTO `outfits`( `ID`,`nickname`, `TORSO`, `UNDERSHIRT`, `TOP`, `TOP_COLOR`, `LEGS`, `LEGS_COLOR`, `FEET`, `FEET_COLOR`, `MASK`, `MASK_COLOR`) VALUES (" + nextid + ", '" + player.name + "', " + API.getPlayerClothesDrawable(player,3) + ", " + API.getPlayerClothesDrawable(player,8) + ", " + API.getPlayerClothesDrawable(player,11) + ", " + API.getPlayerClothesTexture(player,11) + ", " + API.getPlayerClothesDrawable(player,4) + ", " + API.getPlayerClothesTexture(player,4) + ", " + API.getPlayerClothesDrawable(player,6) + ", " + API.getPlayerClothesTexture(player,6) + ", " + API.getPlayerClothesDrawable(player,1) + ", " + API.getPlayerClothesTexture(player,1) + ")";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
API.consoleOutput("Saved Outfit in House: " + player.name + " with SavedID: " + nextid);
}
public const int CALLBACK_LOADOUTFITS = 4664154;
public void loadOutfitMenu(Client player)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `outfits` WHERE nickname = '" + player.name + "'";
//SELECT * FROM `vehicles` WHERE owner = 'Sandy_Cheeks' AND slot = 1
connection.Open();
reader = command.ExecuteReader();
DataTable results = new DataTable();
results.Load(reader);
List<int> ID = new List<int>();
foreach (DataRow dr in results.Rows)
{
ID.Add(Convert.ToInt32(dr["ID"]));
}
//if (slots == null) return;
object[] argslist = new object[7 + ID.Count * 2];
argslist[0] = CALLBACK_LOADOUTFITS;
argslist[1] = "Outfit laden";
argslist[2] = "Optionen:";
argslist[3] = true;
argslist[4] = ID.Count + 1;
argslist[5] = "~r~Verlassen";
var i = 0;
foreach (int s in ID)
{
string x = "Outfit: " + s;
argslist[6 + i] = x;
i++;
}
API.triggerClientEvent(player, "menu_handler_create_menu", argslist);
}
[Command("out")]
public void gout(Client player)
{
if(API.hasEntityData(player,"HOUSE:IN") == true && API.getEntityData(player,"HOUSE:IN") == true)
{
API.setEntityDimension(player, 0);
API.sendNativeToPlayer(player, Hash.DO_SCREEN_FADE_OUT, 2000);
API.setEntityPosition(player, new Vector3(API.getEntityData(player, "HOUSE:OUT:X"), API.getEntityData(player, "HOUSE:OUT:Y"), API.getEntityData(player, "HOUSE:OUT:Z")));
API.sendNativeToPlayer(player, Hash.DO_SCREEN_FADE_IN, 2000);
API.setEntityData(player, "HOUSE:IN", false);
}
}
public void loadHauser()
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM houses";
int hauser = 0;
connection.Open();
reader = command.ExecuteReader();
DataTable results = new DataTable();
results.Load(reader);
foreach (DataRow dr in results.Rows)
{
int ID = Convert.ToInt32(dr["ID"]);
string Owner = Convert.ToString(dr["owner"]);
int Price = Convert.ToInt32(dr["Price"]);
int locked = Convert.ToInt32(dr["locked"]);
int marker_x = Convert.ToInt32(dr["Marker_X"]);
int marker_y = Convert.ToInt32(dr["Marker_Y"]);
int marker_z = Convert.ToInt32(dr["Marker_Z"]);
int Interior_X = Convert.ToInt32(dr["Interior_X"]);
int Interior_Y = Convert.ToInt32(dr["Interior_Y"]);
int Interior_Z = Convert.ToInt32(dr["Interior_Z"]);
int Interior_Rot = Convert.ToInt32(dr["Interior_Rot"]);
string ipl = Convert.ToString(dr["IPL"]);
if (Owner == "Niemandem")
{
API.createMarker(29, new Vector3(marker_x, marker_y, marker_z + 1), new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(1, 1, 1), 100, 0, 0, 255, 0);
} else {
API.createMarker(0, new Vector3(marker_x, marker_y, marker_z + 1), new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(1, 1, 1), 255, 255, 255, 255, 0);
}
ColShape col = API.createCylinderColShape(new Vector3(marker_x, marker_y, marker_z), 1, 2);
TextLabel tx = API.createTextLabel("Haus von ~n~ ~y~" + Owner, new Vector3(marker_x, marker_y, marker_z + 2), 5, 3, true, 0);
RAPI.Marker.textes.Add(tx);
if(Owner == "Niemandem"){
Blip houseblip = API.createBlip(new Vector3(marker_x, marker_y, marker_z ));
houseblip.sprite = 40;
houseblip.scale = 0.32f;
houseblip.name = "Verfügbares Haus";
}
if(ipl != "NONE")
{
API.requestIpl(ipl);
}
col.setData("ID", ID);
col.setData("owner", Owner);
col.setData("Interior_X", Interior_X);
col.setData("Interior_Y", Interior_Y);
col.setData("Interior_Z", Interior_Z);
col.setData("Interior_Rot", Interior_Rot);
col.setData("Price", Price);
col.setData("house", true);
col.setData("locked", locked);
hauser = hauser + 1;
}
API.consoleOutput("" + hauser + " Haeuser wurden geladen!");
connection.Close();
}
public void executeHouseQueryInteger(string value, int tovalue, int houseid)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE `houses` SET `" + value + "` = " + tovalue + " WHERE `ID` = " + houseid;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
API.consoleOutput("Housequery: " + value + " TO " + tovalue);
}
public void executeHouseQueryString(string value, string tovalue, int houseid)
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "UPDATE `houses` SET `" + value + "` = '" + tovalue + "' WHERE `ID` = " + houseid;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
API.consoleOutput("Housequery: " + value + " TO " + tovalue);
}
public static bool hasHouse(Client player)
{
bool state = false;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `houses` WHERE `Owner` = '" + player.name + "'";
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
if (reader.GetString("Owner") == player.name)
{
state = true;
}
}
connection.Close();
return state;
}
public static Vector3 getHousePosition(Client player)
{
Vector3 vec = new Vector3(0, 0, 0);
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `houses` WHERE `Owner` = '" + player.name + "'";
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
vec = new Vector3(reader.GetInt32("Marker_X"), reader.GetInt32("Marker_Y"), reader.GetInt32("Marker_Z"));
}
connection.Close();
return vec;
}
public static Vector3 getHouseInteriorPosition(Client player)
{
Vector3 vec = new Vector3(0, 0, 0);
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `houses` WHERE `Owner` = '" + player.name + "'";
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
vec = new Vector3(reader.GetInt32("Interior_X"), reader.GetInt32("Interior_Y"), reader.GetInt32("Interior_Z"));
}
connection.Close();
return vec;
}
public static int getHouseDimension(Client player)
{
int dim = 0;
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `houses` WHERE `Owner` = '" + player.name + "'";
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
dim = reader.GetInt32("ID");
}
connection.Close();
return dim;
}
[Command("createHouse")]
public void createHouse(Client player, int x, int y, int z, int rot, int preis, string ipl = "NONE")
{
connection = new MySqlConnection(myConnectionString);
command = connection.CreateCommand();
command.CommandText = "INSERT INTO `houses` (`Owner`, `Price`, `locked`, `Marker_X`, `Marker_Y`, `Marker_Z`, `Interior_X`, `Interior_Y`, `Interior_Z`, `Interior_Rot`, `IPL`) VALUES ('Niemandem', " + preis + ", 1, " + Convert.ToInt32(player.position.X) + ", " + Convert.ToInt32(player.position.Y) + ", " + Convert.ToInt32((player.position.Z - 1)) + ", " + x + ", " + y + ", " + z + ", " + rot + ", '" + ipl + "')" ;
API.consoleOutput("" + command.CommandText);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
API.sendNotificationToPlayer(player, "~g~Du hast ein Haus erstellt!");
}
}
}