From 083dcddfe27c7dd6f17850e6a28e3b3299cba95c Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Sun, 19 Apr 2020 20:02:06 +0200 Subject: [PATCH] Code cleanup, fixed cases of shadowing upvalue --- client/main.lua | 257 +++++++++++++++++++----------------------------- server/main.lua | 40 ++++---- 2 files changed, 121 insertions(+), 176 deletions(-) diff --git a/client/main.lua b/client/main.lua index e1d55c6b..f5deb025 100644 --- a/client/main.lua +++ b/client/main.lua @@ -73,159 +73,128 @@ function UpdateSocietyMoneyHUDElement(money) end function OpenBossMenu(society, close, options) - local isBoss = nil - local options = options or {} + options = options or {} local elements = {} - ESX.TriggerServerCallback('esx_society:isBoss', function(result) - isBoss = result + ESX.TriggerServerCallback('esx_society:isBoss', function(isBoss) + if isBoss then + local defaultOptions = { + withdraw = true, + deposit = true, + wash = true, + employees = true, + grades = true + } + + for k,v in pairs(defaultOptions) do + if options[k] == nil then + options[k] = v + end + end + + if options.withdraw then + table.insert(elements, {label = _U('withdraw_society_money'), value = 'withdraw_society_money'}) + end + + if options.deposit then + table.insert(elements, {label = _U('deposit_society_money'), value = 'deposit_money'}) + end + + if options.wash then + table.insert(elements, {label = _U('wash_money'), value = 'wash_money'}) + end + + if options.employees then + table.insert(elements, {label = _U('employee_management'), value = 'manage_employees'}) + end + + if options.grades then + table.insert(elements, {label = _U('salary_management'), value = 'manage_grades'}) + end + + ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'boss_actions_' .. society, { + title = _U('boss_menu'), + align = 'top-left', + elements = elements + }, function(data, menu) + if data.current.value == 'withdraw_society_money' then + ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'withdraw_society_money_amount_' .. society, { + title = _U('withdraw_amount') + }, function(data2, menu2) + local amount = tonumber(data2.value) + + if amount == nil then + ESX.ShowNotification(_U('invalid_amount')) + else + menu2.close() + TriggerServerEvent('esx_society:withdrawMoney', society, amount) + end + end, function(data2, menu2) + menu2.close() + end) + elseif data.current.value == 'deposit_money' then + ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'deposit_money_amount_' .. society, { + title = _U('deposit_amount') + }, function(data2, menu2) + local amount = tonumber(data2.value) + + if amount == nil then + ESX.ShowNotification(_U('invalid_amount')) + else + menu2.close() + TriggerServerEvent('esx_society:depositMoney', society, amount) + end + end, function(data2, menu2) + menu2.close() + end) + elseif data.current.value == 'wash_money' then + ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'wash_money_amount_' .. society, { + title = _U('wash_money_amount') + }, function(data2, menu2) + local amount = tonumber(data2.value) + + if amount == nil then + ESX.ShowNotification(_U('invalid_amount')) + else + menu2.close() + TriggerServerEvent('esx_society:washMoney', society, amount) + end + end, function(data2, menu2) + menu2.close() + end) + elseif data.current.value == 'manage_employees' then + OpenManageEmployeesMenu(society) + elseif data.current.value == 'manage_grades' then + OpenManageGradesMenu(society) + end + end, function(data, menu) + if close then + close(data, menu) + end + end) + end end, society) - - while isBoss == nil do - Citizen.Wait(100) - end - - if not isBoss then - return - end - - local defaultOptions = { - withdraw = true, - deposit = true, - wash = true, - employees = true, - grades = true - } - - for k,v in pairs(defaultOptions) do - if options[k] == nil then - options[k] = v - end - end - - if options.withdraw then - table.insert(elements, {label = _U('withdraw_society_money'), value = 'withdraw_society_money'}) - end - - if options.deposit then - table.insert(elements, {label = _U('deposit_society_money'), value = 'deposit_money'}) - end - - if options.wash then - table.insert(elements, {label = _U('wash_money'), value = 'wash_money'}) - end - - if options.employees then - table.insert(elements, {label = _U('employee_management'), value = 'manage_employees'}) - end - - if options.grades then - table.insert(elements, {label = _U('salary_management'), value = 'manage_grades'}) - end - - ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'boss_actions_' .. society, { - title = _U('boss_menu'), - align = 'top-left', - elements = elements - }, function(data, menu) - - if data.current.value == 'withdraw_society_money' then - - ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'withdraw_society_money_amount_' .. society, { - title = _U('withdraw_amount') - }, function(data, menu) - - local amount = tonumber(data.value) - - if amount == nil then - ESX.ShowNotification(_U('invalid_amount')) - else - menu.close() - TriggerServerEvent('esx_society:withdrawMoney', society, amount) - end - - end, function(data, menu) - menu.close() - end) - - elseif data.current.value == 'deposit_money' then - - ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'deposit_money_amount_' .. society, { - title = _U('deposit_amount') - }, function(data, menu) - - local amount = tonumber(data.value) - - if amount == nil then - ESX.ShowNotification(_U('invalid_amount')) - else - menu.close() - TriggerServerEvent('esx_society:depositMoney', society, amount) - end - - end, function(data, menu) - menu.close() - end) - - elseif data.current.value == 'wash_money' then - - ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'wash_money_amount_' .. society, { - title = _U('wash_money_amount') - }, function(data, menu) - - local amount = tonumber(data.value) - - if amount == nil then - ESX.ShowNotification(_U('invalid_amount')) - else - menu.close() - TriggerServerEvent('esx_society:washMoney', society, amount) - end - - end, function(data, menu) - menu.close() - end) - - elseif data.current.value == 'manage_employees' then - OpenManageEmployeesMenu(society) - elseif data.current.value == 'manage_grades' then - OpenManageGradesMenu(society) - end - - end, function(data, menu) - if close then - close(data, menu) - end - end) - end function OpenManageEmployeesMenu(society) - ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'manage_employees_' .. society, { title = _U('employee_management'), align = 'top-left', elements = { {label = _U('employee_list'), value = 'employee_list'}, - {label = _U('recruit'), value = 'recruit'} - } - }, function(data, menu) - + {label = _U('recruit'), value = 'recruit'} + }}, function(data, menu) if data.current.value == 'employee_list' then OpenEmployeeList(society) - end - - if data.current.value == 'recruit' then + elseif data.current.value == 'recruit' then OpenRecruitMenu(society) end - end, function(data, menu) menu.close() end) end function OpenEmployeeList(society) - ESX.TriggerServerCallback('esx_society:getEmployees', function(employees) local elements = { @@ -263,15 +232,11 @@ function OpenEmployeeList(society) menu.close() OpenManageEmployeesMenu(society) end) - end, society) - end function OpenRecruitMenu(society) - ESX.TriggerServerCallback('esx_society:getOnlinePlayers', function(players) - local elements = {} for i=1, #players, 1 do @@ -290,15 +255,13 @@ function OpenRecruitMenu(society) align = 'top-left', elements = elements }, function(data, menu) - ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'recruit_confirm_' .. society, { title = _U('do_you_want_to_recruit', data.current.name), align = 'top-left', elements = { - {label = _U('no'), value = 'no'}, + {label = _U('no'), value = 'no'}, {label = _U('yes'), value = 'yes'} - } - }, function(data2, menu2) + }}, function(data2, menu2) menu2.close() if data2.current.value == 'yes' then @@ -311,19 +274,14 @@ function OpenRecruitMenu(society) end, function(data2, menu2) menu2.close() end) - end, function(data, menu) menu.close() end) - end) - end function OpenPromoteMenu(society, employee) - ESX.TriggerServerCallback('esx_society:getJob', function(job) - local elements = {} for i=1, #job.grades, 1 do @@ -351,15 +309,11 @@ function OpenPromoteMenu(society, employee) menu.close() OpenEmployeeList(society) end) - end, society) - end function OpenManageGradesMenu(society) - ESX.TriggerServerCallback('esx_society:getJob', function(job) - local elements = {} for i=1, #job.grades, 1 do @@ -376,7 +330,6 @@ function OpenManageGradesMenu(society) align = 'top-left', elements = elements }, function(data, menu) - ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'manage_grades_amount_' .. society, { title = _U('salary_amount') }, function(data2, menu2) @@ -394,17 +347,13 @@ function OpenManageGradesMenu(society) OpenManageGradesMenu(society) end, society, data.current.value, amount) end - end, function(data2, menu2) menu2.close() end) - end, function(data, menu) menu.close() end) - end, society) - end AddEventHandler('esx_society:openBossMenu', function(society, close, options) diff --git a/server/main.lua b/server/main.lua index 9960b071..66dad770 100644 --- a/server/main.lua +++ b/server/main.lua @@ -16,7 +16,7 @@ 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] = result[i] Jobs[result[i].name].grades = {} end @@ -31,18 +31,17 @@ AddEventHandler('esx_society:registerSociety', function(name, label, account, da local found = false local society = { - name = name, - label = label, - account = account, + name = name, + label = label, + account = account, datastore = datastore, inventory = inventory, - data = data + data = data } for i=1, #RegisteredSocieties, 1 do if RegisteredSocieties[i].name == name then - found = true - RegisteredSocieties[i] = society + found, RegisteredSocieties[i] = true, society break end end @@ -61,9 +60,9 @@ AddEventHandler('esx_society:getSociety', function(name, cb) end) RegisterServerEvent('esx_society:withdrawMoney') -AddEventHandler('esx_society:withdrawMoney', function(society, amount) +AddEventHandler('esx_society:withdrawMoney', function(societyName, amount) local xPlayer = ESX.GetPlayerFromId(source) - local society = GetSociety(society) + local society = GetSociety(societyName) amount = ESX.Math.Round(tonumber(amount)) if xPlayer.job.name == society.name then @@ -71,7 +70,6 @@ AddEventHandler('esx_society:withdrawMoney', function(society, amount) 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')) @@ -83,19 +81,18 @@ AddEventHandler('esx_society:withdrawMoney', function(society, amount) end) RegisterServerEvent('esx_society:depositMoney') -AddEventHandler('esx_society:depositMoney', function(society, amount) +AddEventHandler('esx_society:depositMoney', function(societyName, amount) local xPlayer = ESX.GetPlayerFromId(source) - local society = GetSociety(society) + 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) - - xPlayer.showNotification(_U('have_deposited', ESX.Math.GroupDigits(amount))) else xPlayer.showNotification(_U('invalid_amount')) end @@ -116,8 +113,8 @@ AddEventHandler('esx_society:washMoney', function(society, amount) MySQL.Async.execute('INSERT INTO society_moneywash (identifier, society, amount) VALUES (@identifier, @society, @amount)', { ['@identifier'] = xPlayer.identifier, - ['@society'] = society, - ['@amount'] = amount + ['@society'] = society, + ['@amount'] = amount }, function(rowsChanged) xPlayer.showNotification(_U('you_have', ESX.Math.GroupDigits(amount))) end) @@ -135,7 +132,6 @@ AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle) TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store) local garage = store.get('garage') or {} - table.insert(garage, vehicle) store.set('garage', garage) end) @@ -221,7 +217,7 @@ ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, soci end) ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society) - local job = json.decode(json.encode(Jobs[society])) + local job = json.decode(json.encode(Jobs[society])) local grades = {} for k,v in pairs(job.grades) do @@ -306,15 +302,15 @@ end) ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb) local xPlayers = ESX.GetPlayers() - local players = {} + local players = {} for i=1, #xPlayers, 1 do local xPlayer = ESX.GetPlayerFromId(xPlayers[i]) table.insert(players, { - source = xPlayer.source, + source = xPlayer.source, identifier = xPlayer.identifier, - name = xPlayer.name, - job = xPlayer.job + name = xPlayer.name, + job = xPlayer.job }) end