mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-30 09:48:52 +00:00
dee1238459
no longer selecting every column
391 lines
11 KiB
Lua
391 lines
11 KiB
Lua
ESX = nil
|
|
local Jobs = {}
|
|
local RegisteredSocieties = {}
|
|
|
|
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
|
|
|
|
function GetSociety(name)
|
|
for i=1, #RegisteredSocieties, 1 do
|
|
if RegisteredSocieties[i].name == name then
|
|
return RegisteredSocieties[i]
|
|
end
|
|
end
|
|
end
|
|
|
|
MySQL.ready(function()
|
|
local result = MySQL.Sync.fetchAll('SELECT * FROM jobs', {})
|
|
|
|
for i=1, #result, 1 do
|
|
Jobs[result[i].name] = result[i]
|
|
Jobs[result[i].name].grades = {}
|
|
end
|
|
|
|
local result2 = MySQL.Sync.fetchAll('SELECT * FROM job_grades', {})
|
|
|
|
for i=1, #result2, 1 do
|
|
Jobs[result2[i].job_name].grades[tostring(result2[i].grade)] = result2[i]
|
|
end
|
|
end)
|
|
|
|
AddEventHandler('esx_society:registerSociety', function(name, label, account, datastore, inventory, data)
|
|
local found = false
|
|
|
|
local society = {
|
|
name = name,
|
|
label = label,
|
|
account = account,
|
|
datastore = datastore,
|
|
inventory = inventory,
|
|
data = data
|
|
}
|
|
|
|
for i=1, #RegisteredSocieties, 1 do
|
|
if RegisteredSocieties[i].name == name then
|
|
found, RegisteredSocieties[i] = true, society
|
|
break
|
|
end
|
|
end
|
|
|
|
if not found then
|
|
table.insert(RegisteredSocieties, society)
|
|
end
|
|
end)
|
|
|
|
AddEventHandler('esx_society:getSocieties', function(cb)
|
|
cb(RegisteredSocieties)
|
|
end)
|
|
|
|
AddEventHandler('esx_society:getSociety', function(name, cb)
|
|
cb(GetSociety(name))
|
|
end)
|
|
|
|
RegisterServerEvent('esx_society:withdrawMoney')
|
|
AddEventHandler('esx_society:withdrawMoney', function(societyName, amount)
|
|
local xPlayer = ESX.GetPlayerFromId(source)
|
|
local society = GetSociety(societyName)
|
|
amount = ESX.Math.Round(tonumber(amount))
|
|
|
|
if xPlayer.job.name == society.name then
|
|
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
|
|
if amount > 0 and account.money >= amount then
|
|
account.removeMoney(amount)
|
|
xPlayer.addMoney(amount)
|
|
xPlayer.showNotification(_U('have_withdrawn', ESX.Math.GroupDigits(amount)))
|
|
else
|
|
xPlayer.showNotification(_U('invalid_amount'))
|
|
end
|
|
end)
|
|
else
|
|
print(('esx_society: %s attempted to call withdrawMoney!'):format(xPlayer.identifier))
|
|
end
|
|
end)
|
|
|
|
RegisterServerEvent('esx_society:depositMoney')
|
|
AddEventHandler('esx_society:depositMoney', function(societyName, amount)
|
|
local xPlayer = ESX.GetPlayerFromId(source)
|
|
local society = GetSociety(societyName)
|
|
amount = ESX.Math.Round(tonumber(amount))
|
|
|
|
if xPlayer.job.name == society.name then
|
|
if amount > 0 and xPlayer.getMoney() >= amount then
|
|
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
|
|
xPlayer.removeMoney(amount)
|
|
xPlayer.showNotification(_U('have_deposited', ESX.Math.GroupDigits(amount)))
|
|
account.addMoney(amount)
|
|
end)
|
|
else
|
|
xPlayer.showNotification(_U('invalid_amount'))
|
|
end
|
|
else
|
|
print(('esx_society: %s attempted to call depositMoney!'):format(xPlayer.identifier))
|
|
end
|
|
end)
|
|
|
|
RegisterServerEvent('esx_society:washMoney')
|
|
AddEventHandler('esx_society:washMoney', function(society, amount)
|
|
local xPlayer = ESX.GetPlayerFromId(source)
|
|
local account = xPlayer.getAccount('black_money')
|
|
amount = ESX.Math.Round(tonumber(amount))
|
|
|
|
if xPlayer.job.name == society then
|
|
if amount and amount > 0 and account.money >= amount then
|
|
xPlayer.removeAccountMoney('black_money', amount)
|
|
|
|
MySQL.Async.execute('INSERT INTO society_moneywash (identifier, society, amount) VALUES (@identifier, @society, @amount)', {
|
|
['@identifier'] = xPlayer.identifier,
|
|
['@society'] = society,
|
|
['@amount'] = amount
|
|
}, function(rowsChanged)
|
|
xPlayer.showNotification(_U('you_have', ESX.Math.GroupDigits(amount)))
|
|
end)
|
|
else
|
|
xPlayer.showNotification(_U('invalid_amount'))
|
|
end
|
|
else
|
|
print(('esx_society: %s attempted to call washMoney!'):format(xPlayer.identifier))
|
|
end
|
|
end)
|
|
|
|
RegisterServerEvent('esx_society:putVehicleInGarage')
|
|
AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle)
|
|
local society = GetSociety(societyName)
|
|
|
|
TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
|
|
local garage = store.get('garage') or {}
|
|
table.insert(garage, vehicle)
|
|
store.set('garage', garage)
|
|
end)
|
|
end)
|
|
|
|
RegisterServerEvent('esx_society:removeVehicleFromGarage')
|
|
AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
|
|
local society = GetSociety(societyName)
|
|
|
|
TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
|
|
local garage = store.get('garage') or {}
|
|
|
|
for i=1, #garage, 1 do
|
|
if garage[i].plate == vehicle.plate then
|
|
table.remove(garage, i)
|
|
break
|
|
end
|
|
end
|
|
|
|
store.set('garage', garage)
|
|
end)
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, societyName)
|
|
local society = GetSociety(societyName)
|
|
|
|
if society then
|
|
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
|
|
cb(account.money)
|
|
end)
|
|
else
|
|
cb(0)
|
|
end
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
|
|
local employees = {}
|
|
|
|
local xPlayers = ESX.GetPlayers()
|
|
for k, v in pairs(xPlayers) do
|
|
local xPlayer = ESX.GetPlayerFromId(v)
|
|
|
|
local name = GetPlayerName(xPlayer.source)
|
|
if Config.EnableESXIdentity then
|
|
name = xPlayer.get('firstName') .. ' ' .. xPlayer.get('lastName')
|
|
end
|
|
|
|
if xPlayer.getJob().name == society then
|
|
table.insert(employees, {
|
|
name = name,
|
|
identifier = xPlayer.identifier,
|
|
job = {
|
|
name = society,
|
|
label = xPlayer.getJob().label,
|
|
grade = xPlayer.getJob().grade,
|
|
grade_name = xPlayer.getJob().grade_name,
|
|
grade_label = xPlayer.getJob().grade_label
|
|
}
|
|
})
|
|
end
|
|
end
|
|
|
|
local query = "SELECT identifier, job_grade FROM `users` WHERE `job`=@job ORDER BY job_grade DESC"
|
|
|
|
if Config.EnableESXIdentity then
|
|
query = "SELECT identifier, job_grade, firstname, lastname FROM `users` WHERE `job`=@job ORDER BY job_grade DESC"
|
|
end
|
|
|
|
MySQL.Async.fetchAll(query, {
|
|
['@job'] = society
|
|
}, function(result)
|
|
for k, row in pairs(result) do
|
|
local alreadyInTable
|
|
local identifier = row.identifier
|
|
|
|
for k, v in pairs(employees) do
|
|
if v.identifier == identifier then
|
|
alreadyInTable = true
|
|
end
|
|
end
|
|
|
|
if not alreadyInTable then
|
|
local name = "Name not found." -- maybe this should be a locale instead ¯\_(ツ)_/¯
|
|
|
|
if Config.EnableESXIdentity then
|
|
name = row.firstname .. ' ' .. row.lastname
|
|
end
|
|
|
|
table.insert(employees, {
|
|
name = name,
|
|
identifier = identifier,
|
|
job = {
|
|
name = society,
|
|
label = Jobs[society].label,
|
|
grade = row.job_grade,
|
|
grade_name = Jobs[society].grades[tostring(row.job_grade)].name,
|
|
grade_label = Jobs[society].grades[tostring(row.job_grade)].label
|
|
}
|
|
})
|
|
end
|
|
end
|
|
|
|
cb(employees)
|
|
end)
|
|
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society)
|
|
local job = json.decode(json.encode(Jobs[society]))
|
|
local grades = {}
|
|
|
|
for k,v in pairs(job.grades) do
|
|
table.insert(grades, v)
|
|
end
|
|
|
|
table.sort(grades, function(a, b)
|
|
return a.grade < b.grade
|
|
end)
|
|
|
|
job.grades = grades
|
|
|
|
cb(job)
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier, job, grade, type)
|
|
local xPlayer = ESX.GetPlayerFromId(source)
|
|
local isBoss = xPlayer.job.grade_name == 'boss'
|
|
|
|
if isBoss then
|
|
local xTarget = ESX.GetPlayerFromIdentifier(identifier)
|
|
|
|
if xTarget then
|
|
xTarget.setJob(job, grade)
|
|
|
|
if type == 'hire' then
|
|
xTarget.showNotification(_U('you_have_been_hired', job))
|
|
elseif type == 'promote' then
|
|
xTarget.showNotification(_U('you_have_been_promoted'))
|
|
elseif type == 'fire' then
|
|
xTarget.showNotification(_U('you_have_been_fired', xTarget.getJob().label))
|
|
end
|
|
|
|
cb()
|
|
else
|
|
MySQL.Async.execute('UPDATE users SET job = @job, job_grade = @job_grade WHERE identifier = @identifier', {
|
|
['@job'] = job,
|
|
['@job_grade'] = grade,
|
|
['@identifier'] = identifier
|
|
}, function(rowsChanged)
|
|
cb()
|
|
end)
|
|
end
|
|
else
|
|
print(('esx_society: %s attempted to setJob'):format(xPlayer.identifier))
|
|
cb()
|
|
end
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:setJobSalary', function(source, cb, job, grade, salary)
|
|
local xPlayer = ESX.GetPlayerFromId(source)
|
|
|
|
if xPlayer.job.name == job and xPlayer.job.grade_name == 'boss' then
|
|
if salary <= Config.MaxSalary then
|
|
MySQL.Async.execute('UPDATE job_grades SET salary = @salary WHERE job_name = @job_name AND grade = @grade', {
|
|
['@salary'] = salary,
|
|
['@job_name'] = job,
|
|
['@grade'] = grade
|
|
}, function(rowsChanged)
|
|
Jobs[job].grades[tostring(grade)].salary = salary
|
|
local xPlayers = ESX.GetPlayers()
|
|
|
|
for i=1, #xPlayers, 1 do
|
|
local xTarget = ESX.GetPlayerFromId(xPlayers[i])
|
|
|
|
if xTarget.job.name == job and xTarget.job.grade == grade then
|
|
xTarget.setJob(job, grade)
|
|
end
|
|
end
|
|
|
|
cb()
|
|
end)
|
|
else
|
|
print(('esx_society: %s attempted to setJobSalary over config limit!'):format(xPlayer.identifier))
|
|
cb()
|
|
end
|
|
else
|
|
print(('esx_society: %s attempted to setJobSalary'):format(xPlayer.identifier))
|
|
cb()
|
|
end
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb)
|
|
local xPlayers = ESX.GetPlayers()
|
|
local players = {}
|
|
|
|
for i=1, #xPlayers, 1 do
|
|
local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
|
|
table.insert(players, {
|
|
source = xPlayer.source,
|
|
identifier = xPlayer.identifier,
|
|
name = xPlayer.name,
|
|
job = xPlayer.job
|
|
})
|
|
end
|
|
|
|
cb(players)
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:getVehiclesInGarage', function(source, cb, societyName)
|
|
local society = GetSociety(societyName)
|
|
|
|
TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
|
|
local garage = store.get('garage') or {}
|
|
cb(garage)
|
|
end)
|
|
end)
|
|
|
|
ESX.RegisterServerCallback('esx_society:isBoss', function(source, cb, job)
|
|
cb(isPlayerBoss(source, job))
|
|
end)
|
|
|
|
function isPlayerBoss(playerId, job)
|
|
local xPlayer = ESX.GetPlayerFromId(playerId)
|
|
|
|
if xPlayer.job.name == job and xPlayer.job.grade_name == 'boss' then
|
|
return true
|
|
else
|
|
print(('esx_society: %s attempted open a society boss menu!'):format(xPlayer.identifier))
|
|
return false
|
|
end
|
|
end
|
|
|
|
function WashMoneyCRON(d, h, m)
|
|
MySQL.Async.fetchAll('SELECT * FROM society_moneywash', {}, function(result)
|
|
for i=1, #result, 1 do
|
|
local society = GetSociety(result[i].society)
|
|
local xPlayer = ESX.GetPlayerFromIdentifier(result[i].identifier)
|
|
|
|
-- add society money
|
|
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
|
|
account.addMoney(result[i].amount)
|
|
end)
|
|
|
|
-- send notification if player is online
|
|
if xPlayer then
|
|
xPlayer.showNotification(_U('you_have_laundered', ESX.Math.GroupDigits(result[i].amount)))
|
|
end
|
|
|
|
MySQL.Async.execute('DELETE FROM society_moneywash WHERE id = @id', {
|
|
['@id'] = result[i].id
|
|
})
|
|
end
|
|
end)
|
|
end
|
|
|
|
TriggerEvent('cron:runAt', 3, 0, WashMoneyCRON)
|