mirror of
https://github.com/gmod-integration/lua.git
synced 2025-04-29 22:15:26 +00:00
refact: simplify net table
This commit is contained in:
parent
18b904f64a
commit
09727be01b
|
@ -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)
|
||||
net.Start("gmIntegration")
|
||||
net.WriteUInt(netSend[id], 8)
|
||||
net.WriteString(id)
|
||||
net.WriteString(util.TableToJSON(args || {}))
|
||||
if func then func() end
|
||||
net.SendToServer()
|
||||
end
|
||||
|
||||
local netReceive = {
|
||||
[1] = function(data) gmInte.discordSyncChatPly(data) end,
|
||||
[2] = function(data) gmInte.openConfigMenu(data) end,
|
||||
[3] = function(data) gmInte.showTestConnection(data) end,
|
||||
[5] = function(data)
|
||||
["wsRelayDiscordChat"] = function(data) gmInte.discordSyncChatPly(data) end,
|
||||
["adminConfig"] = function(data) gmInte.openConfigMenu(data) end,
|
||||
["testApiConnection"] = function(data) gmInte.showTestConnection(data) end,
|
||||
["publicConfig"] = function(data)
|
||||
gmInte.config = table.Merge(gmInte.config, data.config)
|
||||
gmInte.version = data.other.version
|
||||
gmInte.loadTranslations()
|
||||
if gmInte.config.clientBranch != "any" && gmInte.config.clientBranch != BRANCH then gmInte.openWrongBranchPopup() end
|
||||
if !data.other.aprovedCredentials then RunConsoleCommand("gmod_integration_admin") end
|
||||
end,
|
||||
[6] = function(data) gmInte.chatAddTextFromTable(data) end,
|
||||
[7] = function() gmInte.openVerifPopup() end,
|
||||
[8] = function(data) gmInte.config.token = data.token end
|
||||
["chatColorMessage"] = function(data) gmInte.chatAddTextFromTable(data) end,
|
||||
["openVerifPopup"] = function() gmInte.openVerifPopup() end,
|
||||
["savePlayerToken"] = function(data) gmInte.config.token = data.token end
|
||||
}
|
||||
|
||||
net.Receive("gmIntegration", function()
|
||||
local id = net.ReadUInt(8)
|
||||
local id = 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)
|
|
@ -1,19 +1,7 @@
|
|||
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)
|
||||
if !netSend[id] then return end
|
||||
net.Start("gmIntegration")
|
||||
net.WriteUInt(netSend[id], 8)
|
||||
net.WriteString(id)
|
||||
net.WriteString(util.TableToJSON(data || {}))
|
||||
if func then func() end
|
||||
if ply == nil then
|
||||
|
@ -24,27 +12,31 @@ function gmInte.SendNet(id, data, ply, func)
|
|||
end
|
||||
|
||||
local netReceive = {
|
||||
[0] = function(ply, data)
|
||||
["ready"] = function(ply, data)
|
||||
if ply.gmIntIsReady then return end
|
||||
ply.branch = data.branch
|
||||
hook.Run("gmInte:PlayerReady", ply)
|
||||
end,
|
||||
[1] = function(ply, data) gmInte.testConnection(ply, data) end,
|
||||
[2] = function(ply) gmInte.superadminGetConfig(ply) end,
|
||||
[3] = function(ply, data) gmInte.superadminSetConfig(ply, data) end,
|
||||
[4] = function(ply) gmInte.takeScreenshot(ply) end,
|
||||
[5] = function(ply)
|
||||
["testConnection"] = function(ply, data) gmInte.testConnection(ply, data) end,
|
||||
["getConfig"] = function(ply) gmInte.superadminGetConfig(ply) end,
|
||||
["saveConfig"] = function(ply, data) gmInte.superadminSetConfig(ply, data) end,
|
||||
["takeScreenShot"] = function(ply) gmInte.takeScreenshot(ply) end,
|
||||
["restartMap"] = function(ply)
|
||||
if !ply:gmIntIsAdmin() then return end
|
||||
RunConsoleCommand("changelevel", game.GetMap())
|
||||
end,
|
||||
[6] = function(ply) gmInte.verifyPlayer(ply) end,
|
||||
[7] = function(ply, data) gmInte.sendPlayerToken(ply) end
|
||||
["verifyMe"] = function(ply) gmInte.verifyPlayer(ply) end,
|
||||
["sendFPS"] = function(ply, data) gmInte.sendPlayerToken(ply) end
|
||||
}
|
||||
|
||||
net.Receive("gmIntegration", function(len, ply)
|
||||
if !ply || ply && !ply:IsValid() then return end
|
||||
local id = net.ReadUInt(8)
|
||||
local id = net.ReadString()
|
||||
local data = util.JSONToTable(net.ReadString() || "{}")
|
||||
if !netReceive[id] then return end
|
||||
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)
|
Loading…
Reference in New Issue
Block a user