qbx_core

if not GetResourceState('qbx_core') == 'started' then return end

local serverLicenses = {
  {
    key = 'driver', -- This is the key that will be used
    label = 'Drivers Licence', -- This is the label that will be displayed in the frontend
    enabled = true, -- If the licence is enabled or not
    canGrant = true, -- If the licence can be granted
    canRevoke = true, -- If the licence can be revoked
  },
  {
    key = 'weapon',
    label = 'Weapon Licence',
    enabled = true,
    canGrant = false,
    canRevoke = true,
  },
  {
    key = 'business',
    label = 'Business Licence',
    enabled = true,
    canGrant = false,
    canRevoke = true,
  }
}

Licence = {

  --- Fetches and returns player licences
  --- @param stateid string The state ID of the player
  --- @return table<key: string, label: string, enabled: boolean, canGrant: boolean, canRevoke: boolean, valid: boolean>
  getPlayerLicences = function(stateid)
    local result = MySQL.single.await(
      [[
        SELECT
          JSON_UNQUOTE(JSON_EXTRACT(`metadata`, '$.licences')) as licences
        FROM
          players
        WHERE
          citizenid = ?
      ]],
      { stateid }
    )

    if not result or not result.licences then
      return {}
    end

    local playerLicenses = json.decode(result.licences)
    if not playerLicenses then
      prints.error('Failed to parse licences for stateid: %s', stateid)
      return {}
    end

    local licensesArray = {}
    for _, license in ipairs(serverLicenses) do
      table.insert(licensesArray, {
        key = license.key,
        label = license.label,
        enabled = license.enabled,
        canGrant = license.canGrant,
        canRevoke = license.canRevoke,
        valid = playerLicenses[license.key] or false,
      })
    end

    return licensesArray
  end,

  --- Updates player licenses for both online and offline players
  --- @param stateid string The state ID of the player
  --- @param data table A table of licenses to update with keys: { key, value }
  --- @return boolean
  setPlayerLicences = function(stateid, data)
    local player = exports.qbx_core:GetPlayerByCitizenId(stateid)
    local playerLicenses

    if player then
      playerLicenses = player.PlayerData.metadata.licences

      for _, licenceUpdate in ipairs(data) do
        print('Updating license:', licenceUpdate.key, licenceUpdate.value)
        playerLicenses[licenceUpdate.key] = licenceUpdate.value
      end

      player.Functions.SetMetaData('licences', playerLicenses)
      prints.info('Updated licenses for online player with stateid: %s', stateid)
    else
      local result = MySQL.single.await(
        [[
          SELECT
            JSON_UNQUOTE(JSON_EXTRACT(`metadata`, '$.licences')) as licences
          FROM
            players
          WHERE
            citizenid = ?
        ]],
        { stateid }
      )

      if result and result.licences then
        playerLicenses = json.decode(result.licences)

        for _, licenceUpdate in ipairs(data) do
          playerLicenses[licenceUpdate.key] = licenceUpdate.value
        end

        prints.info('Updated licenses for offline player with stateid: %s', stateid)
      else
        prints.error('Failed to update licenses for stateid: %s', stateid)
        return false
      end
    end

    local updateResult = MySQL.update.await(
      [[
        UPDATE players
        SET metadata = JSON_SET(metadata, '$.licences', ?)
        WHERE citizenid = ?
      ]],
      { json.encode(playerLicenses), stateid }
    )

    if updateResult and updateResult > 0 then
      return true
    else
      prints.error('Failed to update licenses in database for stateid: %s', stateid)
      return false
    end
  end
}

Last updated

Was this helpful?