2025-06-21 07:45:56 -04:00
---@diagnostic disable: duplicate-set-field
2025-03-09 05:49:05 -04:00
Shops = Shops or {}
local registeredShops = {}
2025-06-07 22:50:13 -04:00
---This can open a shop for the client
2025-04-02 07:27:13 -04:00
---@param src number
2025-12-14 16:22:35 -05:00
---@param shopName string
2025-04-02 07:27:13 -04:00
---@return boolean
2025-12-14 16:22:35 -05:00
Shops.OpenShop = function ( src , shopName )
if not shopName and not registeredShops [ shopName ] then return false , print ( "community_bridge PLAYERID " .. src .. " attempted to open invalid shop: " .. shopName ) end
TriggerClientEvent ( 'community_bridge:Client:openShop' , src , 'shop' , shopName , registeredShops [ shopName ])
2025-04-02 07:27:13 -04:00
return true
2025-03-09 05:49:05 -04:00
end
2025-04-02 07:27:13 -04:00
---This will create a shop to use on the client side, shops must be registered server side and exsist in the shop inventory table to allow any purchases passed.
2025-12-14 16:22:35 -05:00
---@param shopName string
2025-04-02 07:27:13 -04:00
---@param shopInventory table
---@param shopCoords table
---@param shopGroups table
---@return boolean
2025-12-14 16:22:35 -05:00
Shops.CreateShop = function ( shopName , shopInventory , shopCoords , shopGroups )
if not shopName and not shopInventory and not shopCoords then return false end
if registeredShops [ shopName ] then return true end
registeredShops [ shopName ] = { name = shopName , inventory = shopInventory , shopCoords = shopCoords , groups = shopGroups }
2025-03-09 05:49:05 -04:00
return true
end
2025-12-14 16:22:35 -05:00
---This is an internal event to complete a shop transaction, it will verify pass items and amounts are registered to the created shop. Please do not use this function directly.
2025-04-02 07:27:13 -04:00
---@param src number
---@param shopName string
---@param item string
---@param amount number
2025-12-14 16:22:35 -05:00
---@param account string
2025-04-02 07:27:13 -04:00
---@return nil
2025-12-14 16:22:35 -05:00
Shops.CompleteCheckout = function ( src , shopName , item , amount , account )
if not src or not shopName or not item or not amount or not account then return end
if not amount or amount <= 0 then return end
2025-03-09 05:49:05 -04:00
local shopData = registeredShops [ shopName ]
if not shopData then return end
2025-12-14 16:22:35 -05:00
2025-03-09 05:49:05 -04:00
local itemData = nil
for _ , data in pairs ( shopData.inventory ) do
if data.name == item then
itemData = data
break
end
end
2025-12-14 16:22:35 -05:00
if not itemData or not itemData.price then return print ( "community_bridge Player ID " .. src .. " attempted to purchase invalid item from shop: " .. shopName ) end
2026-08-18 00:02:04 -07:00
if itemData.count and itemData.count <= 0 then return Notify.SendNotify ( src , lib.locale ( 'Shops.NotEnoughStock' ), "error" , 5000 ) end
2025-12-14 16:22:35 -05:00
local totalCost = tonumber ( itemData.price ) * amount
local balance = Framework.GetAccountBalance ( src , account )
if not balance then return end
2026-08-18 00:02:04 -07:00
if balance <= 0 then return Notify.SendNotify ( src , lib.locale ( 'Shops.NotEnoughMoney' ), "error" , 5000 ) end
if balance < totalCost then return Notify.SendNotify ( src , lib.locale ( 'Shops.NotEnoughMoney' ), "error" , 5000 ) end
2025-12-14 16:22:35 -05:00
if not Framework.RemoveAccountBalance ( src , account , totalCost ) then return end
local success = Inventory.AddItem ( src , itemData.name , amount )
if not success then
Framework.AddAccountBalance ( src , account , totalCost )
2026-08-18 00:02:04 -07:00
return Notify.SendNotify ( src , lib.locale ( 'Shops.PurchaseFailed' ), "error" , 5000 )
2025-12-14 16:22:35 -05:00
end
local itemLabel = Inventory.GetItemInfo ( itemData.name ). label or itemData.name
2026-08-18 00:02:04 -07:00
Notify.SendNotify ( src , lib.locale ( 'Shops.PurchasedItem' , amount , itemLabel ), "success" , 5000 )
2025-03-09 05:49:05 -04:00
end
2025-12-14 16:22:35 -05:00
---This is an internal event to complete a checkout, complete with multiple validations. Please do not use this event directly.
---@param shopName string
---@param item string
---@param amount number
---@param account string
RegisterNetEvent ( "community_bridge:Server:completeCheckout" , function ( shopName , item , amount , account )
2025-03-09 05:49:05 -04:00
local src = source
2025-12-14 16:22:35 -05:00
if not shopName or not item or not account or not amount then return end
if account ~= "money" and account ~= "bank" then return end
amount = tonumber ( amount )
if not amount or amount <= 0 then return end
Shops.CompleteCheckout ( src , shopName , item , amount , account )
2025-05-10 00:01:56 -04:00
end )
2025-12-14 16:22:35 -05:00
return Shops