Jump to content

Problema ao Efetuar compra - Script de Inventario/Loja


Recommended Posts

Bom, fiz umas gambearras em alguns scripts inacabados de inventário e acabou funcionando bem, porém assim que é efetuada a compra de algo como arma, comida, bebida, o item vai normalmente para o inventário, porém não desconta o dinheiro do personagem, e não apresenta nenhum erro no debugscritp, o que me fez vir aqui, visto que não sei o motivo do bug, poderiam me ajudar ? o Script está abaixo

 

Server

local shopPed = {}

addEventHandler("onResourceStart", root, function()
	connection = dbConnect("sqlite", "database.db")
	dbExec(connection, "CREATE TABLE IF NOT EXISTS shops (id INTEGER PRIMARY KEY AUTOINCREMENT, type INTEGER, pos TEXT, skin INTEGER, name TEXT)")
	loadShop()
end)

------------------------------

-- // Shop betöltése

------------------------------

function loadShop()
	local query = dbQuery( connection, "SELECT * FROM shops")
	local result, numrows = dbPoll(query, -1)
	if (result and numrows > 0) then
		for index, shopTable in pairs(result) do
			local position = fromJSON(shopTable["pos"])
			shopPed[shopTable["id"]] = createPed(shopTable["skin"], position[1], position[2], position[3])
			if isElement(shopPed[shopTable["id"]]) then 
				setPedRotation(shopPed[shopTable["id"]], position[6] or 0)
				setElementDimension(shopPed[shopTable["id"]], position[4])
				setElementInterior(shopPed[shopTable["id"]], position[5])
				setElementFrozen(shopPed[shopTable["id"]], true)
				setElementData(shopPed[shopTable["id"]], "shop >> owner", shopTable["owner"])
				setElementData(shopPed[shopTable["id"]], "shop >> type", shopTable["type"])	
				setElementData(shopPed[shopTable["id"]], "shop >> npc", true)
				setElementData(shopPed[shopTable["id"]], "shop >> id", shopTable["id"])
				setElementData(shopPed[shopTable["id"]], "ped >> death", true)
				setElementData(shopPed[shopTable["id"]], "name:tags", "PED")
				setElementData(shopPed[shopTable["id"]], "Ped:Name",shopTable["name"])
			end
		end
	end
end

------------------------------100

-- // Shop törlése

------------------------------

addCommandHandler("delshop",
function(playerSource, cmd)
	if isObjectInACLGroup("user."..getAccountName(getPlayerAccount(playerSource)), aclGetGroup("Console")) then
		local x, y, _ = getElementPosition(playerSource)
		local shopShape = createColCircle ( x, y, 3 )
		local shopNumber = 0
		for _,v in ipairs(getElementsWithinColShape ( shopShape, "ped" ) ) do
				local ShopID = getElementData(v,"shop >> id") or 0
				shopNumber = shopNumber + 1
				destroyElement(shopShape)
				if ShopID >= 1 then 
					destroyElement(v)
				end
				dbPoll ( dbQuery( connection, "DELETE FROM shops WHERE id = '?'", ShopID), 0 )
				outputChatBox("#7cc576[SKY~Itens] #ffffffLoja excluída com sucesso. ID: #F7CA18"..ShopID, playerSource, 255, 255, 255, true)
				return
		end
		if(shopNumber == 0) then
			destroyElement(shopShape)
			outputChatBox("#D24D57[SKY~Itens] #ffffffNenhuma loja perto de você.", playerSource, 255, 255, 255, true)
		end
	end
end)

------------------------------

-- // Shop létrehozása

------------------------------

