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
---@param shopTitle string
---@return boolean
2025-03-09 05:49:05 -04:00
Shops.OpenShop = function ( src , shopTitle )
if not shopTitle and not registeredShops [ shopTitle ] then return false end
TriggerClientEvent ( 'community_bridge:openShop' , src , 'shop' , shopTitle , registeredShops [ shopTitle ])
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.
---@param shopTitle string
---@param shopInventory table
---@param shopCoords table
---@param shopGroups table
---@return boolean
2025-03-09 05:49:05 -04:00
Shops.CreateShop = function ( shopTitle , shopInventory , shopCoords , shopGroups )
2025-04-02 07:27:13 -04:00
if not shopTitle and not shopInventory and not shopCoords then return false end
2025-03-09 05:49:05 -04:00
if registeredShops [ shopTitle ] then return true end
registeredShops [ shopTitle ] = { name = shopTitle , inventory = shopInventory , shopCoords = shopCoords , groups = shopGroups }
return true
end
2025-04-02 07:27:13 -04:00
---This is an internal event to complete a shop transaction, it will verify pass items and amounts are registered to the created shop.
---@param src number
---@param shopName string
---@param item string
---@param amount number
---@param paymentType string
---@return nil
2025-03-09 06:14:38 -04:00
Shops.CompleteCheckout = function ( src , shopName , item , amount , paymentType )
if not src and not shopName and not item and not amount and not paymentType then return end
2025-03-09 05:49:05 -04:00
local ilocale = Language.Locale
local shopData = registeredShops [ shopName ]
if not shopData then return end
local itemData = nil
for _ , data in pairs ( shopData.inventory ) do
if data.name == item then
itemData = data
break
end
end
2025-04-12 02:31:47 -04:00
if not itemData then return Prints.Error ( "Player ID " .. src .. " Is Possibly A Cheater And Has Attempted To Purchase A " .. item ) end
2025-03-09 05:49:05 -04:00
if not itemData.price then return end
local balance = Framework.GetAccountBalance ( src , paymentType )
2025-03-09 06:14:38 -04:00
local mathStuff = tonumber ( itemData.price ) * tonumber ( amount )
if balance < mathStuff then return Notify.SendNotify ( src , ilocale ( 'Shops.NotEnoughMoney' ), "error" , 5000 ) end
if not Framework.RemoveAccountBalance ( src , paymentType , mathStuff ) then return end
Inventory.AddItem ( src , itemData.name , amount )
2025-03-09 05:49:05 -04:00
local itemLabel = Inventory.GetItemInfo ( itemData.name ). label
Notify.SendNotify ( src , ilocale ( 'Shops.PurchasedItem' ) .. itemLabel , "success" , 5000 )
end
2025-03-09 06:14:38 -04:00
RegisterNetEvent ( "community_bridge:completeCheckout" , function ( shopName , item , amount , paymentType )
2025-03-09 05:49:05 -04:00
local src = source
if not shopName and not item and not paymentType then return end
2025-03-09 06:14:38 -04:00
if not paymentType == "money" or not paymentType == "bank" then return end
Shops.CompleteCheckout ( src , shopName , item , amount , paymentType )
2025-05-10 00:01:56 -04:00
end )
return Shops