From 177149082a220f801e6760cf606806d852834f34 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Tue, 30 Aug 2022 14:17:03 +0100 Subject: [PATCH] feat: es_extended - allow decimals in accounts --- [esx]/es_extended/config.lua | 15 +++++-- [esx]/es_extended/server/classes/player.lua | 49 +++++++++++++++------ [esx]/es_extended/server/main.lua | 12 +++-- 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/[esx]/es_extended/config.lua b/[esx]/es_extended/config.lua index 99a9680a..8d5bb3d5 100644 --- a/[esx]/es_extended/config.lua +++ b/[esx]/es_extended/config.lua @@ -2,9 +2,18 @@ Config = {} Config.Locale = 'en' Config.Accounts = { - bank = _U('account_bank'), - black_money = _U('account_black_money'), - money = _U('account_money') + bank = { + label = _U('account_bank'), + round = true + }, + black_money = { + label = _U('account_black_money'), + round = true + }, + money = { + label = _U('account_money'), + round = true + } } Config.StartingAccountMoney = {bank = 50000} diff --git a/[esx]/es_extended/server/classes/player.lua b/[esx]/es_extended/server/classes/player.lua index 33368615..aef0d756 100644 --- a/[esx]/es_extended/server/classes/player.lua +++ b/[esx]/es_extended/server/classes/player.lua @@ -107,9 +107,9 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, end function self.getAccount(account) - for k,v in ipairs(self.accounts) do - if v.name == account then - return v + for i=1, #self.accounts do + if self.accounts[i].name == account then + return self.accounts[i], i end end end @@ -172,41 +172,64 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, end function self.setAccountMoney(accountName, money) + if not tonumber(money) then + print(('[^1ERROR^7] Tried To Set Account ^5%s^0 For Player ^5%s^0 To An Invalid Number!'):format(accountName, self.playerId)) + return + end if money >= 0 then - local account = self.getAccount(accountName) + local account, index = self.getAccount(accountName) if account then - local newMoney = ESX.Math.Round(money) - account.money = newMoney + money = account.round and ESX.Math.Round(money) or money + self.accounts[index].money = money self.triggerEvent('esx:setAccountMoney', account) + else + print(('[^1ERROR^7] Tried To Set Invalid Account ^5%s^0 For Player ^5%s^0!'):format(accountName, self.playerId)) end + else + print(('[^1ERROR^7] Tried To Set Account ^5%s^0 For Player ^5%s^0 To An Invalid Number!'):format(accountName, self.playerId)) end end function self.addAccountMoney(accountName, money) + if not tonumber(money) then + print(('[^1ERROR^7] Tried To Set Account ^5%s^0 For Player ^5%s^0 To An Invalid Number!'):format(accountName, self.playerId)) + return + end if money > 0 then - local account = self.getAccount(accountName) - + local account, index = self.getAccount(accountName) if account then - local newMoney = account.money + ESX.Math.Round(money) - account.money = newMoney + money = account.round and ESX.Math.Round(money) or money + self.accounts[index].money = money self.triggerEvent('esx:setAccountMoney', account) + else + print(('[^1ERROR^7] Tried To Set Add To Invalid Account ^5%s^0 For Player ^5%s^0!'):format(accountName, self.playerId)) end + else + print(('[^1ERROR^7] Tried To Set Account ^5%s^0 For Player ^5%s^0 To An Invalid Number!'):format(accountName, self.playerId)) end end function self.removeAccountMoney(accountName, money) + if not tonumber(money) then + print(('[^1ERROR^7] Tried To Set Account ^5%s^0 For Player ^5%s^0 To An Invalid Number!'):format(accountName, self.playerId)) + return + end if money > 0 then - local account = self.getAccount(accountName) + local account, index = self.getAccount(accountName) if account then - local newMoney = account.money - ESX.Math.Round(money) - account.money = newMoney + money = account.round and ESX.Math.Round(money) or money + self.accounts[index].money = money self.triggerEvent('esx:setAccountMoney', account) + else + print(('[^1ERROR^7] Tried To Set Add To Invalid Account ^5%s^0 For Player ^5%s^0!'):format(accountName, self.playerId)) end + else + print(('[^1ERROR^7] Tried To Set Account ^5%s^0 For Player ^5%s^0 To An Invalid Number!'):format(accountName, self.playerId)) end end diff --git a/[esx]/es_extended/server/main.lua b/[esx]/es_extended/server/main.lua index f9708bd9..5f58d7c8 100644 --- a/[esx]/es_extended/server/main.lua +++ b/[esx]/es_extended/server/main.lua @@ -135,11 +135,15 @@ function loadESXPlayer(identifier, playerId, isNew) end end - for account, label in pairs(Config.Accounts) do + for account, data in pairs(Config.Accounts) do + if data.round == nil then + data.round = true + end userData.accounts[#userData.accounts + 1] ={ name = account, money = foundAccounts[account] or Config.StartingAccountMoney[account] or 0, - label = label + label = data.label, + round = data.round } end @@ -432,9 +436,9 @@ if not Config.OxInventory then targetXPlayer.addAccountMoney(itemName, itemCount) sourceXPlayer.showNotification(_U('gave_account_money', ESX.Math.GroupDigits(itemCount), - Config.Accounts[itemName], targetXPlayer.name)) + Config.Accounts[itemName].label, targetXPlayer.name)) targetXPlayer.showNotification(_U('received_account_money', ESX.Math.GroupDigits(itemCount), - Config.Accounts[itemName], sourceXPlayer.name)) + Config.Accounts[itemName].label, sourceXPlayer.name)) else sourceXPlayer.showNotification(_U('imp_invalid_amount')) end