Completed server-sided code, and fixed serious caution flaws

This commit is contained in:
ElPumpo
2019-12-07 12:44:36 +01:00
parent 5a38d951e8
commit 5e8b248e0a
17 changed files with 181 additions and 132 deletions
+107 -73
View File
@@ -1,105 +1,139 @@
local PlayersWorking = {}
local playersWorking = {}
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
local function Work(source, item)
Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)
local timeNow = os.clock()
SetTimeout(item[1].time, function()
for playerId,data in pairs(playersWorking) do
Citizen.Wait(10)
local xPlayer = ESX.GetPlayerFromId(playerId)
if PlayersWorking[source] == true then
-- is player still online?
if xPlayer then
local distance = #(xPlayer.getCoords(true) - data.zoneCoords)
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer == nil then
return
end
-- player still within zone limits?
if distance <= data.zoneMaxDistance then
-- calculate the elapsed time
local timeElapsed = timeNow - data.time
for i=1, #item, 1 do
local itemQtty = 0
if item[i].name ~= _U('delivery') then
itemQtty = xPlayer.getInventoryItem(item[i].db_name).count
end
if timeElapsed > data.jobItem[1].time then
data.time = os.clock()
local requiredItemQtty = 0
if item[1].requires ~= 'nothing' then
requiredItemQtty = xPlayer.getInventoryItem(item[1].requires).count
end
for k,v in ipairs(data.jobItem) do
local itemQtty, requiredItemQtty = 0, 0
if item[i].name ~= _U('delivery') and itemQtty >= item[i].max then
xPlayer.showNotification(_U('max_limit', item[i].name))
elseif item[i].requires ~= 'nothing' and requiredItemQtty <= 0 then
xPlayer.showNotification(_U('not_enough', item[1].requires_name))
else
if item[i].name ~= _U('delivery') then
-- Chances to drop the item
if item[i].drop == 100 then
xPlayer.addInventoryItem(item[i].db_name, item[i].add)
else
local chanceToDrop = math.random(100)
if chanceToDrop <= item[i].drop then
xPlayer.addInventoryItem(item[i].db_name, item[i].add)
if v.name ~= _U('delivery') then
itemQtty = xPlayer.getInventoryItem(v.db_name).count
end
if data.jobItem[1].requires ~= 'nothing' then
requiredItemQtty = xPlayer.getInventoryItem(data.jobItem[1].requires).count
end
if v.name ~= _U('delivery') and itemQtty >= v.max then
xPlayer.showNotification(_U('max_limit', v.name))
playersWorking[playerId] = nil
elseif v.requires ~= 'nothing' and requiredItemQtty <= 0 then
xPlayer.showNotification(_U('not_enough', data.jobItem[1].requires_name))
playersWorking[playerId] = nil
else
if v.name ~= _U('delivery') then
-- chances to drop the item
if v.drop == 100 then
xPlayer.addInventoryItem(v.db_name, v.add)
else
local chanceToDrop = math.random(100)
if chanceToDrop <= v.drop then
xPlayer.addInventoryItem(v.db_name, v.add)
end
end
else
xPlayer.addMoney(v.price)
end
end
end
if data.jobItem[1].requires ~= 'nothing' then
local itemToRemoveQtty = xPlayer.getInventoryItem(data.jobItem[1].requires).count
if itemToRemoveQtty > 0 then
xPlayer.removeInventoryItem(data.jobItem[1].requires, data.jobItem[1].remove)
end
end
else
xPlayer.addMoney(item[i].price)
end
else
playersWorking[playerId] = nil
end
else
playersWorking[playerId] = nil
end
if item[1].requires ~= 'nothing' then
local itemToRemoveQtty = xPlayer.getInventoryItem(item[1].requires).count
if itemToRemoveQtty > 0 then
xPlayer.removeInventoryItem(item[1].requires, item[1].remove)
end
end
Work(source, item)
end
end)
end
end
end)
RegisterServerEvent('esx_jobs:startWork')
AddEventHandler('esx_jobs:startWork', function(item)
if not PlayersWorking[source] then
PlayersWorking[source] = true
Work(source, item)
else
print(('esx_jobs: %s attempted to exploit the marker!'):format(GetPlayerIdentifiers(source)[1]))
AddEventHandler('esx_jobs:startWork', function(zoneIndex)
if not playersWorking[source] then
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer then
local jobObject = Config.Jobs[xPlayer.job.name]
if jobObject then
local jobZone = jobObject.Zones[zoneIndex]
if jobZone and jobZone.Item then
playersWorking[source] = {
jobItem = jobZone.Item,
zoneCoords = vector3(jobZone.Pos.x, jobZone.Pos.y, jobZone.Pos.z),
zoneMaxDistance = jobZone.Size.x,
time = os.clock()
}
end
end
end
end
end)
RegisterServerEvent('esx_jobs:stopWork')
AddEventHandler('esx_jobs:stopWork', function()
PlayersWorking[source] = false
if playersWorking[source] then
playersWorking[source] = nil
end
end)
RegisterServerEvent('esx_jobs:caution')
RegisterNetEvent('esx_jobs:caution')
AddEventHandler('esx_jobs:caution', function(cautionType, cautionAmount, spawnPoint, vehicle)
local xPlayer = ESX.GetPlayerFromId(source)
if cautionType == 'take' then
TriggerEvent('esx_addonaccount:getAccount', 'caution', xPlayer.identifier, function(account)
xPlayer.removeAccountMoney('bank', cautionAmount)
account.addMoney(cautionAmount)
end)
xPlayer.showNotification(_U('bank_deposit_taken', ESX.Math.GroupDigits(cautionAmount)))
TriggerClientEvent('esx_jobs:spawnJobVehicle', source, spawnPoint, vehicle)
elseif cautionType == 'give_back' then
if cautionAmount > 1 then
print(('esx_jobs: %s is using cheat engine!'):format(xPlayer.identifier))
return
if cautionAmount <= Config.MaxCaution and cautionAmount > 0 then
TriggerEvent('esx_addonaccount:getAccount', 'caution', xPlayer.identifier, function(account)
if xPlayer.getAccount('bank').money >= cautionAmount then
xPlayer.removeAccountMoney('bank', cautionAmount)
account.addMoney(cautionAmount)
xPlayer.showNotification(_U('bank_deposit_taken', ESX.Math.GroupDigits(cautionAmount)))
TriggerClientEvent('esx_jobs:spawnJobVehicle', xPlayer.source, spawnPoint, vehicle)
else
xPlayer.showNotification(_U('caution_afford', ESX.Math.GroupDigits(cautionAmount)))
end
end)
end
elseif cautionType == 'give_back' then
if cautionAmount <= 1 and cautionAmount > 0 then
TriggerEvent('esx_addonaccount:getAccount', 'caution', xPlayer.identifier, function(account)
local caution = account.money
local toGive = ESX.Math.Round(caution * cautionAmount)
xPlayer.addAccountMoney('bank', toGive)
account.removeMoney(toGive)
TriggerClientEvent('esx:showNotification', source, _U('bank_deposit_returned', ESX.Math.GroupDigits(toGive)))
end)
end
TriggerEvent('esx_addonaccount:getAccount', 'caution', xPlayer.identifier, function(account)
local caution = account.money
local toGive = ESX.Math.Round(caution * cautionAmount)
xPlayer.addAccountMoney('bank', toGive)
account.removeMoney(toGive)
xPlayer.showNotification(_U('bank_deposit_returned', ESX.Math.GroupDigits(toGive)))
end)
end
end)