Jump to content

Problem receive profit properties


OrbTanT

Recommended Posts

Well, I edited gaming nerd 's resource to make a system of profit properties after buying it, it' s all working, part of buying and selling, adding the owner in sql, but I 'm not able to hit part of the profit, can anybody help me.

local _setElementData = setElementData
function setElementData ( element, group, value )
	return _setElementData ( element, group, value, true )
end

local propertiesLocs = { }
function createProperties ( x, y, z, value, earnings, owner, forcedId, name )

	local pickup = createPickup ( x, y, z, 3, 1273, 0)
	local blip = createBlip ( x, y, z, 31)
	
	if ( not forcedId or propertiesLocs [ id ] ) then
		id = 0
		while ( propertiesLocs [ id ] ) do
			id = id + 1
		end
	else
		id = forcedId
	end

	propertiesLocs[id] = { }
	propertiesLocs[id].pickup = pickup
	propertiesLocs[id].blip = blip
	propertiesLocs[id].owner = owner or "offline"
	propertiesLocs[id].name = name
	propertiesLocs[id].value = value
	propertiesLocs[id].earnings = earnings
	setBlipVisibleDistance ( propertiesLocs[id].blip, 200 )
	setElementData ( propertiesLocs[id].pickup, "propertieId", id )
	setElementData ( propertiesLocs[id].pickup, "propertieTable", propertiesLocs [ id ] )
	setElementData ( propertiesLocs[id].pickup, "propertieName", propertiesLocs[id].name )
	setElementData ( propertiesLocs[id].pickup, "propertieValue", propertiesLocs[id].value )
	setElementData ( propertiesLocs[id].pickup, "propertieEarnings", propertiesLocs[id].earnings )
	setElementData ( propertiesLocs[id].pickup, "propertieOwner", propertiesLocs[id].owner )
	addEventHandler("onPickupHit", propertiesLocs[id].pickup, onPickupPropertieHit)
	return propertiesLocs[id];
end

function onPickupPropertieHit ( player ) 
	if ( player and isElement ( player ) and getElementType ( player ) == "player" ) then
	local propertieOwner = getPropertieOwner(id)
		if ( propertieOwner ~= getPlayerName(player) ) then
			outputChatBox("[PROPRIEDADES] Digite /comprar . Para comprar esta propriedade!", player, 255, 255, 0, false)
		else
			outputChatBox("[PROPRIEDADES] Digite /vender . Para vender esta propriedade!", player, 255, 255, 0, false)
		end
	end
end

function buyPropertie ( player ) 
	if ( player and isElement ( player ) and getElementType ( player ) == "player" ) then
		for id, data in pairs ( propertiesLocs ) do
			if (isElementWithinPickup(player, propertiesLocs[id].pickup)) then
				local propertieOwner = getPropertieOwner(id)
				if ( propertieOwner ~= getPlayerName(player) ) then
					if (getPlayerMoney(player) >= tonumber(propertiesLocs[id].value)) then
						takePlayerMoney(player, tonumber(propertiesLocs[id].value))
						exports.GWsql:db_exec ( "UPDATE propriedades SET owner=? WHERE id=?", getPlayerName(player), id )
						outputChatBox("Você comprou a propriedade "..propertiesLocs[id].name.." !.", player, 0, 255, 0, false)
						setBlipIcon ( propertiesLocs[id].blip, 32 )
						setElementData(propertiesLocs[id].pickup, "propertieOwner", getPlayerName(player))
					else
						outputChatBox("Dinheiro insuficiente para comprar está propriedade!", player, 255, 255, 0, false)
					end
				else
					return outputChatBox("Você já é dono desta propriedade!", player, 255, 255, 0, false)
				end
			end
		end
	end
end
addCommandHandler("comprar", buyPropertie)

function sellPropertie ( player ) 
	if ( player and isElement ( player ) and getElementType ( player ) == "player" ) then
	
		for id, data in pairs ( propertiesLocs ) do 
		
			if (isElementWithinPickup(player, propertiesLocs[id].pickup)) then
				local propertieOwner = getPropertieOwner(id)
				if ( propertieOwner == getPlayerName(player) ) then
					givePlayerMoney(player, tonumber(propertiesLocs[id].value))
					exports.GWsql:db_exec ( "UPDATE propriedades SET owner=? WHERE id=?", "offline", id )
					outputChatBox("Você vendeu a propriedade "..propertiesLocs[id].name.." !.", player, 0, 255, 0, false)
					setBlipIcon ( propertiesLocs[id].blip, 31 )
					setElementData(propertiesLocs[id].pickup, "propertieOwner", "offline")
				else
					outputChatBox("Esta propriedade não é sua!", player, 255, 255, 0, false)
				end
			end
		end
	end
end
addCommandHandler("vender", sellPropertie)

