0.0.1
This commit is contained in:
132
resources/[esx_addons]/esx_jobs/server/main.lua
Normal file
132
resources/[esx_addons]/esx_jobs/server/main.lua
Normal file
@@ -0,0 +1,132 @@
|
||||
local playersWorking = {}
|
||||
|
||||
CreateThread(function()
|
||||
while true do
|
||||
Wait(1000)
|
||||
local timeNow = os.clock()
|
||||
|
||||
for playerId,data in pairs(playersWorking) do
|
||||
Wait(0)
|
||||
local xPlayer = ESX.Player(playerId)
|
||||
|
||||
-- is player still online?
|
||||
if xPlayer then
|
||||
local distance = #(xPlayer.getCoords(true) - data.zoneCoords)
|
||||
|
||||
-- player still within zone limits?
|
||||
if distance <= data.zoneMaxDistance then
|
||||
-- calculate the elapsed time
|
||||
local timeElapsed = timeNow - data.time
|
||||
|
||||
if timeElapsed > data.jobItem[1].time then
|
||||
data.time = os.clock()
|
||||
|
||||
for k,v in ipairs(data.jobItem) do
|
||||
local itemQtty, requiredItemQtty = 0, 0
|
||||
|
||||
if v.name ~= TranslateCap('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 ~= TranslateCap('delivery') and itemQtty >= v.max then
|
||||
xPlayer.showNotification(TranslateCap('max_limit', v.name))
|
||||
playersWorking[playerId] = nil
|
||||
elseif v.requires ~= 'nothing' and requiredItemQtty <= 0 then
|
||||
xPlayer.showNotification(TranslateCap('not_enough', data.jobItem[1].requires_name))
|
||||
playersWorking[playerId] = nil
|
||||
else
|
||||
if v.name ~= TranslateCap('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, "Job Payment")
|
||||
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
|
||||
end
|
||||
else
|
||||
playersWorking[playerId] = nil
|
||||
end
|
||||
else
|
||||
playersWorking[playerId] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
RegisterServerEvent('esx_jobs:startWork', function(zoneIndex, zoneKey)
|
||||
if not playersWorking[source] then
|
||||
local xPlayer = ESX.Player(source)
|
||||
|
||||
if xPlayer then
|
||||
local jobObject = Config.Jobs[xPlayer.getJob().name]
|
||||
|
||||
if jobObject then
|
||||
local jobZone = jobObject.Zones[zoneKey]
|
||||
|
||||
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', function()
|
||||
if playersWorking[source] then
|
||||
playersWorking[source] = nil
|
||||
end
|
||||
end)
|
||||
|
||||
RegisterNetEvent('esx_jobs:caution', function(cautionType, cautionAmount, spawnPoint, vehicle)
|
||||
local xPlayer = ESX.Player(source)
|
||||
local identifier = xPlayer.getIdentifier()
|
||||
if cautionType == 'take' then
|
||||
if cautionAmount <= Config.MaxCaution and cautionAmount >= 0 then
|
||||
TriggerEvent('esx_addonaccount:getAccount', 'caution', identifier, function(account)
|
||||
if xPlayer.getAccount('bank').money >= cautionAmount then
|
||||
xPlayer.removeAccountMoney('bank', cautionAmount, "Caution Fine")
|
||||
account.addMoney(cautionAmount)
|
||||
xPlayer.showNotification(TranslateCap('bank_deposit_taken', ESX.Math.GroupDigits(cautionAmount)))
|
||||
TriggerClientEvent('esx_jobs:spawnJobVehicle', xPlayer.src, spawnPoint, vehicle)
|
||||
else
|
||||
xPlayer.showNotification(TranslateCap('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', identifier, function(account)
|
||||
local caution = account.money
|
||||
local toGive = ESX.Math.Round(caution * cautionAmount)
|
||||
|
||||
xPlayer.addAccountMoney('bank', toGive, "Caution Return")
|
||||
account.removeMoney(toGive)
|
||||
TriggerClientEvent('esx:showNotification', source, TranslateCap('bank_deposit_returned', ESX.Math.GroupDigits(toGive)))
|
||||
end)
|
||||
end
|
||||
end
|
||||
end)
|
||||
Reference in New Issue
Block a user