lua/lua/gmod_integration/server/sv_settings.lua
2024-03-31 15:51:07 +02:00

90 lines
2.6 KiB
Lua

function gmInte.saveSetting(setting, value)
if gmInte.config[setting] == nil then
gmInte.log("Unknown Setting")
return
end
// Boolean
if (value == "true") then value = true end
if (value == "false") then value = false end
// Number
if (tonumber(value) != nil) then value = tonumber(value) end
gmInte.config[setting] = value
file.Write("gm_integration/config.json", util.TableToJSON(gmInte.config, true))
gmInte.log("Setting Saved")
// send to all players if it's a public setting
for _, ply in pairs(player.GetAll()) do
if (ply:IsValid() && ply:IsPlayer(ply)) then
gmInte.log("Sending new Public Config to " .. ply:Nick())
gmInte.publicGetConfig(ply)
end
end
end
function gmInte.tryConfig()
gmInte.http.get("",
function(code, body)
print(" ")
gmInte.log("Congratulations your server is now connected to Gmod Integration")
gmInte.log("Server Name: " .. body.name)
gmInte.log("Server ID: " .. body.id)
print(" ")
end
)
end
function gmInte.testConnection(ply)
gmInte.http.get("",
function(code, body)
if (ply) then gmInte.SendNet("testApiConnection", body, ply) end
end,
function(code, body)
if (ply) then gmInte.SendNet("testApiConnection", body, ply) end
end
)
end
function gmInte.refreshSettings()
gmInte.config = util.JSONToTable(file.Read("gm_integration/config.json", "DATA"))
gmInte.log("Settings Refreshed")
gmInte.tryConfig()
end
function gmInte.superadminGetConfig(ply)
if (!ply:IsValid() || !ply:IsPlayer(ply) || !ply:IsSuperAdmin()) then return end
gmInte.config.websocket = GWSockets && true || false
gmInte.SendNet("adminConfig", gmInte.config, ply)
end
function gmInte.publicGetConfig(ply)
if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end
gmInte.SendNet("publicConfig", {
["config"] = {
["id"] = gmInte.config.id,
["debug"] = gmInte.config.debug,
["apiFQDN"] = gmInte.config.apiFQDN,
["websocketFQDN"] = gmInte.config.websocketFQDN,
["version"] = gmInte.config.version,
},
["other"] = {
["aprovedCredentials"] = gmInte.aprovedCredentials,
}
}, ply)
end
function gmInte.superadminSetConfig(ply, data)
if (!ply:IsValid() || !ply:IsPlayer(ply) || !ply:IsSuperAdmin()) then return end
for k, v in pairs(data) do
gmInte.saveSetting(k, v)
end
if data.token || data.id then
gmInte.testConnection(ply)
end
end