refact: simplify net table

This commit is contained in:
Linventif 2025-04-24 09:21:50 +00:00
parent 18b904f64a
commit 09727be01b
2 changed files with 29 additions and 43 deletions

View File

@ -1,40 +1,34 @@
local netSend = {
["ready"] = 0,
["testConnection"] = 1,
["getConfig"] = 2,
["saveConfig"] = 3,
["takeScreenShot"] = 4,
["restartMap"] = 5,
["verifyMe"] = 6,
["sendFPS"] = 7
}
function gmInte.SendNet(id, args, func) function gmInte.SendNet(id, args, func)
net.Start("gmIntegration") net.Start("gmIntegration")
net.WriteUInt(netSend[id], 8) net.WriteString(id)
net.WriteString(util.TableToJSON(args || {})) net.WriteString(util.TableToJSON(args || {}))
if func then func() end if func then func() end
net.SendToServer() net.SendToServer()
end end
local netReceive = { local netReceive = {
[1] = function(data) gmInte.discordSyncChatPly(data) end, ["wsRelayDiscordChat"] = function(data) gmInte.discordSyncChatPly(data) end,
[2] = function(data) gmInte.openConfigMenu(data) end, ["adminConfig"] = function(data) gmInte.openConfigMenu(data) end,
[3] = function(data) gmInte.showTestConnection(data) end, ["testApiConnection"] = function(data) gmInte.showTestConnection(data) end,
[5] = function(data) ["publicConfig"] = function(data)
gmInte.config = table.Merge(gmInte.config, data.config) gmInte.config = table.Merge(gmInte.config, data.config)
gmInte.version = data.other.version gmInte.version = data.other.version
gmInte.loadTranslations() gmInte.loadTranslations()
if gmInte.config.clientBranch != "any" && gmInte.config.clientBranch != BRANCH then gmInte.openWrongBranchPopup() end if gmInte.config.clientBranch != "any" && gmInte.config.clientBranch != BRANCH then gmInte.openWrongBranchPopup() end
if !data.other.aprovedCredentials then RunConsoleCommand("gmod_integration_admin") end if !data.other.aprovedCredentials then RunConsoleCommand("gmod_integration_admin") end
end, end,
[6] = function(data) gmInte.chatAddTextFromTable(data) end, ["chatColorMessage"] = function(data) gmInte.chatAddTextFromTable(data) end,
[7] = function() gmInte.openVerifPopup() end, ["openVerifPopup"] = function() gmInte.openVerifPopup() end,
[8] = function(data) gmInte.config.token = data.token end ["savePlayerToken"] = function(data) gmInte.config.token = data.token end
} }
net.Receive("gmIntegration", function() net.Receive("gmIntegration", function()
local id = net.ReadUInt(8) local id = net.ReadString()
local args = util.JSONToTable(net.ReadString()) local args = util.JSONToTable(net.ReadString())
if netReceive[id] then netReceive[id](args) end if !netReceive[id] then return end
netReceive[id](args)
if gmInte.config.debug then
gmInte.log("[net] Received net message: " .. id)
gmInte.log("[net] Data: " .. util.TableToJSON(args))
end
end) end)

View File

@ -1,19 +1,7 @@
util.AddNetworkString("gmIntegration") util.AddNetworkString("gmIntegration")
local netSend = {
["wsRelayDiscordChat"] = 1,
["adminConfig"] = 2,
["testApiConnection"] = 3,
["publicConfig"] = 5,
["chatColorMessage"] = 6,
["openVerifPopup"] = 7,
["savePlayerToken"] = 8
}
// Send
function gmInte.SendNet(id, data, ply, func) function gmInte.SendNet(id, data, ply, func)
if !netSend[id] then return end
net.Start("gmIntegration") net.Start("gmIntegration")
net.WriteUInt(netSend[id], 8) net.WriteString(id)
net.WriteString(util.TableToJSON(data || {})) net.WriteString(util.TableToJSON(data || {}))
if func then func() end if func then func() end
if ply == nil then if ply == nil then
@ -24,27 +12,31 @@ function gmInte.SendNet(id, data, ply, func)
end end
local netReceive = { local netReceive = {
[0] = function(ply, data) ["ready"] = function(ply, data)
if ply.gmIntIsReady then return end if ply.gmIntIsReady then return end
ply.branch = data.branch ply.branch = data.branch
hook.Run("gmInte:PlayerReady", ply) hook.Run("gmInte:PlayerReady", ply)
end, end,
[1] = function(ply, data) gmInte.testConnection(ply, data) end, ["testConnection"] = function(ply, data) gmInte.testConnection(ply, data) end,
[2] = function(ply) gmInte.superadminGetConfig(ply) end, ["getConfig"] = function(ply) gmInte.superadminGetConfig(ply) end,
[3] = function(ply, data) gmInte.superadminSetConfig(ply, data) end, ["saveConfig"] = function(ply, data) gmInte.superadminSetConfig(ply, data) end,
[4] = function(ply) gmInte.takeScreenshot(ply) end, ["takeScreenShot"] = function(ply) gmInte.takeScreenshot(ply) end,
[5] = function(ply) ["restartMap"] = function(ply)
if !ply:gmIntIsAdmin() then return end if !ply:gmIntIsAdmin() then return end
RunConsoleCommand("changelevel", game.GetMap()) RunConsoleCommand("changelevel", game.GetMap())
end, end,
[6] = function(ply) gmInte.verifyPlayer(ply) end, ["verifyMe"] = function(ply) gmInte.verifyPlayer(ply) end,
[7] = function(ply, data) gmInte.sendPlayerToken(ply) end ["sendFPS"] = function(ply, data) gmInte.sendPlayerToken(ply) end
} }
net.Receive("gmIntegration", function(len, ply) net.Receive("gmIntegration", function(len, ply)
if !ply || ply && !ply:IsValid() then return end if !ply || ply && !ply:IsValid() then return end
local id = net.ReadUInt(8) local id = net.ReadString()
local data = util.JSONToTable(net.ReadString() || "{}") local data = util.JSONToTable(net.ReadString() || "{}")
if !netReceive[id] then return end if !netReceive[id] then return end
netReceive[id](ply, data) netReceive[id](ply, data)
if gmInte.config.debug then
gmInte.log("[net] Received net message: " .. id .. " from " .. (ply && ply:Nick() || "Unknown"))
gmInte.log("[net] Data: " .. util.TableToJSON(data))
end
end) end)