Jump to content

Why can't the players buy the properties?


Pedro001

Recommended Posts

Please, Could someone help me find out why can't players buy the properties in this script? 

I mean, part of it works just fine, I can create the properties, and everything is ok, but the other players on the server, can't buy the properties, because the windows won't pop up for them when they press the letter N. I went through the script and I can't figure out why this happens. 

 

core.client.lua

local screen_width, screen_height = GuiElement.getScreenSize();
local is_cursor_over_gui = false;
local action;
local settings = {};
GuiElement.setInputMode("no_binds_when_editing");

addEvent("business.showCreateBusinessWindow", true);
addEventHandler("business.showCreateBusinessWindow", root,
	function()
		gui.cb.window.visible = true;
		showCursor(true);
	end
);

function outputMessage(message, r, g, b)
	triggerServerEvent("business.outputMessage", localPlayer, message, r, g, b);
end

addEventHandler("onClientRender", root,
	function()
		for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
			local b_data = b_marker:getData("b_data");
			local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
			local x, y, z = b_marker.position.x, b_marker.position.y, b_marker.position.z;
			local cam_x, cam_y, cam_z = getCameraMatrix();
			if getDistanceBetweenPoints3D(cam_x, cam_y, cam_z, x, y, z) < 15 then
				local screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 1.6);
				if screen_x then
					local scale = 1920 / screen_width;
					local width = 80 / scale;
					dxDrawImage(screen_x - width / 2, screen_y - screen_height / 10, width, 80, "files/business.png");
				end
				if settings.show_business_info_on_marker then
					screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 1.4);
					if screen_x then
						if #tostring(id) == 1 then id = "0"..tostring(id) end
						dxDrawFramedText("ID: #"..id, screen_x, screen_y, screen_x, screen_y, tocolor(255, 255, 255, 255), 1.0, "default-bold", "center", "center", false, false, false);
					end
					screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 1.2);
					if screen_x then
						dxDrawFramedText("Nome: "..name, screen_x, screen_y, screen_x, screen_y, tocolor(255, 255, 255, 255), 1.0, "default-bold", "center", "center", false, false, false);
					end
					screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 1.0);
					if screen_x then
						dxDrawFramedText("Dono: "..owner, screen_x, screen_y, screen_x, screen_y, tocolor(255, 255, 255, 255), 1.0, "default-bold", "center", "center", false, false, false);
					end
					screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 0.8);
					if screen_x then
						dxDrawFramedText("Custo: $"..cost, screen_x, screen_y, screen_x, screen_y, tocolor(255, 255, 255, 255), 1.0, "default-bold", "center", "center", false, false, false);
					end
					screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 0.6);
					if screen_x then
						dxDrawFramedText("Pagamento: $"..payout, screen_x, screen_y, screen_x, screen_y, tocolor(255, 255, 255, 255), 1.0, "default-bold", "center", "center", false, false, false);
					end
					screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 0.4);
					if screen_x then
						dxDrawFramedText("Tempo de Pagamento: "..payout_otime.." "..payout_unit, screen_x, screen_y, screen_x, screen_y, tocolor(255, 255, 255, 255), 1.0, "default-bold", "center", "center", false, false, false);
					end
					screen_x, screen_y = getScreenFromWorldPosition(x, y, z + 0.2);
					if screen_x then
						dxDrawFramedText("Banco: $"..bank, screen_x, screen_y, screen_x, screen_y, tocolor(255, 255, 255, 255), 1.0, "default-bold", "center", "center", false, false, false);
					end
				end
			end
		end
	end
);

addEvent("business.showInstructions", true);
addEventHandler("business.showInstructions", root,
	function()
		addEventHandler("onClientRender", root, showInstructions);
	end
);

function showInstructions()
	if settings.key then
		dxDrawText("Use",(screen_width / 1400) * 550,(screen_height / 900) * 450,(screen_width / 1440) * 100,(screen_height / 900) * 100, tocolor(255, 255, 255, 255),(screen_width / 1440) * 2.0);
		dxDrawText(settings.key:upper(),(screen_width / 1440) * 615,(screen_height / 900) * 450,(screen_width / 1440) * 100,(screen_height / 900) * 100, tocolor(255, 0, 0, 255),(screen_width / 1440) * 2.0);
		dxDrawText(" Para abrir o Negocio",(screen_width / 1440) * 630,(screen_height / 900) * 450,(screen_width / 1440) * 100,(screen_height / 900) * 100, tocolor(255, 255, 255, 255),(screen_width / 1440) * 2.0);
	end
end

addEvent("business.hideInstructions", true);
addEventHandler("business.hideInstructions", root,
	function()
		removeEventHandler("onClientRender", root, showInstructions);
	end
);

addEvent("business.showBusinessWindow", true);
addEventHandler("business.showBusinessWindow", root,
	function(b_marker, is_owner, is_admin)
		local b_data = b_marker:getData("b_data");
		local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
		local x, y, z = b_marker.position;
		if #tostring(id) == 1 then id = "0"..tostring(id) end
		gui.b.window.text = name;
		gui.b.label.id.text = "ID: #"..id;
		gui.b.label.name.text = "Nome: "..name;
		gui.b.label.owner.text = "Dono: "..owner;
		gui.b.label.cost.text = "Custo: $"..cost;
		gui.b.label.payout.text = "Pagamento: $"..payout;
		gui.b.label.payout_time.text = "Tempo de Pagamento: "..payout_otime.." "..payout_unit;
		gui.b.label.location.text = "Localização: "..getZoneName(x, y, z, false).."("..getZoneName(x, y, z, true)..")";
		gui.b.label.bank.text = "Banco: $"..bank;

		if is_admin and is_owner then
			gui.b.tab.action.enabled = true;
			gui.b.button.sell.enabled = true;
			gui.b.button.deposit.enabled = true;
			gui.b.button.withdraw.enabled = true;
			gui.b.button.set_name.enabled = true;
			gui.b.button.set_owner.enabled = true;
			gui.b.button.set_cost.enabled = true;
			gui.b.button.set_bank.enabled = true;
			gui.b.button.buy.enabled = false;
		elseif is_admin and not is_owner and owner ~= "For Sale" then
			gui.b.tab.action.enabled = true;
			gui.b.button.set_name.enabled = true;
			gui.b.button.set_owner.enabled = true;
			gui.b.button.set_cost.enabled = true;
			gui.b.button.set_bank.enabled = true;
			gui.b.button.sell.enabled = true;
			gui.b.button.buy.enabled = false;
			gui.b.button.deposit.enabled = false;
			gui.b.button.withdraw.enabled = false;
		elseif is_admin and not is_owner and owner == "For Sale" then
			gui.b.tab.action.enabled = true;
			gui.b.button.set_name.enabled = true;
			gui.b.button.set_owner.enabled = true;
			gui.b.button.set_cost.enabled = true;
			gui.b.button.set_bank.enabled = true;
			gui.b.button.sell.enabled = false;
			gui.b.button.buy.enabled = true;
			gui.b.button.deposit.enabled = false;
			gui.b.button.withdraw.enabled = false;
		elseif not is_admin and is_owner then
			gui.b.tab.action.enabled = true;
			gui.b.button.set_name.enabled = false;
			gui.b.button.set_owner.enabled = false;
			gui.b.button.set_cost.enabled = false;
			gui.b.button.set_bank.enabled = false;
			gui.b.button.sell.enabled = true;
			gui.b.button.deposit.enabled = true;
			gui.b.button.withdraw.enabled = true;
			gui.b.button.buy.enabled = false;
		elseif not is_admin and not is_owner and owner ~= "For Sale" then
			gui.b.tab.action.enabled = false;
			gui.b.tab_panel:setSelectedTab(gui.b.tab.info);
		elseif not is_admin and not is_owner and owner == "For Sale" then
			gui.b.tab.action.enabled = true;
			gui.b.button.set_name.enabled = false;
			gui.b.button.set_owner.enabled = false;
			gui.b.button.set_cost.enabled = false;
			gui.b.button.set_bank.enabled = false;
			gui.b.button.accept.enabled = false;
			gui.b.button.sell.enabled = false;
			gui.b.button.deposit.enabled = false;
			gui.b.button.withdraw.enabled = false;
			gui.b.button.buy.enabled = true;
		end

		gui.b.window.visible = true;
		showCursor(true);
		removeEventHandler("onClientRender", root, showInstructions)
	end
);