function createShop(element, cmd, skin, type, name)
	if isObjectInACLGroup("user."..getAccountName(getPlayerAccount(element)), aclGetGroup("Console")) then
		if not (skin) or not type or not name and tonumber(type) < 0 and tonumber(type) < #shopItemList+1 then
			outputChatBox("#7cc576[use]:#ffffff /".. cmd .." [Olhar NPC] [tipo] [NPC nome]", element,255,255,255,true)
			outputChatBox("#ffffffTípus: 1: Food Individual 2: Loja de bebidas 3: Loja geral 4: eletrônica 5: Loja de passatempo 6: Burger Shot 7: Cluck'n'Bell 8: Pizza 9: Suprimentos de mineração 10: Suprimentos para lenhadores 11: Vajdaság 12: Loja da máfia 13: pescaria 14: loja Arma", element,255,255,255,true)
			outputChatBox("15: Lojas de tabaco 16: Mudas ilegais 17: artigos de papelaria 18: Armazem de peixe 19: farmácia 20: pet Store 21: Mascaras", element,255,255,255,true)
		return
		end
		local x, y, z = getElementPosition(element)
		local dim = getElementDimension(element)
		local int = getElementInterior(element)
		local rot = getPedRotation(element)
		
		local position = toJSON( {x, y, z, dim, int, rot})
		local insertQuery = dbQuery( connection, "INSERT INTO shops (type, pos, skin, name) VALUES(?, ?, ?, ?)", tonumber(type), position, skin, name)
		local insertQueryData, _, insertID = dbPoll ( insertQuery, -1 )
		if (insertQueryData) then
			shopPed[insertID] = createPed(skin,x,y,z)
			setPedRotation(shopPed[insertID], rot)
			setElementDimension(shopPed[insertID], dim)
			setElementInterior(shopPed[insertID], int)
			setElementFrozen(shopPed[insertID], true)
			setElementData(shopPed[insertID], "shop >> type", type)	
			setElementData(shopPed[insertID], "ped >> death", true)
			setElementData(shopPed[insertID], "shop >> npc", true)
			setElementData(shopPed[insertID], "shop >> id", insertID)
			setElementData(shopPed[insertID], "name:tags", "NPC")
			setElementData(shopPed[insertID], "Ped:Name", name)
		end
	end
end
addCommandHandler('addshop', createShop)
addCommandHandler('createshop', createShop)

------------------------------

-- // Item adás

------------------------------

function buyItem(player, item, value, count, amount, duty)
	money = getPlayerMoney(player) or 0
	if money >= amount then
		if exports.cbr_inventario:giveItem(player, item, value, count, amount, true) then 
			takePlayerMoney(player, amount)
			outputChatBox("#ffffffVocê conseguiu um #32B3FF".. exports.cbr_inventario:getItemName(item).." #ffffffobjeto.",player,255,0,0,true)
		end
    else
		outputChatBox("#ffffffVocê não possui (#32B3FF".. amount .." #ffffff)$.",player,255,255,255,true)
	end
end
addEvent("btcMTA->#buyItem", true)
addEventHandler("btcMTA->#buyItem", getRootElement(), buyItem)



function kit_vida(source)
	setElementHealth(source, 100)
end
addEvent("DS:kit_vida", true)
addEventHandler("DS:kit_vida", root, kit_vida)

function kit_reparo(source)
	
end
addEvent("DS:kit_reparo", true)
addEventHandler("DS:kit_reparo", root, kit_reparo)

Client

local monitorSize = {guiGetScreenSize()} -- // Lekérdezi a monitor méreteit
local panelData = {550, 510} -- // Panel szélessége és magassága
local panelX, panelY = monitorSize[1]/2-panelData[1]/2, monitorSize[2]/2-panelData[2]/2 -- // X, Y Pozició
local font = dxCreateFont("files/Calibri.ttf", 10) -- // Font
local showShop = false -- // A panel felvan-e rajzolva
local currentShop = 0 -- // Jelenlegi shop
local scroll = 0 -- // Görgetés
local count = 1 -- // Darabszám
local maxDraw = 8 -- // Maximum kirajzolás
local createdGuis = {} -- // A guik táblázata
local shopElement = nil --// Shop Element

------------------------------

-- // Klikkelés

------------------------------

