Major improvements, added slider

This commit is contained in:
ElPumpo
2018-06-11 22:07:57 +02:00
parent b4a7a43fa2
commit df4b505fe7
10 changed files with 164 additions and 104 deletions
+69 -42
View File
@@ -1,70 +1,97 @@
ESX = nil
local ItemsLabels = {}
ESX = nil
local ShopItems = {}
local hasSqlRun = false
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
-- Load item labels
Citizen.CreateThread(function()
Citizen.Wait(5000)
MySQL.Async.fetchAll(
'SELECT * FROM items',
{},
function(result)
for i=1, #result, 1 do
ItemsLabels[result[i].name] = result[i].label
end
end)
-- Load items
AddEventHandler('onMySQLReady', function()
hasSqlRun = true
LoadShop()
end)
ESX.RegisterServerCallback('esx_shops:requestDBItems', function(source, cb)
-- extremely useful when restarting script mid-game
Citizen.CreateThread(function()
Citizen.Wait(20000) -- hopefully enough for connection to the SQL server
MySQL.Async.fetchAll(
'SELECT * FROM shops',
{},
function(result)
local shopItems = {}
for i=1, #result, 1 do
if shopItems[result[i].name] == nil then
shopItems[result[i].name] = {}
end
table.insert(shopItems[result[i].name], {
name = result[i].item,
price = result[i].price,
label = ItemsLabels[result[i].item]
})
if not hasSqlRun then
LoadShop()
hasSqlRun = true
end
end)
function LoadShop()
local itemResult = MySQL.Sync.fetchAll('SELECT * FROM items')
local shopResult = MySQL.Sync.fetchAll('SELECT * FROM shops')
local itemInformation = {}
for i=1, #itemResult, 1 do
if itemInformation[itemResult[i].name] == nil then
itemInformation[itemResult[i].name] = {}
end
cb(shopItems)
end)
itemInformation[itemResult[i].name].label = itemResult[i].label
itemInformation[itemResult[i].name].limit = itemResult[i].limit
end
for i=1, #shopResult, 1 do
if ShopItems[shopResult[i].store] == nil then
ShopItems[shopResult[i].store] = {}
end
table.insert(ShopItems[shopResult[i].store], {
label = itemInformation[shopResult[i].item].label,
item = shopResult[i].item,
price = shopResult[i].price,
limit = itemInformation[shopResult[i].item].limit
})
end
end
ESX.RegisterServerCallback('esx_shops:requestDBItems', function(source, cb)
if not hasSqlRun then
TriggerClientEvent('esx:showNotification', source, 'The shop database has not been loaded yet, try again in a few moments.')
end
cb(ShopItems)
end)
RegisterServerEvent('esx_shops:buyItem')
AddEventHandler('esx_shops:buyItem', function(itemName, price)
AddEventHandler('esx_shops:buyItem', function(itemName, amount, zone)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local sourceItem = xPlayer.getInventoryItem(itemName)
-- is the player trying to cheat?
if price < 0 then
print('esx_shops: ' .. xPlayer.identifier .. ' attempted to cheat money!')
-- is the player trying to exploit?
if amount < 0 then
print('esx_shops: ' .. xPlayer.identifier .. ' attempted to exploit the shop!')
return
end
-- get price
local price = 0
local itemLabel = ''
for i=1, #ShopItems[zone], 1 do
if ShopItems[zone][i].item == itemName then
price = ShopItems[zone][i].price
itemLabel = ShopItems[zone][i].label
break
end
end
price = price * amount
-- can the player afford this item?
if xPlayer.getMoney() >= price then
-- can the player carry the said amount of x item?
if sourceItem.limit ~= -1 and (sourceItem.count + 1) > sourceItem.limit then
if sourceItem.limit ~= -1 and (sourceItem.count + amount) > sourceItem.limit then
TriggerClientEvent('esx:showNotification', _source, _U('player_cannot_hold'))
else
xPlayer.removeMoney(price)
xPlayer.addInventoryItem(itemName, 1)
TriggerClientEvent('esx:showNotification', _source, _U('bought', ItemsLabels[itemName], price))
xPlayer.addInventoryItem(itemName, amount)
TriggerClientEvent('esx:showNotification', _source, _U('bought', amount, itemLabel, price))
end
else
local missingMoney = price - xPlayer.getMoney()
TriggerClientEvent('esx:showNotification', _source, _U('not_enough', missingMoney))
end
end)