editable.lua

---@param plate string
---@param citizenid string
---@return boolean
function IsPlayerVehicle(plate, citizenid)
    if config.framework == "esx" then
        return MySQL.single.await('SELECT `plate` FROM `owned_vehicles` WHERE `plate` = ? AND `owner` = ?', { plate, citizenid }) ~= nil
    else
        return MySQL.single.await('SELECT `plate` FROM `player_vehicles` WHERE `plate` = ? AND `citizenid` = ?', { plate, citizenid }) ~= nil
    end
end

---@param plate string
---@return false | table
function GetPLayerVehicleData(plate)
    if config.framework == "esx" then
        local playerVehicle = MySQL.single.await('SELECT `vehicle` FROM `owned_vehicles` WHERE `plate` = ?', { plate })
        return playerVehicle and json.decode(playerVehicle.vehicle) or false
    else
        local playerVehicle = MySQL.single.await('SELECT `mods` AS `vehicle` FROM `player_vehicles` WHERE `plate` = ?', { plate })
        return playerVehicle and json.decode(playerVehicle.mods) or false
    end
end

---@param citizenid string
---@param jobname string
function GetPlayerVehicles(citizenid, jobname)
    if config.framework == "esx" then
        if jobname then
            return MySQL.query.await('SELECT `vehicle` FROM `owned_vehicles` WHERE `owner` = ? AND job = ?', { citizenid, jobname })
        else
            return MySQL.query.await('SELECT `vehicle` FROM `owned_vehicles` WHERE `owner` = ?', { citizenid })
        end
    else
        if jobname then
            return MySQL.query.await('SELECT `mods` AS `vehicle` FROM `player_vehicles` WHERE `citizenid` = ? AND job = ?', { citizenid, jobname })
        else
            return MySQL.query.await('SELECT `mods` AS `vehicle` FROM `player_vehicles` WHERE `citizenid` = ?', { citizenid })
        end
    end
end

---@param plate string
---@param properties table
function SaveVehicleProperties(plate, properties)
    if config.framework == "esx" then
        MySQL.update('UPDATE `owned_vehicles` SET vehicle = ? WHERE plate = ?', { json.encode(properties), plate })
    else
        MySQL.update('UPDATE `player_vehicles` SET mods = ? WHERE plate = ?', { json.encode(properties), plate })
    end
end

---@param properties table
---@param isImpound boolean
function getVehData(properties, isImpound)
    if not properties then return end
    local vehicleIsInsuranced, towing = GetInsurance(properties.plate)

    return {
        model = properties.model,
        plate = properties.plate,
        fuel = (isImpound and config.impound.setFuel.active) and config.impound.setFuel.value or (properties.fuelLevel or 0),
        body = (properties.bodyHealth or 1000) / 10,
        engine = (properties.engineHealth or 1000) / 10,
        vehicleIsInsuranced = vehicleIsInsuranced,
        towing = towing,
    }
end

