Skip to content

Commit

Permalink
0.00 ms Optimization + Server-Side Player Tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
WLVF committed Aug 22, 2022
1 parent fa0db30 commit 56708a8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 47 deletions.
82 changes: 53 additions & 29 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ For support - Lama#9612 on Discord
Do not edit below if you don't know what you are doing
]] --

amount = 0
local amount = 0
local playerCoords = nil
local jobStarted = false
local truck, trailer = nil, nil
local opti

-- draw blip on the map
Citizen.CreateThread(function()
CreateThread(function()
local blip = AddBlipForCoord(Config.BlipLocation.x, Config.BlipLocation.y, Config.BlipLocation.z)
SetBlipSprite(blip, 457)
SetBlipDisplay(blip, 4)
Expand All @@ -18,27 +22,38 @@ Citizen.CreateThread(function()
EndTextCommandSetBlipName(blip)
end)

CreateThread(function()
while true do
playerCoords = GetEntityCoords(PlayerPedId())
Wait(500)
end
end)

-- starting the job
Citizen.CreateThread(function()
CreateThread(function()
AddTextEntry("press_start_job", "Press ~INPUT_CONTEXT~ to start your shift")
while true do
Citizen.Wait(0)
local player = PlayerPedId()
opti = 2
-- get distance between blip and player and check if player is near it
if #(GetEntityCoords(player) - vector3(Config.BlipLocation.x, Config.BlipLocation.y, Config.BlipLocation.z)) <= 5 then
DisplayHelpTextThisFrame("press_start_job")
if IsControlPressed(1, 38) then
if IsPedSittingInAnyVehicle(player) then
DisplayNotification("~r~You can't start the job while you're in a vehicle.")
else
SpawnVehicle(Config.TruckModel, Config.DepotLocation)
SetPedIntoVehicle(player, vehicle, -1)
-- tell server we are starting the job
TriggerServerEvent("lama_jobs:started")
StartJob()
if not jobStarted then
if #(playerCoords - vector3(Config.BlipLocation.x, Config.BlipLocation.y, Config.BlipLocation.z)) <= 5 then
DisplayHelpTextThisFrame("press_start_job")
if IsControlPressed(1, 38) then
if IsPedSittingInAnyVehicle(player) then
DisplayNotification("~r~You can't start the job while you're in a vehicle.")
else
SpawnVehicle(Config.TruckModel, Config.DepotLocation)
SetPedIntoVehicle(player, vehicle, -1)
-- tell server we are starting the job
TriggerServerEvent("lama_jobs:started")
StartJob()
end
end
else
opti = 2000
end
end
Wait(opti)
end
end)

Expand All @@ -57,23 +72,26 @@ function StartJob()
-- clear area first
ClearArea(location.x, location.y, location.z, 50, false, false, false, false, false);
-- delete previous trailer before spawning a new one
if trailer ~= nil then
if trailer then
DeleteVehicle(trailer)
end
trailer = SpawnTrailer(model, location)
DisplayNotification("~b~New task: ~w~pick up the trailer at the marked location.")
jobStarted = true
while true do
Citizen.Wait(0)
local player = PlayerPedId()
opti = 2
-- gets distance between player and trailer location and check if player is in the vicinity of it
if #(GetEntityCoords(player) - vector3(location.x, location.y, location.z)) <= 20 then
if #(playerCoords - vector3(location.x, location.y, location.z)) <= 20 then
-- and check if they have picked up the trailer
if IsVehicleAttachedToTrailer(vehicle) then
RemoveBlip(blip)
DeliverTrailer()
break
end
else
opti = 2000
end
Wait(opti)
end
end

Expand All @@ -88,18 +106,20 @@ function DeliverTrailer()
SetBlipRouteColour(blip, 26)
DisplayNotification("~b~New task: ~w~deliver the trailer at the marked location.")
while true do
Citizen.Wait(0)
local player = PlayerPedId()
opti = 2
-- gets distance between player and task location and check f player is in the vicinity of it
if #(GetEntityCoords(player) - vector3(location.x, location.y, location.z)) <= 20 then
if #(playerCoords - vector3(location.x, location.y, location.z)) <= 20 then
DisplayHelpTextThisFrame("press_detach_trailer")
-- and check if they don't have a trailer attached anymore
if not IsVehicleAttachedToTrailer(vehicle) then
RemoveBlip(blip)
NewChoice(location)
break
end
else
opti = 2000
end
Wait(opti)
end
end

Expand All @@ -110,7 +130,7 @@ function NewChoice(location)
TriggerServerEvent("lama_jobs:delivered", location)
DisplayNotification("Press ~b~E~w~ to accept another job.\nPress ~r~X~w~ to end your shift.")
while true do
Citizen.Wait(0)
Wait(0)
if IsControlPressed(1, 38) then
StartJob()
break
Expand All @@ -134,31 +154,35 @@ function EndJob()
else
DisplayNotification("~b~New task: ~w~return the truck to the depot.")
end
jobStarted = false
while true do
Citizen.Wait(0)
local player = PlayerPedId()
opti = 2
-- gets distance between player and depot location and check if player is in the vicinity of it
if #(GetEntityCoords(player) - vector3(Config.DepotLocation.x, Config.DepotLocation.y, Config.DepotLocation.z)) <= 10 then
if #(playerCoords - vector3(Config.DepotLocation.x, Config.DepotLocation.y, Config.DepotLocation.z)) <= 10 then
DisplayHelpTextThisFrame("press_end_job")
if IsControlPressed(1, 38) then
RemoveBlip(blip)
-- deletes truck and trailer
local truck = GetVehiclePedIsIn(player, false)
local truck = GetVehiclePedIsIn(PlayerPedId(), false)
if GetEntityModel(truck) == GetHashKey(Config.TruckModel) then
DeleteVehicle(GetVehiclePedIsIn(player, false))
DeleteVehicle(GetVehiclePedIsIn(PlayerPedId(), false))
end
DeleteVehicle(trailer)
if Config.UseND then
-- tell server ve've finished the job and need to pay us
TriggerServerEvent("lama_jobs:finished")
DisplayNotification("You've received ~g~$" .. amount .. " ~w~for completing the job.")
amount = 0
break
else
DisplayNotification("~g~You've successfully completed the job.")
break
end
end
else
opti = 1000
end
Wait(opti)
end
end

Expand Down
39 changes: 21 additions & 18 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ For support - Lama#9612 on Discord
Do not edit below if you don't know what you are doing
]] --

RegisterServerEvent("lama_jobs:started")
RegisterServerEvent("lama_jobs:delivered")
RegisterServerEvent("lama_jobs:finished")

-- ND_Framework exports (edit with your framework's)
NDCore = exports["ND_Core"]:GetCoreObject()

-- variables, do not touch
local sessions = {}
local deliveries = 0
local isOnJob = false
local deliveries = {}
local playersOnJob = {}

function isClientTooFar(location)
local distance = #(GetEntityCoords(GetPlayerPed(source)) - vector3(location.x, location.y, location.z))
Expand All @@ -24,29 +19,37 @@ function isClientTooFar(location)
end
end

AddEventHandler("lama_jobs:started", function()
isOnJob = true
RegisterNetEvent("lama_jobs:started", function()
local src = source
playersOnJob[src] = true
end)

AddEventHandler("lama_jobs:delivered", function(location)
if isOnJob and not isClientTooFar(location) then
RegisterNetEvent("lama_jobs:delivered", function(location)
local src = source
if playersOnJob[src] and not isClientTooFar(location) then
-- keep track of amount of deliveries made
deliveries = deliveries + 1
if not deliveries[src] then
deliveries[src] = 0
end
deliveries[src] = deliveries[src] + 1
else
print(string.format("^1Possible exploiter detected\nName: ^0%s\n^1Identifier: ^0%s\n^1Reason: ^0has delivered from a too big distance", GetPlayerName(source), GetPlayerIdentifier(source, 0)))
end
end)

AddEventHandler("lama_jobs:finished", function()
if deliveries == 0 then
RegisterNetEvent("lama_jobs:finished", function()
local src = source
if not deliveries[src] or deliveries[src] == 0 then
print(string.format("^1Possible exploiter detected\nName: ^0%s\n^1Identifier: ^0%s\n^1Reason: ^0has somehow requested to be paid without delivering anything", GetPlayerName(source), GetPlayerIdentifier(source, 0)))
else
-- calculate amount of money to give to the player
amount = Config.PayPerDelivery * deliveries
if isOnJob and not isClientTooFar(Config.DepotLocation) then
local amount = Config.PayPerDelivery * deliveries[src]
if playersOnJob[src] and not isClientTooFar(Config.DepotLocation) then
-- give the money to player
-- if using another framework than ND, simply change the function below to your framework's
NDCore.Functions.AddMoney(amount, source, "bank")
deliveries[src] = 0
playersOnJob[src] = false
NDCore.Functions.AddMoney(amount, src, "bank")
else
print(string.format("^1Possible exploiter detected\nName: ^0%s\n^1Identifier: ^0%s\n^1Reason: ^0has somehow requested to be paid without being near the job ending location", GetPlayerName(source), GetPlayerIdentifier(source, 0)))
end
Expand All @@ -60,7 +63,7 @@ Citizen.CreateThread(function()

function checkVersion(err, responseText, headers)
-- Returns the version set in the file
curVersion = GetResourceMetadata(GetCurrentResourceName(), "version")
local curVersion = GetResourceMetadata(GetCurrentResourceName(), "version")

if responseText == nil or curVersion == nil then
print("^1There was an error retrieving the version of " .. resourceName .. ": the version checker will be skipped.")
Expand Down

0 comments on commit 56708a8

Please sign in to comment.