mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 01:08:54 +00:00
📦 Implemented new better createJob method
This commit is contained in:
@@ -30,7 +30,8 @@ server_scripts {
|
||||
'common/modules/*.lua',
|
||||
'common/functions.lua',
|
||||
'server/modules/actions.lua',
|
||||
'server/modules/npwd.lua'
|
||||
'server/modules/npwd.lua',
|
||||
'server/modules/createJob.lua'
|
||||
}
|
||||
|
||||
client_scripts {
|
||||
|
||||
@@ -406,37 +406,6 @@ function ESX.DiscordLogFields(name, title, color, fields)
|
||||
)
|
||||
end
|
||||
|
||||
--- Create Job at Runtime
|
||||
--- @param name string
|
||||
--- @param label string
|
||||
--- @param grades table
|
||||
function ESX.CreateJob(name, label, grades)
|
||||
if not name then
|
||||
return print("[^3WARNING^7] missing argument `name(string)` while creating a job")
|
||||
end
|
||||
|
||||
if not label then
|
||||
return print("[^3WARNING^7] missing argument `label(string)` while creating a job")
|
||||
end
|
||||
|
||||
if not grades or not next(grades) then
|
||||
return print("[^3WARNING^7] missing argument `grades(table)` while creating a job!")
|
||||
end
|
||||
|
||||
local parameters = {}
|
||||
local job = { name = name, label = label, grades = {} }
|
||||
|
||||
for _, v in pairs(grades) do
|
||||
job.grades[tostring(v.grade)] = { job_name = name, grade = v.grade, name = v.name, label = v.label, salary = v.salary, skin_male = v.skin_male or "{}", skin_female = v.skin_female or "{}" }
|
||||
parameters[#parameters + 1] = { name, v.grade, v.name, v.label, v.salary, v.skin_male or "{}", v.skin_female or "{}" }
|
||||
end
|
||||
|
||||
MySQL.insert("INSERT IGNORE INTO jobs (name, label) VALUES (?, ?)", { name, label })
|
||||
MySQL.prepare("INSERT INTO job_grades (job_name, grade, name, label, salary, skin_male, skin_female) VALUES (?, ?, ?, ?, ?, ?, ?)", parameters)
|
||||
|
||||
ESX.Jobs[name] = job
|
||||
end
|
||||
|
||||
function ESX.RefreshJobs()
|
||||
local Jobs = {}
|
||||
local jobs = MySQL.query.await("SELECT * FROM jobs")
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
|
||||
local NOTIFY_TYPES = {
|
||||
INFO = "^5[%s]^7-^6[INFO]^7 %s",
|
||||
SUCCESS = "^5[%s]^7-^2[SUCCESS]^7 %s",
|
||||
ERROR = "^5[%s]^7-^1[ERROR]^7 %s"
|
||||
}
|
||||
|
||||
local function doesJobAndGradesExist(name, grades)
|
||||
local jobExists = false
|
||||
for _, grade in ipairs(grades) do
|
||||
if ESX.DoesJobExist(name, grade.grade) then
|
||||
jobExists = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return jobExists
|
||||
end
|
||||
|
||||
local function generateTransactionQueries(name,grades)
|
||||
local queries = {}
|
||||
for _, grade in ipairs(grades) do
|
||||
queries[#queries+1] = {
|
||||
query = 'INSERT INTO job_grades (job_name, grade, name, label, salary, skin_male, skin_female) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
values = {name, grade.grade, grade.name, grade.label, grade.salary, '{}', '{}'}
|
||||
}
|
||||
end
|
||||
|
||||
return queries
|
||||
end
|
||||
|
||||
local function generateNewJobTable(name, label, grades)
|
||||
local job = { name = name, label = label, grades = {} }
|
||||
for _, v in pairs(grades) do
|
||||
job.grades[tostring(v.grade)] = { job_name = name, grade = v.grade, name = v.name, label = v.label, salary = v.salary, skin_male = {}, skin_female = {} }
|
||||
end
|
||||
|
||||
return job
|
||||
end
|
||||
|
||||
local function notify(notifyType,resourceName,message,...)
|
||||
local formattedMessage = string.format(message, ...)
|
||||
|
||||
if not NOTIFY_TYPES[notifyType] then
|
||||
return print(NOTIFY_TYPES.INFO:format(resourceName,formattedMessage))
|
||||
end
|
||||
|
||||
return print(NOTIFY_TYPES[notifyType]:format(resourceName,formattedMessage))
|
||||
end
|
||||
|
||||
--- Create Job at Runtime
|
||||
--- @param name string
|
||||
--- @param label string
|
||||
--- @param grades table
|
||||
function ESX.CreateJob(name, label, grades)
|
||||
local currentResourceName = GetInvokingResource()
|
||||
local success = false
|
||||
|
||||
if not name or name == '' then
|
||||
notify("ERROR",currentResourceName, 'Missing argument `name`')
|
||||
return
|
||||
end
|
||||
if not label or label == '' then
|
||||
notify("ERROR",currentResourceName, 'Missing argument `label`')
|
||||
return
|
||||
end
|
||||
if not grades or not next(grades) then
|
||||
notify("ERROR",currentResourceName, 'Missing argument `grades`')
|
||||
return
|
||||
end
|
||||
|
||||
local currentJobExist = doesJobAndGradesExist(name, grades)
|
||||
|
||||
if currentJobExist then
|
||||
notify("ERROR",currentResourceName, 'Job or grades already exists: `%s`', name)
|
||||
return
|
||||
end
|
||||
|
||||
MySQL.insert('INSERT IGNORE INTO jobs (name, label) VALUES (?, ?)', {name, label}, function(jobId)
|
||||
if not jobId == 0 then
|
||||
notify("ERROR",currentResourceName, 'Failed to insert job: `%s`', name)
|
||||
return
|
||||
end
|
||||
|
||||
local queries = generateTransactionQueries(name, grades)
|
||||
|
||||
MySQL.transaction(queries, function(results)
|
||||
success = results
|
||||
if not results then
|
||||
notify("ERROR",currentResourceName, 'Failed to insert one or more grades for job: `%s`', name)
|
||||
return
|
||||
end
|
||||
|
||||
ESX.Jobs[name] = generateNewJobTable(name,label,grades)
|
||||
notify("SUCCESS",currentResourceName, 'Job created successfully: `%s`', name)
|
||||
end)
|
||||
end)
|
||||
|
||||
return success
|
||||
end
|
||||
Reference in New Issue
Block a user