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)
ESX.TriggerServerCallback('esx_society:getJob', function(job)
if not job then
return
end
local elements = {
{unselectable = true, icon = "fas fa-user", title = TranslateCap('promote_employee', employee.name)}
}
@@ -306,6 +310,10 @@ end
function OpenManageSalaryMenu(society, options)
ESX.TriggerServerCallback('esx_society:getJob', function(job)
if not job then
return
end
local elements = {
{unselectable = true, icon = "fas fa-wallet", title = TranslateCap('salary_management')}
}
@@ -354,6 +362,10 @@ end
function OpenManageGradesMenu(society, options)
ESX.TriggerServerCallback('esx_society:getJob', function(job)
if not job then
return
end
local elements = {
{unselectable = true, icon = "fas fa-wallet", title = TranslateCap('grade_management')}
}
+6 -2
View File
@@ -12,10 +12,11 @@ Locales['hu'] = {
['grade'] = 'Fokozat',
['have_deposited'] = 'Sikeresen beraktál ~r~%s $',
['have_withdrawn'] = 'Sikeresen kivettél %s $',
['check_balance'] = 'egyenleg ~g~%s$',
['invalid_amount'] = 'Érvénytelen mennyiség',
['invalid_amount_max'] = 'Ez az összeg több mint a megengedett',
['invalid_value'] = 'invalid value',
['invalid_value_nochanges']= 'invalid value, no changes applied',
['invalid_value'] = 'érvénytelen érték',
['invalid_value_nochanges']= 'érvénytelen érték, nem történt változtatás',
['no'] = 'Nem',
['promote'] = 'Elöléptetés',
['promote_employee'] = 'Elöléptetés %s',
@@ -23,6 +24,9 @@ Locales['hu'] = {
['recruiting'] = 'Felvétel',
['salary_amount'] = 'Fizetés',
['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_amount'] = 'Piszkos 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 SocietiesByName = {}
function GetSociety(name)
for i=1, #RegisteredSocieties, 1 do
if RegisteredSocieties[i].name == name then
return RegisteredSocieties[i]
end
end
return SocietiesByName[name]
end
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)
local found = false
if SocietiesByName[name] then
print(('[^3WARNING^7] society already registered, name: ^5%s^7'):format(name))
return
end
local society = {
name = name,
@@ -38,20 +25,10 @@ function registerSociety(name, label, account, datastore, inventory, data)
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
RegisteredSocieties[#RegisteredSocieties + 1] = society
print(("[^2INFO^7] Registered Society ^5%s^7"):format(label))
end
SocietiesByName[name] = society
table.insert(RegisteredSocieties, society)
end
AddEventHandler('esx_society:registerSociety',registerSociety)
AddEventHandler('esx_society:registerSociety', registerSociety)
exports("registerSociety", registerSociety)
AddEventHandler('esx_society:getSocieties', function(cb)
@@ -87,19 +64,19 @@ AddEventHandler('esx_society:withdrawMoney', function(societyName, amount)
end
local xPlayer = ESX.GetPlayerFromId(source)
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, "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))
if xPlayer.job.name ~= society.name then
return print(('[^3WARNING^7] Player ^5%s^7 attempted to withdraw from society - ^5%s^7!'):format(source, society.name))
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)
RegisterServerEvent('esx_society:depositMoney')
@@ -113,18 +90,17 @@ AddEventHandler('esx_society:depositMoney', function(societyName, amount)
end
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, "Society Deposit")
xPlayer.showNotification(TranslateCap('have_deposited', ESX.Math.GroupDigits(amount)))
account.addMoney(amount)
end)
else
xPlayer.showNotification(TranslateCap('invalid_amount'))
end
if xPlayer.job.name ~= society.name then
return print(('[^3WARNING^7] Player ^5%s^7 attempted to deposit to society - ^5%s^7!'):format(source, society.name))
end
if amount > 0 and xPlayer.getMoney() >= amount then
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
xPlayer.removeMoney(amount, "Society Deposit")
xPlayer.showNotification(TranslateCap('have_deposited', ESX.Math.GroupDigits(amount)))
account.addMoney(amount)
end)
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)
@@ -135,26 +111,24 @@ AddEventHandler('esx_society:washMoney', function(society, amount)
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, "Washing")
if xPlayer.job.name ~= society then
return print(('[^3WARNING^7] Player ^5%s^7 attempted to wash money in society - ^5%s^7!'):format(source, society))
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},
function(rowsChanged)
xPlayer.showNotification(TranslateCap('you_have', ESX.Math.GroupDigits(amount)))
end)
else
xPlayer.showNotification(TranslateCap('invalid_amount'))
end
MySQL.insert('INSERT INTO society_moneywash (identifier, society, amount) VALUES (?, ?, ?)', {xPlayer.identifier, society, amount},
function(rowsChanged)
xPlayer.showNotification(TranslateCap('you_have', ESX.Math.GroupDigits(amount)))
end)
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)
RegisterServerEvent('esx_society:putVehicleInGarage')
AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle)
local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local society = GetSociety(societyName)
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))
@@ -170,7 +144,6 @@ end)
RegisterServerEvent('esx_society:removeVehicleFromGarage')
AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local society = GetSociety(societyName)
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))
@@ -194,15 +167,14 @@ ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, s
local society = GetSociety(societyName)
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))
return
return cb(0)
end
if society then
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
cb(account.money)
end)
else
cb(0)
if not society then
return cb(0)
end
TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
cb(account.money or 0)
end)
end)
ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
@@ -275,6 +247,10 @@ ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, soci
end)
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 grades = {}
@@ -296,33 +272,33 @@ ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier
local isBoss = xPlayer.job.grade_name == 'boss'
local xTarget = ESX.GetPlayerFromIdentifier(identifier)
if 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
if not isBoss then
print(('[^3WARNING^7] Player ^5%s^7 attempted to setJob for Player ^5%s^7!'):format(source, xTarget.source))
cb()
return cb()
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)