Merge pull request #794 from fivemland/society-refactor

refactor(esx_society)
This commit is contained in:
Gellipapa
2023-01-09 23:31:05 +01:00
committed by GitHub
3 changed files with 98 additions and 106 deletions
+12
View File
@@ -276,6 +276,10 @@ end
function OpenPromoteMenu(society, employee, options) function OpenPromoteMenu(society, employee, options)
ESX.TriggerServerCallback('esx_society:getJob', function(job) ESX.TriggerServerCallback('esx_society:getJob', function(job)
if not job then
return
end
local elements = { local elements = {
{unselectable = true, icon = "fas fa-user", title = TranslateCap('promote_employee', employee.name)} {unselectable = true, icon = "fas fa-user", title = TranslateCap('promote_employee', employee.name)}
} }
@@ -306,6 +310,10 @@ end
function OpenManageSalaryMenu(society, options) function OpenManageSalaryMenu(society, options)
ESX.TriggerServerCallback('esx_society:getJob', function(job) ESX.TriggerServerCallback('esx_society:getJob', function(job)
if not job then
return
end
local elements = { local elements = {
{unselectable = true, icon = "fas fa-wallet", title = TranslateCap('salary_management')} {unselectable = true, icon = "fas fa-wallet", title = TranslateCap('salary_management')}
} }
@@ -354,6 +362,10 @@ end
function OpenManageGradesMenu(society, options) function OpenManageGradesMenu(society, options)
ESX.TriggerServerCallback('esx_society:getJob', function(job) ESX.TriggerServerCallback('esx_society:getJob', function(job)
if not job then
return
end
local elements = { local elements = {
{unselectable = true, icon = "fas fa-wallet", title = TranslateCap('grade_management')} {unselectable = true, icon = "fas fa-wallet", title = TranslateCap('grade_management')}
} }
+6 -2
View File
@@ -12,10 +12,11 @@ Locales['hu'] = {
['grade'] = 'Fokozat', ['grade'] = 'Fokozat',
['have_deposited'] = 'Sikeresen beraktál ~r~%s $', ['have_deposited'] = 'Sikeresen beraktál ~r~%s $',
['have_withdrawn'] = 'Sikeresen kivettél %s $', ['have_withdrawn'] = 'Sikeresen kivettél %s $',
['check_balance'] = 'egyenleg ~g~%s$',
['invalid_amount'] = 'Érvénytelen mennyiség', ['invalid_amount'] = 'Érvénytelen mennyiség',
['invalid_amount_max'] = 'Ez az összeg több mint a megengedett', ['invalid_amount_max'] = 'Ez az összeg több mint a megengedett',
['invalid_value'] = 'invalid value', ['invalid_value'] = 'érvénytelen érték',
['invalid_value_nochanges']= 'invalid value, no changes applied', ['invalid_value_nochanges']= 'érvénytelen érték, nem történt változtatás',
['no'] = 'Nem', ['no'] = 'Nem',
['promote'] = 'Elöléptetés', ['promote'] = 'Elöléptetés',
['promote_employee'] = 'Elöléptetés %s', ['promote_employee'] = 'Elöléptetés %s',
@@ -23,6 +24,9 @@ Locales['hu'] = {
['recruiting'] = 'Felvétel', ['recruiting'] = 'Felvétel',
['salary_amount'] = 'Fizetés', ['salary_amount'] = 'Fizetés',
['salary_management'] = 'Fizetések kezelése', ['salary_management'] = 'Fizetések kezelése',
['grade_management'] = 'Rangok nevének kezelése',
['grade_label'] = 'Rang név',
['check_society_balance'] = 'Egyenleg megtekintése',
['wash_money'] = 'Pénz mosás', ['wash_money'] = 'Pénz mosás',
['wash_money_amount'] = 'Piszkos pénz mennyiség', ['wash_money_amount'] = 'Piszkos pénz mennyiség',
['withdraw_amount'] = 'Pénz mennyiség', ['withdraw_amount'] = 'Pénz mennyiség',
+80 -104
View File
@@ -1,33 +1,20 @@
local Jobs = {} local Jobs = setmetatable({}, {__index = function(_, key)
return ESX.GetJobs()[key]
end
})
local RegisteredSocieties = {} local RegisteredSocieties = {}
local SocietiesByName = {}
function GetSociety(name) function GetSociety(name)
for i=1, #RegisteredSocieties, 1 do return SocietiesByName[name]
if RegisteredSocieties[i].name == name then
return RegisteredSocieties[i]
end
end
end end
exports("GetSociety", GetSociety) exports("GetSociety", GetSociety)
CreateThread(function()
local result = MySQL.query.await('SELECT * FROM jobs')
for i = 1, #result, 1 do
Jobs[result[i].name] = result[i]
Jobs[result[i].name].grades = {}
end
local result2 = MySQL.query.await('SELECT * FROM job_grades')
for i = 1, #result2, 1 do
Jobs[result2[i].job_name].grades[tostring(result2[i].grade)] = result2[i]
end
end)
function registerSociety(name, label, account, datastore, inventory, data) function registerSociety(name, label, account, datastore, inventory, data)
local found = false if SocietiesByName[name] then
print(('[^3WARNING^7] society already registered, name: ^5%s^7'):format(name))
return
end
local society = { local society = {
name = name, name = name,
@@ -38,20 +25,10 @@ function registerSociety(name, label, account, datastore, inventory, data)
data = data data = data
} }
for i=1, #RegisteredSocieties, 1 do SocietiesByName[name] = society
if RegisteredSocieties[i].name == name then table.insert(RegisteredSocieties, society)
found, RegisteredSocieties[i] = true, society
break
end
end
if not found then
RegisteredSocieties[#RegisteredSocieties + 1] = society
print(("[^2INFO^7] Registered Society ^5%s^7"):format(label))
end
end end
AddEventHandler('esx_society:registerSociety', registerSociety)
AddEventHandler('esx_society:registerSociety',registerSociety)
exports("registerSociety", registerSociety) exports("registerSociety", registerSociety)
AddEventHandler('esx_society:getSocieties', function(cb) AddEventHandler('esx_society:getSocieties', function(cb)
@@ -87,19 +64,19 @@ AddEventHandler('esx_society:withdrawMoney', function(societyName, amount)
end end
local xPlayer = ESX.GetPlayerFromId(source) local xPlayer = ESX.GetPlayerFromId(source)
amount = ESX.Math.Round(tonumber(amount)) amount = ESX.Math.Round(tonumber(amount))
if xPlayer.job.name == society.name then if xPlayer.job.name ~= society.name then
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account) return print(('[^3WARNING^7] Player ^5%s^7 attempted to withdraw from society - ^5%s^7!'):format(source, society.name))
if amount > 0 and account.money >= amount then
account.removeMoney(amount)
xPlayer.addMoney(amount, "Society Withdraw")
xPlayer.showNotification(TranslateCap('have_withdrawn', ESX.Math.GroupDigits(amount)))
else
xPlayer.showNotification(TranslateCap('invalid_amount'))
end
end)
else
print(('[^3WARNING^7] Player ^5%s^7 attempted to withdraw from society - ^5%s^7!'):format(source, society.name))
end end
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
if amount > 0 and account.money >= amount then
account.removeMoney(amount)
xPlayer.addMoney(amount, "Society Withdraw")
xPlayer.showNotification(TranslateCap('have_withdrawn', ESX.Math.GroupDigits(amount)))
else
xPlayer.showNotification(TranslateCap('invalid_amount'))
end
end)
end) end)
RegisterServerEvent('esx_society:depositMoney') RegisterServerEvent('esx_society:depositMoney')
@@ -113,18 +90,17 @@ AddEventHandler('esx_society:depositMoney', function(societyName, amount)
end end
amount = ESX.Math.Round(tonumber(amount)) amount = ESX.Math.Round(tonumber(amount))
if xPlayer.job.name == society.name then if xPlayer.job.name ~= society.name then
if amount > 0 and xPlayer.getMoney() >= amount then return print(('[^3WARNING^7] Player ^5%s^7 attempted to deposit to society - ^5%s^7!'):format(source, society.name))
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account) end
xPlayer.removeMoney(amount, "Society Deposit") if amount > 0 and xPlayer.getMoney() >= amount then
xPlayer.showNotification(TranslateCap('have_deposited', ESX.Math.GroupDigits(amount))) TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
account.addMoney(amount) xPlayer.removeMoney(amount, "Society Deposit")
end) xPlayer.showNotification(TranslateCap('have_deposited', ESX.Math.GroupDigits(amount)))
else account.addMoney(amount)
xPlayer.showNotification(TranslateCap('invalid_amount')) end)
end
else else
print(('[^3WARNING^7] Player ^5%s^7 attempted to deposit to society - ^5%s^7!'):format(source, society.name)) xPlayer.showNotification(TranslateCap('invalid_amount'))
end end
end) end)
@@ -135,26 +111,24 @@ AddEventHandler('esx_society:washMoney', function(society, amount)
local account = xPlayer.getAccount('black_money') local account = xPlayer.getAccount('black_money')
amount = ESX.Math.Round(tonumber(amount)) amount = ESX.Math.Round(tonumber(amount))
if xPlayer.job.name == society then if xPlayer.job.name ~= society then
if amount and amount > 0 and account.money >= amount then return print(('[^3WARNING^7] Player ^5%s^7 attempted to wash money in society - ^5%s^7!'):format(source, society))
xPlayer.removeAccountMoney('black_money', amount, "Washing") end
if amount and amount > 0 and account.money >= amount then
xPlayer.removeAccountMoney('black_money', amount, "Washing")
MySQL.insert('INSERT INTO society_moneywash (identifier, society, amount) VALUES (?, ?, ?)', {xPlayer.identifier, society, amount}, MySQL.insert('INSERT INTO society_moneywash (identifier, society, amount) VALUES (?, ?, ?)', {xPlayer.identifier, society, amount},
function(rowsChanged) function(rowsChanged)
xPlayer.showNotification(TranslateCap('you_have', ESX.Math.GroupDigits(amount))) xPlayer.showNotification(TranslateCap('you_have', ESX.Math.GroupDigits(amount)))
end) end)
else
xPlayer.showNotification(TranslateCap('invalid_amount'))
end
else else
print(('[^3WARNING^7] Player ^5%s^7 attempted to wash money in society - ^5%s^7!'):format(source, society)) xPlayer.showNotification(TranslateCap('invalid_amount'))
end end
end) end)
RegisterServerEvent('esx_society:putVehicleInGarage') RegisterServerEvent('esx_society:putVehicleInGarage')
AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle) AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle)
local source = source local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local society = GetSociety(societyName) local society = GetSociety(societyName)
if not society then if not society then
print(('[^3WARNING^7] Player ^5%s^7 attempted to put vehicle in non-existing society garage - ^5%s^7!'):format(source, societyName)) print(('[^3WARNING^7] Player ^5%s^7 attempted to put vehicle in non-existing society garage - ^5%s^7!'):format(source, societyName))
@@ -170,7 +144,6 @@ end)
RegisterServerEvent('esx_society:removeVehicleFromGarage') RegisterServerEvent('esx_society:removeVehicleFromGarage')
AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle) AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
local source = source local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local society = GetSociety(societyName) local society = GetSociety(societyName)
if not society then if not society then
print(('[^3WARNING^7] Player ^5%s^7 attempted to remove vehicle from non-existing society garage - ^5%s^7!'):format(source, societyName)) print(('[^3WARNING^7] Player ^5%s^7 attempted to remove vehicle from non-existing society garage - ^5%s^7!'):format(source, societyName))
@@ -194,15 +167,14 @@ ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, s
local society = GetSociety(societyName) local society = GetSociety(societyName)
if not society then if not society then
print(('[^3WARNING^7] Player ^5%s^7 attempted to get money from non-existing society - ^5%s^7!'):format(source, societyName)) print(('[^3WARNING^7] Player ^5%s^7 attempted to get money from non-existing society - ^5%s^7!'):format(source, societyName))
return return cb(0)
end end
if society then if not society then
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account) return cb(0)
cb(account.money)
end)
else
cb(0)
end end
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
cb(account.money or 0)
end)
end) end)
ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society) ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
@@ -275,6 +247,10 @@ ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, soci
end) end)
ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society) ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society)
if not Jobs[society] then
return cb(false)
end
local job = json.decode(json.encode(Jobs[society])) local job = json.decode(json.encode(Jobs[society]))
local grades = {} local grades = {}
@@ -296,33 +272,33 @@ ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier
local isBoss = xPlayer.job.grade_name == 'boss' local isBoss = xPlayer.job.grade_name == 'boss'
local xTarget = ESX.GetPlayerFromIdentifier(identifier) local xTarget = ESX.GetPlayerFromIdentifier(identifier)
if isBoss then if not isBoss then
if xTarget then
xTarget.setJob(job, grade)
if actionType == 'hire' then
xTarget.showNotification(TranslateCap('you_have_been_hired', job))
xPlayer.showNotification(TranslateCap("you_have_hired", xTarget.getName()))
elseif actionType == 'promote' then
xTarget.showNotification(TranslateCap('you_have_been_promoted'))
xPlayer.showNotification(TranslateCap("you_have_promoted", xTarget.getName(), xTarget.getJob().label))
elseif actionType == 'fire' then
xTarget.showNotification(TranslateCap('you_have_been_fired', xTarget.getJob().label))
xPlayer.showNotification(TranslateCap("you_have_fired", xTarget.getName()))
end
cb()
else
MySQL.update('UPDATE users SET job = ?, job_grade = ? WHERE identifier = ?', {job, grade, identifier},
function(rowsChanged)
cb()
end)
end
else
print(('[^3WARNING^7] Player ^5%s^7 attempted to setJob for Player ^5%s^7!'):format(source, xTarget.source)) print(('[^3WARNING^7] Player ^5%s^7 attempted to setJob for Player ^5%s^7!'):format(source, xTarget.source))
cb() return cb()
end end
if not xTarget then
MySQL.update('UPDATE users SET job = ?, job_grade = ? WHERE identifier = ?', {job, grade, identifier},
function()
cb()
end)
return
end
xTarget.setJob(job, grade)
if actionType == 'hire' then
xTarget.showNotification(TranslateCap('you_have_been_hired', job))
xPlayer.showNotification(TranslateCap("you_have_hired", xTarget.getName()))
elseif actionType == 'promote' then
xTarget.showNotification(TranslateCap('you_have_been_promoted'))
xPlayer.showNotification(TranslateCap("you_have_promoted", xTarget.getName(), xTarget.getJob().label))
elseif actionType == 'fire' then
xTarget.showNotification(TranslateCap('you_have_been_fired', xTarget.getJob().label))
xPlayer.showNotification(TranslateCap("you_have_fired", xTarget.getName()))
end
cb()
end) end)