---@param jobname string
---@param garageKey string
---@param vehicles table
---@param citizenid string
---@return table
function getPlayerVehiclesInGarageKey(jobname, garageKey, vehicles, citizenid)
    local vehicleData = {}
    if vehicles then
        for _, data in pairs(vehicles) do
            local vehData = getVehData(data.properties)
            if vehData then
                vehData.parkedOnGarage = true
                vehicleData[#vehicleData + 1] = vehData
            end
        end
    end

    local playerVehicles = GetPlayerVehicles(citizenid, jobname)
    for i = 1, #playerVehicles do
        playerVehicles[i].vehicle = json.decode(playerVehicles[i].vehicle)
        local parkedAnyGarage = IsVehicleParkedAnyGarage(playerVehicles[i].vehicle.plate)
        if parkedAnyGarage then
            if parkedAnyGarage.garageKey ~= garageKey then
                local vehData = getVehData(playerVehicles[i].vehicle)
                if vehData then
                    vehData.parkedOnGarage = false
                    vehData.isImpounded = false
                    vehData.otherGarageData = parkedAnyGarage
                    vehicleData[#vehicleData + 1] = vehData
                end
            end
        elseif config.showImpoundedVehiclesOnGarageMenu then
            local vehData = getVehData(playerVehicles[i].vehicle)
            if vehData then
                vehData.isImpounded = true
                vehicleData[#vehicleData + 1] = vehData
            end
        end
    end

    return vehicleData
end

---@param cid string
---@param jobname string
---@return table
function GetPlayerImpoundVehicles(cid, jobname)
    local vehicleData = {}
    local playerVehicles = GetPlayerVehicles(cid, jobname)

    if playerVehicles then
        for i = 1, #playerVehicles do
            local data = playerVehicles[i]
            local properties = json.decode(data.vehicle)
            if not IsVehicleParkedAnyGarage(properties.plate) then
                local vehData = getVehData(properties, true)
                if vehData then
                    local impoundData = GetImpoundVehicle(properties.plate)
                    if impoundData then
                        vehData.impound = impoundData.Functions.GetData()
                    end

                    vehicleData[#vehicleData + 1] = vehData
                end
            end
        end
    end
    return vehicleData
end

---@return string
function GeneratePlate()
    local plate = ""
    for i = 1, #config.platePatern do
        plate = plate .. (string.sub(config.platePatern, i, i) == "1" and RandomInt(1) or RandomStr(1))
    end
    if config.framework == "esx" then
        local result = MySQL.query.await('SELECT plate FROM owned_vehicles WHERE plate = ?', { plate })
        if result[1] then
            return GeneratePlate()
        else
            return plate:upper()
        end
    else
        local result = MySQL.query.await('SELECT plate FROM player_vehicles WHERE plate = ?', { plate })
        if result[1] then
            return GeneratePlate()
        else
            return plate:upper()
        end
    end
end

---@param vehicleType "car" | "air" | "boat"
---@param vehicleProperties table
---@param carData table
---@param xPlayer table
function BuyVehicle(vehicleType, carData, vehicleProperties, xPlayer)
    local citizenid = tgiCore.getCid(xPlayer)
    local jobname = tgiCore.getJob(xPlayer).name
    local encodedVehicleProperties = json.encode(vehicleProperties)

    if config.framework == "esx" then
        local scriptTypeToDbType = {
            car = "car",
            air = "helicopter",
            boat = "boat",
        }

        MySQL.query('INSERT INTO `owned_vehicles` (owner, plate, vehicle, type, job, stored) VALUES (?, ?, ?, ?, ?, 0)', {
            citizenid,
            vehicleProperties.plate,
            encodedVehicleProperties,
            scriptTypeToDbType[vehicleType],
            jobname,
        })
    else
        MySQL.query('INSERT INTO `player_vehicles` (license, citizenid, plate, mods, job, hash, vehicle) VALUES (?, ?, ?, ?, ?, ?, ?)', {
            xPlayer.PlayerData.license,
            xPlayer.PlayerData.citizenid,
            vehicleProperties.plate,
            encodedVehicleProperties,
            jobname,
            vehicleProperties.model,
            carData.model
        })
    end
end

---@param plate string
function GetInsurance(plate)
    local result = MySQL.single.await('SELECT insurance FROM tgiann_realparking_insurance WHERE plate = ?', { plate })
    if result then
        local data = json.decode(result.insurance)
        return isInsuranced(data.time), data.towing
    else
        return false, 1
    end
end

exports("getInsurance", GetInsurance)
exports("GetInsurance", GetInsurance)

---@param oldPlate string
---@param newPlate string
function ChangedVehiclePlate(oldPlate, newPlate)
    MySQL.update('UPDATE `tgiann_realparking_insurance` SET plate = ? WHERE plate = ?', { newPlate, oldPlate })
end

exports("changedVehiclePlate", ChangedVehiclePlate)
exports("ChangedVehiclePlate", ChangedVehiclePlate)

---@param src number
---@return boolean
function isAdmin(src)
    if config.framework == "qb" then
        return IsPlayerAceAllowed(src, "command") -- refarance: https://github.com/qbcore-framework/qb-adminmenu/blob/main/server/server.lua#L52
    else
        local xPlayer = tgiCore.getPlayer(src)
        return xPlayer.getGroup(src) == "admin"
    end
end

Last updated