Major improvement to networking & caching

master
gnomerd 5 years ago
parent 0647df8d64
commit 7c8d1522fb
  1. 1
      gamemode/engine/core/server/sv_player_init.lua
  2. 2
      gamemode/engine/derma/cl_menu.lua
  3. 2
      gamemode/engine/derma/menus/menu_intro.lua
  4. 77
      gamemode/engine/lib/server/sv_networking.lua

@ -11,6 +11,7 @@ local ply = FindMetaTable( "Player" )
function GM:PlayerInitialSpawn( ply ) function GM:PlayerInitialSpawn( ply )
ply.isloaded = false -- REMOVE THIS WHEN MYSQL DB IS ADDED ply.isloaded = false -- REMOVE THIS WHEN MYSQL DB IS ADDED
ply.cache = {}
-- load in all of the players characters and stuff from the MySQL DB -- load in all of the players characters and stuff from the MySQL DB
end end

@ -15,7 +15,7 @@ local libs = {
Quantum.Client.Menu.GetAPI = function( lib ) return include( libs[lib] ) end Quantum.Client.Menu.GetAPI = function( lib ) return include( libs[lib] ) end
net.Receive( "quantum_menu_net", function( len, pl ) net.Receive( "quantum_menu_net", function( len, pl )
local dt = net.ReadTable() -- TO DO: merge datatable with cached ( if same id/type ) local dt = util.JSONToTable(net.ReadString()) -- TO DO: merge datatable with cached ( if same id/type )
if( Quantum.Client.Cache[dt.id] ~= nil && #Quantum.Client.Cache[dt.id] >= 1 ) then if( Quantum.Client.Cache[dt.id] ~= nil && #Quantum.Client.Cache[dt.id] >= 1 ) then
table.Merge( Quantum.Client.Cache[dt.id], dt ) table.Merge( Quantum.Client.Cache[dt.id], dt )
else else

@ -10,8 +10,6 @@ local intro = {}
local log = Quantum.Client.Menu.GetAPI( "dialogue" ) local log = Quantum.Client.Menu.GetAPI( "dialogue" )
local theme = Quantum.Client.Menu.GetAPI( "theme" ) local theme = Quantum.Client.Menu.GetAPI( "theme" )
print("Git remote push test")
local scenes = { -- 5031.821777 3866.334961 120.090790;setang 0.898059 56.421352 0.000000 local scenes = { -- 5031.821777 3866.334961 120.090790;setang 0.898059 56.421352 0.000000
["rp_truenorth_v1a_livin"] = { ["rp_truenorth_v1a_livin"] = {
[1] = { [1] = {

@ -9,26 +9,45 @@ Quantum.Net = {}
util.AddNetworkString( "quantum_menu_net" ) util.AddNetworkString( "quantum_menu_net" )
util.AddNetworkString( "quantum_menu_button_net" ) util.AddNetworkString( "quantum_menu_button_net" )
local function CacheDatatableMethod( id, datatable, ply ) local function checkCacheTable( ply, cache_id, dt )
if( ply.cache == nil ) then ply.cache = {} end Quantum.Debug( "Checking cache tables (" .. tostring(ply) .. " | " .. tostring(cache_id) .. " | " .. tostring(dt) .. ")" )
local datatable = dt || {}
if( ply.cache[id] == nil ) then -- if this is the first time then create a cache record if( ply.cache == nil ) then
ply.cache[id] = { Quantum.Error( tostring(ply) .. " does not have a cache table, creating..." )
cache = datatable ply.cache = {}
-- won't be defining the count here if( ply.cache ~= nil ) then Quantum.Debug( "Success! Created cache table for " .. tostring(ply) ) end
} else
end if( ply.cache[cache_id] == nil ) then
Quantum.Debug( tostring(ply) .. " does not have a cache for '" .. tostring(cache_id) .. "'. Creating..." )
if( ply.cache[id].count == nil ) then ply.cache[id].count = 1 else ply.cache[id].count = ply.cache[id].count + 1 end -- keep count of how many times we have cached this datatable ply.cache[cache_id] = {
id = cache_id,
cache = datatable
}
if( ply.cache[cache_id] ~= nil && table.Count( ply.cache[cache_id] ) == 1 ) then
Quantum.Debug( "Success! Created cache '" .. tostring(cache_id) .. "' for " .. tostring(ply) .. "." )
if( ply.cache[id].count == nil ) then ply.cache[id].count = 1 else ply.cache[id].count = ply.cache[id].count + 1 end -- keep count
else
Quantum.Error( "Failed. Creation of cache '" .. tostring(cache_id) .. "' for " .. tostring(ply) .. " failed to validate or did not get created." )
ply.cache[cache_id] = nil -- remove the cache since it is "broken"
end
end
end
return ply.cache[cache_id]
end
if( ply.cache[id].count > 1 ) then -- dont want to filter out data if this is the first time. local function CacheDatatableMethod( id, datatable, ply )
for k, v in pairs( datatable ) do -- loop through the datatable ply.cache[id] = checkCacheTable( ply, id, datatable ) -- check caching tables etc
for k2, v2 in pairs( table.GetKeys( ply.cache[id].cache ) ) do -- check each key with each key from the record cache Quantum.Debug( "(" .. tostring(ply) .. " | " .. tostring(id) .. ") Removing known data in cache from datatable..." )
if( tostring(k) == tostring(v2) ) then -- check if the keys are the same if( ply.cache[id] ~= nil ) then
if( v == ply.cache[id].cache[tostring(v2)] ) then -- check if the value/contents are the same if( ply.cache[id].count > 1 ) then -- dont want to filter out data if this is the first time.
datatable[k] = nil -- if so then remove the key from the datatable for k, v in pairs( datatable ) do -- loop through the datatable
else -- if the key's value has changed we dont remove it since the client needs to know about it for k2, v2 in pairs( table.GetKeys( ply.cache[id].cache ) ) do -- check each key with each key from the record cache
ply.cache[id].cache[tostring(v2)] = v -- and then update the cache so we know about it next time if( tostring(k) == tostring(v2) ) then -- check if the keys are the same
if( v == ply.cache[id].cache[tostring(v2)] ) then -- check if the value/contents are the same
datatable[k] = nil -- if so then remove the key from the datatable
else -- if the key's value has changed we dont remove it since the client needs to know about it
ply.cache[id].cache[tostring(v2)] = v -- and then update the cache so we know about it next time
end
end end
end end
end end
@ -40,13 +59,25 @@ local function CacheDatatableMethod( id, datatable, ply )
return { id = id, cont = datatable } return { id = id, cont = datatable }
end end
local function shortenDataTableMethod( datatable )
Quantum.Debug( "(" .. datatable.id .. ") Converting datatable '" .. tostring( datatable ) .. "' to json..." )
return util.TableToJSON( datatable, false )
end
local function initializeDatatable( id, datatable, ply )
Quantum.Debug( "(" .. tostring(ply) .. ") Initializing datatable for client net message.." )
return shortenDataTableMethod( CacheDatatableMethod( id, datatable, ply ) )
end
local function SendDatatableToClient( client, dt, type ) local function SendDatatableToClient( client, dt, type )
local datatable = CacheDatatableMethod( type, dt, client ) -- before we actually send the stuff, cache it and remove unneeded stuff local datatable = initializeDatatable( type, dt, client ) -- before we actually send the stuff, cache it and remove unneeded stuff
net.Start( "quantum_menu_net" ) local net_start = net.Start( "quantum_menu_net" )
if( table.Count( datatable ) > 0 ) then -- if it's empty just dont send it because we will save 8 bits if( net_start ) then Quantum.Debug( "Sending net message to " tostring(ply) .. "..." )
net.WriteTable( datatable ) -- send the data to the player if( #datatable > 0 ) then -- if it's empty just dont send it because we will save 8 bits
net.WriteString( datatable ) -- send the data to the player
end end
net.Send( client ) net.Send( client )
Quantum.Debug("Net message sent.")
end end
function Quantum.Net.OpenMenu( pl, type, dt ) function Quantum.Net.OpenMenu( pl, type, dt )

Loading…
Cancel
Save