addEventHandler('onClientClick', root, function (button, state, _, _, _, _, _, element)
	if button == 'right' and state == 'down' and element and not showShop then 
			if getElementData(element, 'shop >> npc') or false then 
				local x, y, z = getElementPosition(localPlayer)
				local elementX, elementY, elementZ = getElementPosition(element)
				if getDistanceBetweenPoints3D(x, y, z, elementX, elementY, elementZ) <= 5 then 
					if tonumber(getElementData(element,"shop:type") or 1) == 14 and not exports['cbr_inventario']:hasItem(localPlayer, 112) then
						outputChatBox('#D24D57[FVR] #ffffffVocê não tem uma licença de arma!', 255, 255, 255, true)
						return 
					end
					getShopDatas(element)
				else
					outputChatBox('#D24D57[SKY~Itens] #ffffffVocê não pode falar com o vendedor sobre essa bagunça!', 255, 255, 255, true)
				end
			end
	elseif button == 'left' and state == 'down' and showShop then 
		local elem = 0
		for index, value in ipairs(shopItemList[currentShop].itemList) do
			if (index > scroll and elem < maxDraw) then
				elem = elem + 1
				if isMouseInPosition (panelX+panelData[1]-155, panelY-25+elem*57+56/2-20/2, 77, 20) then
					if guiEditSetCaretIndex(createdGuis[elem], string.len(guiGetText(createdGuis[elem]))) then
						guiBringToFront(createdGuis[elem])
					end
				elseif isMouseInPosition (panelX+panelData[1]-45, panelY-25+elem*57+56/2-32/2, 32, 32) then 	
					if isElement(createdGuis[elem]) then
						count = guiGetText(createdGuis[elem])
						if tonumber(count) then 
							if count == '' or count == ' ' then 
								count = 1
							end		
						else
							count = 1
							guiSetText(createdGuis[elem], '')
						end
					else
						count = 1
					end
					
					setTimer(function(value, count)
							triggerServerEvent('btcMTA->#buyItem', localPlayer, localPlayer, value[2], value[3], math.floor(count), value[5]* math.floor(count))
						end, 100, 1, value, count)
				end
			end
		end
	end
end)

------------------------------

-- // destoy and draw funkciók

------------------------------

function getShopDatas(element)
	showShop = not showShop 
	if showShop then
		currentShop = tonumber(getElementData(element, 'shop >> type'))
		createGui('create')
		shopElement = element
		addEventHandler('onClientRender', root, createShopPanel, true, 'low-5')
		bindKey('backspace', 'down', desroyPanel)
	else
		createGui('destroy')
		removeEventHandler('onClientRender', root, createShopPanel)
		unbindKey('backspace', 'down', desroyPanel)
	end
end

function desroyPanel ()
	removeEventHandler('onClientRender', root, createShopPanel) 
	showShop = false 
	createGui('destroy')
	count = 1
	scroll = 0
	currentShop = 0
	unbindKey('backspace', 'down', desroyPanel)
end

------------------------------

-- // Edit létrehozása és törlése

------------------------------

function createGui(type)
	if tostring(type) == "destroy" then
		for index, value in ipairs(shopItemList[currentShop].itemList) do
			if isElement(createdGuis[index]) then
				destroyElement(createdGuis[index])
			end
		end
	else
		for index, value in ipairs(shopItemList[currentShop].itemList) do
			createdGuis[index] = guiCreateEdit(-1000, -1000, 0, 0, "", false)
			guiSetText(createdGuis[index], value[4])
			guiEditSetMaxLength(createdGuis[index], 4)	
		end
	end
end

------------------------------

-- // felrajzolás

------------------------------