addEventHandler("onClientResourceStart", resourceRoot,
	function()
		triggerServerEvent("business.getSettings", localPlayer);
	end
);

addEvent("business.getSettings", true);
addEventHandler("business.getSettings", root,
	function(_settings)
		settings = _settings;
	end
);

core.server.lua

local _settings = get("");
local settings = {};
for k, v in pairs(_settings) do
	k = split(k, ".")[2];
	settings[k] = v;
end

if (settings.key:len() < 1 or settings.key:len() > 1) then
	settings.key = "N";
end
if (not settings.blip or tonumber(settings.blip) == nil) then
	settings.blip = false;
end

addEventHandler("onResourceStart", resourceRoot, function()
	if (settings.database == "mysql") then
		local host, db, username, password, port, socket = unpack(settings.database_data);
		if not (host and db and username and password) then outputDebugString("Business: Failed to connect to the MySQL server - The data is invalid"); return; end
		if (tonumber(port)) then port = "port="..port..";"; else port = ""; end
		if (socket and socket ~= "") then socket = "socket="..socket..";"; else socket = ""; end
		database = Connection("mysql", "host="..host..";dbname="..db..";"..port..socket, username, password);
		if (not database) then outputDebugString("Business: Failed to connect to the MySQL server"); return; end
	else
		database = Connection("sqlite", "files/business.db");
		if (not database) then outputDebugString("Business: Failed to connect to the SQLite file"); return; end
	end
	database:exec("CREATE TABLE IF NOT EXISTS business(id INT, name TEXT, owner TEXT, cost INT, pos TEXT, payout INT, payout_time INT, payout_otime INT, payout_unit TEXT, payout_cur_time INT, bank INT)");
	database:query(dbCreateBusinessesCallback,  "SELECT * FROM business");
end);

