Jump to content

[ERROR] Advertisements /ads


WRIST

Recommended Posts

local SCREEN_X, SCREEN_Y = guiGetScreenSize()
local resourceName = getResourceName( getThisResource( ) )
local ONE_HOUR = 3600
local SERVICES_SECTION = 1		--Places to offer services such as house cleaning or mechanics and whatnot
local CARS_VEHICLES_SECTION = 2	--Offer to buy or sell a vehicle in this section
local REAL_ESTATE_SECTION = 3	--Houses for sale go in this section
local COMMUNITY_SECTION = 4		--Advertisements about communities can go here, for example, palomino creek.
local JOBS_SECTION = 5 			--Advertisements about hiring people or looking for work will go in this section
local PERSONALS_SECTION = 6		--People looking for other people go in this section
local sections = { "Servicii", "Vehicule", "Proprietăți", "Communitate", "Slujbe", "Personal" }
local deleteAny = false
local window = { } 
local viewad = {}
local postad = {}

function getTime( day, month, timestamp )
	local months = { "Ianuarie", "Februarie", "Martie", "Aprile", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octobrie", "Niembrie", "Decembrie" }
	local days = { "Sâmbătă", "Luni", "Marți", "Miercuri", "Joi", "Vineri", "Duminică" }
	local time = nil
	local ts = nil
	if timestamp then
		time = getRealTime( timestamp )
	else
		time = getRealTime( )
	end
	ts = ( tonumber( time.hour ) >= 12 and tostring( tonumber( time.hour ) - 12 ) or time.hour ) .. ":"..("%02d"):format(time.minute)..( tonumber( time.hour ) >= 12 and " PM" or " AM" )	
	if month then
		ts =  months[ time.month + 1 ] .. " ".. time.monthday .. ", " .. ts
	end
	if day then
		ts = days[ time.weekday + 1 ].. ", " .. ts
	end
	return ts
end

addEvent( resourceName .. ":ad_create_fail", true )
addEventHandler( resourceName .. ":ad_create_fail", root,
	function()
		local window = { } --Store all of our window elements
		local width = 230 -- The width of our window
		local height = 110 -- The height of our window
		local x = SCREEN_X / 2 - width / 2 --Where on the screen our window will be located
		local y = SCREEN_Y / 2 - height / 2
		window.window = guiCreateWindow( x, y, width, height, "Eroare de creatie", false ) --Create the window.
		window.errorLabel = guiCreateLabel( 10, 30, width - 20, 20, "A fost o eroare la introducere.", false, window.window )
		window.closeButton = guiCreateButton( 10, 60, width - 20, 40, "Ieși", false, window.window )
		addEventHandler( "onClientGUIClick", window.closeButton, 
			function ()
				guiSetVisible( window.window, false )
				destroyElement( window.window )
				window = { }
			end
		)
	end
)

function createAdvertisement( )
	closePostAd()
	if window and window.window and isElement(window.window) then
		guiSetEnabled(window.window, false)
	end
	guiSetInputEnabled ( true )
	local window = { } -- Store all of our window elements
	local width = 400 -- The width of our window
	local height = 440 -- The height of our window
	local x = SCREEN_X / 2 - width / 2 --Where on the screen our window will be located
	local y = SCREEN_Y / 2 - height / 2
	postad.window = guiCreateWindow( x, y, width, height, "Crează Reclamă", false ) --Create the postad.
	postad.label = { } --This will hold our label elements
	local labels = { "Telefon", "Nume", "Adresă", "Expiră", "Secțiune", "Reclamă" } --This holds all of the labels we will create here
	local y = 35 --We'll set y to 30, which is the y coordinate of where our first label will go.
	for label = 1, #labels do
		postad.label[ label ] = guiCreateLabel( 10, y * label, 100, 30, labels[ label ], false, postad.window )
	end
	postad.input = { } -- Will hold all of our input elements.
	y = 30 -- We'll start y off at 25 here to stay even with the inputs.
	postad.input[ 1 ] = guiCreateEdit( 100, y, width - 120, 30, "", false, postad.window ) --Phone input
	y = y + 35
	postad.input[ 2 ] = guiCreateEdit( 100, y, width - 120, 30, "", false, postad.window ) --Name Input
	y = y + 35
	postad.input[ 3 ] = guiCreateEdit( 100, y, width - 120, 30, "", false, postad.window ) --Address input
	y = y + 40
	postad.input[ 4 ] = guiCreateComboBox( 100, y, width - 120, 95, "", false, postad.window ) --Expiry
	guiComboBoxAddItem( postad.input[ 4 ], "1 oră" )
	guiComboBoxAddItem( postad.input[ 4 ], "2 ore" )
	guiComboBoxAddItem( postad.input[ 4 ], "6 ore" )
	guiComboBoxAddItem( postad.input[ 4 ], "O zi" )
	y = y + 34
	postad.input[ 5 ] = guiCreateComboBox( 100, y, width - 120, 125, "", false, postad.window ) --Section
	for i = 1, #sections do --Loop through each of the 6 advertisement sections.
		guiComboBoxAddItem( postad.input[ 5 ], sections[ i ] )
	end
	postad.input[ 6 ] = guiCreateMemo( 10, y + 60, width - 20, 90, "", false, postad.window ) --Advertisement
	for i = 1, 6 do
		addEventHandler(getElementType( postad.input[i] ) == 'gui-combobox' and 'onClientGUIComboBoxAccepted' or 'onClientGUIChanged', postad.input[i],
			function( )
				for i = 1, 6 do
					if getElementType( postad.input[i] ) == 'gui-combobox' then
						if guiComboBoxGetSelected( postad.input[i] ) == -1 then
							guiSetEnabled( postad.postButton, false )
							return
						end
					else
						local text = ( guiGetText( postad.input[i] ) or '' ):gsub("\n", ''):gsub("\r", '')
						if #text == 0 then
							guiSetEnabled( postad.postButton, false )
							return
						end
					end
				end
				guiSetEnabled( postad.postButton, true )
			end, false
		)
	end
	postad.postButton = guiCreateButton( 10, height - 100, width - 20, 40, "Postează Reclamă", false, postad.window )
	guiSetEnabled( postad.postButton, false )
	addEventHandler( "onClientGUIClick", postad.postButton, 
		function ()
			local phone = guiGetText( postad.input[ 1 ] ) or ""
			local name = guiGetText( postad.input[ 2 ] ) or ""
			local address = guiGetText( postad.input[ 3 ] ) or ""
			local advertisement = guiGetText( postad.input[ 6 ] )			
			local expirySelected = guiComboBoxGetSelected( postad.input[ 4 ] )
			local expires = nil
			if expirySelected == -1 or expirySelected == 0 then
				expires = ONE_HOUR
			elseif	expirySelected == 1 then
				expires = ONE_HOUR * 2
			elseif expirySelected == 2 then
				expires = ONE_HOUR * 6
			else
				expires = ONE_HOUR * 24
			end			
			local section = tostring( guiComboBoxGetSelected( postad.input[ 5 ] ) + 1 )
			closePostAd()
			triggerServerEvent( resourceName .. ":create_advertisement", getLocalPlayer(), phone, name, address, advertisement, expires, section )
		end
	, false )
	postad.closeButton = guiCreateButton( 10, height - 50, width - 20, 40, "Ieși", false, postad.window )
	addEventHandler( "onClientGUIClick", postad.closeButton, 
		function ()
			if source == postad.closeButton then
				closePostAd()
			end
		end
	, false )
end
addCommandHandler( "postad", createAdvertisement, false, false )

function closePostAd()
	if postad and postad.window and isElement(postad.window) then
		guiSetInputEnabled ( false )
		destroyElement( postad.window )
		postad = { }
		if window and window.window and isElement(window.window) then
			guiSetEnabled(window.window, true)
		else
			showCursor( false, false )
		end
	end
end

function viewAdvertisement( advertisement )
	closeViewAd()
	if window and window.window and isElement(window.window) then
		guiSetEnabled(window.window, false)
	end
	guiSetInputEnabled ( false )
	local window = { }
	local width = 400
	local height = 530
	local x = SCREEN_X / 2 - width / 2
	local y = SCREEN_Y / 2 - height / 2
	viewad.window = guiCreateWindow( x, y, width, height, "Vezi reclamă", false )
	viewad.label = { }
	local labels = { "Telefon", "Nume", "Adresă", "Start", "Expiră", "Secțiune", "Reclamă" } 
	local y = 35
	for label = 1, #labels do
		viewad.label[ label ] = guiCreateLabel( 10, y * label, 100, 30, labels[ label ], false, viewad.window )
	end
	viewad.input = { }
	y = 30 
	viewad.input[ 1 ] = guiCreateEdit( 100, y, width - 120, 30, advertisement.phone, false, viewad.window ) 
	guiEditSetReadOnly( viewad.input[ 1 ], true )
	y = y + 35
	viewad.input[ 2 ] = guiCreateEdit( 100, y, width - 120, 30, advertisement.name, false, viewad.window )
	guiEditSetReadOnly( viewad.input[ 2 ], true )
	y = y + 35
	viewad.input[ 3 ] = guiCreateEdit( 100, y, width - 120, 30, advertisement.address, false, viewad.window ) 
	guiEditSetReadOnly( viewad.input[ 3 ], true )
	y = y + 35
	viewad.input[ 4 ] = guiCreateEdit( 100, y, width - 120, 30, getTime( true, true, advertisement.start ), false, viewad.window )
	guiEditSetReadOnly( viewad.input[ 4 ], true )
	y = y + 35
	viewad.input[ 5 ] = guiCreateEdit( 100, y, width - 120, 30, getTime( true, true, advertisement.expiry ), false, viewad.window ) 
	guiEditSetReadOnly( viewad.input[ 5 ], true )
	y = y + 35
	viewad.input[ 6 ] = guiCreateEdit( 100, y, width - 120, 30, sections[ tonumber( advertisement.section ) ], false, viewad.window ) 
	guiEditSetReadOnly( viewad.input[ 6 ], true )
	viewad.input[ 7 ] = guiCreateMemo( 10, y + 60, width - 20, 90, advertisement.advertisement, false, viewad.window )
	guiMemoSetReadOnly( viewad.input[ 7 ], true )	
	if tonumber( getElementData( localPlayer, "dbid" ) ) == tonumber( advertisement.created_by ) or deleteAny then
		guiCreateLabel( 100, 245, 200, 25, "Creat de: " .. advertisement.author:gsub("_", " "), false, viewad.window )
		viewad.pushButton = guiCreateButton( 10, height - 150, width - 20, 40, "Trimite reclama ($100)", false, viewad.window )	
		if tonumber( getElementData( localPlayer, "dbid" ) ) ~= tonumber( advertisement.created_by ) then
			guiSetProperty( viewad.pushButton, 'NormalTextColour', 'FFFF0000' )
		end
		addEventHandler( "onClientGUIClick", viewad.pushButton, 
			function ()
				triggerServerEvent( resourceName .. ":push_advertisement", localPlayer, advertisement.id )
			end
		, false )
	end
	if tonumber( getElementData( localPlayer, "dbid" ) ) == tonumber( advertisement.created_by ) or deleteAny then
		viewad.deleteButton = guiCreateButton( 10, height - 100, width - 20, 40, "Șterge Reclamă", false, viewad.window )	
		if tonumber( getElementData( localPlayer, "dbid" ) ) ~= tonumber( advertisement.created_by ) then
			guiSetProperty( viewad.deleteButton, 'NormalTextColour', 'FFFF0000' )
		end
		addEventHandler( "onClientGUIClick", viewad.deleteButton, 
			function ()
				closeViewAd()
				triggerServerEvent( resourceName .. ":delete_advertisement", localPlayer, advertisement.id )
			end
		, false )
	end
	viewad.closeButton = guiCreateButton( 10, height - 50, width - 20, 40, "Ieși", false, viewad.window )
	addEventHandler( "onClientGUIClick", viewad.closeButton, 
		function ()
			if source == viewad.closeButton then
				closeViewAd()
			end
		end
	, false )
end

function closeViewAd()
	if viewad and viewad.window and isElement(viewad.window) then
		guiSetInputEnabled ( false )
		guiSetVisible( viewad.window, false )
		destroyElement( viewad.window )
		viewad = { }
		if window and window.window and isElement(window.window) then
			guiSetEnabled(window.window, true)
		else
			showCursor( false, false )
		end
	end
end

addEvent( resourceName .. ":display_all", true )
addEventHandler( resourceName .. ":display_all", root, 
	function( advertisements, canDeleteAnyAd )
		closeAds()
		deleteAny = canDeleteAnyAd
		showCursor( true, true )
		local width = 500 
		local height = 500 
		local x = SCREEN_X / 2 - width / 2
		local y = SCREEN_Y / 2 - height / 2
		window.window = guiCreateWindow( x, y, width, height, "Reclame", false )
		window.closeButton = guiCreateButton( 10, 30, width - 20, 40, "Creează Reclamă", false, window.window )
		addEventHandler( "onClientGUIClick", window.closeButton, 
			function ()
				createAdvertisement( )
			end
		, false )
		window.mainPanel	= guiCreateTabPanel ( 10, 90, width - 15, height - 150, false, window.window )
		window.tab		= { }
		window.table	= { }
		window.colPhone = { }
		window.colName 	= { }
		window.colAd 	= { }		
		for i = 1, #sections do
			window.tab[ i ]		= guiCreateTab( sections[ i ], window.mainPanel ) 
			window.table[ i ]	= guiCreateGridList ( 10, 10, width - 35, height - 190, false, window.tab[ i ] ) 
			window.colPhone[ i ]= guiGridListAddColumn( window.table[ i ], "Telefon", 0.2 )
			window.colName[ i ]	= guiGridListAddColumn( window.table[ i ], "Nume", 0.2 )
			window.colAd[ i ]	= guiGridListAddColumn( window.table[ i ], "Reclamă", 0.5 )
			for ad = 1, #advertisements do
				if tonumber( advertisements[ ad ].section ) == i then
					local row = guiGridListAddRow ( window.table[ i ] ) 			
					guiGridListSetItemText( window.table[ i ], row, window.colPhone[ i ], advertisements[ ad ].phone, false, false )
					guiGridListSetItemText( window.table[ i ], row, window.colName[ i ], advertisements[ ad ].name, false, false )
					guiGridListSetItemText( window.table[ i ], row, window.colAd[ i ], advertisements[ ad ].advertisement, false, false )
					guiGridListSetItemData( window.table[ i ], row, window.colPhone[ i ], ad )
					addEventHandler( "onClientGUIDoubleClick", window.table[ i ],
						function ( button, state )
							if button == 'left' and state == 'up' and window and window.table then
								local selectedRow, selectedCol = guiGridListGetSelectedItem( window.table[ i ] )
								local key = guiGridListGetItemData( window.table[ i ], selectedRow, window.colPhone[ i ] )
								if advertisements[ key ] then
									viewAdvertisement( advertisements[ key ] )
								end
							end
						end
					, false )
				end
			end
			if guiGridListGetRowCount( window.table[ i ] ) == 0 then
				local row = guiGridListAddRow ( window.table[ i ] )
				guiGridListSetItemText ( window.table[ i ], row, window.colPhone[ i ], "Nici-o reclamă", false, false )
			end
		end
		window.closeButton = guiCreateButton( 10, height - 50, width - 20, 40, "Ieși", false, window.window )
		addEventHandler( "onClientGUIClick", window.closeButton, 
			function ()
				if source == window.closeButton then
					closeAds()
				end
			end
		, false )
	end
)

function closeAds()
	if window and window.window and isElement(window.window) then
		showCursor( false, false )
		destroyElement( window.window )
		window = { }
		closePostAd()
	end
end

 

local SERVICES_SECTION = 1        --Places to offer services such as house cleaning or mechanics and whatnot
local CARS_VEHICLES_SECTION = 2    --Offer to buy or sell a vehicle in this section
local REAL_ESTATE_SECTION = 3    --Houses for sale go in this section
local COMMUNITY_SECTION = 4        --Advertisements about communities can go here, for example, palomino creek.
local JOBS_SECTION = 5             --Advertisements about hiring people or looking for work will go in this section
local PERSONALS_SECTION = 6        --People looking for other people go in this section
local COOLDOWN_MINUTES = 5         --Number of minutes between push alerts being sent by players
local resourceName = getResourceName( getThisResource( ) )
local sections = { "Servicii", "Vehicule", "Proprietăți", "Communitate", "Slujbe", "Personal" }

function escape( value )
    return exports.mysql:escape_string( value )
end

function now( )
    return tonumber( getRealTime().timestamp )
end

addEvent( resourceName .. ":create_advertisement", true )
addEventHandler( resourceName .. ":create_advertisement", root, 
    function( phone, name, address, advertisement, expires, section )
        --Check if all fields have been entered by the user.
        
        if not ( phone == nil or name == nil or address == nil or advertisement == nil ) then
            --Fetch the created by 
            local createdBy = tostring( getElementData( source, "dbid" ) )
            
            --Get the current server time to store as our start time
            local start = getRealTime().timestamp
            --Add the time until expiry to the start time to get the actual time it will expire.
            local expiry = start + expires
            
            --Check if our query went into the database successfully.
            if exports.mysql:insert( 'advertisements', { phone = phone, name = name, address = address, advertisement = advertisement, start = start, expiry = expiry, section = section, created_by = createdBy } ) then
                --We'll send something to the client side so they can close the add form and reopen the main advertisements form.
                --openAdvertisements( source, nil )
            else
                --If the database query was unsucessful, alert the end user.
                outputChatBox( "SQL Error.", source )
                triggerClientEvent( source, resourceName .. ":ad_create_fail", root )
            end
        else
            --If all fields were not entered, alert the user.
            outputChatBox( "Field Error.", source )
            triggerClientEvent( source, resourceName .. ":ad_create_fail", root )
        end
    end
)

local coolDown = {}

addEvent( resourceName .. ":push_advertisement", true )
addEventHandler( resourceName .. ":push_advertisement", root, 
    function( id )
        local advertisement = exports.mysql:select_one( "advertisements", { id = id } )
        advertisement.author = exports.mysql:select_one( "characters", { id = advertisement.created_by } ).charactername
        if not coolDown[ id ] or ( coolDown[ id ] < now() - ( 60 * COOLDOWN_MINUTES ) ) then
            if exports.bank:takeBankMoney( client, 100 ) then
                coolDown[ id ] = now()
                for i, k in pairs( getElementsByType( 'player' ) ) do
                    if exports.integration:isPlayerTrialAdmin( k ) then
                        outputChatBox( "ADVERT: " .. advertisement.advertisement .. " | Ph: " .. advertisement.phone .. " | Cat: " .. sections[ tonumber( advertisement.section ) ] .. " (( " .. advertisement.author:gsub("_", " ") .." )) .", k, 0, 255, 0 )
                    else
                        outputChatBox( "ADVERT: " .. advertisement.advertisement .. " | Ph: " .. advertisement.phone .. " | Cat: " .. sections[ tonumber( advertisement.section ) ] .. ".", k, 0, 255, 0 )
                    end
                end
            else
                outputChatBox( "Nu ai destui bani în bancă să trimiți această reclamă.", client, 255, 155, 155 )
            end
        else
            outputChatBox( "Poți trimite o reclamă o dată la " .. COOLDOWN_MINUTES .. " minute.", client, 255, 155, 155 )
        end
    end
)

function deleteAdvertisement( id )
    return exports.mysql:delete('advertisements', {id = id})
end

addEvent( "advertisements:delete_advertisement", true )
addEventHandler( "advertisements:delete_advertisement", getRootElement(), 
    function( id )
        if deleteAdvertisement( id ) then
            --openAdvertisements( source )
        else
            outputChatBox( "An error occured with deleting that ad.", source, 255, 100, 100 )
        end
    end
)

function openAdvertisements( player, command )
    local advertisements = { } --These will hold our advertisements to send to the client and populate our advertisement tables.
    if not player then player = source end
    for _, ad in ipairs( exports.mysql:select('advertisements') ) do
        if tonumber( ad.expiry ) >= tonumber( getRealTime().timestamp ) then --Check if the advertisement has expired, delete it if so.
            ad.author = exports.mysql:select_one( "characters", { id = ad.created_by } ).charactername
            table.insert( advertisements, ad )
        else
            deleteAdvertisement( ad.id )
        end
    end
    triggerClientEvent( player, resourceName .. ":display_all", root, advertisements, exports.integration:isPlayerAdmin( player ) ) --Send the advertisements to the client to create the GUI.
end
addCommandHandler( "advertisements", openAdvertisements, false, false )
addCommandHandler( "ads", openAdvertisements, false, false )
addCommandHandler( "classifieds", openAdvertisements, false, false )
addEvent( resourceName .. ":open_ads", true )
addEventHandler( resourceName .. ":open_ads", root, openAdvertisements )

Hello guys I just wanna know what I messed up in my script  I'm facing this error for hours!!

ERROR: advertisements\advertisements.Lua:187: call: failed to call 'mysql:select' [string "?"]
ERROR: advertisements\advertisements.Lua:187: bad argument #1 to 'ipairs' (table expected, got boolean)

 

Edited by SparkySky
Link to comment
On 30/05/2020 at 10:18, Tekken said:

There is no 187 line in the code you posted, but I assume there is a problem with your MySQL database.

I found out that in the database to work /ads you have to enter the following codes !

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

--
-- Structura de tabel pentru tabelul `advertisements`
--

CREATE TABLE `advertisements` (
  `id` int(11) NOT NULL,
  `phone` varchar(10) NOT NULL,
  `name` varchar(50) NOT NULL,
  `address` varchar(100) NOT NULL,
  `advertisement` text NOT NULL,
  `start` int(11) NOT NULL,
  `expiry` int(11) NOT NULL,
  `created_by` int(11) NOT NULL,
  `section` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Salvarea datelor din tabel `advertisements`
--

INSERT INTO `advertisements` (`id`, `phone`, `name`, `address`, `advertisement`, `start`, `expiry`, `created_by`, `section`) VALUES

-- --------------------------------------------------------
--
-- Indexes for table `advertisements`
--
ALTER TABLE `advertisements`
  ADD PRIMARY KEY (`id`);
  
--
-- AUTO_INCREMENT for table `advertisements`
--
ALTER TABLE `advertisements`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=697;

I put it in and when I gave /ads it tells me to wait 2 hours to place an ad! I gave /forcepaydayall to have 2 hours and when I give /ads again I get this error in the console ?!

ERROR: advertisements\advertisements.Lua:187: bad argument #1 to 'ipairs' (table expected, got boolean)

ERROR: advertisements\advertisements.Lua:187: call: failed to call 'mysql:select' [string "?"]

how can i solve ?!

please help me !

Edited by SparkySky
Link to comment

There is an error within your mysql resource as instead of a table it returns boolean otherwise said 'false' that means the resource could not connect or check for whatever you may check in the database, so you should check the function select in resource mysql. good luck!

SQL it's not exactly my thing, so I can only tell you there is a problem there, as also I only worked with SQLite witch it's the same thing basically but SQLite it's offline.

Link to comment
17 minutes ago, Tekken said:

There is an error within your mysql resource as instead of a table it returns boolean otherwise said 'false' that means the resource could not connect or check for whatever you may check in the database, so you should check the function select in resource mysql. good luck!

SQL it's not exactly my thing, so I can only tell you there is a problem there, as also I only worked with SQLite witch it's the same thing basically but SQLite it's offline.

Thanks anyway, I'll see what I can do!

Link to comment
  • 2 months later...
9 minutes ago, IAN21 said:

Yo, wait. This is a leaked resource. Please, don't ask for help with leaked scripts.

Let the kids make servers :(

I agree the script is leaked.
If you really wanna start a MTASA server you need to build it from nothing.
I see lots and lots of posts with the old Owlgaming gamemode , come on guys , i'm tired of seeing every server in my country with the same gamemode , build something original!

  • Sad 1
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...