Add websocket support and related functionality

This commit is contained in:
Linventif 2024-01-15 05:39:44 +01:00
parent fc5117b63e
commit e6f7d5bead
2 changed files with 87 additions and 21 deletions

View File

@ -103,6 +103,7 @@ local possibleConfig = {
["syncChat"] = { ["syncChat"] = {
["label"] = "Sync Chat", ["label"] = "Sync Chat",
["description"] = "Sync chat between the server and the discord server.", ["description"] = "Sync chat between the server and the discord server.",
["websocket"] = true,
["type"] = "checkbox", ["type"] = "checkbox",
["value"] = function(setting, value) ["value"] = function(setting, value)
return value return value
@ -187,6 +188,22 @@ local possibleConfig = {
}, },
} }
/*
// if data.websocket is false add button to download websocket
if !data.websocket then
local button = vgui.Create("DButton")
button:SetText("Download Websocket")
button.DoClick = function()
gui.OpenURL("https://github.com/FredyH/GWSockets/releases")
end
button:SetSize(buttonGrid:GetColWide(), buttonGrid:GetRowHeight())
buttonGrid:AddItem(button)
end
*/
local buttonsInfo = { local buttonsInfo = {
{ {
["label"] = "Open Webpanel", ["label"] = "Open Webpanel",
@ -200,6 +217,21 @@ local buttonsInfo = {
gmInte.SendNet("1") gmInte.SendNet("1")
end, end,
}, },
{
["label"] = "Buy Premium",
["func"] = function()
gui.OpenURL("https://gmod-integration.com/premium")
end,
},
{
["label"] = "Install Websocket",
["condition"] = function(data)
return !data.websocket
end,
["func"] = function()
gui.OpenURL("https://github.com/FredyH/GWSockets/releases")
end,
}
} }
function gmInte.openConfigMenu(data) function gmInte.openConfigMenu(data)
@ -223,7 +255,7 @@ function gmInte.openConfigMenu(data)
local messageLabel = vgui.Create("DLabel", messagePanel) local messageLabel = vgui.Create("DLabel", messagePanel)
messageLabel:Dock(FILL) messageLabel:Dock(FILL)
messageLabel:SetText("This config is superior to the webpanel config. If you change something here you can override the webpanel config. For example if you disable the logs here, the logs will not be available on the webpanel.") messageLabel:SetText("This config is superior to the webpanel config.\nIf you change something here you can override the webpanel config.\nSome features require a websocket connection to work properly.")
messageLabel:SetWrap(true) messageLabel:SetWrap(true)
@ -273,6 +305,10 @@ function gmInte.openConfigMenu(data)
end end
elseif v.type == "checkbox" then elseif v.type == "checkbox" then
input = vgui.Create("DComboBox", panel) input = vgui.Create("DComboBox", panel)
// if websocket is required and websocket is not enabled (!GWSockets) then disable the checkbox
if v.websocket && !data.websocket then
input:SetEnabled(false)
end
input:AddChoice("Enabled") input:AddChoice("Enabled")
input:AddChoice("Disabled") input:AddChoice("Disabled")
input:SetText(v.value(k, data[k]) && "Enabled" || "Disabled") input:SetText(v.value(k, data[k]) && "Enabled" || "Disabled")
@ -285,6 +321,9 @@ function gmInte.openConfigMenu(data)
input:SetSize(150, 25) input:SetSize(150, 25)
if v.description then if v.description then
if (v.websocket && !data.websocket) then
v.description = v.description .. "\n\nThis feature require a websocket connection to work properly."
end
input:SetTooltip(v.description) input:SetTooltip(v.description)
end end
@ -293,22 +332,30 @@ function gmInte.openConfigMenu(data)
end end
end end
// grid of buttons (2 buttons per row take 50% of the width) // grid of buttons (2 buttons per row take 50% of the width)
local buttonGrid = vgui.Create("DGrid", frame) local buttonGrid = vgui.Create("DGrid", frame)
buttonGrid:Dock(BOTTOM) buttonGrid:Dock(BOTTOM)
buttonGrid:DockMargin(5, 10, 5, 5) buttonGrid:DockMargin(5, 10, 5, 5)
buttonGrid:SetCols(2) buttonGrid:SetCols(2)
buttonGrid:SetColWide(frame:GetWide() / 2 - 10) buttonGrid:SetColWide(frame:GetWide() / 2 - 10)
buttonGrid:SetRowHeight(35) buttonGrid:SetRowHeight(35)
for k, v in pairs(buttonsInfo) do local buttonsCount = 0
local button = vgui.Create("DButton") for k, v in pairs(buttonsInfo) do
button:SetText(v.label) if v.condition && !v.condition(data) then continue end
button.DoClick = function() local button = vgui.Create("DButton")
v.func() button:SetText(v.label)
button.DoClick = function()
v.func()
end
button:SetSize(buttonGrid:GetColWide(), buttonGrid:GetRowHeight())
buttonGrid:AddItem(button)
buttonsCount = buttonsCount + 1
end end
button:SetSize(buttonGrid:GetColWide(), buttonGrid:GetRowHeight())
buttonGrid:AddItem(button) if buttonsCount % 2 == 1 then
local lastButton = buttonGrid:GetItems()[buttonsCount]
lastButton:SetWide(frame:GetWide() - 20)
end end
end end

View File

@ -2,15 +2,34 @@
// WebSocket // WebSocket
// //
// TODO made a proper system to detect if the server need to be connected to the websocket local useWebsocket = false
if (!gmInte.config.syncChat && !gmInte.config.websocket) then local websocketFeature = {
gmInte.log("WebSocket is disabled", true) 'syncChat',
'websocket',
}
for k, v in pairs(websocketFeature) do
if (gmInte.config[v]) then
useWebsocket = true
end
end
if (!useWebsocket) then
return gmInte.log("WebSocket is not used")
end
require("gwsockets")
if (!GWSockets) then
timer.Simple(1, function()
if (!GWSockets) then
gmInte.logError("GWSockets is not installed! Please install it from https://github.com/FredyH/GWSockets/releases")
end
end)
return return
end end
if (!GWSockets) then return gmInte.logError("GWSockets is not installed! Please install it from https://github.com/FredyH/GWSockets") end gmInte.config.websocket = true
require("gwsockets")
local socket = GWSockets.createWebSocket("wss://ws.gmod-integration.com") local socket = GWSockets.createWebSocket("wss://ws.gmod-integration.com")