Skip to content

Commit

Permalink
tweak(all): State bags (optimisation)
Browse files Browse the repository at this point in the history
less client and server event vice versa data transfering.
  • Loading branch information
renzuzu committed Oct 8, 2021
1 parent 63c3777 commit adcc91c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
8 changes: 7 additions & 1 deletion renzu_hud/client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ AddEventHandler('renzu_hud:install_turbo', function(type)
Hud.veh_stats[plate].turbo = type
Hud.veh_stats[plate].turbo_health = config.turbo_health
TriggerServerEvent('renzu_hud:savedata', plate, Hud.veh_stats[tostring(plate)])
LocalPlayer.state:set( --[[keyName]] 'adv_stat', --[[value]] Hud.veh_stats, --[[replicate to server]] true)
Hud:Notify('success','Turbo Install',""..Hud.veh_stats[plate].turbo.." turbine has been install")
Hud:playanimation('rcmepsilonism8','bag_handler_close_trunk_walk_left')
Wait(2000)
Expand Down Expand Up @@ -1520,7 +1521,7 @@ CreateThread(function()
local sleep = 2000
for k,v in pairs(Hud.nearstancer) do
if v.speed > 1 and not v.wheeledit and v.dist < 100 and Hud.veh_stats[v.plate] ~= nil and Hud.veh_stats[v.plate]['wheelsetting'] ~= nil then
sleep = 30
sleep = 11
SetVehicleWheelWidth(v.entity,0.7) -- trick to avoid stance bug
SetVehicleWheelXOffset(v.entity,0,tonumber(Hud.veh_stats[v.plate]['wheelsetting']['wheeloffsetfront'].wheel0))
SetVehicleWheelXOffset(v.entity,1,tonumber(Hud.veh_stats[v.plate]['wheelsetting']['wheeloffsetfront'].wheel1))
Expand Down Expand Up @@ -1585,6 +1586,7 @@ RegisterNUICallback('wheelsetting', function(data, cb)
Hud.veh_stats[plate].height = vehicle_height
if Hud.vehicle ~= nil and Hud.vehicle ~= 0 and not data.bool then
TriggerServerEvent('renzu_hud:savedata', plate, Hud.veh_stats[tostring(plate)])
LocalPlayer.state:set( --[[keyName]] 'adv_stat', --[[value]] Hud.veh_stats, --[[replicate to server]] true)
end
Wait(1000)
Hud.nearstancer[plate].wheeledit = false
Expand Down Expand Up @@ -1953,6 +1955,7 @@ AddEventHandler("renzu_hud:installtire", function(type)
Hud.veh_stats[plate].tires = type
end
TriggerServerEvent('renzu_hud:savedata', plate, Hud.veh_stats[tostring(plate)])
LocalPlayer.state:set( --[[keyName]] 'adv_stat', --[[value]] Hud.veh_stats, --[[replicate to server]] true)
Hud:ReqAndDelete(Hud.proptire,true)
break
else
Expand Down Expand Up @@ -1992,6 +1995,7 @@ AddEventHandler("renzu_hud:installtire", function(type)
end
end
TriggerServerEvent('renzu_hud:savedata', plate, Hud.veh_stats[tostring(plate)])
LocalPlayer.state:set( --[[keyName]] 'adv_stat', --[[value]] Hud.veh_stats, --[[replicate to server]] true)
Hud:Notify('success','Tire System',"New Tire #"..i.." has been Successfully Install")
Hud:ReqAndDelete(Hud.proptire,true)
break
Expand Down Expand Up @@ -2366,7 +2370,9 @@ end)

RegisterNetEvent('renzu_hud:syncengine')
AddEventHandler('renzu_hud:syncengine', function(plate, t)
while Hud.veh_stats == nil do Wait(1) Hud.veh_stats = LocalPlayer.state.adv_stat end
Hud.veh_stats[tostring(plate)] = t
Hud.onlinevehicles[tostring(plate)] = t
end)

RegisterCommand(config.commands['carui'], function(source, args, raw)
Expand Down
19 changes: 13 additions & 6 deletions renzu_hud/client/function.lua
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ function Hud:SetVehicleOnline() -- for vehicle loop
if self.veh_stats[self.plate] ~= nil then
self.veh_stats[self.plate].entity = VehToNet(self.vehicle )
TriggerServerEvent('renzu_hud:savedata', self.plate, self.veh_stats[tostring(self.plate)],true)
LocalPlayer.state:set( --[[keyName]] 'adv_stat', --[[value]] self.veh_stats, --[[replicate to server]] true)
end
end

Expand Down Expand Up @@ -768,9 +769,12 @@ end

function Hud:get_veh_stats(v,p)
--if self.veh_stats[plate] ~= nil then return end
while not self.veh_stats_loaded do
while not LocalPlayer.state.loaded do
Wait(10)
end
while self.veh_stats == nil do Wait(1) self.veh_stats = LocalPlayer.state.adv_stat end
self.veh_stats = LocalPlayer.state.adv_stat
--while self.veh_stats[self.plate] == nil do Wait(10) self.veh_ end
if v ~= nil and p ~= nil then
self.vehicle = v
self.plate = p
Expand Down Expand Up @@ -816,7 +820,7 @@ function Hud:get_veh_stats(v,p)
end
if self.veh_stats[self.plate].engine ~= nil and self.veh_stats[self.plate].engine ~= 'default' and self.currentengine[self.plate] ~= GetHashKey(tostring(self.veh_stats[self.plate].engine)) and self.invehicle then
self:SetEngineSpecs(self.vehicle , GetHashKey(tostring(self.veh_stats[self.plate].engine)))
print("new ENGINE")
print("new ENGINE",self.veh_stats[self.plate].engine)
Citizen.Wait(1500)
end
if self.veh_stats[self.plate].tires ~= nil and self.veh_stats[self.plate].tires ~= 'default' and self.invehicle then
Expand All @@ -838,6 +842,7 @@ function Hud:get_veh_stats(v,p)
end
if lets_save then
TriggerServerEvent('renzu_hud:savedata', saveplate, self.veh_stats[tostring(saveplate)])
LocalPlayer.state:set( --[[keyName]] 'adv_stat', --[[value]] self.veh_stats, --[[replicate to server]] true)
lets_save = false -- why?
end
end
Expand Down Expand Up @@ -991,6 +996,7 @@ function Hud:NuiMileAge()
elseif savemile and lastve ~= nil and saveplate ~= nil then
savemile = false
TriggerServerEvent('renzu_hud:savedata', saveplate, self.veh_stats[tostring(saveplate)])
LocalPlayer.state:set( --[[keyName]] 'adv_stat', --[[value]] self.veh_stats, --[[replicate to server]] true)
Wait(1000)
lastve = nil
saveplate = nil
Expand Down Expand Up @@ -3172,7 +3178,7 @@ function Hud:Carlock()
for k,v in pairs(GetGamePool('CVehicle')) do
if #(mycoords - GetEntityCoords(v, false)) < config.carlock_distance then
self.plate = GetVehicleNumberPlateText(v)
if self.veh_stats[self.plate] ~= nil and self.veh_stats[self.plate].owner ~= nil and self.identifier ~= nil then
if self.veh_stats[self.plate] ~= nil and self.veh_stats[self.plate].owner ~= nil and LocalPlayer.state.identifier ~= nil then
checkindentifier = string.gsub(self.veh_stats[self.plate].owner, 'Char5', '')
checkindentifier = string.gsub(checkindentifier, 'Char4', '')
checkindentifier = string.gsub(checkindentifier, 'Char3', '')
Expand All @@ -3186,8 +3192,8 @@ function Hud:Carlock()
checkindentifier = string.gsub(checkindentifier, 'steam', '')
checkindentifier = string.gsub(checkindentifier,":","")
checkindentifier = string.gsub(checkindentifier, string.gsub(config.identifier,":",""), '')
myidentifier = string.gsub(self.identifier, 'steam', '')
myidentifier = string.gsub(self.identifier, string.gsub(config.identifier,":",""), '')
myidentifier = string.gsub(LocalPlayer.state.identifier, 'steam', '')
myidentifier = string.gsub(LocalPlayer.state.identifier, string.gsub(config.identifier,":",""), '')
myidentifier = string.gsub(myidentifier,":","")
myidentifier = string.gsub(myidentifier, 'Char5', '')
myidentifier = string.gsub(myidentifier, 'Char4', '')
Expand Down Expand Up @@ -3839,7 +3845,8 @@ end

function Hud:SyncWheelAndSound(sounds,wheels)
local coords = GetEntityCoords(PlayerPedId())
for k,v in pairs(self.onlinevehicles) do
while LocalPlayer.state.onlinevehicles == nil do Wait(1) print(LocalPlayer.state.onlinevehicles) end
for k,v in pairs(LocalPlayer.state.onlinevehicles) do
if v.entity ~= nil and NetworkDoesEntityExistWithNetworkId(v.entity) and v.plate == self:tostringplate(GetVehicleNumberPlateText(NetToVeh(v.entity))) then
local vv = NetToVeh(v.entity)
local vehcoords = GetEntityCoords(vv)
Expand Down
3 changes: 2 additions & 1 deletion renzu_hud/conf/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
config = {}
config.gamebuild = 'auto' -- if 2189 pedshot transparent and 1604 or < 2000 = Enter vehicle game event will not work, we will use normal pedshot ( gamebuild is what you set on your server start example: +set sv_enforceGameBuild 2189, available build 1604, 2060, 2189 and more.) this is important if you are using UI Normal with Ped Face.
config.framework = 'ESX' -- ESX | QBCORE | VRP | STANDALONE (VRP not supported yet, but you can use standalone, it will work!)
config.Mysql = 'mysql-async' -- 'ghmattisql' | 'mysql-async'
config.Mysql = 'mysql-async' -- 'ghmattisql' | 'mysql-async' | 'oxmysql'
config.weight_type = false -- ESX item weight or limit type
config.vehicle_table = 'owned_vehicles' -- change this if you use different sql table for player vehicles (note identifier steam is always being used here)
config.Owner_column = 'owner' -- owner column name for identifier eg.steam hex,licensed
Expand All @@ -37,6 +37,7 @@ config.commanditem_permission = { -- item command permission -- standalone purpo
'steam:11000013ec77a2e', -- steam hex id, change this to yours, you can add as many as you want
'steam:11000013ec77a2e',
'df845523fc29c5159ece179359f22a741ca2ca9a',
'license:df845523fc29c5159ece179359f22a741ca2ca9a',
}
config.identifier = 'license:' -- standalone purpose, ignore if using framework
--MULTI CHARACTER START -- set config.multichar = false if you are not using any multi character ( configuring this is needed to avoid problems saving to database )
Expand Down
62 changes: 49 additions & 13 deletions renzu_hud/server/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ Citizen.CreateThread(function()
MySQL.Sync.execute([[
CREATE TABLE IF NOT EXISTS `vehicle_status` (
`stats` LONGTEXT NULL COLLATE 'utf8mb4_general_ci',
`plate` VARCHAR(64) NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`owner` VARCHAR(64) NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`plate` VARCHAR(64) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`owner` VARCHAR(64) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`plate`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
CREATE TABLE IF NOT EXISTS `body_status` (
`status` LONGTEXT NULL COLLATE 'utf8mb4_general_ci',
`identifier` VARCHAR(64) NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
`identifier` VARCHAR(64) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`identifier`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
Expand Down Expand Up @@ -136,12 +136,10 @@ Citizen.CreateThread(function()
if config.use_esx_accesories then
-- COPYRIGHT TO ESX ACCESOSRIES LINK https://github.com/esx-framework/esx_accessories/blob/e812dde63bcb746e9b49bad704a9c9174d6329fa/server/main.lua#L31
ESX.RegisterServerCallback('esx_accessories:get2', function(source, cb, accessory)
print("SKINS")
local xPlayer = GetPlayerFromId(source)
TriggerEvent('esx_datastore:getDataStore', 'user_' .. string.lower(accessory), xPlayer.identifier, function(store)
local hasAccessory = (store.get('has' .. accessory) and store.get('has' .. accessory) or false)
local skin = (store.get('skin') and store.get('skin') or {})

cb(hasAccessory, skin)
end)
end)
Expand All @@ -163,8 +161,33 @@ function isVehicleOwned(plate)
return owner
end

SyncStat = function(stat)
for i=0, GetNumPlayerIndices()-1 do
--if IsPlayerAceAllowed(GetPlayerFromIndex(i), "ace.test") then
local ply = Player(GetPlayerFromIndex(i))
ply.state.adv_stat = stat
local temp = {}
for k,v in pairs(stat) do
if v.entity ~= nil then
if temp[k] == nil then
temp[k] = {}
end
temp[k].entity = v.entity
temp[k].plate = k
if v.height ~= nil then
temp[k].height = v.height
end
if v.engine ~= nil then
temp[k].engine = v.engine
end
end
end
ply.state.onlinevehicles = temp
end
end

local kids = {}
RegisterServerEvent("renzu_hud:savedata")
RegisterNetEvent("renzu_hud:savedata")
AddEventHandler("renzu_hud:savedata", function(plate,table,updatevehicles)
local source = source
if plate ~= nil and kids[source] ~= nil and kids[source] < GetGameTimer() or plate ~= nil and kids[source] == nil then -- block any request until the timer is lessthan the gametimer or if its nil
Expand All @@ -173,12 +196,12 @@ AddEventHandler("renzu_hud:savedata", function(plate,table,updatevehicles)
local foundplate = false
local newcreated = false
if plate ~= nil then
print("SAVING")
--print("SAVING")
adv_table[tostring(plate)] = table
local results = SQLQuery(config.Mysql,'fetchAll',"SELECT stats,plate,owner FROM vehicle_status WHERE UPPER(plate)=@plate", {['@plate'] = plate:upper()})
if #results <= 0 then
local owner = isVehicleOwned(plate)
print(owner,#owner)
--print(owner,#owner)
if #owner > 0 then
SQLQuery(config.Mysql,'execute',"INSERT INTO vehicle_status (stats, plate, owner) VALUES (@stats, @plate, @owner)", {
['@stats'] = json.encode(adv_table[tostring(plate)]),
Expand All @@ -202,7 +225,10 @@ AddEventHandler("renzu_hud:savedata", function(plate,table,updatevehicles)
if not foundplate then
adv_table[tostring(plate)].owner = nil
end
TriggerClientEvent('renzu_hud:receivedata', -1, adv_table)
SyncStat(adv_table)
--ply.state:set( --[[keyName]] 'adv_stat', --[[value]] adv_table, --[[replicate to server]] true)
--print(ply.state.adv_stat) -- returns true
--TriggerClientEvent('renzu_hud:receivedata', -1, adv_table)
end
else
print('plate is nil')
Expand All @@ -223,6 +249,7 @@ AddEventHandler("renzu_hud:getdata", function(slot, fetchslot)
-- print(slot)
-- print("SLOT")
-- print(fetchslot)
local xPlayer = GetPlayerFromId(source)
local source = source
if slot ~= nil and charslot[source] == nil then
charslot[source] = slot
Expand All @@ -234,7 +261,12 @@ AddEventHandler("renzu_hud:getdata", function(slot, fetchslot)
if Renzu[tonumber(source)] == nil then
CreatePlayer(source)
end
TriggerClientEvent('renzu_hud:receivedata', source, adv_table, PlayerIdentifier(source))
local ply = Player(source)
-- ply.state.adv_stat = adv_table
SyncStat(adv_table)
ply.state.loaded = true
ply.state.identifier = xPlayer.identifier
--TriggerClientEvent('renzu_hud:receivedata', source, adv_table, PlayerIdentifier(source))
end
end)

Expand All @@ -260,7 +292,7 @@ AddEventHandler('renzu_hud:checkbody', function(target)
end
local xPlayer = GetPlayerFromId(source)
local done = false
print(xPlayer,"PLAYERLOADED",xPlayer)
--print(xPlayer,"PLAYERLOADED",xPlayer)
while xPlayer == nil do
print("Creating Player")
CreatePlayer(source)
Expand All @@ -284,7 +316,7 @@ AddEventHandler('renzu_hud:checkbody', function(target)
else
target = true
end
print(target,source)
--print(target,source)
TriggerClientEvent('renzu_hud:bodystatus', originalsource, done, target)
end)

Expand All @@ -304,7 +336,7 @@ end)

RegisterServerEvent('renzu_hud:healbody')
AddEventHandler('renzu_hud:healbody', function(target,part)
print("1",part)
--("1",part)
local xPlayer = GetPlayerFromId(source)
local identifier = xPlayer.identifier
if config.framework == 'Standalone' or config.framework == 'ESX' and xPlayer.job.name == config.checkbodycommandjob or config.framework == 'QBCORE' and xPlayer.PlayerData.job.name == config.checkbodycommandjob then
Expand Down Expand Up @@ -366,6 +398,8 @@ end)
local wheelsetting = {}
RegisterServerEvent("renzu_hud:wheelsetting")
AddEventHandler("renzu_hud:wheelsetting", function(entity,val,coords)
SyncStat(adv_table)
Wait(100)
TriggerClientEvent("renzu_hud:wheelsetting", -1, entity,wheelsetting)
end)

Expand Down Expand Up @@ -565,6 +599,8 @@ AddEventHandler('renzu_hud:change_engine', function(plate, stats)
['@plate'] = plate:upper()
})
end
SyncStat(adv_table)
Wait(100)
TriggerClientEvent("renzu_hud:syncengine", -1, plate, stats)
print("syncing to all")
end)
Expand Down

0 comments on commit adcc91c

Please sign in to comment.