Configuration

In this section we will put more information about the editable part of the script, therefore for the script to play the rewards automatically, a series of functions are assigned to it which you will.

Go to the following path: jc_subastas/Editable/Server/SMain.lua

function generarMatricula()
    local charset = {}
    for i = 48, 57 do table.insert(charset, string.char(i)) end -- 0-9
    for i = 65, 90 do table.insert(charset, string.char(i)) end -- A-Z

    local plate = ''
    for i = 1, 8 do
        plate = plate .. charset[math.random(1, #charset)]
    end
    return plate
end

-- Función para subasta de coche
function procesarSubastaCoche(tipo, puja, modelo, cantidad, identifier)
    local plate = generarMatricula()

    local vehiculoData = {
        model = GetHashKey(modelo),
        plate = plate,
        fuelLevel = 100.0,
        dirtLevel = 0.0,
        color1 = 0,
        color2 = 0,
        neonEnabled = {false, false, false, false},
        neonColor = {255, 255, 255},
        tyreSmokeColor = {255, 255, 255},
        windowsBroken = {["0"]=false,["1"]=false,["2"]=false,["3"]=false,["4"]=false,["5"]=false,["6"]=false,["7"]=false},
        doorsBroken = {["0"]=false,["1"]=false,["2"]=false,["3"]=false,["4"]=false},
        engineHealth = 1000.0,
        bodyHealth = 1000.0
    }

    local vehiculoJSON = json.encode(vehiculoData)

    if Config.Framework == "esx" then
        MySQL.Async.execute('INSERT INTO owned_vehicles (vehicle, owner, plate, type, garage_name) VALUES (@veh, @owner, @plate, @type, @garage)', {
            ['@veh'] = vehiculoJSON,
            ['@owner'] = identifier,
            ['@plate'] = plate,
            ['@type'] = 'car',
            ['@garage'] = 'Garage_Centre'
        }, function(rowsChanged)
            if Config.Debug then print(("[SUBASTA] Vehículo %s entregado a %s (Puja: %s$)"):format(modelo, identifier, puja)) end
        end)
    elseif Config.Framework == "qb" then
        MySQL.Async.execute([[
            INSERT INTO player_vehicles 
            (license, citizenid, vehicle, hash, mods, plate, garage, fuel, engine, body, state, garage_id, garage_type) 
            VALUES 
            (@license, @citizenid, @vehicle, @hash, @mods, @plate, @garage, @fuel, @engine, @body, 1, 'A', 'car')
        ]], {
            ['@license'] = 'subasta', 
            ['@citizenid'] = identifier,
            ['@vehicle'] = modelo,
            ['@hash'] = tostring(GetHashKey(modelo)),
            ['@mods'] = vehiculoJSON,
            ['@plate'] = plate,
            ['@garage'] = 'pillboxgarage',
            ['@fuel'] = 100,
            ['@engine'] = 1000,
            ['@body'] = 1000
        }, function(rowsChanged)
            if Config.Debug then print(("[SUBASTA] Vehículo %s entregado a %s (Puja: %s$)"):format(modelo, identifier, puja)) end
        end)
    end
end

-- Función para subasta de arma
function procesarSubastaArma(tipo, puja, modelo, cantidad, identifier)
    local player = nil

    if Config.Framework == "esx" then
        for _, xPlayer in pairs(ESX.GetPlayers()) do
            local target = ESX.GetPlayerFromId(xPlayer)
            if target and target.getIdentifier() == identifier then
                player = target
                break
            end
        end

        if player then
            player.addInventoryItem(modelo, cantidad)
            player.showNotification("Has ganado la subasta y recibido un "..modelo.." x"..cantidad.." en tu inventario.")
        else
            if Config.Debug then print(("[SUBASTA] %s ganó un %s pero está offline"):format(identifier, modelo)) end
        end

    elseif Config.Framework == "qb" then
        for _, xPlayer in pairs(QBCore.Players) do
            if xPlayer.PlayerData.citizenid == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.Functions.AddItem(modelo, cantidad)
            TriggerClientEvent('QBCore:Notify', player.PlayerData.source, "Has ganado la subasta y recibido un "..modelo.." x"..cantidad, "success")
        else
            if Config.Debug then print(("[SUBASTA] %s ganó un %s pero está offline"):format(identifier, modelo)) end
        end
    end
end

-- Función para subasta de dinero
function procesarSubastaDinero(tipo, puja, modelo, cantidad, identifier)
    local cantidadDinero = tonumber(cantidad) or 0
    local player = nil

    if Config.Framework == "esx" then
        for _, id in pairs(ESX.GetPlayers()) do
            local xPlayer = ESX.GetPlayerFromId(id)
            if xPlayer and xPlayer.getIdentifier() == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.addMoney(cantidadDinero)
            player.showNotification("Has ganado la subasta y recibido "..cantidadDinero.."$.")
        else
            if Config.Debug then print(("[SUBASTA] %s ganó %s$ pero está offline"):format(identifier, cantidadDinero)) end
        end

    elseif Config.Framework == "qb" then
        for _, xPlayer in pairs(QBCore.Players) do
            if xPlayer.PlayerData.citizenid == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.Functions.AddMoney("cash", cantidadDinero)
            TriggerClientEvent('QBCore:Notify', player.PlayerData.source, "Has ganado la subasta y recibido "..cantidadDinero.."$", "success")
        else
            if Config.Debug then print(("[SUBASTA DINERO QB] %s ganó %s$ pero está offline"):format(identifier, cantidadDinero)) end
        end
    end
end

-- Función para subasta de item
function procesarSubastaItem(tipo, puja, modelo, cantidad, identifier)
    local player = nil

    if Config.Framework == "esx" then
        for _, id in pairs(ESX.GetPlayers()) do
            local xPlayer = ESX.GetPlayerFromId(id)
            if xPlayer and xPlayer.getIdentifier() == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.addInventoryItem(modelo, cantidad)
            player.showNotification("Has ganado la subasta y recibido x"..cantidad.." "..modelo.." en tu inventario.")
        else
            if Config.Debug then print(("[SUBASTA] %s ganó x%s %s pero está offline"):format(identifier, cantidad, modelo)) end
        end

    elseif Config.Framework == "qb" then
        for _, xPlayer in pairs(QBCore.Players) do
            if xPlayer.PlayerData.citizenid == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.Functions.AddItem(modelo, cantidad)
            TriggerClientEvent('QBCore:Notify', player.PlayerData.source, "Has ganado la subasta y recibido x"..cantidad.." "..modelo, "success")
        else
            if Config.Debug then print(("[SUBASTA] %s ganó x%s %s pero está offline"):format(identifier, cantidad, modelo)) end
        end
    end
end


function procesarSubastaCasa(tipo, puja, modelo, cantidad, identifier)
    MySQL.Async.fetchScalar('SELECT firstname FROM users WHERE identifier = @identifier', {
        ['@identifier'] = identifier
    }, function(name)

        -- lógica para entregar la casa al ganador
    end)
end

-- ENTREGA SORTEOS

function entregarSorteoCoche(sorteo, identifier)
    local plate = generarMatricula()

    local vehiculo = {
        model = GetHashKey(sorteo.modelo),
        plate = plate,
        fuelLevel = 100.0,
        dirtLevel = 0.0,
        color1 = 0,
        color2 = 0,
        neonEnabled = {false, false, false, false},
        neonColor = {255, 255, 255},
        tyreSmokeColor = {255, 255, 255},
        windowsBroken = {["0"]=false,["1"]=false,["2"]=false,["3"]=false,["4"]=false,["5"]=false,["6"]=false,["7"]=false},
        doorsBroken = {["0"]=false,["1"]=false,["2"]=false,["3"]=false,["4"]=false},
        engineHealth = 1000.0,
        bodyHealth = 1000.0
    }

    local vehiculoJSON = json.encode(vehiculo)

    if Config.Framework == "esx" then
        MySQL.Async.execute('INSERT INTO owned_vehicles (vehicle, owner, plate, type, garage_name) VALUES (@veh, @owner, @plate, @type, @garage)', {
            ['@veh'] = vehiculoJSON,
            ['@owner'] = identifier,
            ['@plate'] = plate,
            ['@type'] = 'car',
            ['@garage'] = 'Garage_Centre'
        }, function(rowsChanged)
            if Config.Debug then print(("[SORTEO] Ganador %s recibe %s (Modelo: %s)"):format(identifier, sorteo.nombre, sorteo.modelo)) end
        end)

    elseif Config.Framework == "qb" then
        MySQL.Async.execute([[
            INSERT INTO player_vehicles 
            (license, citizenid, vehicle, hash, mods, plate, garage, fuel, engine, body, state, garage_id, garage_type) 
            VALUES 
            (@license, @citizenid, @vehicle, @hash, @mods, @plate, @garage, @fuel, @engine, @body, 1, 'A', 'car')
        ]], {
            ['@license'] = 'sorteo', -- Puedes cambiarlo por el license real si lo tienes
            ['@citizenid'] = identifier,
            ['@vehicle'] = sorteo.modelo,
            ['@hash'] = tostring(GetHashKey(sorteo.modelo)),
            ['@mods'] = vehiculoJSON,
            ['@plate'] = plate,
            ['@garage'] = 'pillboxgarage', -- O el garage que prefieras
            ['@fuel'] = 100,
            ['@engine'] = 1000,
            ['@body'] = 1000
        }, function(rowsChanged)
            if Config.Debug then print(("[SORTEO] Ganador %s recibe %s (Modelo: %s)"):format(identifier, sorteo.nombre, sorteo.modelo)) end
        end)
    end
end


function entregarSorteoArma(sorteo, identifier)
    local player = nil

    if Config.Framework == "esx" then
        for _, id in pairs(ESX.GetPlayers()) do
            local xPlayer = ESX.GetPlayerFromId(id)
            if xPlayer and xPlayer.getIdentifier() == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.addInventoryItem(sorteo.modelo, 1)
            player.showNotification("¡Has ganado un "..sorteo.modelo.." en el sorteo!")
        else
            if Config.Debug then print(("[SORTEO] %s ha ganado un %s pero está offline"):format(identifier, sorteo.modelo)) end
        end

    elseif Config.Framework == "qb" then
        for _, xPlayer in pairs(QBCore.Players) do
            if xPlayer.PlayerData.citizenid == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.Functions.AddItem(sorteo.modelo, 1)
            TriggerClientEvent('QBCore:Notify', player.PlayerData.source, "¡Has ganado un "..sorteo.modelo.." en el sorteo!", "success")
        else
            if Config.Debug then print(("[SORTEO] %s ha ganado un %s pero está offline"):format(identifier, sorteo.modelo)) end
        end
    end
end


function entregarSorteoDinero(sorteo, identifier)
    local cantidad = tonumber(sorteo.modelo) or 0
    local player = nil

    if Config.Framework == "esx" then
        for _, id in pairs(ESX.GetPlayers()) do
            local xPlayer = ESX.GetPlayerFromId(id)
            if xPlayer and xPlayer.getIdentifier() == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.addMoney(cantidad)
            player.showNotification("¡Has ganado "..cantidad.."$ en el sorteo!")
        else
           if Config.Debug then print(("[SORTEO] %s ha ganado %s$ pero está offline"):format(identifier, cantidad)) end
        end

    elseif Config.Framework == "qb" then
        for _, xPlayer in pairs(QBCore.Players) do
            if xPlayer.PlayerData.citizenid == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.Functions.AddMoney("cash", cantidad)
            TriggerClientEvent('QBCore:Notify', player.PlayerData.source, "¡Has ganado "..cantidad.."$ en el sorteo!", "success")
        else
            if Config.Debug then print(("[SORTEO] %s ha ganado %s$ pero está offline"):format(identifier, cantidad)) end
        end
    end
end

function entregarSorteoItem(sorteo, identifier)
    local cantidad = tonumber(sorteo.cantidad) or 1
    local player = nil

    if Config.Framework == "esx" then
        for _, id in pairs(ESX.GetPlayers()) do
            local xPlayer = ESX.GetPlayerFromId(id)
            if xPlayer and xPlayer.getIdentifier() == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.addInventoryItem(sorteo.modelo, cantidad)
            player.showNotification("¡Has ganado "..cantidad.."x "..sorteo.modelo.." en el sorteo!")
        else
            if Config.Debug then print(("[SORTEO] %s ha ganado %sx %s pero está offline"):format(identifier, cantidad, sorteo.modelo)) end
        end

    elseif Config.Framework == "qb" then
        for _, xPlayer in pairs(QBCore.Players) do
            if xPlayer.PlayerData.citizenid == identifier then
                player = xPlayer
                break
            end
        end

        if player then
            player.Functions.AddItem(sorteo.modelo, cantidad)
            TriggerClientEvent('QBCore:Notify', player.PlayerData.source, "¡Has ganado "..cantidad.."x "..sorteo.modelo.." en el sorteo!", "success")
        else
            if Config.Debug then print(("[SORTEO] %s ha ganado %sx %s pero está offline"):format(identifier, cantidad, sorteo.modelo)) end
        end
    end
end

function entregarSorteoCasa(sorteo, identifier)
    MySQL.Async.fetchScalar('SELECT firstname FROM users WHERE identifier = @identifier', {
        ['@identifier'] = identifier
    }, function(name)
        -- lógica para entregar la casa al ganador
        -- puedes hacer un insert en tu sistema de casas
    end)
end

In this file we will have the functions that give rewards when players win a raffle or an auction. If you have any problems or do not know how to adapt a function, you can open a ticket and we will adapt it for you.

Last updated