remake networking

This commit is contained in:
Linventif 2023-09-25 11:11:51 +02:00
parent 91e4029aed
commit f5a707eea4
2 changed files with 50 additions and 5 deletions

View File

@ -0,0 +1,32 @@
//
// Network
//
/*
Upload
0 - Say I'm ready
Receive
1 - Sync Chat
*/
// Send
function gmInte.SendNet(id, args, func)
net.Start("gmIntegration")
net.WriteUInt(id, 8)
net.WriteString(util.TableToJSON(args || {}))
func && func()
net.SendToServer()
end
// Receive
local netFunc = {
[1] = function(args)
chat.AddText(unpack(args))
end
}
net.Receive("gmIntegration", function()
local id = net.ReadUInt(8)
local args = util.JSONToTable(net.ReadString())
netFunc[id] && netFunc[id](args)
end)

View File

@ -2,8 +2,25 @@
// Network
//
/*
Upload
1 - Add Chat Message
Receive
0 - Player is Ready
*/
util.AddNetworkString("gmIntegration")
// Send
function gmInte.SendNet(id, data, ply, func)
net.Start("gmIntegration")
net.WriteUInt(id, 8)
net.WriteString(util.TableToJSON(data))
func && func()
net.Send(ply)
end
// Receive
local netFuncs = {
[0] = function(ply)
gmInte.userFinishConnect(ply)
@ -14,9 +31,5 @@ net.Receive("gmIntegration", function(len, ply)
if !ply:IsPlayer() then return end
local id = net.ReadUInt(8)
local data = util.JSONToTable(net.ReadString() || "{}")
// check if argument is valid
if netFuncs[id] then
netFuncs[id](ply, data)
end
netFuncs[id] && netFuncs[id](ply, data)
end)