Jump to content

DbQuery - Help Me


Ionel

Recommended Posts

Hello, I want to ask you how I can adopt this script from dbQuery to mysql.

 

local gas_stations = {}
local db = exports.mysql:getConnection()
local mysql = exports.mysql

addEventHandler("onResourceStart", resourceRoot, function()
  dbQuery(function(qh)
    local result = dbPoll(qh, -1)
    if result and #result > 0 then
      for _, v in ipairs(result) do
        gas_stations[tostring(v.id)] = {}
        gas_stations[tostring(v.id)].pos = {x = v.x, y = v.y, z = v.z, int = v.interior, dim = v.dimension}
        gas_stations[tostring(v.id)].status = {owner = v.owner, price = tonumber(v.price), stock = tonumber(v.stock), id = tonumber(v.id), fuelPrice = tonumber(v.fuelPrice), money = tonumber(v.money)}
        gas_stations[tostring(v.id)].colshape = createColSphere(v.x, v.y, v.z, 10)
        gas_stations[tostring(v.id)].pickup = createPickup(v.x, v.y, v.z, 3, 1239)
      end

      triggerClientEvent("gas:fetchGasStations", root, gas_stations)
    end
  end, {}, db, "SELECT * FROM `gas_stations` WHERE `deleted`='0' ORDER BY `id` ASC")
end)

addEvent("gas:getGasStations", true)
addEventHandler("gas:getGasStations", root, function()
  triggerClientEvent("gas:fetchGasStations", root, gas_stations)
end)

addCommandHandler("makegas", function(thePlayer, command, price)
  if not exports.integration:isPlayerLeadAdmin(thePlayer) then return exports.fuel_notifications:addNotification(thePlayer, "error", "Nu ai acces la aceasta comanda!") end
  if not tonumber(price) then return outputChatBox("SYNTAX: /" .. command .. " [Pret]", thePlayer, 255, 194, 14) end

  local x, y, z = getElementPosition(thePlayer)
  local interior, dimension = getElementInterior(thePlayer), getElementDimension(thePlayer)
  dbQuery(function(qh, thePlayer)
    local result, numRows, lastID = dbPoll(qh, -1)
    if numRows > 0 then
      outputChatBox("A fost creata o noua benzinarie. ID: " .. lastID .. "!", thePlayer, 255, 194, 14)

      gas_stations[tostring(lastID)] = {}
      gas_stations[tostring(lastID)].pos = {x = x, y = y, z = z, int = interior, dim = dimension}
      gas_stations[tostring(lastID)].status = {owner = -1, price = tonumber(price), stock = 0, id = lastID, money = 0, fuelPrice = 4}
      gas_stations[tostring(lastID)].colshape = createColSphere(x, y, z, 10)
      gas_stations[tostring(lastID)].pickup = createPickup(x, y, z, 3, 1239)

      triggerClientEvent("gas:fetchGasStations", thePlayer, gas_stations)
    end
  end, {thePlayer}, db, "INSERT INTO `gas_stations` (`x`, `y`, `z`, `interior`, `dimension`, `stock`, `owner`, `price`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", x, y, z, interior, dimension, 0, -1, price)
end)

