forked from Simnation/Main
169 lines
5 KiB
Lua
169 lines
5 KiB
Lua
local Server = lib.require('sv_config')
|
|
local Config = lib.require('config')
|
|
local QBCore = exports['qb-core']:GetCoreObject()
|
|
local players = {}
|
|
|
|
-- Check if player has required job
|
|
local function hasRequiredJob(source)
|
|
local Player = QBCore.Functions.GetPlayer(source)
|
|
if not Player then return false end
|
|
|
|
local PlayerJob = Player.PlayerData.job.name
|
|
local PlayerGrade = Player.PlayerData.job.grade.level
|
|
|
|
for _, allowedJob in ipairs(Config.AllowedJobs) do
|
|
if PlayerJob == allowedJob then
|
|
if Config.RequiredJobGrade and PlayerGrade >= Config.RequiredJobGrade then
|
|
return true
|
|
elseif not Config.RequiredJobGrade then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
-- Start work callback
|
|
lib.callback.register('randol_pizzajob:server:spawnVehicle', function(source, spawnLocation)
|
|
local src = source
|
|
|
|
-- Check if player already has an active job
|
|
if players[src] then
|
|
DoNotification(src, 'You already have an active delivery job!', 'error')
|
|
return false
|
|
end
|
|
|
|
-- Check if player has required job
|
|
if not hasRequiredJob(src) then
|
|
DoNotification(src, 'You don\'t have the required job to work here!', 'error')
|
|
return false
|
|
end
|
|
|
|
-- Create vehicle at specified spawn location
|
|
local veh = CreateVehicle(Server.Vehicle, spawnLocation.x, spawnLocation.y, spawnLocation.z, spawnLocation.w, true, true)
|
|
local ped = GetPlayerPed(src)
|
|
|
|
while not DoesEntityExist(veh) do Wait(0) end
|
|
|
|
while GetVehiclePedIsIn(ped, false) ~= veh do
|
|
TaskWarpPedIntoVehicle(ped, veh, -1)
|
|
Wait(0)
|
|
end
|
|
|
|
local netid = NetworkGetNetworkIdFromEntity(veh)
|
|
|
|
-- Generate delivery locations
|
|
local deliveryLocations = {}
|
|
local addedLocs = {}
|
|
|
|
while #deliveryLocations < Server.Deliveries do
|
|
local index = math.random(#Server.Locations)
|
|
if not addedLocs[index] then
|
|
deliveryLocations[#deliveryLocations + 1] = Server.Locations[index]
|
|
addedLocs[index] = true
|
|
end
|
|
end
|
|
|
|
local currentLocIndex = math.random(#deliveryLocations)
|
|
local currentLoc = deliveryLocations[currentLocIndex]
|
|
table.remove(deliveryLocations, currentLocIndex)
|
|
|
|
local payout = math.random(Server.Payout.min, Server.Payout.max)
|
|
|
|
players[src] = {
|
|
entity = veh,
|
|
locations = deliveryLocations,
|
|
payment = payout,
|
|
current = currentLoc,
|
|
startTime = os.time()
|
|
}
|
|
|
|
return netid, players[src]
|
|
end)
|
|
|
|
-- Payment callback
|
|
lib.callback.register('randol_pizzajob:server:Payment', function(source)
|
|
local src = source
|
|
local Player = QBCore.Functions.GetPlayer(src)
|
|
|
|
if not players[src] then
|
|
return false
|
|
end
|
|
|
|
local pos = GetEntityCoords(GetPlayerPed(src))
|
|
if #(pos - players[src].current) > 5.0 then
|
|
return false
|
|
end
|
|
|
|
Player.Functions.AddMoney(Server.Account, players[src].payment)
|
|
|
|
if #players[src].locations == 0 then
|
|
TriggerClientEvent('QBCore:Notify', src, ('You received $%s. No more deliveries left, return the vehicle.'):format(players[src].payment))
|
|
return true
|
|
end
|
|
|
|
TriggerClientEvent('QBCore:Notify', src, ('You received $%s. Deliveries left: %s'):format(players[src].payment, #players[src].locations))
|
|
|
|
local index = math.random(#players[src].locations)
|
|
local newLoc = players[src].locations[index]
|
|
local payout = math.random(Server.Payout.min, Server.Payout.max)
|
|
table.remove(players[src].locations, index)
|
|
|
|
players[src].current = newLoc
|
|
players[src].payment = payout
|
|
|
|
return true, players[src]
|
|
end)
|
|
|
|
-- Clock out callback
|
|
lib.callback.register('randol_pizzajob:server:clockOut', function(source)
|
|
local src = source
|
|
if not players[src] then return false end
|
|
|
|
local ent = players[src].entity
|
|
if DoesEntityExist(ent) then
|
|
DeleteEntity(ent)
|
|
end
|
|
players[src] = nil
|
|
return true
|
|
end)
|
|
|
|
-- Cleanup on player dropped
|
|
AddEventHandler('playerDropped', function()
|
|
local src = source
|
|
if players[src] then
|
|
local ent = players[src].entity
|
|
if DoesEntityExist(ent) then
|
|
DeleteEntity(ent)
|
|
end
|
|
players[src] = nil
|
|
end
|
|
end)
|
|
|
|
-- Cleanup on player logout
|
|
function ServerOnLogout(source)
|
|
if players[source] then
|
|
local ent = players[source].entity
|
|
if DoesEntityExist(ent) then
|
|
DeleteEntity(ent)
|
|
end
|
|
players[source] = nil
|
|
end
|
|
end
|
|
|
|
|
|
-- Optional: Add periodic cleanup check
|
|
CreateThread(function()
|
|
while true do
|
|
Wait(300000) -- Check every 5 minutes
|
|
for src, data in pairs(players) do
|
|
if not GetPlayerPed(src) or not DoesEntityExist(data.entity) then
|
|
if DoesEntityExist(data.entity) then
|
|
DeleteEntity(data.entity)
|
|
end
|
|
players[src] = nil
|
|
end
|
|
end
|
|
end
|
|
end)
|