Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved AddSharedAccount function #14

Merged
merged 6 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,4 @@ server_scripts {
'server/main.lua'
}

server_exports {
'GetSharedAccount',
'AddSharedAccount',
'GetAccount'
}

dependency 'es_extended'
74 changes: 50 additions & 24 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,63 @@ function GetAccount(name, owner)
end
end
end
exports("GetAccount", GetAccount)

function GetSharedAccount(name)
return SharedAccounts[name]
end
exports("GetSharedAccount", GetSharedAccount)

---@class Society
---@field name string Unique job/society identifier (e.g., "mechanic", "police").
---@field label string Display label for the job/society (e.g., "Mechanic", "Police Department").

---@param society Society The society table containing name and label.
---@param amount? number The starting balance for the shared account. Default is 0.
---@return table? account Returns the account table on success, or `nil` on failure.
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]
-- Validate input parameters
if type(society) ~= "table" then
error("Expected society as a table")
end

if type(society.name) ~= "string" or society.name == "" then
error("Invalid society.name provided")
end

if type(society.label) ~= "string" or society.label == "" then
error("Invalid society.label provided")
end

-- Check if account already exists
if SharedAccounts[society.name] then
return SharedAccounts[society.name]
end

-- Insert into `addon_account` table
local accountInsert = MySQL.insert.await('INSERT INTO `addon_account` (name, label, shared) VALUES (?, ?, ?)', {
society.name, society.label, 1
})

if not accountInsert then
error("Database error: Failed to insert into addon_account")
end

-- Insert into `addon_account_data` table
local accountDataInsert = MySQL.insert.await('INSERT INTO `addon_account_data` (account_name, money) VALUES (?, ?)', {
society.name, amount or 0
})

if not accountDataInsert then
error("Database error: Failed to insert into addon_account_data")
end

-- Successfully created account
SharedAccounts[society.name] = CreateAddonAccount(society.name, nil, amount or 0)

return SharedAccounts[society.name]
end
exports("AddSharedAccount", AddSharedAccount)

AddEventHandler('esx_addonaccount:getAccount', function(name, owner, cb)
cb(GetAccount(name, owner))
Expand Down