--comanda de fill
function refillCommand(thePlayer, command, amount)
  local money = 0
  local gasStation = nil
  for _, gs in pairs(gas_stations) do
    if isElementWithinColShape(thePlayer, gs.colshape) then
      gasStation = gs
      break
    end
  end

  if not gasStation then
    return exports.fuel_notifications:addNotification(thePlayer, "error", "Trebuie să te aflii într-o benzinărie.")
  end

  if not tonumber(amount) then
    return outputChatBox("SYNTAX: /" .. command .. " [Sumă ($) | 0 = plin]", thePlayer, 255, 194, 17)
  end

    local posX, posY, posZ = getElementPosition(thePlayer)

    local theVehicle = getPedOccupiedVehicle(thePlayer)

    if (getVehicleEngineState(theVehicle) == true) then
        exports.fuel_notifications:addNotification(thePlayer, "error", "Nu poti face plinul la masina cu motorul pornit.")
        return
    end

    if not (isPedInVehicle(thePlayer) == true) then
        exports.fuel_notifications:addNotification(thePlayer, "error", "Trebuie sa te aflii intr-un vehicul.")
        return
    end

    local theLitres = calculateFuelPrice(thePlayer, amount, 1, tonumber(gasStation.status.fuelPrice))
  if tonumber(amount) == 0 then
    theLitres = calculateFuelPrice(thePlayer, 99999999, 1, tonumber(gasStation.status.fuelPrice))
  end

  if tonumber(gasStation.status.owner) ~= -1 then
    if tonumber(gasStation.status.stock) < theLitres then
      return exports.fuel_notifications:addNotification(thePlayer, "error", "Această benzinărie nu are destul combustibil.")
    end
  end

    local fuelCost = theLitres*tonumber(gasStation.status.fuelPrice)

    money = exports.global:getMoney(thePlayer)
    local factionPlayer = getElementData(thePlayer, "faction")
    local factionVehicle = getElementData(theVehicle, "faction")

    local factionPaid = false
    if factionPlayer == factionVehicle and factionsThatPayForFuel[factionPlayer] and factionsThatPayForFuel[factionVehicle] then
        local theTeam = getPlayerTeam(thePlayer)
        if exports.global:takeMoney(theTeam, fuelCost, true) then
            exports.fuel_notifications:addNotification(thePlayer, "info", "Angajatorul dvs. plateste pentru plin.")
            mysql:query_free("INSERT INTO wiretransfers (`from`, `to`, `amount`, `reason`, `type`) VALUES (" .. mysql:escape_string(( -getElementData( theTeam, "id" ) )) .. ", " .. mysql:escape_string(getElementData(thePlayer, "dbid")) .. ", " .. mysql:escape_string(fuelCost) .. ", '"..mysql:escape_string(theLitres).."', 9)" )

            factionPaid = true
        end
    end

    if not factionPaid then
        if (fuelCost > 0 and money > 0) then
            if not exports.donators:hasPlayerPerk(thePlayer, 7) then
        if not exports.global:takeMoney(thePlayer, fuelCost) then
          exports.fuel_notifications:addNotification(thePlayer, "error", "Nu ai destui bani la tine.")
          return
        end
            end
        else
            exports.fuel_notifications:addNotification(thePlayer, "error", "Rezervorul dvs. este plin.")
            return
        end
    end

    local loldFuel = getElementData(theVehicle, "fuel")
    local newFuel = loldFuel+theLitres
    exports.anticheat:changeProtectedElementDataEx(theVehicle, "fuel", newFuel, false)
    triggerClientEvent(thePlayer, "syncFuel", theVehicle, newFuel)

    local info = {
            {"Chitanță benzinărie"},
            {""},
        }

    if exports.donators:hasPlayerPerk(thePlayer, 7) and not factionPaid then
        table.insert(info, {"    " .. math.ceil(theLitres) .. " litrii de combustibil    - (( Gratuit ))"})
    else
        table.insert(info, {"    " .. math.ceil(theLitres) .. " litrii de combustibil    -    " .. fuelCost .. "$"})
        if factionPaid then
            table.insert(info, {"    Plătit de angajator "..tostring(exports.factions:getFactionName(factionPlayer))})
        end
    end
    table.insert(info, {"    "..exports.global:getVehicleName(theVehicle).." - "..exports.global:round(newFuel, 2).."/"..exports.global:round(getMaxFuel(theVehicle), 2).." litres"})
    triggerClientEvent(thePlayer, "hudOverlay:drawOverlayTopRight", thePlayer, info )

  --ia stock-ul din benzinarie
  local newStock = tonumber(gasStation.status.stock) - theLitres
  local newMoney = 0
  if tonumber(gasStation.status.owner) ~= -1 then
    newMoney = tonumber(gasStation.status.money) + fuelCost
  end
  if newStock < 0 then newStock = 0 end

  gasStation.status.stock = newStock
  gasStation.status.money = newMoney

  triggerClientEvent("gas:fetchOneGasStation", root, gasStation.status.id, gasStation)
  dbQuery(function(qh)

    dbFree(qh)
    return true
  end, {}, db, "UPDATE `gas_stations` SET `stock`=?, `money`=? WHERE `id`=?", newStock, newMoney, gasStation.status.id)
end
addCommandHandler("refill", refillCommand)

--cumpara benzinaria
addEvent("gas:buyGasStation", true)
addEventHandler("gas:buyGasStation", root, function(gasStation)
  if not exports.global:hasMoney(client, tonumber(gasStation.status.price)) then return false end
  exports.global:takeMoney(client, tonumber(gasStation.status.price))

  dbQuery(function(qh, player, gasStation)
    local result, numRows, lastID = dbPoll(qh, -1)
    if numRows > 0 then
      if gasStation.status.owner > 0 then
        for _, p in ipairs(getElementsByType("player")) do
          if getElementData(p, "dbid") == tonumber(gasStation.status.owner) then
            setElementData(p, "bankmoney", (getElementData(p, "bankmoney") or 0) + gasStation.status.price)
          end
        end

        local updateBankMoney = exports.mysql:query_free("UPDATE `characters` SET `bankmoney`=`bankmoney` + '" .. gasStation.status.price .."' WHERE `id`='" .. gasStation.status.owner .."'")
      end

      exports.fuel_notifications:addNotification(player, "succes", "Ai cumpărat această benzinărie cu succes!")
      local gasStationID = tostring(gasStation.status.id)
      local gasStation = gas_stations[gasStationID]
      --reloadOneGasStation(tonumber(gasStation.status.id))
      gasStation.status.owner = getElementData(player, "dbid")
      gasStation.status.price = 0
      gasStation.status.stock = 50

      triggerClientEvent("gas:fetchOneGasStation", root, gasStation.status.id, gasStation)
    end
    dbFree(qh)
  end, {client, gasStation}, db, "UPDATE `gas_stations` SET `owner`=?, `price`=?, `stock`=? WHERE `id`=?", getElementData(client, "dbid"), 0, 50, tonumber(gasStation.status.id))
end)

