Files
Elite-Gaming-FiveM/resources/[EGRP-Map-Addons]/[jd-weighstations]/jd-weighstation01/WeighUi/client.lua
T
2025-12-25 13:17:59 -08:00

185 lines
6.4 KiB
Lua

-- WeighUi/client.lua
local uiOpen = false
local checkDistance = 4.0
local truckWeights = {}
local truckTrailers = {}
local trucksOnLane = {}
local lastLaneWeight = {}
local serverCallbacks = {}
-- =========================
-- CUSTOM COORDINATES
-- =========================
local laneTriggerCenters = {
LaneA = vector3(2905.2993, 4171.1538, 50.2786)
}
-- =========================
-- Helpers
-- =========================
local function IsModelInList(modelHash, list)
for _, name in ipairs(list or {}) do
if GetHashKey(name) == modelHash then
return true
end
end
return false
end
function TriggerServerCallback(name, cb, ...)
local id = math.random(1, 999999)
serverCallbacks[id] = cb
TriggerServerEvent(name, id, ...)
end
RegisterNetEvent("weighstation:serverCallback", function(id, ...)
if serverCallbacks[id] then
serverCallbacks[id](...)
serverCallbacks[id] = nil
end
end)
-- =========================
-- Lane initialization
-- =========================
trucksOnLane["LaneA"] = {}
lastLaneWeight["LaneA"] = nil
-- =========================
-- UI Logic
-- =========================
local function ToggleUI(open)
uiOpen = open
SetNuiFocus(open, open)
SendNUIMessage({ action = open and "showUI" or "hideUI" })
if open then
TriggerServerCallback("weighstation:getRecentVehicles", function(recent)
SendNUIMessage({ action = "updateRecentVehicles", recent = recent })
end)
TriggerServerCallback("weighstation:getStats", function(stats)
SendNUIMessage({
action = "updateStats",
totalTrucks = stats.totalTrucks or 0,
avgWeight = stats.avgWeight or 0
})
end)
end
end
local function ShowPopup(text)
SendNUIMessage({ action = "showPopup", text = text })
end
local function HidePopup()
SendNUIMessage({ action = "hidePopup" })
end
-- =========================
-- Detection Threads
-- =========================
CreateThread(function()
while true do
Wait(0)
local ped = PlayerPedId()
local pos = GetEntityCoords(ped)
local controlPos = Config.MainLights.ControlCoords
local dist = #(pos - controlPos)
if dist < (Config.MainLights.ControlDistance or 2.0) then
ShowPopup("[G] Open Weigh Station")
if IsControlJustPressed(0, 47) and not uiOpen then
ToggleUI(true)
end
else
HidePopup()
end
end
end)
CreateThread(function()
while true do
Wait(1000)
local vehicles = GetGamePool('CVehicle')
local laneName = "LaneA"
local laneCenter = laneTriggerCenters[laneName]
if laneCenter then
local laneWeight = nil
local laneTruckFound = nil
local minDist = checkDistance + 0.01
for _, vehicle in ipairs(vehicles) do
if DoesEntityExist(vehicle) and not IsEntityDead(vehicle) then
local model = GetEntityModel(vehicle)
if IsModelInList(model, Config.RampLight.AllowedModels) then
local vehiclePos = GetEntityCoords(vehicle)
local distToLane = #(vehiclePos - laneCenter)
if distToLane <= checkDistance and distToLane < minDist then
minDist = distToLane
local hasTrailer, trailer = GetVehicleTrailerVehicle(vehicle)
local plate = GetVehicleNumberPlateText(vehicle)
-- PERSISTENT WEIGHT LOGIC START
if not truckWeights[vehicle] or truckTrailers[vehicle] ~= trailer then
-- 1. Generate Seed from License Plate
local seed = 0
for i = 1, #plate do
seed = seed + string.byte(plate, i)
end
-- 2. Set Seed (Forces math.random to be identical to Station 02)
math.randomseed(seed)
if hasTrailer and trailer ~= 0 then
local trailerModel = GetEntityModel(trailer)
if IsModelInList(trailerModel, Config.RampLight.HeavyModels) then
truckWeights[vehicle] = math.random(85000, 125000)
elseif IsModelInList(trailerModel, Config.RampLight.LightModels) then
truckWeights[vehicle] = math.random(35000, 55000)
else
truckWeights[vehicle] = math.random(40000, 100000)
end
else
truckWeights[vehicle] = math.random(32000, 45000) -- Bobtail
end
-- 3. Reset Seed for other system functions
math.randomseed(GetGameTimer())
truckTrailers[vehicle] = trailer
local truckModel = GetDisplayNameFromVehicleModel(model)
local trailerModelName = (hasTrailer and trailer ~= 0) and GetDisplayNameFromVehicleModel(GetEntityModel(trailer)) or nil
TriggerServerEvent("weighstation:logTruck", plate, truckModel, trailerModelName, truckWeights[vehicle])
end
-- PERSISTENT WEIGHT LOGIC END
laneWeight = truckWeights[vehicle]
laneTruckFound = vehicle
end
end
end
end
if lastLaneWeight[laneName] ~= laneWeight then
lastLaneWeight[laneName] = laneWeight
SendNUIMessage({
action = "updateWeight",
lane = laneName,
weight = laneWeight,
vehicle = laneTruckFound or nil
})
end
end
end
end)
RegisterNUICallback('closeUI', function(data, cb)
ToggleUI(false)
cb('ok')
end)