function getPropertieOwner(id)
	if (id) then
		local result = exports.GWsql:db_query("SELECT owner FROM propriedades WHERE id = '".. id .."' ");
		return result[1]["owner"]
	end
	return nil
end


function isElementWithinPickup(theElement, thePickup)
	if (isElement(theElement) and getElementType(thePickup) == "pickup") then
		local x, y, z = getElementPosition(theElement)
		local x2, y2, z2 = getElementPosition(thePickup)
		if (getDistanceBetweenPoints3D(x2, y2, z2, x, y, z) <= 1) then
			return true
		end
	end
	return false
end

function table.len ( tb ) 
	local c = 0
	for i, v in pairs ( tb ) do
		c = c + 1
	end
	return c
end

addEventHandler( "onResourceStart", resourceRoot, function ( )
	exports.GWsql:db_exec ( "CREATE TABLE IF NOT EXISTS propriedades ( id INT, name VARCHAR(50), value INT, earnings INT, owner VARCHAR(50), x FLOAT, y FLOAT, z FLOAT )" )
	local query = exports.GWsql:db_query ( "SELECT * FROM propriedades" )
	if ( #query == 0 ) then 
		local data = { 
			-- PROPRIEDADES
			{"Four Dragons", 135000, 13500, 2022.261352, 1000.560607, 10.820312},
			{"Sex Shop", 30000, 3000, 2088.078125, 2078.019042, 11.057899},
			{"Snooker Bar", 20000, 2000, 2306.662353, -1643.453735, 14.421875},
			{"Caligula's", 150000, 15000, 2195.359619, 1681.807739, 12.367187},
			{"Lojas Zip", 25000, 2500, 1461.488281, -1137.913330, 24.068649},
			{"Lojas Binco", 20000, 2000, 2248.006591, -1663.412475, 15.469003},
			{"Tatoo Shop", 10000, 1000, 2071.049072, -1776.437011, 13.558420},
			{"Angel Pine Motel", 15000, 1500, -2194.968017, -2257.319091, 30.677650},
			{"Strip Club", 35000, 3500, 2416.866455, -1225.032592, 24.891101},
			{"Verdant Meadows Air Strip", 25000, 2500, 418.121002, 2536.876220, 10.000000},
			{"Emerald Isle", 100000, 10000, 2127.593994, 2370.425537, 10.820300},
			{"The Visage", 125000, 12500, 2022.517944, 1916.684814, 12.339699},
			{"Sprunk Factory", 25000, 2500, 1343.197265, 281.531036, 19.561452},
			{"The Well Stacked Pizza", 20000, 2000, 212.153274, -204.281005, 1.578125},
			{"Lojas Victim", 20000, 2000, 457.403350, -1504.311157, 31.018157},
			{"Camel's Toe", 100000, 10000, 2210.599121, 1285.864501, 10.820300},
			{"Come-a-Lot", 200000, 20000, 2181.033447, 1116.374877, 12.648400},
			{"Autobahn Imports", 40000, 4000, 2201.160888, 1391.220092, 10.820300},
			{"The Royal Casino", 80000, 8000, 2088.149902, 1449.090576, 10.820300},
			{"The Motel", 50000, 5000, 2087.002197, 2175.820800, 10.820300},
			{"Pirates in Mans Pants Hotel", 50000, 5000, 1971.739990, 1623.162963, 12.862400},
			{"Las Venturas Bandits Stadion", 135000, 13500, 1477.944702, 2248.830078, 11.023400},
			{"Xoomer Corporation", 350000, 35000, 270.562896, 1369.568725, 10.585900},
			{"Big Ear Radioteleskop", 15000, 1500, -360.792907, 1593.684448, 76.816497},
			{"The King Ring", 20000, 2000, -143.919296, 1224.510009, 19.899200},
			{"Jays Diner", 20000, 2000, -1941.351074, 2379.801025, 49.694301},
			{"Tee Pee Motel", 25000, 2500, -844.719177, 2746.094726, 46.140899},
			{"The Snakefarm", 20000, 2000, -36.079299, 2349.664306, 24.302600},
			{"Lojas Pro Laps", 20000, 2000, 503.761077, -1361.570556, 16.125158},
			{"Lojas Dider Sachs", 20000, 2000, 451.216705, -1475.865112, 30.720579},
			{"Teatro Cathay", 60000, 6000, 1022.766906, -1124.102783, 23.870800},
			{"Shopping Verona", 115000, 11500, 1129.056884, -1489.081176, 22.768999},
			{"Zero RC Shop", 25000, 2500, -2233.709716, 133.708694, 1035.421020},
			{"Jizzy's Club", 50000, 5000, -2244.912597, 132.652008, 35.320312},
			{"Country Club", 200000, 20000, -2724.392089, -314.795684, 7.186100},
			{"Wang Cars", 90000, 9000, -1957.312377, 302.89239, 35.468799},
			{"Hotel", 110000, 11000, -1754.213867, 960.234680, 24.882799},
			{"Otto's Autos", 90000, 9000, -1660.478149, 1218.463623, 7.250000},
			{"Pink Flamingo Hotel", 50000, 5000, 2010.500610, 1167.573730, 10.820300},
			{"The High Roller Casino", 100000, 10000, 1933.024047, 1345.544677, 9.968799},
			{"Casa de Carnes Las Venturas", 25000, 2500, 2367.984619, 1983.124267, 10.820300},
			{"Las Venturas Casino", 90000, 9000, 2318.662841, 2117.777343, 10.828100},
			{"Starfish Casino", 75000, 7500, 2205.292724, 1900.587646, 10.820300},
			{"Clowns Pocket Casino", 90000, 9000, 2221.172119, 1839.124267, 10.820300},
			{"Tikki Motel", 30000, 3000, 2483.155517, 1527.300048, 11.250399},
			{"Estacionamento Central", 50000, 5000, 2310.416015, 1389.879394, 10.820300},
			{"Supa Save Supermercado", 50000, 5000, -2442.810058, 753.777587, 35.171901},
			{"Tuff Nut Donuts", 20000, 2000, -2766.106201, 788.763916, 52.781299}
			
		}
		outputDebugString ( "SAGWproperties: 0 propriedades -- Generating ".. tostring ( #data ) )
		for i, v in pairs ( data ) do 
			x = { 
				['name'] = v[1],
				['value'] = v[2],
				['earnings'] = v[3],
				['x'] = v[4],
				['y'] = v[5],
				['z'] = v[6],
				['owner'] = "offline"
			}
			
			query[i] = x;
			
			exports.GWsql:db_exec ( "INSERT INTO propriedades ( id, name, value, earnings, owner, x, y, z ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )",
				tostring ( i ), x['name'], tostring ( x['value'] ), tostring ( x['earnings'] ), "offline", tostring ( x['x'] ), tostring ( x['y'] ), tostring ( x['z'] ));
		end
	end 
	
	for i, v in pairs ( query ) do
		local id, name, value, earnings, owner, x, y, z = tonumber ( v['id'] ), v['name'],  v['value'],  v['earnings'], v['owner'], tonumber ( v['x'] ), tonumber ( v['y'] ), tonumber ( v['z'] )
		createProperties ( x, y, z, value, earnings, owner, id, name )
	end
end )

function sendPropertieEarning ( ) 
	local playerProperties = { }
	for i, v in pairs ( propertiesLocs ) do 
		if (not playerProperties[v.lucre]) then
			playerProperties [ v.lucre ] = 0
		else
			playerProperties [ v.lucre ] = playerProperties [ v.lucre ] + playerProperties [ v.lucre ]
		end
	end 

	for i, v in pairs ( getElementsByType ( 'player' ) ) do 
		for n, k in pairs (playerProperties) do
			local g = getPlayerName ( v )
			local owner = k.owner
			local lucre = tonumber(k.lucre)
			if ( g == owner ) then 
				givePlayerMoney ( getPlayerFromName(g), tonumber(lucre) )
				outputChatBox ( "[PROPRIEDADES]: Você recebeu $"..tostring(lucre).."$ de suas propriedades", getPlayerFromName(v), 0, 255, 0 )
			end
		end
	end
end 
setTimer ( sendPropertieEarning, (60*tonumber(get("*PAYOUT_TIME")))*1000, 0 )

 

Edited by Shinigami
Link to comment
11 hours ago, Shinigami said:

but I 'm not able to hit part of the profit, can anybody help me.

I'm to lazy to read 230 lines of code: what do you mean with the statement in the quote? Also are there errors?

 

 

Spoiler

after reading the first lines of the code i have some questions:

11 hours ago, Shinigami said:


local _setElementData = setElementData
function setElementData ( element, group, value )
return _setElementData ( element, group, value, true )
end

 

Why force the sync to true? it's the default value 

 

11 hours ago, Shinigami said:


id = 0
while ( propertiesLocs [ id ] ) do
id = id + 1
end

 

... is something like the # operator what are you searching for?

 

11 hours ago, Shinigami said:


local propertieOwner = getPropertieOwner(id)

 

Why id? What is "id" for this function?

 

I know it's offtopic but the cleaner the code the easier is for us

 

 

Edited by LoPollo
Link to comment

So if you have like 3 properties with $500, $7500, $1500, and you hit the pickup of the property with $1500, you get $9500 instead of $1500?

Also isn't that function to increase the cash of the properties? mmm no i'm not understanding the code, it's not similar to my style...

No after reading it more times i can't understand what do you want to do with that function honestly.

 

Can you make something like this to explain the code?

or post that function with comments that explain what every code block should do

 

 

PS: i can't find what propertiesLocs[...].lucre should be

Edited by LoPollo
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...