-
Notifications
You must be signed in to change notification settings - Fork 18
/
sv_jobs.lua
165 lines (145 loc) · 6.36 KB
/
sv_jobs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
local Config = lib.require('config')
local function GetJobCount(cid)
local result = MySQL.query.await('SELECT COUNT(*) as jobCount FROM save_jobs WHERE cid = ?', {cid})
local jobCount = result[1].jobCount
return jobCount
end
local function CanSetJob(cid, jobName)
local jobs = MySQL.query.await('SELECT job, grade FROM save_jobs WHERE cid = ? ', {cid})
if not jobs then return false end
for i = 1, #jobs do
if jobs[i].job == jobName then
return true, jobs[i].grade
end
end
return false
end
lib.callback.register('randol_multijob:server:myJobs', function(source)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local storeJobs = {}
local result = MySQL.query.await('SELECT * FROM save_jobs WHERE cid = ?', {Player.PlayerData.citizenid})
for k, v in pairs(result) do
local job = QBCore.Shared.Jobs[v.job]
if not job then
return error(('MISSING JOB FROM jobs.lua: "%s" | CITIZEN ID: %s'): format(v.job, Player.PlayerData.citizenid))
end
local grade = job.grades[tostring(v.grade)]
if not grade then
return error(('MISSING JOB GRADE for "%s". GRADE MISSING: %s | CITIZEN ID: %s'): format(v.job, v.grade, Player.PlayerData.citizenid))
end
storeJobs[#storeJobs + 1] = {
job = v.job,
salary = grade.payment,
jobLabel = job.label,
gradeLabel = grade.name,
grade = v.grade,
}
end
return storeJobs
end)
RegisterNetEvent('randol_multijob:server:changeJob', function(job)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player.PlayerData.job.name == job then
QBCore.Functions.Notify(src, 'Your current job is already set to this.', 'error')
return
end
local jobInfo = QBCore.Shared.Jobs[job]
if not jobInfo then
QBCore.Functions.Notify(src, 'Invalid job.', 'error')
return
end
local cid = Player.PlayerData.citizenid
local canSet, grade = CanSetJob(cid, job)
if not canSet then
return
end
Player.Functions.SetJob(job, grade)
Player.Functions.SetJobDuty(false)
TriggerClientEvent('QBCore:Client:SetDuty', src, false)
QBCore.Functions.Notify(src, 'Your job is now: ' .. jobInfo.label)
end)
RegisterNetEvent('randol_multijob:server:newJob', function(newJob)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local hasJob = false
local cid = Player.PlayerData.citizenid
if newJob.name == 'unemployed' then return end
local result = MySQL.query.await('SELECT * FROM save_jobs WHERE cid = ? AND job = ?', {cid, newJob.name})
if result[1] then
MySQL.query.await('UPDATE save_jobs SET grade = ? WHERE job = ? and cid = ?', {newJob.grade.level, newJob.name, cid})
hasJob = true
return
end
if not hasJob and GetJobCount(cid) < Config.MaxJobs then
MySQL.insert.await('INSERT INTO save_jobs (cid, job, grade) VALUE (?, ?, ?)', {cid, newJob.name, newJob.grade.level})
else
return QBCore.Functions.Notify(src, 'You have the max amount of jobs.', 'error')
end
end)
RegisterNetEvent('randol_multijob:server:deleteJob', function(job)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
MySQL.query.await('DELETE FROM save_jobs WHERE cid = ? and job = ?', {Player.PlayerData.citizenid, job})
QBCore.Functions.Notify(src, 'You deleted '..QBCore.Shared.Jobs[job].label..' job from your menu.')
if Player.PlayerData.job.name == job then
Player.Functions.SetJob('unemployed', 0)
end
end)
RegisterNetEvent('qb-bossmenu:server:FireEmployee', function(target) -- Removes job when fired from qb-bossmenu.
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local Employee = QBCore.Functions.GetPlayerByCitizenId(target)
if Employee then
local oldJob = Employee.PlayerData.job.name
MySQL.query.await('DELETE FROM save_jobs WHERE cid = ? AND job = ?', {Employee.PlayerData.citizenid, oldJob})
else
local player = MySQL.query.await('SELECT * FROM players WHERE citizenid = ? LIMIT 1', { target })
if player[1] then
Employee = player[1]
Employee.job = json.decode(Employee.job)
if Employee.job.grade.level > Player.PlayerData.job.grade.level then return end
MySQL.query.await('DELETE FROM save_jobs WHERE cid = ? AND job = ?', {target, Employee.job.name})
end
end
end)
local function adminRemoveJob(src, id, job)
local Player = QBCore.Functions.GetPlayer(id)
local cid = Player.PlayerData.citizenid
local result = MySQL.query.await('SELECT * FROM save_jobs WHERE cid = ? AND job = ?', {cid, job})
if result[1] then
MySQL.query.await('DELETE FROM save_jobs WHERE cid = ? AND job = ?', {cid, job})
QBCore.Functions.Notify(src, ('Job: %s was removed from ID: %s'):format(job, id), 'success')
if Player.PlayerData.job.name == job then
Player.Functions.SetJob('unemployed', 0)
end
else
QBCore.Functions.Notify(src, 'Player doesn\'t have this job?', 'error')
end
end
QBCore.Commands.Add('removejob', "Remove a job from the player's multijob.", { { name = 'id', help = 'ID of the player' }, { name = 'job', help = 'Name of Job' } }, true, function(source, args)
local src = source
if not args[1] then
QBCore.Functions.Notify(src, 'Must provide a player id.', 'error')
return
end
if not args[2] then
QBCore.Functions.Notify(src, 'Must provide the name of the job to remove from the player.', 'error')
return
end
local id = tonumber(args[1])
local Player = QBCore.Functions.GetPlayer(id)
if not Player then QBCore.Functions.Notify(src, 'Player not online.', 'error') return end
adminRemoveJob(src, id, args[2])
end, 'admin')
AddEventHandler('onResourceStart', function(resource)
if resource ~= GetCurrentResourceName() then return end
MySQL.query([=[
CREATE TABLE IF NOT EXISTS `save_jobs` (
`cid` VARCHAR(100) NOT NULL,
`job` VARCHAR(100) NOT NULL,
`grade` INT(11) NOT NULL
);
]=])
end)