--reload la o benzinarie doar
function reloadOneGasStation(gasID)
  if not gas_stations[tostring(gasID)] then return false end

  destroyElement(gas_stations[tostring(gasID)].colshape)
  destroyElement(gas_stations[tostring(gasID)].pickup)
  gas_stations[tostring(gasID)] = nil

  loadOneGasStation(tonumber(gasID))
end

function loadOneGasStation(gasID)
  dbQuery(function(qh)
    local result = dbPoll(qh, -1)
    if result and #result > 0 then
      local gasData = result[1]
      gas_stations[tostring(gasData.id)] = {}
      gas_stations[tostring(gasData.id)].pos = {x = gasData.x, y = gasData.y, z = gasData.z, int = gasData.interior, dim = gasData.dimension}
      gas_stations[tostring(gasData.id)].status = {owner = gasData.owner, price = tonumber(gasData.price), stock = tonumber(gasData.stock), id = tonumber(gasData.id), money = tonumber(gasData.money), fuelPrice = tonumber(gasData.fuelPrice)}
      gas_stations[tostring(gasData.id)].colshape = createColSphere(gasData.x, gasData.y, gasData.z, 10)
      gas_stations[tostring(gasData.id)].pickup = createPickup(gasData.x, gasData.y, gasData.z, 3, 1239)
    end

    triggerClientEvent("gas:fetchGasStations", root, gas_stations)
    dbFree(qh)
  end, {}, db, "SELECT * FROM `gas_stations` WHERE `id`=?", tonumber(gasID))
end

--update la pret benzina
addEvent("gas:updateFuelPrice", true)
addEventHandler("gas:updateFuelPrice", root, function(gasStation, price)
  dbQuery(function(qh, player, gasStation)
    local result, numRows, lastID = dbPoll(qh, -1)
    if numRows > 0 then
      exports.fuel_notifications:addNotification(player, "succes", "Ai setat prețul la benzină cu succes!")
      local gasStationID = tostring(gasStation.status.id)
      local gasStation = gas_stations[gasStationID]
      --reloadOneGasStation(tonumber(gasStation.status.id))
      gasStation.status.fuelPrice = tonumber(price)

      triggerClientEvent("gas:fetchOneGasStation", root, gasStation.status.id, gasStation)
    end
    dbFree(qh)
  end, {client, gasStation}, db, "UPDATE `gas_stations` SET `fuelPrice`=? WHERE `id`=?", price, tonumber(gasStation.status.id))
end)

addEvent("gas:withdrawMoney", true)
addEventHandler("gas:withdrawMoney", root, function(gasStation, amount)
  dbQuery(function(qh, player, gasStation)
    local result, numRows, lastID = dbPoll(qh, -1)
    if numRows > 0 then
      exports.fuel_notifications:addNotification(player, "succes", "Ai scos banii cu succes din seif!")
      local gasStationID = tostring(gasStation.status.id)
      local gasStation = gas_stations[gasStationID]
      --reloadOneGasStation(tonumber(gasStation.status.id))
      exports.global:giveMoney(player, amount)
      gasStation.status.money = gasStation.status.money - tonumber(amount)

      triggerClientEvent("gas:fetchOneGasStation", root, gasStation.status.id, gasStation)
    end
    dbFree(qh)
  end, {client, gasStation}, db, "UPDATE `gas_stations` SET `money`=? WHERE `id`=?", gasStation.status.money - tonumber(amount), tonumber(gasStation.status.id))
end)

