Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📦 Implemented new better createJob method #1355

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion [core]/es_extended/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 0 additions & 31 deletions [core]/es_extended/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
102 changes: 102 additions & 0 deletions [core]/es_extended/server/modules/createJob.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

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)
if not ESX.Jobs[name] then
return false
end

for _, grade in ipairs(grades) do
if not ESX.DoesJobExist(name, grade.grade) then
return false
end
end

return true
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
Loading