Skip to content

Commit

Permalink
feat: more bridge functions for qbcore
Browse files Browse the repository at this point in the history
  • Loading branch information
solareon committed Jul 20, 2024
1 parent 8d313d3 commit 01080a1
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 48 deletions.
84 changes: 75 additions & 9 deletions bridge/qb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,89 @@ 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

function SetPlayerDuty(source, duty)
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)

Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions bridge/qbx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 10 additions & 30 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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] <br /> 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)
1 change: 1 addition & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ client_scripts {
server_scripts {
'@oxmysql/lib/MySQL.lua',
'server/main.lua',
'server/storage.lua'
}

files {
Expand Down
4 changes: 2 additions & 2 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions bridge/storage.lua → server/storage.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
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
return nil
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)
Expand Down Expand Up @@ -42,17 +48,25 @@ 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
return false
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

0 comments on commit 01080a1

Please sign in to comment.