addEvent("gas:setSellPrice", true)
addEventHandler("gas:setSellPrice", root, function(gasStation, price)
  dbQuery(function(qh, player, gasStation)
    local result, numRows, lastID = dbPoll(qh, -1)
    if numRows > 0 then
      exports.fuel_notifications:addNotification(player, "succes", "Ai scos la vânzare benzinăria cu succes!")
      local gasStationID = tostring(gasStation.status.id)
      local gasStation = gas_stations[gasStationID]
      gasStation.status.price = tonumber(price)

      triggerClientEvent("gas:fetchOneGasStation", root, gasStation.status.id, gasStation)
    end
    dbFree(qh)
  end, {client, gasStation}, db, "UPDATE `gas_stations` SET `price`=? WHERE `id`=?", tonumber(price), tonumber(gasStation.status.id))
end)

addEvent("gas:updateGasStationStock", true)
addEventHandler("gas:updateGasStationStock", root, function(gasStationID, stockAmount, orderID)
  local newStock = gas_stations[tostring(gasStationID)].status.stock + tonumber(stockAmount)

  dbQuery(function(qh, player, newStock, gasStationID)
    local result, numRows, lastID = dbPoll(qh, -1)
    if numRows > 0 then

      local gasStation = gas_stations[tostring(gasStationID)]
      gasStation.status.stock = tonumber(newStock)

      triggerClientEvent("gas:fetchOneGasStation", root, gasStation.status.id, gasStation)
    end
    dbFree(qh)
  end, {client, newStock, gasStationID}, db, "UPDATE `gas_stations` SET `stock`=? WHERE `id`=?", tonumber(newStock), tonumber(gasStationID))
end)

addEvent("gas:buyStock", true)
addEventHandler("gas:buyStock", root, function(gasStation, stock)
  exports.fuel_notifications:addNotification(client, "succes", "Ai dat comandă de combustibil cu succes!")

  --reloadOneGasStation(tonumber(gasStation.status.id))
  exports.global:takeMoney(client, stock * 4)
  dbQuery(function(qh)
    dbFree(qh)
  end, {}, db, "INSERT INTO `stock_orders` (`biz_id`, `biz_type`, `stock_amount`, `x`, `y`, `z`, `trailer`) VALUES (?, ?, ?, ?, ?, ?, ?)", tonumber(gasStation.status.id), 1, tonumber(stock), gasStation.pos.x, gasStation.pos.y, gasStation.pos.z, 584)

  triggerClientEvent("gas:fetchOneGasStation", root, gasStation.status.id, gasStation)
end)

--fetch benzinarii
addEvent("gas:fetchGasStations", true)
addEventHandler("gas:fetchGasStations", root, function(temp_table)
  gas_stations = temp_table
end)

--opreste jucatorul din a lua pickup-ul lol
function serverPickupHit(matchingDimension)
    for _, pickup in pairs(gas_stations) do
      if pickup.pickup == source then
        cancelEvent()

        break
      end
    end
end
addEventHandler("onPickupHit", root, serverPickupHit)

--refillgs
function pedWillFillFuelCan(thePlayer, command)
    if not (thePlayer and isElement(thePlayer)) then
        return
    end

  local gasStation = nil
  for _, gs in pairs(gas_stations) do
    if isElementWithinColShape(thePlayer, gs.colshape) then
      gasStation = gs
      break
    end
  end

  if not gasStation then 
    return exports.fuel_notifications:addNotification(thePlayer, "error", "Nu te aflii intr-o benzinarie!")
  end

    local hasItem, itemSlot, itemValue, itemUniqueID = exports.global:hasItem(thePlayer, 57)
    if not (hasItem) then
        exports.fuel_notifications:addNotification(thePlayer, "error", "Nu ai o canistra la tine.")
        return
    end

    if itemValue >= 10 then
        exports.fuel_notifications:addNotification(thePlayer, "error", "Este deja plina.")
        return
    end

    local theLitres = 10 - itemValue
    local fuelCost = math.floor(theLitres * tonumber(gasStation.status.fuelPrice))

    local money = exports.global:getMoney(thePlayer)
    if tonumber(money) == 0 then
        exports.fuel_notifications:addNotification(thePlayer, "error", "Nu ai destui bani la tine.")
        return
    else
        if not exports.global:takeMoney(thePlayer, fuelCost) then
      exports.fuel_notifications:addNotification(thePlayer, "error", "Nu ai destui bani la tine - " .. fuelCost .. "$!")
            return
        end
    end

    if not (exports['item-system']:updateItemValue(thePlayer, itemSlot, itemValue + theLitres)) then
        exports.fuel_notifications:addNotification(thePlayer, "error", "Ups.. ceva nu a mers bine.")
        return
    end

    local info = {
            {"Gas Station Receipt"},
            {""},
            {"    " .. math.ceil(theLitres) .. " Litrii de combustibil    -    " .. fuelCost .. "$"},
        }
    triggerClientEvent(thePlayer, "hudOverlay:drawOverlayTopRight", thePlayer, info )
end
addCommandHandler("refillgs", pedWillFillFuelCan)

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