From 01080a12b257a50d625154072887558c59ad4f95 Mon Sep 17 00:00:00 2001 From: Solareon <769465+solareon@users.noreply.github.com> Date: Sat, 20 Jul 2024 22:04:53 +0200 Subject: [PATCH] feat: more bridge functions for qbcore --- bridge/qb.lua | 84 ++++++++++++++++++++++++++++++---- bridge/qbx.lua | 24 ++++++++++ client/main.lua | 40 ++++------------ fxmanifest.lua | 1 + server/main.lua | 4 +- {bridge => server}/storage.lua | 28 +++++++++--- 6 files changed, 133 insertions(+), 48 deletions(-) rename {bridge => server}/storage.lua (52%) diff --git a/bridge/qb.lua b/bridge/qb.lua index 1bae148..8384bf8 100644 --- a/bridge/qb.lua +++ b/bridge/qb.lua @@ -11,39 +11,49 @@ end JOBS = GetJobs() if isServer then + + -- Get player object function GetPlayer(source) return QBCore.Functions.GetPlayer(source) end + -- Get player data function GetPlayerData(source) return GetPlayer(source).PlayerData end + -- Notify player function Notify(source, message, type) return QBCore.Functions.Notify(source, message, type) end + -- Compare job to current job function CheckCurrentJob(source, job) - local playerData = GetPlayerData(source) - return playerData.job.name == job + return GetPlayerData(source).job.name == job end + -- Validate if job can be set function CanSetJob(source, jobName) - local playerData = GetPlayerData(source) - local jobs = playerData.jobs - for job, _ in pairs(jobs) do - if jobName == job then + local jobs = GetPlayerJobs(GetPlayerData(source).citizenid) + for _, job in pairs(jobs) do + if jobName == job.job then return true end end return false end - function SetPlayerJob(source, job) - return exports.qbx_core:SetPlayerPrimaryJob(GetPlayerData(source).citizenid, job) + -- Set players job + function SetPlayerJob(source, jobData) + return GetPlayer(source).Functions.SetJob(jobData.job, jobData.grade) end + -- Remove job from storage function RemovePlayerFromJob(source, job) + if CheckCurrentJob(source, job) then + GetPlayer(source).Functions.SetJob('unemployed', 0) + return + end return exports.qbx_core:RemovePlayerFromJob(GetPlayerData(source).citizenid, job) end @@ -51,9 +61,39 @@ if isServer then return GetPlayer(source).Functions.SetJobDuty(duty) end + local function adminRemoveJob(src, playerData, job) + if DoesPlayerHaveJob(playerData.citizenid, job) then + DeletePlayerJob(playerData.citizenid, job) + Notify(src, ('Job: %s was removed from ID: %s'):format(job, playerData.source), 'success') + if playerData.job.name == job then + local player = GetPlayer(playerData.source) + player.Functions.SetJob('unemployed', 0) + end + else + Notify(src, 'Player doesn\'t have this job?', 'error') + end + end + + lib.addCommand('removejob', { + help = "Remove a job from the player's multijob.", + params = { + { name = 'id', help = 'Server ID of the player', type = 'playerId' }, + { name = 'job', help = 'Name of Job to remove', type = 'string' } + }, restricted = 'group.admin' + }, function(source, args) + local playerData = GetPlayerData(args['id']) + if not playerData then + Notify(source, 'Player not online.', 'error') + return + end + + adminRemoveJob(source, playerData, args['job']) + end) + AddEventHandler('QBCore:Server:OnJobUpdate', function(playerSource, job) + if job.name == 'unemployed' then return end local citizenid = GetPlayerData(playerSource).citizenid - if GetPlayerJob(citizenid, job) or GetPlayerJobCount(citizenid) then return end + if GetPlayerJobWithGrade(citizenid, job) or GetPlayerJobCount(citizenid) then return end AddPlayerJob(citizenid, job.name, job.grade) end) @@ -82,6 +122,32 @@ else return QBCore.Functions.Notify(message, type) end + function ToggleDuty() + return TriggerServerEvent('QBCore:ToggleDuty') + end + + function GetPlayerJobs() + local playerData = GetPlayerData() + local jobMenu = {} + local playerJobs = lib.callback.await('slrn_multijob:server:myJobs', false) + for _, job in pairs(playerJobs) do + local jobData = JOBS[job] + local primaryJob = playerData.job.name == job.job + jobMenu[#jobMenu + 1] = { + currentJob = primaryJob, + title = jobData.label, + grade = jobData.grades[job.grade], + jobName = job.job, + duty = primaryJob and playerData.job.onduty or false, + } + end + return jobMenu + end + + RegisterNetEvent('QBCore:Client:OnJobUpdate', function() + exports["lb-phone"]:SendCustomAppMessage('slrn_multijob', { action = 'update-jobs' }) + end) + AddEventHandler('QBCore:Client:OnSharedUpdate', function(data) Wait(0) if data ~= 'Jobs' then return end diff --git a/bridge/qbx.lua b/bridge/qbx.lua index 190449e..bd0f263 100644 --- a/bridge/qbx.lua +++ b/bridge/qbx.lua @@ -64,6 +64,30 @@ else return exports.qbx_core:Notify(message, type) end + function GetJobs() + local playerData = GetPlayerData() + local jobMenu = {} + for job, grade in pairs(playerData.jobs) do + local jobData = JOBS[job] + jobMenu[#jobMenu + 1] = { + currentJob = playerData.job.name == job, + title = jobData.label, + grade = jobData.grades[grade], + jobName = job, + duty = playerData.job.name == job and playerData.job.onduty or false, + } + end + return jobMenu + end + + function ToggleDuty() + return TriggerServerEvent('QBCore:ToggleDuty') + end + + RegisterNetEvent('QBCore:Client:OnJobUpdate', function() + exports["lb-phone"]:SendCustomAppMessage('slrn_multijob', { action = 'update-jobs' }) + end) + AddEventHandler('qbx_core:client:onJobUpdate', function() JOBS = GetJobs() end) diff --git a/client/main.lua b/client/main.lua index f8053d8..6e84c1a 100644 --- a/client/main.lua +++ b/client/main.lua @@ -37,45 +37,25 @@ CreateThread(function() end) end) +RegisterNUICallback('getPlayerData', function(_, cb) + cb(GetPlayerData()) +end) + RegisterNUICallback('getJobs', function(_, cb) - local PlayerData = QBX.PlayerData - local jobMenu = {} - for job, grade in pairs(PlayerData.jobs) do - local isDisabled = PlayerData.job.name == job - local jobData = sharedJobs[job] - jobMenu[#jobMenu + 1] = { - title = jobData.label, - description = ('Grade: %s [%s]
Salary: $%s'):format(jobData.grades[grade].name, grade, jobData.grades[grade].payment), - disabled = isDisabled, - jobName = job, - duty = isDisabled and PlayerData.job.onduty or false, - } - end - cb(jobMenu) + cb(GetJobs()) end) RegisterNUICallback('toggleDuty', function(_, cb) - TriggerServerEvent('QBCore:ToggleDuty') - cb(true) - Wait(500) - exports["lb-phone"]:SendCustomAppMessage('slrn_multijob', { action = 'update-jobs' }) + ToggleDuty() + cb({}) end) RegisterNUICallback('removeJob', function(job, cb) TriggerServerEvent('slrn_multijob:server:deleteJob', job) - lib.callback('slrn_multijob:server:deleteJob', false, function() - cb(true) - exports["lb-phone"]:SendCustomAppMessage('slrn_multijob', { action = 'update-jobs' }) - end, job) + cb({}) end) RegisterNUICallback('changeJob', function(job, cb) - lib.callback('slrn_multijob:server:changeJob', false, function() - cb(true) - exports["lb-phone"]:SendCustomAppMessage('slrn_multijob', { action = 'update-jobs' }) - end, job) + TriggerServerEvent('slrn_multijob:server:changeJob', job) + cb({}) end) - -RegisterNetEvent('QBCore:Client:OnJobUpdate', function() - exports["lb-phone"]:SendCustomAppMessage('slrn_multijob', { action = 'update-jobs' }) -end) \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua index 0a62ba5..52bd55f 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -20,6 +20,7 @@ client_scripts { server_scripts { '@oxmysql/lib/MySQL.lua', 'server/main.lua', + 'server/storage.lua' } files { diff --git a/server/main.lua b/server/main.lua index ca0972e..2c9eeb8 100644 --- a/server/main.lua +++ b/server/main.lua @@ -18,8 +18,8 @@ RegisterNetEvent('slrn_multijob:server:changeJob', function(job) end if not CanSetJob(src, job) then return end - - SetPlayerJob(src, job) + local setJob = GetPlayerJob(src) + SetPlayerJob(src, setJob) Notify(src, ('Your job is now: %s'):format(JOBS[job].label)) SetPlayerDuty(false) return true diff --git a/bridge/storage.lua b/server/storage.lua similarity index 52% rename from bridge/storage.lua rename to server/storage.lua index 7e3e9c7..681901a 100644 --- a/bridge/storage.lua +++ b/server/storage.lua @@ -1,9 +1,15 @@ -if not IsDuplicityVersion() then return end - local config = lib.load('config') function GetPlayerJob(identifier, job) - local result = MySQL.Sync.fetchAll('SELECT * FROM save_jobs WHERE cid = ? AND job = ? and grade = ?', { identifier, job.name, job.grade }) + local result = MySQL.query.fetchAll('SELECT * FROM save_jobs WHERE cid = ? AND job = ?', { identifier, job.name}) + if result[1] then + return result[1] + end + return nil +end + +function GetPlayerJobWithGrade(identifier, job) + local result = MySQL.query.fetchAll('SELECT * FROM save_jobs WHERE cid = ? AND job = ? and grade = ?', { identifier, job.name, job.grade }) if result[1] then return result[1] end @@ -11,7 +17,7 @@ function GetPlayerJob(identifier, job) end function GetPlayerJobs(identifier) - return MySQL.Sync.fetchAll('SELECT * FROM save_jobs WHERE cid = ?', { identifier }) + return MySQL.query.fetchAll('SELECT * FROM save_jobs WHERE cid = ?', { identifier }) end function SortPlayerJobs(identifier) @@ -42,7 +48,7 @@ function SortPlayerJobs(identifier) end function GetPlayerJobCount(identifier) - local result = MySQL.Sync.await('SELECT COUNT(*) as count FROM save_jobs WHERE cid = ?', { identifier }) + local result = MySQL.query.await('SELECT COUNT(*) as count FROM save_jobs WHERE cid = ?', { identifier }) if result[1] then return result[1].count >= config.maxJobs end @@ -50,9 +56,17 @@ function GetPlayerJobCount(identifier) end function AddPlayerJob(identifier, job, grade) - return MySQL.Sync.await('INSERT INTO save_jobs (cid, job, grade) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE grade = VALUES(grade)', { identifier, job, grade }) + return MySQL.query.await('INSERT INTO save_jobs (cid, job, grade) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE grade = VALUES(grade)', { identifier, job, grade }) +end + +function DoesPlayerHaveJob(identifier, job) + local result = MySQL.query.await('SELECT * FROM save_jobs WHERE cid = ? AND job = ?', { identifier, job }) + if result[1] then + return true + end + return false end function DeletePlayerJob(identifier, job) - return MySQL.Sync.await('DELETE FROM save_jobs WHERE cid = ? AND job = ?', { identifier, job }) + return MySQL.query.await('DELETE FROM save_jobs WHERE cid = ? AND job = ?', { identifier, job }) end \ No newline at end of file