function createShopPanel()
	local x, y, z = getElementPosition(localPlayer)
	local elementX, elementY, elementZ = getElementPosition(shopElement)
	if (getDistanceBetweenPoints3D(x, y, z, elementX, elementY, elementZ) > 5 ) then desroyPanel() return end
	dxDrawRectangle(panelX, panelY, panelData[1], panelData[2], tocolor(0, 0, 0, 150)) -- // background
	dxDrawRectangle(panelX, panelY, panelData[1], 25, tocolor(0, 0, 0, 200))
	dxDrawText("Para sair, pressione a tecla #D24D57'backspace'", panelX+panelData[1]/2, (panelY+panelData[1]-65)+25/2, panelX+panelData[1]/2, (panelY+panelData[1]-65)+25/2, tocolor(255, 255, 255, 255), 1, font, 'center', 'center', false, false, false, true)
	dxDrawText('#7cc576SKY#FFFFFFMTA - #7cc576'..shopItemList[currentShop].name, panelX+panelData[1]/2, panelY+25/2, panelX+panelData[1]/2, panelY+25/2, tocolor(255, 255, 255, 255), 1, font, 'center', 'center', false, false, false, true)
	local elem = 0
	for index, value in ipairs(shopItemList[currentShop].itemList) do
		if (index > scroll and elem < maxDraw) then
			elem = elem + 1
			dxDrawRectangle(panelX+5, panelY-25+elem*57, panelData[1]-10, 56, tocolor(0, 0, 0, 150))	
			dxDrawRectangle(panelX+10, panelY-17+elem*57, 40, 40, tocolor(255, 255, 255, 60))
			dxDrawImage(panelX+10+2, panelY-17+elem*57+2, 36, 36, getItemImage(value[2]))
			if isElement(createdGuis[elem]) then
				count = guiGetText(createdGuis[elem])
				if tonumber(count) then 
					if count == '' or count == ' ' then 
						count = 1
					end		
				else
					count = 1
					guiSetText(createdGuis[elem], '')
				end
			else
				count = 1
			end
			dxDrawText('Nome: #7cc576'.. value[1] .. '\n#ffffffPreço: #7cc576R$: '.. formatMoney(value[5]*math.floor(count))..'', panelX+10+2+43, panelY-25+elem*57+2, 36, 36, tocolor(255, 255, 255, 255), 1, font, 'left', 'top', false, false, false, true)
			dxDrawText('Quantidade: #7cc576'.. math.floor(count), panelX+panelData[1]-155, panelY-25+elem*57+56/2, 36, panelY-25+elem*57+56/2, tocolor(255, 255, 255, 255), 1, font, 'left', 'center', false, false, false, true)
			if isMouseInPosition (panelX+panelData[1]-45, panelY-25+elem*57+56/2-32/2, 32, 32) then 
				dxDrawImage(panelX+panelData[1]-45, panelY-25+elem*57+56/2-32/2, 32, 32, 'files/shopIcon.png', 0, 0, 0, tocolor(124, 197, 118, 255))
			else
				dxDrawImage(panelX+panelData[1]-45, panelY-25+elem*57+56/2-32/2, 32, 32, 'files/shopIcon.png', 0, 0, 0, tocolor(255, 255, 255, 255))
			end
		end
	end
	if #shopItemList[currentShop].itemList > maxDraw then 
		dxDrawRectangle(panelX+panelData[1]+5, panelY+25, 10, panelData[2]-25, tocolor(0, 0, 0, 150)) -- // scrole backfround
		dxDrawRectangle(panelX+panelData[1]+5+1, panelY+25+1+((panelData[2]-25)/(#shopItemList[currentShop].itemList-maxDraw+1))*scroll, 8, (panelData[2]-25)/(#shopItemList[currentShop].itemList-maxDraw+1)-2, tocolor(124,197,118, 255)) -- // Scros line
	end
end

------------------------------

-- // Görgetés

------------------------------

bindKey("mouse_wheel_down", "down", 
	function() 
		if showShop then
			if scroll < #shopItemList[currentShop].itemList - maxDraw then
				scroll = scroll + 1	
			end
		end
	end
)

bindKey("mouse_wheel_up", "down", 
	function() 
		if showShop then
			if scroll > 0 then
				scroll = scroll - 1		
			end
		end
	end
)

------------------------------

-- // Egyéb

------------------------------

function formatMoney(amount)
	local formatted = tonumber(amount)
	if formatted then
		while true do  
			formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1 %2')
		if (k==0) then
			break
		end
	end
		return formatted
	else
		return amount
	end
end

function isMouseInPosition ( x, y, width, height ) 
    if ( not isCursorShowing ( ) ) then 
        return false 
    end 
  
    local sx, sy = guiGetScreenSize ( ) 
    local cx, cy = getCursorPosition ( ) 
    local cx, cy = ( cx * sx ), ( cy * sy ) 
    if ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) then 
        return true 
    else 
        return false 
    end 
end 

function getItemImage(item)
	return fileExists(":btc_items/files/items/"..item..".png") and ":btc_items/files/items/"..item..".png" or ":btc_items/files/items/0.png"
end

addEventHandler("onClientPedDamage",  getRootElement(), function ()
	if (getElementData(source, "ped >> death")) then
		cancelEvent()
	end
end)

addEventHandler("onClientPlayerStealthKill", getRootElement(), function(targetPlayer) 
	if (getElementType(targetPlayer) == 'ped' and getElementData(targetPlayer, "ped >> death")) then  
		cancelEvent()
	end
end)

 

Edited by Lord Henry
Scripts convertidos de HTML para Lua.
Link to comment

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...