From 3f9ce9ef2d475d7735293ebfe92ceefce8341143 Mon Sep 17 00:00:00 2001 From: Mirow Date: Fri, 26 Jul 2024 21:03:06 +0200 Subject: [PATCH 1/3] feat(server.lua): added exports to get addon accounts --- server/classes/addonaccount.lua | 25 +++++----- server/main.lua | 88 ++++++++++++++++++--------------- 2 files changed, 60 insertions(+), 53 deletions(-) diff --git a/server/classes/addonaccount.lua b/server/classes/addonaccount.lua index 56fcb91..f5fb4a6 100644 --- a/server/classes/addonaccount.lua +++ b/server/classes/addonaccount.lua @@ -5,32 +5,33 @@ function CreateAddonAccount(name, owner, money) self.owner = owner self.money = money - function self.addMoney(m) - self.money = self.money + m + function self.addMoney(amount) + self.money = self.money + amount self.save() - TriggerEvent('esx_addonaccount:addMoney', self.name, m) + TriggerEvent('esx_addonaccount:addMoney', self.name, amount) end - function self.removeMoney(m) - self.money = self.money - m + function self.removeMoney(amount) + self.money = self.money - amount self.save() - TriggerEvent('esx_addonaccount:removeMoney', self.name, m) + TriggerEvent('esx_addonaccount:removeMoney', self.name, amount) end - function self.setMoney(m) - self.money = m + function self.setMoney(amount) + self.money = amount self.save() - TriggerEvent('esx_addonaccount:setMoney', self.name, m) + TriggerEvent('esx_addonaccount:setMoney', self.name, amount) end function self.save() if self.owner == nil then - MySQL.update('UPDATE addon_account_data SET money = ? WHERE account_name = ?', {self.money, self.name}) + MySQL.update('UPDATE addon_account_data SET money = ? WHERE account_name = ?', { self.money, self.name }) else - MySQL.update('UPDATE addon_account_data SET money = ? WHERE account_name = ? AND owner = ?', {self.money, self.name, self.owner}) + MySQL.update('UPDATE addon_account_data SET money = ? WHERE account_name = ? AND owner = ?', + { self.money, self.name, self.owner }) end TriggerClientEvent('esx_addonaccount:setMoney', -1, self.name, self.money) end return self -end \ No newline at end of file +end diff --git a/server/main.lua b/server/main.lua index e7b4b26..6eb6d6f 100644 --- a/server/main.lua +++ b/server/main.lua @@ -2,7 +2,8 @@ local AccountsIndex, Accounts, SharedAccounts = {}, {}, {} AddEventHandler('onResourceStart', function(resourceName) if resourceName == GetCurrentResourceName() then - local accounts = MySQL.query.await('SELECT * FROM addon_account LEFT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name UNION SELECT * FROM addon_account RIGHT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name') + local accounts = MySQL.query.await( + 'SELECT * FROM addon_account LEFT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name UNION SELECT * FROM addon_account RIGHT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name') local newAccounts = {} for i = 1, #accounts do @@ -12,12 +13,13 @@ AddEventHandler('onResourceStart', function(resourceName) AccountsIndex[#AccountsIndex + 1] = account.name Accounts[account.name] = {} end - Accounts[account.name][#Accounts[account.name] + 1] = CreateAddonAccount(account.name, account.owner, account.money) + Accounts[account.name][#Accounts[account.name] + 1] = CreateAddonAccount(account.name, account.owner, + account.money) else if account.money then SharedAccounts[account.name] = CreateAddonAccount(account.name, nil, account.money) else - newAccounts[#newAccounts + 1] = {account.name, 0} + newAccounts[#newAccounts + 1] = { account.name, 0 } end end end @@ -35,7 +37,7 @@ AddEventHandler('onResourceStart', function(resourceName) end) function GetAccount(name, owner) - for i=1, #Accounts[name], 1 do + for i = 1, #Accounts[name], 1 do if Accounts[name][i].owner == owner then return Accounts[name][i] end @@ -47,30 +49,30 @@ function GetSharedAccount(name) end function AddSharedAccount(society, amount) - -- society.name = job_name/society_name - -- society.label = label for the job/account - -- amount = if the shared account should start with x amount - if type(society) ~= 'table' or not society?.name or not society?.label then return end - - -- check if account already exist? - if SharedAccounts[society.name] ~= nil then return SharedAccounts[society.name] end - - -- addon account: - local account = MySQL.insert.await('INSERT INTO `addon_account` (name, label, shared) VALUES (?, ?, ?)', { - society.name, society.label, 1 - }) - if not account then return end - - -- if addon account inserted, insert addon account data: - local account_data = MySQL.insert.await('INSERT INTO `addon_account_data` (account_name, money) VALUES (?, ?)', { - society.name, (amount or 0) - }) - if not account_data then return end - - -- if all data inserted successfully to sql: - SharedAccounts[society.name] = CreateAddonAccount(society.name, nil, (amount or 0)) - - return SharedAccounts[society.name] + -- society.name = job_name/society_name + -- society.label = label for the job/account + -- amount = if the shared account should start with x amount + if type(society) ~= 'table' or not society?.name or not society?.label then return end + + -- check if account already exist? + if SharedAccounts[society.name] ~= nil then return SharedAccounts[society.name] end + + -- addon account: + local account = MySQL.insert.await('INSERT INTO `addon_account` (name, label, shared) VALUES (?, ?, ?)', { + society.name, society.label, 1 + }) + if not account then return end + + -- if addon account inserted, insert addon account data: + local account_data = MySQL.insert.await('INSERT INTO `addon_account_data` (account_name, money) VALUES (?, ?)', { + society.name, (amount or 0) + }) + if not account_data then return end + + -- if all data inserted successfully to sql: + SharedAccounts[society.name] = CreateAddonAccount(society.name, nil, (amount or 0)) + + return SharedAccounts[society.name] end AddEventHandler('esx_addonaccount:getAccount', function(name, owner, cb) @@ -84,12 +86,13 @@ end) AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) local addonAccounts = {} - for i=1, #AccountsIndex, 1 do + for i = 1, #AccountsIndex, 1 do local name = AccountsIndex[i] local account = GetAccount(name, xPlayer.identifier) if account == nil then - MySQL.insert('INSERT INTO addon_account_data (account_name, money, owner) VALUES (?, ?, ?)', {name, 0, xPlayer.identifier}) + MySQL.insert('INSERT INTO addon_account_data (account_name, money, owner) VALUES (?, ?, ?)', + { name, 0, xPlayer.identifier }) account = CreateAddonAccount(name, xPlayer.identifier, 0) Accounts[name][#Accounts[name] + 1] = account @@ -103,32 +106,31 @@ end) RegisterNetEvent('esx_addonaccount:refreshAccounts') AddEventHandler('esx_addonaccount:refreshAccounts', function() - local result = MySQL.query.await('SELECT * FROM addon_account') + local addonAccounts = MySQL.query.await('SELECT * FROM addon_account') - for i = 1, #result, 1 do - local name = result[i].name - local label = result[i].label - local shared = result[i].shared + for i = 1, #addonAccounts, 1 do + local name = addonAccounts[i].name + local shared = addonAccounts[i].shared - local result2 = MySQL.query.await('SELECT * FROM addon_account_data WHERE account_name = ?', { name }) + local addonAccountData = MySQL.query.await('SELECT * FROM addon_account_data WHERE account_name = ?', { name }) if shared == 0 then table.insert(AccountsIndex, name) Accounts[name] = {} - for j = 1, #result2, 1 do - local addonAccount = CreateAddonAccount(name, result2[j].owner, result2[j].money) + for j = 1, #addonAccountData, 1 do + local addonAccount = CreateAddonAccount(name, addonAccountData[j].owner, addonAccountData[j].money) table.insert(Accounts[name], addonAccount) end else local money = nil - if #result2 == 0 then + if #addonAccountData == 0 then MySQL.insert('INSERT INTO addon_account_data (account_name, money, owner) VALUES (?, ?, ?)', - { name, 0, NULL }) + { name, 0, nil }) money = 0 else - money = result2[1].money + money = addonAccountData[1].money end local addonAccount = CreateAddonAccount(name, nil, money) @@ -136,3 +138,7 @@ AddEventHandler('esx_addonaccount:refreshAccounts', function() end end end) + +exports('getAccount', GetSharedAccount) + +exports('getSharedAccount', GetSharedAccount) From ef55ab9986e6b3ef0fd303a068065012994ef4b5 Mon Sep 17 00:00:00 2001 From: Mirow Date: Fri, 26 Jul 2024 21:07:16 +0200 Subject: [PATCH 2/3] refactor(README): added guide how to use exports --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index db8da6a..f5cc2fb 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ An addon account must be configured in the database before using it. Don't forge | name of the account | label of the account (not used) | is the account shared with others? (boolean either `0` or `1`) | ```lua +-- Using events TriggerEvent('esx_addonaccount:getSharedAccount', 'society_realestateagent', function(account) account.addMoney(500) end) @@ -25,6 +26,13 @@ end) TriggerEvent('esx_addonaccount:getAccount', 'property_black_money', 'steam:0123456789', function(account) account.removeMoney(500) end) + +-- Using exports +local account = exports['esx_addonaccount']:getAccount('property_black_money', 'steam:0123456789') +account.addMoney(500) + +local sharedAccount = exports['esx_addonaccount']:getSharedAccount('society_realestateagent') +sharedAccount.addMoney(500) ``` # Legal From e5251c71da09519eefedca10a76a0fe14eb6f69e Mon Sep 17 00:00:00 2001 From: Mirow Date: Sun, 28 Jul 2024 18:20:27 +0200 Subject: [PATCH 3/3] fix(server): wrong fn being called --- server/main.lua | 288 ++++++++++++++++++++++++------------------------ 1 file changed, 144 insertions(+), 144 deletions(-) diff --git a/server/main.lua b/server/main.lua index 6eb6d6f..f509dbe 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,144 +1,144 @@ -local AccountsIndex, Accounts, SharedAccounts = {}, {}, {} - -AddEventHandler('onResourceStart', function(resourceName) - if resourceName == GetCurrentResourceName() then - local accounts = MySQL.query.await( - 'SELECT * FROM addon_account LEFT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name UNION SELECT * FROM addon_account RIGHT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name') - - local newAccounts = {} - for i = 1, #accounts do - local account = accounts[i] - if account.shared == 0 then - if not Accounts[account.name] then - AccountsIndex[#AccountsIndex + 1] = account.name - Accounts[account.name] = {} - end - Accounts[account.name][#Accounts[account.name] + 1] = CreateAddonAccount(account.name, account.owner, - account.money) - else - if account.money then - SharedAccounts[account.name] = CreateAddonAccount(account.name, nil, account.money) - else - newAccounts[#newAccounts + 1] = { account.name, 0 } - end - end - end - GlobalState.SharedAccounts = SharedAccounts - - if next(newAccounts) then - MySQL.prepare('INSERT INTO addon_account_data (account_name, money) VALUES (?, ?)', newAccounts) - for i = 1, #newAccounts do - local newAccount = newAccounts[i] - SharedAccounts[newAccount[1]] = CreateAddonAccount(newAccount[1], nil, 0) - end - GlobalState.SharedAccounts = SharedAccounts - end - end -end) - -function GetAccount(name, owner) - for i = 1, #Accounts[name], 1 do - if Accounts[name][i].owner == owner then - return Accounts[name][i] - end - end -end - -function GetSharedAccount(name) - return SharedAccounts[name] -end - -function AddSharedAccount(society, amount) - -- society.name = job_name/society_name - -- society.label = label for the job/account - -- amount = if the shared account should start with x amount - if type(society) ~= 'table' or not society?.name or not society?.label then return end - - -- check if account already exist? - if SharedAccounts[society.name] ~= nil then return SharedAccounts[society.name] end - - -- addon account: - local account = MySQL.insert.await('INSERT INTO `addon_account` (name, label, shared) VALUES (?, ?, ?)', { - society.name, society.label, 1 - }) - if not account then return end - - -- if addon account inserted, insert addon account data: - local account_data = MySQL.insert.await('INSERT INTO `addon_account_data` (account_name, money) VALUES (?, ?)', { - society.name, (amount or 0) - }) - if not account_data then return end - - -- if all data inserted successfully to sql: - SharedAccounts[society.name] = CreateAddonAccount(society.name, nil, (amount or 0)) - - return SharedAccounts[society.name] -end - -AddEventHandler('esx_addonaccount:getAccount', function(name, owner, cb) - cb(GetAccount(name, owner)) -end) - -AddEventHandler('esx_addonaccount:getSharedAccount', function(name, cb) - cb(GetSharedAccount(name)) -end) - -AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) - local addonAccounts = {} - - for i = 1, #AccountsIndex, 1 do - local name = AccountsIndex[i] - local account = GetAccount(name, xPlayer.identifier) - - if account == nil then - MySQL.insert('INSERT INTO addon_account_data (account_name, money, owner) VALUES (?, ?, ?)', - { name, 0, xPlayer.identifier }) - - account = CreateAddonAccount(name, xPlayer.identifier, 0) - Accounts[name][#Accounts[name] + 1] = account - end - - addonAccounts[#addonAccounts + 1] = account - end - - xPlayer.set('addonAccounts', addonAccounts) -end) - -RegisterNetEvent('esx_addonaccount:refreshAccounts') -AddEventHandler('esx_addonaccount:refreshAccounts', function() - local addonAccounts = MySQL.query.await('SELECT * FROM addon_account') - - for i = 1, #addonAccounts, 1 do - local name = addonAccounts[i].name - local shared = addonAccounts[i].shared - - local addonAccountData = MySQL.query.await('SELECT * FROM addon_account_data WHERE account_name = ?', { name }) - - if shared == 0 then - table.insert(AccountsIndex, name) - Accounts[name] = {} - - for j = 1, #addonAccountData, 1 do - local addonAccount = CreateAddonAccount(name, addonAccountData[j].owner, addonAccountData[j].money) - table.insert(Accounts[name], addonAccount) - end - else - local money = nil - - if #addonAccountData == 0 then - MySQL.insert('INSERT INTO addon_account_data (account_name, money, owner) VALUES (?, ?, ?)', - { name, 0, nil }) - money = 0 - else - money = addonAccountData[1].money - end - - local addonAccount = CreateAddonAccount(name, nil, money) - SharedAccounts[name] = addonAccount - end - end -end) - -exports('getAccount', GetSharedAccount) - -exports('getSharedAccount', GetSharedAccount) +local AccountsIndex, Accounts, SharedAccounts = {}, {}, {} + +AddEventHandler('onResourceStart', function(resourceName) + if resourceName == GetCurrentResourceName() then + local accounts = MySQL.query.await( + 'SELECT * FROM addon_account LEFT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name UNION SELECT * FROM addon_account RIGHT JOIN addon_account_data ON addon_account.name = addon_account_data.account_name') + + local newAccounts = {} + for i = 1, #accounts do + local account = accounts[i] + if account.shared == 0 then + if not Accounts[account.name] then + AccountsIndex[#AccountsIndex + 1] = account.name + Accounts[account.name] = {} + end + Accounts[account.name][#Accounts[account.name] + 1] = CreateAddonAccount(account.name, account.owner, + account.money) + else + if account.money then + SharedAccounts[account.name] = CreateAddonAccount(account.name, nil, account.money) + else + newAccounts[#newAccounts + 1] = { account.name, 0 } + end + end + end + GlobalState.SharedAccounts = SharedAccounts + + if next(newAccounts) then + MySQL.prepare('INSERT INTO addon_account_data (account_name, money) VALUES (?, ?)', newAccounts) + for i = 1, #newAccounts do + local newAccount = newAccounts[i] + SharedAccounts[newAccount[1]] = CreateAddonAccount(newAccount[1], nil, 0) + end + GlobalState.SharedAccounts = SharedAccounts + end + end +end) + +function GetAccount(name, owner) + for i = 1, #Accounts[name], 1 do + if Accounts[name][i].owner == owner then + return Accounts[name][i] + end + end +end + +function GetSharedAccount(name) + return SharedAccounts[name] +end + +function AddSharedAccount(society, amount) + -- society.name = job_name/society_name + -- society.label = label for the job/account + -- amount = if the shared account should start with x amount + if type(society) ~= 'table' or not society?.name or not society?.label then return end + + -- check if account already exist? + if SharedAccounts[society.name] ~= nil then return SharedAccounts[society.name] end + + -- addon account: + local account = MySQL.insert.await('INSERT INTO `addon_account` (name, label, shared) VALUES (?, ?, ?)', { + society.name, society.label, 1 + }) + if not account then return end + + -- if addon account inserted, insert addon account data: + local account_data = MySQL.insert.await('INSERT INTO `addon_account_data` (account_name, money) VALUES (?, ?)', { + society.name, (amount or 0) + }) + if not account_data then return end + + -- if all data inserted successfully to sql: + SharedAccounts[society.name] = CreateAddonAccount(society.name, nil, (amount or 0)) + + return SharedAccounts[society.name] +end + +AddEventHandler('esx_addonaccount:getAccount', function(name, owner, cb) + cb(GetAccount(name, owner)) +end) + +AddEventHandler('esx_addonaccount:getSharedAccount', function(name, cb) + cb(GetSharedAccount(name)) +end) + +AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) + local addonAccounts = {} + + for i = 1, #AccountsIndex, 1 do + local name = AccountsIndex[i] + local account = GetAccount(name, xPlayer.identifier) + + if account == nil then + MySQL.insert('INSERT INTO addon_account_data (account_name, money, owner) VALUES (?, ?, ?)', + { name, 0, xPlayer.identifier }) + + account = CreateAddonAccount(name, xPlayer.identifier, 0) + Accounts[name][#Accounts[name] + 1] = account + end + + addonAccounts[#addonAccounts + 1] = account + end + + xPlayer.set('addonAccounts', addonAccounts) +end) + +RegisterNetEvent('esx_addonaccount:refreshAccounts') +AddEventHandler('esx_addonaccount:refreshAccounts', function() + local addonAccounts = MySQL.query.await('SELECT * FROM addon_account') + + for i = 1, #addonAccounts, 1 do + local name = addonAccounts[i].name + local shared = addonAccounts[i].shared + + local addonAccountData = MySQL.query.await('SELECT * FROM addon_account_data WHERE account_name = ?', { name }) + + if shared == 0 then + table.insert(AccountsIndex, name) + Accounts[name] = {} + + for j = 1, #addonAccountData, 1 do + local addonAccount = CreateAddonAccount(name, addonAccountData[j].owner, addonAccountData[j].money) + table.insert(Accounts[name], addonAccount) + end + else + local money = nil + + if #addonAccountData == 0 then + MySQL.insert('INSERT INTO addon_account_data (account_name, money, owner) VALUES (?, ?, ?)', + { name, 0, nil }) + money = 0 + else + money = addonAccountData[1].money + end + + local addonAccount = CreateAddonAccount(name, nil, money) + SharedAccounts[name] = addonAccount + end + end +end) + +exports('getAccount', GetAccount) + +exports('getSharedAccount', GetSharedAccount)