function dbCreateBusinessesCallback(query_handle)
	local sql = query_handle:poll(0);
	if (sql and #sql > 0) then
		for index, row in ipairs(sql) do
			local pos = split(row["pos"], ",");
			local b_marker = Marker(pos[1], pos[2], pos[3], "cylinder", 1.5, settings.marker_color[1], settings.marker_color[2], settings.marker_color[3], settings.marker_color[4]);
			b_marker.interior = pos[4];
			b_marker.dimension = pos[5];
			if (settings.blip ~= false) then
				if (row["owner"] == "For Sale") then
					local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
					b_blip.interior = pos[4];
					b_blip.dimension = pos[5];
				else
					local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
					b_blip.interior = pos[4];
					b_blip.dimension = pos[5];
				end
			end
			addEventHandler("onMarkerHit", b_marker, onBusinessMarkerHit);
			addEventHandler("onMarkerLeave", b_marker, onBusinessMarkerLeave);
			local timer = Timer(businessPayout, row["payout_cur_time"] , 1, b_marker);
			b_marker:setData("b_data", {row["id"], row["name"], row["owner"], row["cost"], row["payout"], row["payout_time"], row["payout_otime"], row["payout_unit"], row["bank"], timer});
		end
	end
end

addCommandHandler("negocios", function(player)
	if (ACL.hasObjectPermissionTo(player, "function.banPlayer")) then
		triggerClientEvent(player, "business.showCreateBusinessWindow", player);
	else
		player:outputMessage("Negocios: Voce nao tem acesso a este comando.", 255, 0, 0);
	end
end);

function Player:outputMessage(message, r, g, b)
	if (settings.info_messages_type == "dx") then
		dxOutputMessage(message, self, r, g, b);
	else
		self:outputChat(message, r, g, b, true);
	end
end

function outputMessage(message, player, r, g, b)
	if (settings.info_messages_type == "dx") then
		dxOutputMessage(message, player, r, g, b);
	else
		player:outputChat(message, r, g, b, true);
	end
end

function dxOutputMessage(message, player, r, g, b)
	triggerClientEvent(player, "business.dxOutputMessage", player, message, r, g, b);
end

addEvent("business.outputMessage", true);
addEventHandler("business.outputMessage", root, function(message, r, g, b)
	source:outputMessage(message, r, g, b);
end);

addEvent("business.createBusiness", true);
addEventHandler("business.createBusiness", root, function(x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit)
	database:query(dbCreateBusinessCallback,  {client, x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit}, "SELECT * FROM business");
end);

function dbCreateBusinessCallback(query_handle, client, x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit)
	local sql = query_handle:poll(0);
	if (sql) then
		local id;
		if (#sql > 0) then
			id = sql[#sql]["id"] + 1;
		else
			id = 1;
		end
		local unit;
		if (payout_unit == "Segundos") then
			unit = 1000;
		elseif (payout_unit == "Minutos") then
			unit = 60000;
		elseif (payout_unit == "Horas") then
			unit = 3600000;
		elseif (payout_unit == "Dias") then
			unit = 86400000;
		end

		x = tonumber(x);
		y = tonumber(y);
		z = tonumber(z);
		interior = tonumber(interior);
		dimension = tonumber(dimension);
		cost = tonumber(cost);
		payout = tonumber(payout);
		payout_time = tonumber(payout_time);

		z = z - 1;

		database:exec("INSERT INTO business(id,name,owner,cost,pos,payout,payout_time,payout_otime,payout_unit,payout_cur_time,bank) VALUES(?,?,?,?,?,?,?,?,?,?,?)", id, name, "For Sale", cost, x..","..y..","..z..","..interior..","..dimension, payout, payout_time * unit, payout_time, payout_unit, payout_time * unit, 0);

		local b_marker = Marker(x, y, z, "cylinder", 1.5, settings.marker_color[1], settings.marker_color[2], settings.marker_color[3], settings.marker_color[4]);
		b_marker.interior = interior;
		b_marker.dimension = dimension;
		if (settings.blip ~= false) then
			local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
			b_blip.interior = interior;
			b_blip.dimension = dimension;
		end
		local timer = Timer(businessPayout, payout_time * unit , 1, b_marker);
		b_marker:setData("b_data", {id, name, "For Sale", cost, payout, payout_time * unit, payout_time, payout_unit, 0, timer});
		addEventHandler("onMarkerHit", b_marker, onBusinessMarkerHit);
		addEventHandler("onMarkerLeave", b_marker, onBusinessMarkerLeave);
		if (#tostring(id) == 1) then id = "0".. tostring(id) end
		client:outputMessage("Negocios: Negocio(ID #"..id..") foi criado com sucesso", 0, 255, 0);
	end
end

function onBusinessMarkerHit(hElement, mDim)
	if (hElement:getType() ~= "player") then return; end
	if (hElement:isInVehicle()) then return; end
	if (not mDim) then return; end
	triggerClientEvent(hElement, "business.showInstructions", hElement);
end

function onBusinessMarkerLeave(hElement, mDim)
	if (hElement:getType() ~= "player") then return; end
	if (hElement:isInVehicle()) then return; end
	if (not mDim) then return; end
	triggerClientEvent(hElement, "business.hideInstructions", hElement);
end

function businessPayout(b_marker)
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (owner ~= "For Sale") then
		bank = bank + payout;
		database:exec("UPDATE business SET bank = ? WHERE id = ?", bank, id);
		if (settings.inform_player_of_payout) then
			local account = Account(owner);
			if (account) then
				local player = account:getPlayer();
				if (player and player.isElement) then
					player:outputMessage("Negocios: Negocio \" "..name.." \" pagou($"..payout..")", 0, 255, 0);
				end
			end
		end
	end
	timer = Timer(businessPayout, payout_time, 1, b_marker);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
end

addEventHandler("onResourceStop", resourceRoot, function()
	for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
		local b_data = b_marker:getData("b_data");
		local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
		if (timer and timer:isValid()) then
			local left = timer:getDetails();
			if (left >= 50) then
				database:exec("UPDATE business SET payout_cur_time = ? WHERE id = ?", left, id);
			else
				database:exec("UPDATE business SET payout_cur_time = ? WHERE id = ?", payout_time, id);
			end
		end
	end
end);

function Ped:isInMarker(marker)
	local colshape = marker.colShape;
	return self:isWithinColShape(colshape);
end

addEventHandler("onResourceStart", resourceRoot, function()
	for index, player in ipairs(Element.getAllByType("player")) do
		bindKey(player, settings.key, "up", onPlayerAttemptToOpenBusiness);
	end
end);

addEventHandler("onPlayerJoin", root,function()
	bindKey(source, settings.key, "up", onPlayerAttemptToOpenBusiness);
end);

function onPlayerAttemptToOpenBusiness(player)
	for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
		if (player:isInMarker(b_marker)) then
			local b_data = b_marker:getData("b_data");
			local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
			triggerClientEvent(player, "business.showBusinessWindow", player, b_marker, getAccountName(getPlayerAccount(player)) == owner, ACL.hasObjectPermissionTo(player, "function.banPlayer"));
			break;
		end
	end
end

function Ped:getMarker()
	for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
		if (self:isInMarker(b_marker)) then
			return b_marker;
		end
	end
end

addEvent("business.buy", true);
addEventHandler("business.buy", root, function()
	local account = client.account;
	if (not account or account:isGuest()) then client:outputMessage("Business: You are not logged in", 255, 0, 0) return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (owner ~= "For Sale") then client:outputMessage("Business: This business is owned", 255, 0, 0); return; end
	database:query(dbBuyBusinessCallback, {client, b_marker, id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer}, "SELECT * FROM business WHERE owner = ?", account.name);
end);

addEvent("business.sell", true);
addEventHandler("business.sell", root, function()
	local account = client.account;
	if (not account or account:isGuest()) then return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (owner ~= account.name) then
		if (ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
			database:exec("UPDATE business SET owner = ? WHERE id = ?", "For Sale", id);
			b_marker:setData("b_data", {id, name, "For Sale", cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
			client:outputMessage("Negocios: Voce vendeu o Negocio com Sucesso", 0, 255, 0);
			return;
		else
			client:outputMessage("Negocios: Voce nao e proprietario deste Negocio", 255, 0, 0);
			return;
		end
	end
	database:exec("UPDATE business SET owner = ?, bank = ? WHERE id = ?", "For Sale", 0, id);
	client:giveMoney(tonumber(("%.f"):format(cost / 2)));
	client:giveMoney(bank);
	b_marker:setData("b_data", {id, name, "For Sale", cost, payout, payout_time, payout_otime, payout_unit, 0});
	client:outputMessage("Negocios: Voce vendeu o Negocio com Sucesso, todo o dinheiro no banco foi pago para você.", 0, 255, 0);
end);

addEvent("business.deposit", true);
addEventHandler("business.deposit", root, function(amount)
	local account = client.account;
	if (not account or account:isGuest()) then return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (not tonumber(amount)) then client:outputMessage("Negocios: Quantidade invalida", 255, 0, 0); return; end
	if (owner ~= account.name) then
		client:outputMessage("Negocios: Você não é o proprietário deste Negocio", 255, 0, 0);
		return;
	end
	if (client.money < amount) then
		client:outputMessage("Negocios: Você não tem dinheiro suficiente", 255, 0, 0);
		return;
	end
	database:exec("UPDATE business SET bank = ? WHERE id = ?", bank + amount, id);
	client:takeMoney(amount);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank + amount, timer});
	client:outputMessage("Negocios: Você depositou com sucesso $"..amount.." no negócio", 0, 255, 0);
end);

addEvent("business.withdraw", true);
addEventHandler("business.withdraw", root, function(amount)
	local account = client.account;
	if (not account or account:isGuest()) then return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (not tonumber(amount)) then client:outputMessage("Negocios: Quantidade Invalida", 255, 0, 0); return; end
	if (owner ~= account.name) then
		client:outputMessage("Negocios: Voce nao e o dono do negocio", 255, 0, 0);
		return;
	end
	if (bank < amount) then
		client:outputMessage("Negocios: Voce nao tem muito no Banco de Negocios", 255, 0, 0);
		return;
	end
	database:exec("UPDATE business SET bank = ? WHERE id = ?", bank - amount, id);
	client:giveMoney(amount);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank - amount, timer});
	client:outputMessage("Negocios: Voce retirou com sucesso $"..amount.." do Negocio", 0, 255, 0);
end);

addEvent("business.setName", true);
addEventHandler("business.setName", root, function(new_name)
	if (new_name == "" or #new_name > 30) then
		client:outputMessage("Negocios: Valor Invalido", 255, 0, 0);
		return;
	end
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET name = ? WHERE id = ?", new_name, id);
	b_marker:setData("b_data", {id, new_name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
	client:outputMessage("Negocios: Voce renomeou com sucesso o negocio", 0, 255, 0);
end);

addEvent("business.setOwner", true);
addEventHandler("business.setOwner", root, function(new_owner)
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET owner = ? WHERE id = ?", new_owner, id);
	b_marker:setData("b_data", {id, name, new_owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
	client:outputMessage("Negocios: Voce mudou com sucesso o proprietario da empresa", 0, 255, 0);
end);

addEvent("business.setCost", true);
addEventHandler("business.setCost", root, function(amount)
	if (tonumber(amount) == nil) then
		client:outputMessage("Negocios: Valor Invalido", 255, 0, 0);
		return;
	end
	amount = tonumber(amount);
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET cost = ? WHERE id = ?", amount, id);
	b_marker:setData("b_data", {id, name, owner, amount, payout, payout_time, payout_otime, payout_unit, bank, timer});
	client:outputMessage("Negocios: Voce mudou com sucesso o custo do negocio", 0, 255, 0);
end);

addEvent("business.setBank", true);
addEventHandler("business.setBank", root, function(amount)
	if (tonumber(amount) == nil) then
		client:outputMessage("Negocios: Valor Invalido", 255, 0, 0);
		return;
	end
	amount = tonumber(amount);
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET bank = ? WHERE id = ?", amount, id);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, amount, timer});
	client:outputMessage("Negocios: Voce mudou com sucesso a quantidade de banco comercial", 0, 255, 0);
end);

addEvent("business.destroy", true);
addEventHandler("business.destroy", root, function()
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (timer and timer:isValid()) then timer:destroy() end
	database:exec("DELETE FROM business WHERE id = ?", id);
	b_marker:destroyAttachedBlips();
	b_marker:destroy();
	client:outputMessage("Negocios: Voce destruiu com sucesso o negocio", 0, 255, 0);
	triggerClientEvent(client, "business.hideInstructions", client);
	database:query(dbReOrderBusinessesCallback,  "SELECT * FROM business");
end);

function dbBuyBusinessCallback(query_handle, source, b_marker, id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer)
	local sql = query_handle:poll(0);
	if (#sql == settings.owned_businesses) then
		source:outputMessage("Negocios: Voce ja possui "..#sql.." negocios , que e o valor maximo", 255, 0, 0);
		return;
	end
	local money = source.money;
	if (money < cost) then source:outputMessage("Negocios: Voce nao tem dinheiro suficiente", 255, 0, 0) return end
	database:exec("UPDATE business SET owner = ? WHERE id = ?", source.account.name, id);
	source:takeMoney(cost);
	source:outputMessage("Negocios: Voce comprou com sucesso neste negocio", 0, 255, 0);
	b_marker:setData("b_data", {id, name, source.account.name, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
end

function dbReOrderBusinessesCallback(query_handle)
	local sql = query_handle:poll(0);
	if (sql and #sql > 0) then
		for index, row in ipairs(sql) do
			database:exec("UPDATE business SET id = ? WHERE id = ?", index, row["id"]);
		end
		for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
			database:query(dbUpdateBusinessesIDsCallback, {b_marker, index}, "SELECT id FROM business WHERE id = ?", index);
		end
	end
end

function dbUpdateBusinessesIDsCallback(query_handle, b_marker, index)
	local sql = query_handle:poll(0);
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	b_marker:setData("b_data", {index, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
end

function Element:destroyAttachedBlips()
	if (not self) then return; end
	for index, element in pairs(self:getAttachedElements()) do
		if (element and element.isElement) then
			element:destroy();
		end
	end
end

addEvent("business.getSettings", true);
addEventHandler("business.getSettings", root, function()
	triggerClientEvent(source, "business.getSettings", source, settings);
end);

dxoutput.client

local _settings = get("");
local settings = {};
for k, v in pairs(_settings) do
	k = split(k, ".")[2];
	settings[k] = v;
end

if (settings.key:len() < 1 or settings.key:len() > 1) then
	settings.key = "N";
end
if (not settings.blip or tonumber(settings.blip) == nil) then
	settings.blip = false;
end

addEventHandler("onResourceStart", resourceRoot, function()
	if (settings.database == "mysql") then
		local host, db, username, password, port, socket = unpack(settings.database_data);
		if not (host and db and username and password) then outputDebugString("Business: Failed to connect to the MySQL server - The data is invalid"); return; end
		if (tonumber(port)) then port = "port="..port..";"; else port = ""; end
		if (socket and socket ~= "") then socket = "socket="..socket..";"; else socket = ""; end
		database = Connection("mysql", "host="..host..";dbname="..db..";"..port..socket, username, password);
		if (not database) then outputDebugString("Business: Failed to connect to the MySQL server"); return; end
	else
		database = Connection("sqlite", "files/business.db");
		if (not database) then outputDebugString("Business: Failed to connect to the SQLite file"); return; end
	end
	database:exec("CREATE TABLE IF NOT EXISTS business(id INT, name TEXT, owner TEXT, cost INT, pos TEXT, payout INT, payout_time INT, payout_otime INT, payout_unit TEXT, payout_cur_time INT, bank INT)");
	database:query(dbCreateBusinessesCallback,  "SELECT * FROM business");
end);

function dbCreateBusinessesCallback(query_handle)
	local sql = query_handle:poll(0);
	if (sql and #sql > 0) then
		for index, row in ipairs(sql) do
			local pos = split(row["pos"], ",");
			local b_marker = Marker(pos[1], pos[2], pos[3], "cylinder", 1.5, settings.marker_color[1], settings.marker_color[2], settings.marker_color[3], settings.marker_color[4]);
			b_marker.interior = pos[4];
			b_marker.dimension = pos[5];
			if (settings.blip ~= false) then
				if (row["owner"] == "For Sale") then
					local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
					b_blip.interior = pos[4];
					b_blip.dimension = pos[5];
				else
					local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
					b_blip.interior = pos[4];
					b_blip.dimension = pos[5];
				end
			end
			addEventHandler("onMarkerHit", b_marker, onBusinessMarkerHit);
			addEventHandler("onMarkerLeave", b_marker, onBusinessMarkerLeave);
			local timer = Timer(businessPayout, row["payout_cur_time"] , 1, b_marker);
			b_marker:setData("b_data", {row["id"], row["name"], row["owner"], row["cost"], row["payout"], row["payout_time"], row["payout_otime"], row["payout_unit"], row["bank"], timer});
		end
	end
end

addCommandHandler("negocios", function(player)
	if (ACL.hasObjectPermissionTo(player, "function.banPlayer")) then
		triggerClientEvent(player, "business.showCreateBusinessWindow", player);
	else
		player:outputMessage("Negocios: Voce nao tem acesso a este comando.", 255, 0, 0);
	end
end);

function Player:outputMessage(message, r, g, b)
	if (settings.info_messages_type == "dx") then
		dxOutputMessage(message, self, r, g, b);
	else
		self:outputChat(message, r, g, b, true);
	end
end

function outputMessage(message, player, r, g, b)
	if (settings.info_messages_type == "dx") then
		dxOutputMessage(message, player, r, g, b);
	else
		player:outputChat(message, r, g, b, true);
	end
end

function dxOutputMessage(message, player, r, g, b)
	triggerClientEvent(player, "business.dxOutputMessage", player, message, r, g, b);
end

addEvent("business.outputMessage", true);
addEventHandler("business.outputMessage", root, function(message, r, g, b)
	source:outputMessage(message, r, g, b);
end);

addEvent("business.createBusiness", true);
addEventHandler("business.createBusiness", root, function(x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit)
	database:query(dbCreateBusinessCallback,  {client, x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit}, "SELECT * FROM business");
end);

function dbCreateBusinessCallback(query_handle, client, x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit)
	local sql = query_handle:poll(0);
	if (sql) then
		local id;
		if (#sql > 0) then
			id = sql[#sql]["id"] + 1;
		else
			id = 1;
		end
		local unit;
		if (payout_unit == "Segundos") then
			unit = 1000;
		elseif (payout_unit == "Minutos") then
			unit = 60000;
		elseif (payout_unit == "Horas") then
			unit = 3600000;
		elseif (payout_unit == "Dias") then
			unit = 86400000;
		end

		x = tonumber(x);
		y = tonumber(y);
		z = tonumber(z);
		interior = tonumber(interior);
		dimension = tonumber(dimension);
		cost = tonumber(cost);
		payout = tonumber(payout);
		payout_time = tonumber(payout_time);

		z = z - 1;

		database:exec("INSERT INTO business(id,name,owner,cost,pos,payout,payout_time,payout_otime,payout_unit,payout_cur_time,bank) VALUES(?,?,?,?,?,?,?,?,?,?,?)", id, name, "For Sale", cost, x..","..y..","..z..","..interior..","..dimension, payout, payout_time * unit, payout_time, payout_unit, payout_time * unit, 0);

		local b_marker = Marker(x, y, z, "cylinder", 1.5, settings.marker_color[1], settings.marker_color[2], settings.marker_color[3], settings.marker_color[4]);
		b_marker.interior = interior;
		b_marker.dimension = dimension;
		if (settings.blip ~= false) then
			local b_blip = Blip.createAttachedTo(b_marker, settings.blip, 2, 255, 0, 0, 255, 0, 100.0);
			b_blip.interior = interior;
			b_blip.dimension = dimension;
		end
		local timer = Timer(businessPayout, payout_time * unit , 1, b_marker);
		b_marker:setData("b_data", {id, name, "For Sale", cost, payout, payout_time * unit, payout_time, payout_unit, 0, timer});
		addEventHandler("onMarkerHit", b_marker, onBusinessMarkerHit);
		addEventHandler("onMarkerLeave", b_marker, onBusinessMarkerLeave);
		if (#tostring(id) == 1) then id = "0".. tostring(id) end
		client:outputMessage("Negocios: Negocio(ID #"..id..") foi criado com sucesso", 0, 255, 0);
	end
end

function onBusinessMarkerHit(hElement, mDim)
	if (hElement:getType() ~= "player") then return; end
	if (hElement:isInVehicle()) then return; end
	if (not mDim) then return; end
	triggerClientEvent(hElement, "business.showInstructions", hElement);
end

function onBusinessMarkerLeave(hElement, mDim)
	if (hElement:getType() ~= "player") then return; end
	if (hElement:isInVehicle()) then return; end
	if (not mDim) then return; end
	triggerClientEvent(hElement, "business.hideInstructions", hElement);
end

function businessPayout(b_marker)
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (owner ~= "For Sale") then
		bank = bank + payout;
		database:exec("UPDATE business SET bank = ? WHERE id = ?", bank, id);
		if (settings.inform_player_of_payout) then
			local account = Account(owner);
			if (account) then
				local player = account:getPlayer();
				if (player and player.isElement) then
					player:outputMessage("Negocios: Negocio \" "..name.." \" pagou($"..payout..")", 0, 255, 0);
				end
			end
		end
	end
	timer = Timer(businessPayout, payout_time, 1, b_marker);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
end

addEventHandler("onResourceStop", resourceRoot, function()
	for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
		local b_data = b_marker:getData("b_data");
		local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
		if (timer and timer:isValid()) then
			local left = timer:getDetails();
			if (left >= 50) then
				database:exec("UPDATE business SET payout_cur_time = ? WHERE id = ?", left, id);
			else
				database:exec("UPDATE business SET payout_cur_time = ? WHERE id = ?", payout_time, id);
			end
		end
	end
end);

function Ped:isInMarker(marker)
	local colshape = marker.colShape;
	return self:isWithinColShape(colshape);
end

addEventHandler("onResourceStart", resourceRoot, function()
	for index, player in ipairs(Element.getAllByType("player")) do
		bindKey(player, settings.key, "up", onPlayerAttemptToOpenBusiness);
	end
end);

addEventHandler("onPlayerJoin", root,function()
	bindKey(source, settings.key, "up", onPlayerAttemptToOpenBusiness);
end);

function onPlayerAttemptToOpenBusiness(player)
	for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
		if (player:isInMarker(b_marker)) then
			local b_data = b_marker:getData("b_data");
			local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
			triggerClientEvent(player, "business.showBusinessWindow", player, b_marker, getAccountName(getPlayerAccount(player)) == owner, ACL.hasObjectPermissionTo(player, "function.banPlayer"));
			break;
		end
	end
end

function Ped:getMarker()
	for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
		if (self:isInMarker(b_marker)) then
			return b_marker;
		end
	end
end

addEvent("business.buy", true);
addEventHandler("business.buy", root, function()
	local account = client.account;
	if (not account or account:isGuest()) then client:outputMessage("Business: You are not logged in", 255, 0, 0) return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (owner ~= "For Sale") then client:outputMessage("Business: This business is owned", 255, 0, 0); return; end
	database:query(dbBuyBusinessCallback, {client, b_marker, id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer}, "SELECT * FROM business WHERE owner = ?", account.name);
end);

addEvent("business.sell", true);
addEventHandler("business.sell", root, function()
	local account = client.account;
	if (not account or account:isGuest()) then return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (owner ~= account.name) then
		if (ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
			database:exec("UPDATE business SET owner = ? WHERE id = ?", "For Sale", id);
			b_marker:setData("b_data", {id, name, "For Sale", cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
			client:outputMessage("Negocios: Voce vendeu o Negocio com Sucesso", 0, 255, 0);
			return;
		else
			client:outputMessage("Negocios: Voce nao e proprietario deste Negocio", 255, 0, 0);
			return;
		end
	end
	database:exec("UPDATE business SET owner = ?, bank = ? WHERE id = ?", "For Sale", 0, id);
	client:giveMoney(tonumber(("%.f"):format(cost / 2)));
	client:giveMoney(bank);
	b_marker:setData("b_data", {id, name, "For Sale", cost, payout, payout_time, payout_otime, payout_unit, 0});
	client:outputMessage("Negocios: Voce vendeu o Negocio com Sucesso, todo o dinheiro no banco foi pago para você.", 0, 255, 0);
end);

addEvent("business.deposit", true);
addEventHandler("business.deposit", root, function(amount)
	local account = client.account;
	if (not account or account:isGuest()) then return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (not tonumber(amount)) then client:outputMessage("Negocios: Quantidade invalida", 255, 0, 0); return; end
	if (owner ~= account.name) then
		client:outputMessage("Negocios: Você não é o proprietário deste Negocio", 255, 0, 0);
		return;
	end
	if (client.money < amount) then
		client:outputMessage("Negocios: Você não tem dinheiro suficiente", 255, 0, 0);
		return;
	end
	database:exec("UPDATE business SET bank = ? WHERE id = ?", bank + amount, id);
	client:takeMoney(amount);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank + amount, timer});
	client:outputMessage("Negocios: Você depositou com sucesso $"..amount.." no negócio", 0, 255, 0);
end);

addEvent("business.withdraw", true);
addEventHandler("business.withdraw", root, function(amount)
	local account = client.account;
	if (not account or account:isGuest()) then return; end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (not tonumber(amount)) then client:outputMessage("Negocios: Quantidade Invalida", 255, 0, 0); return; end
	if (owner ~= account.name) then
		client:outputMessage("Negocios: Voce nao e o dono do negocio", 255, 0, 0);
		return;
	end
	if (bank < amount) then
		client:outputMessage("Negocios: Voce nao tem muito no Banco de Negocios", 255, 0, 0);
		return;
	end
	database:exec("UPDATE business SET bank = ? WHERE id = ?", bank - amount, id);
	client:giveMoney(amount);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank - amount, timer});
	client:outputMessage("Negocios: Voce retirou com sucesso $"..amount.." do Negocio", 0, 255, 0);
end);

addEvent("business.setName", true);
addEventHandler("business.setName", root, function(new_name)
	if (new_name == "" or #new_name > 30) then
		client:outputMessage("Negocios: Valor Invalido", 255, 0, 0);
		return;
	end
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET name = ? WHERE id = ?", new_name, id);
	b_marker:setData("b_data", {id, new_name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
	client:outputMessage("Negocios: Voce renomeou com sucesso o negocio", 0, 255, 0);
end);

addEvent("business.setOwner", true);
addEventHandler("business.setOwner", root, function(new_owner)
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET owner = ? WHERE id = ?", new_owner, id);
	b_marker:setData("b_data", {id, name, new_owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
	client:outputMessage("Negocios: Voce mudou com sucesso o proprietario da empresa", 0, 255, 0);
end);

addEvent("business.setCost", true);
addEventHandler("business.setCost", root, function(amount)
	if (tonumber(amount) == nil) then
		client:outputMessage("Negocios: Valor Invalido", 255, 0, 0);
		return;
	end
	amount = tonumber(amount);
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET cost = ? WHERE id = ?", amount, id);
	b_marker:setData("b_data", {id, name, owner, amount, payout, payout_time, payout_otime, payout_unit, bank, timer});
	client:outputMessage("Negocios: Voce mudou com sucesso o custo do negocio", 0, 255, 0);
end);

addEvent("business.setBank", true);
addEventHandler("business.setBank", root, function(amount)
	if (tonumber(amount) == nil) then
		client:outputMessage("Negocios: Valor Invalido", 255, 0, 0);
		return;
	end
	amount = tonumber(amount);
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	database:exec("UPDATE business SET bank = ? WHERE id = ?", amount, id);
	b_marker:setData("b_data", {id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, amount, timer});
	client:outputMessage("Negocios: Voce mudou com sucesso a quantidade de banco comercial", 0, 255, 0);
end);

addEvent("business.destroy", true);
addEventHandler("business.destroy", root, function()
	if (not ACL.hasObjectPermissionTo(client, "function.banPlayer")) then
		client:outputMessage("Negocios: Voce nao tem acesso para fazer isso", 255, 0, 0);
		return;
	end
	local b_marker = client:getMarker();
	if (not isElement(b_marker)) then return; end
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	if (timer and timer:isValid()) then timer:destroy() end
	database:exec("DELETE FROM business WHERE id = ?", id);
	b_marker:destroyAttachedBlips();
	b_marker:destroy();
	client:outputMessage("Negocios: Voce destruiu com sucesso o negocio", 0, 255, 0);
	triggerClientEvent(client, "business.hideInstructions", client);
	database:query(dbReOrderBusinessesCallback,  "SELECT * FROM business");
end);

function dbBuyBusinessCallback(query_handle, source, b_marker, id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer)
	local sql = query_handle:poll(0);
	if (#sql == settings.owned_businesses) then
		source:outputMessage("Negocios: Voce ja possui "..#sql.." negocios , que e o valor maximo", 255, 0, 0);
		return;
	end
	local money = source.money;
	if (money < cost) then source:outputMessage("Negocios: Voce nao tem dinheiro suficiente", 255, 0, 0) return end
	database:exec("UPDATE business SET owner = ? WHERE id = ?", source.account.name, id);
	source:takeMoney(cost);
	source:outputMessage("Negocios: Voce comprou com sucesso neste negocio", 0, 255, 0);
	b_marker:setData("b_data", {id, name, source.account.name, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
end

function dbReOrderBusinessesCallback(query_handle)
	local sql = query_handle:poll(0);
	if (sql and #sql > 0) then
		for index, row in ipairs(sql) do
			database:exec("UPDATE business SET id = ? WHERE id = ?", index, row["id"]);
		end
		for index, b_marker in ipairs(Element.getAllByType("marker", resourceRoot)) do
			database:query(dbUpdateBusinessesIDsCallback, {b_marker, index}, "SELECT id FROM business WHERE id = ?", index);
		end
	end
end

function dbUpdateBusinessesIDsCallback(query_handle, b_marker, index)
	local sql = query_handle:poll(0);
	local b_data = b_marker:getData("b_data");
	local id, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer = unpack(b_data);
	b_marker:setData("b_data", {index, name, owner, cost, payout, payout_time, payout_otime, payout_unit, bank, timer});
end

function Element:destroyAttachedBlips()
	if (not self) then return; end
	for index, element in pairs(self:getAttachedElements()) do
		if (element and element.isElement) then
			element:destroy();
		end
	end
end

addEvent("business.getSettings", true);
addEventHandler("business.getSettings", root, function()
	triggerClientEvent(source, "business.getSettings", source, settings);
end);

ui.client

gui = {};
local screen_width, screen_height = GuiElement.getScreenSize();

addEventHandler("onClientResourceStart", resourceRoot, function()
	-- Create Business Window
	gui.cb = {text = {}, edit = {}, button = {}};

	local width, height = 511, 530;
	local x, y = math.floor((screen_width / 2) - (width / 2)), math.floor(screen_height/2 - height/2);

	gui.cb.window = dxWindow(x, y, width, height, "Criar Négocios    -    (Traduzido por +YuMi~')");
		gui.cb.window.visible = false;

	gui.cb.text.info = dxText(0, 25, 511, 51, "Colete as coordenadas e insira os dados para criar o negócio", gui.cb.window);
	gui.cb.text.posx = dxText(10, 75, 101, 20, "Posição X", gui.cb.window);
	gui.cb.text.posy = dxText(140, 75, 101, 20, "Posição Y", gui.cb.window);
	gui.cb.text.posz = dxText(270, 75, 101, 20, "Posição Z", gui.cb.window);
	gui.cb.text.intdim = dxText(400, 75, 101, 20, "Interior,Dimensão", gui.cb.window);
		gui.cb.text.info:setAlignX("center");
		gui.cb.text.info:setAlignY("center");
		gui.cb.text.info:setColor(0, 173, 0);
		gui.cb.text.posx:setAlignX("center");
		gui.cb.text.posx:setAlignY("center");
		gui.cb.text.posy:setAlignX("center");
		gui.cb.text.posy:setAlignY("center");
		gui.cb.text.posz:setAlignX("center");
		gui.cb.text.posz:setAlignY("center");
		gui.cb.text.intdim:setAlignX("center");
		gui.cb.text.intdim:setAlignY("center");
		
	gui.cb.edit.intdim = dxEditField(400, 105, 101, 21, "", gui.cb.window);
	gui.cb.edit.posx = dxEditField(10, 105, 101, 21, "", gui.cb.window);
	gui.cb.edit.posy = dxEditField(140, 105, 101, 21, "", gui.cb.window);
	gui.cb.edit.posz = dxEditField(270, 105, 101, 21, "", gui.cb.window);
		gui.cb.edit.intdim:setReadOnly(true);
		gui.cb.edit.posx:setReadOnly(true);
		gui.cb.edit.posy:setReadOnly(true);
		gui.cb.edit.posz:setReadOnly(true);

	gui.cb.button.pickup = dxButton(60, 145, 391, 41, "Pegar Cordenadas", gui.cb.window);
		gui.cb.button.pickup:setColor(125, 0, 0);

	gui.cb.text.name = dxText(0, 225, 141, 31, "Nome do Négocio:", gui.cb.window);
	gui.cb.text.cost = dxText(0, 265, 141, 31, "Custo do Négocio:", gui.cb.window);
	gui.cb.text.payout = dxText(0, 305, 141, 31, "Pagamento do Négocio:", gui.cb.window);
	gui.cb.text.payout_time = dxText(0, 345, 141, 31, "Tempo de Pagamento:", gui.cb.window);
		gui.cb.text.name:setAlignX("center");
		gui.cb.text.name:setAlignY("center");
		gui.cb.text.cost:setAlignX("center");
		gui.cb.text.cost:setAlignY("center");
		gui.cb.text.payout:setAlignX("center");
		gui.cb.text.payout:setAlignY("center");
		gui.cb.text.payout_time:setAlignX("center");
		gui.cb.text.payout_time:setAlignY("center");

	gui.cb.edit.name = dxEditField(160, 225, 281, 31, "", gui.cb.window);
	gui.cb.edit.cost = dxEditField(160, 265, 281, 31, "", gui.cb.window);
	gui.cb.edit.payout = dxEditField(160, 305, 281, 31, "", gui.cb.window);
	gui.cb.edit.payout_time = dxEditField(160, 345, 191, 31, "", gui.cb.window);

	gui.cb.unit = dxComboBox(360, 345, 141, nil, gui.cb.window);
		gui.cb.unit:addItem("Segundos");
		gui.cb.unit:addItem("Minutos");
		gui.cb.unit:addItem("Horas");
		gui.cb.unit:addItem("Dias");
		gui.cb.unit:setSelected(2);

	gui.cb.button.clear = dxButton(10, 490, 111, 31, "Limpar", gui.cb.window);
	gui.cb.button.cancel = dxButton(390, 490, 111, 31, "Cancelar", gui.cb.window);
	gui.cb.button.create = dxButton(140, 490, 231, 31, "Criar Négocio", gui.cb.window);
		gui.cb.button.clear:setColor(125, 0, 0);
		gui.cb.button.cancel:setColor(125, 0, 0);
		gui.cb.button.create:setColor(125, 0, 0);

	gui.cb.button.pickup.func = function(state)
		if (state ~= "up") then return; end
		local pos = localPlayer.position;
		local int = localPlayer.interior;
		local dim = localPlayer.dimension;
		local x, y, z = string.format("%.2f", pos.x), string.format("%.2f", pos.y), string.format("%.2f", pos.z);
		gui.cb.edit.posx.text = x;
		gui.cb.edit.posy.text = y;
		gui.cb.edit.posz.text = z;
		gui.cb.edit.intdim.text = int..","..dim;
	end

	gui.cb.button.clear.func = function(state)
		if (state ~= "up") then return; end
		gui.cb.edit.posx.text = "";
		gui.cb.edit.posy.text = "";
		gui.cb.edit.posz.text = "";
		gui.cb.edit.intdim.text = "";
		gui.cb.edit.name.text = "";
		gui.cb.edit.cost.text = "";
		gui.cb.edit.payout.text = "";
		gui.cb.edit.payout_time.text = "";
		gui.cb.unit:setSelected(2);
	end

	gui.cb.button.cancel.func = function(state)
		if (state ~= "up") then return; end
		gui.cb.window.visible = false;
		showCursor(false);
	end

	gui.cb.button.create.func = function(state)
		if (state ~= "up") then return; end
		local x, y, z, intdim = gui.cb.edit.posx.text, gui.cb.edit.posy.text, gui.cb.edit.posz.text, gui.cb.edit.intdim.text;
		local name = gui.cb.edit.name.text;
		local cost = gui.cb.edit.cost.text;
		local payout = gui.cb.edit.payout.text;
		local payout_time = gui.cb.edit.payout_time.text;
		-- local payout_unit = gui.cb.unit:getItemText(gui.cb.unit:getSelected());
		if x ~= "" and name ~= "" and cost ~= ""  and tonumber(cost) ~= nil and payout ~= "" and tonumber(payout) ~= nil and payout_time ~= "" and tonumber(payout_time) ~= nil and tonumber(payout_time) > 0 and tonumber(cost) > 0 and tonumber(payout) > 0 then
			if #name > 30 then outputMessage("BUSINESS: Business name must not be more than 30 characters", 255, 0, 0); return; end
			local zone = getZoneName(tonumber(x), tonumber(y), tonumber(z), false);
			if zone == "Unknown" then zone = "the middle of no where" end;
			local interior = tonumber(gettok(intdim, 1, ","));
			local dimension = tonumber(gettok(intdim, 2, ","));
			dxPrompt("Tem certeza de que deseja criar o negócio '"..name.."' em "..zone, function()
				Sound("files/cash.mp3", false);
				showCursor(false);
				local x, y, z, intdim = gui.cb.edit.posx.text, gui.cb.edit.posy.text, gui.cb.edit.posz.text, gui.cb.edit.intdim.text;
				local interior, dimension = unpack(split(intdim, ","));
				local name = gui.cb.edit.name.text;
				local cost = gui.cb.edit.cost.text;
				local payout = gui.cb.edit.payout.text;
				local payout_time, payout_unit = gui.cb.edit.payout_time.text, gui.cb.unit:getItemText(gui.cb.unit:getSelected());
				triggerServerEvent("business.createBusiness", root, x, y, z, interior, dimension, name, cost, payout, payout_time, payout_unit);
				gui.cb.button.clear.func("up");
				gui.cb.window.visible = false;
			end);
		else
			outputMessage("BUSINESS: The data isn't correct, please correct it", 255, 0, 0);
		end
	end

	-- Business Window
	gui.b = {label = {}, tab = {}, edit = {}, button = {}};

	local width, height = 524, 300;
	local x = screen_width/2 - width/2;
	local y = screen_height/2 - height/2;

	gui.b.window = dxWindow(x, y, width, height, "Business Name", false);
		gui.b.window.visible = false;
		gui.b.window:setTitleColor(125, 0, 0);

	gui.b.tab_panel = dxTabPanel(5, 50, 511, 231, gui.b.window);

	gui.b.tab.info = dxTab("Informações", gui.b.tab_panel);

	gui.b.label.id = dxText(10, 20, 81, 16, "ID: #", gui.b.tab.info);
	gui.b.label.name = dxText(10, 70, 241, 16, "Name:", gui.b.tab.info);
	gui.b.label.owner = dxText(10, 120, 231, 16, "Owner: ", gui.b.tab.info);
	gui.b.label.cost = dxText(10, 170, 191, 16, "Cost: ", gui.b.tab.info);
	gui.b.label.payout = dxText(290, 20, 211, 16, "Payout: ", gui.b.tab.info);
	gui.b.label.payout_time = dxText(290, 70, 211, 16, "Payout Time:", gui.b.tab.info);
	gui.b.label.location = dxText(290, 120, 211, 16, "Location:", gui.b.tab.info);
	gui.b.label.bank = dxText(290, 170, 211, 16, "Bank:", gui.b.tab.info);
		gui.b.label.id:setAlignX("left", false);
		gui.b.label.id:setAlignY("center");
		gui.b.label.name:setAlignX("left", false);
		gui.b.label.name:setAlignY("center");
		gui.b.label.owner:setAlignX("left", false);
		gui.b.label.owner:setAlignY("center");
		gui.b.label.cost:setAlignX("left", false);
		gui.b.label.cost:setAlignY("center");
		gui.b.label.payout:setAlignX("left", false);
		gui.b.label.payout:setAlignY("center");
		gui.b.label.payout_time:setAlignX("left", false);
		gui.b.label.payout_time:setAlignY("center");
		gui.b.label.location:setAlignX("left", false);
		gui.b.label.location:setAlignY("center");
		gui.b.label.bank:setAlignX("left", false);
		gui.b.label.bank:setAlignY("center");

	gui.b.tab.action = dxTab("Ações", gui.b.tab_panel);

	gui.b.button.buy = dxButton(10, 10, 101, 31, "Comprar", gui.b.tab.action);
	gui.b.button.sell = dxButton(10, 60, 101, 31, "Vender", gui.b.tab.action);
	gui.b.button.deposit = dxButton(10, 110, 101, 31, "Deposito", gui.b.tab.action);
	gui.b.button.withdraw = dxButton(10, 160, 101, 31, "Retirar", gui.b.tab.action);
	gui.b.button.set_name = dxButton(390, 10, 101, 31, "Mudar Nome", gui.b.tab.action);
	gui.b.button.set_owner = dxButton(390, 60, 101, 31, "Mudar Dono", gui.b.tab.action);
	gui.b.button.set_cost = dxButton(390, 110, 101, 31, "Mudar Custo", gui.b.tab.action);
	gui.b.button.set_bank = dxButton(390, 160, 101, 31, "Mudar Banco", gui.b.tab.action);

	gui.b.button.destroy = dxButton(130, 155, 241, 41, "Deletar", gui.b.tab.action);
	gui.b.button.x = dxButton(480, 25, 31, 31, "X", gui.b.window);
	

	gui.b.button.x.func = function(state)
		if (state ~= "up") then return; end
		gui.b.window.visible = false;
		showCursor(false);
	end

	gui.b.button.buy.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Tem certeza de que quer comprar este negócio?", function()
			triggerServerEvent("business.buy", root);
			gui.b.window.visible = false;
			showCursor(false);
		end);
	end

	gui.b.button.sell.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Tem certeza de que quer vender esse negócio?", function()
			triggerServerEvent("business.sell", root);
			gui.b.window.visible = false;
			showCursor(false);
		end);
	end;

	gui.b.button.deposit.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Digite o valor a depositar", function(amount)
			amount = tonumber(amount);
			if (not amount) then outputMessage("You must enter a correct amount", 255, 0, 0); return; end
			triggerServerEvent("business.deposit", root, amount);
			gui.b.window.visible = false;
			showCursor(false);
		end, nil, true);
	end;

	gui.b.button.withdraw.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Digite o valor a retirar", function(amount)
			amount = tonumber(amount);
			if (not amount) then outputMessage("You must enter a correct amount", 255, 0, 0); return; end
			triggerServerEvent("business.withdraw", root, amount);
			gui.b.window.visible = false;
			showCursor(false);
		end, nil, true);
	end;

	gui.b.button.set_name.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Digite o novo nome", function(name)
			if (name == "" or name:len() < 4) then outputMessage("Name must be at least 4 characters long", 255, 0, 0); return; end
			triggerServerEvent("business.setName", root, name);
			gui.b.window.visible = false;
			showCursor(false);
		end, nil, true);
	end;

	gui.b.button.set_owner.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Digite o nome da conta do novo proprietário", function(owner)
			if (owner == "") then outputMessage("Owner's account name must be at least 1 character long", 255, 0, 0); return; end
			triggerServerEvent("business.setOwner", root, owner);
			gui.b.window.visible = false;
			showCursor(false);
		end, nil, true);
	end;

	gui.b.button.set_cost.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Digite o valor para definir o custo", function(amount)
			amount = tonumber(amount);
			if (not amount) then outputMessage("You must enter a correct amount", 255, 0, 0); return; end
			triggerServerEvent("business.setCost", root, amount);
			gui.b.window.visible = false;
			showCursor(false);
		end, nil, true);
	end;

	gui.b.button.set_bank.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Digite o valor para definir o banco", function(amount)
			amount = tonumber(amount);
			if (not amount) then outputMessage("You must enter a correct amount", 255, 0, 0); return; end
			triggerServerEvent("business.setBank", root, amount);
			gui.b.window.visible = false;
			showCursor(false);
		end, nil, true);
	end;

	gui.b.button.destroy.func = function(state)
		if (state ~= "up") then return; end
		dxPrompt("Tem certeza de que quer destruir o negócio", function()
			triggerServerEvent("business.destroy", root);
			gui.b.window.visible = false;
			showCursor(false);
		end);
	end;
end);

 

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...