Deltarix Scripts
TebexDiscord
  • Information
    • About Me - Deltarix
  • Keymaster
    • FiveM Asset Escrow
    • Authentication
    • Transfers
  • Paid Assets
    • MDT V3
      • Setup Guide
      • Configurations
        • Settings
        • Graphics
        • Locales
        • Themes
      • Exports
        • Server
          • OpenMDT
          • CloseMDT
          • CreateLog
          • GetBoloByLinkedId
          • GetIdentifierByStateId
          • GetOfficerByBadgenumber
          • GetOfficerByStateId
          • GetOfficerExists
          • GetOfficerFiredByStateId
          • GetOfficerSuspendedByStateId
          • GetPlayerFullNameByStateId
          • GetStateIdBySource
          • GetWantedByLinkedId
      • Public
        • Bridges
          • Server
            • Framework
              • qbx_core
              • qb-core
              • es_extended
            • Garage
              • qbx_garages
              • qb-garages
              • jg-advancedgarages
            • Inventory
              • ox_inventory
            • Licence
              • qbx_core
              • qb-core
            • Media
              • fivemanage
            • Property
              • qbx_properties
              • qb-apartments
              • qb-houses
              • qb-apartments & qb-housing
              • nolag_properties
              • qbx_properties & nolag_properties
        • Functions
          • Server
            • Create Card
            • Log
            • Prints
          • Client
            • Waypoint
            • Anim
      • Dependencies
    • MDT V2
      • Setup
      • Locales
      • Configurations
        • Server
        • Client
        • Settings
        • 10 Codes
        • Commands
      • Exports
        • Server
          • OpenMDT
          • OpenVehicleDetailsInMDT
          • GetStateId
          • GetOfficer
          • GetPlayerWanted
          • GetPlateFlagged
          • GetOfficerSuspended
          • GetWeaponRegistered
          • GetOfficerClockedIn
          • GetClosestCamera
          • CreateWeapon
          • CreateOfficer
          • DeleteOfficer
          • UpdateCitizenData
          • UpdateCitizenPoints
          • ToggleCamera
          • ToggleCameraTimeout
          • ToggleClock
        • Client
          • OpenMDT
          • InsertPhotoToGallery
      • Bridges
        • Server
          • Framework
            • es_extended
            • qb-core
            • qbx_core
          • Garage
            • esx_garage
            • qb-garages
            • qbx_garages
          • Inventory
            • ox_inventory
            • qb-inventory
          • Licence
            • es_extended
            • qb-core
            • qbx_core
          • Property
            • esx_property
            • qb-apartments
            • qbx_apartments
          • Utilities
            • Logger
        • Client
          • Garage
            • esx_garage
            • qb-garages
            • qbx_garages
          • Utilities
            • Camera
      • Dependencies
      • Known Bugs
      • Common Issues and Troubleshooting
    • Dispatch
      • Exports
        • Server
          • Notifications
          • IsDispatcher
          • GetDispatchersByGroup
          • GetGroups
          • GetStateId
        • Client
          • defaultNotification
          • officerNotification
          • dispatchNotification
          • Copy of dispatchNotification
      • Dependencies
Powered by GitBook
On this page

Was this helpful?

  1. Paid Assets
  2. MDT V3
  3. Public
  4. Bridges
  5. Server
  6. Licence

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 5 days ago

Was this helpful?