Auto-Reconnect

Auto-Reconnect

http://vgy.me/7RzDR2.png

Small auto-reconnection script I made, because I feel like this should be free as it’s so easy.

Features:
Crash “menu” (UI)
Auto-Reconnection
Configurable reconnection time
Very easy installation
Doesn’t conflict with any addons besides other auto-reconnection scripts

Installation:
Just put it in lua/autorun

Configuration:
Reconnection time, edit reconnect_time variable in source (seconds).
Message when the server has crashed, kind’ve obvious format. In the 3rd line there is the {time} variable which is the time until it reconnects in seconds.

Source:
[lua]local line1 = “Uh oh!”
local line2 = “It looks like the server is experiencing some problems.”
local line3 = “We will try to reconnect you in {time} seconds.”
local reconnect_time = 30

if SERVER then
util.AddNetworkString(“Auto-Reconnect”)

local function Broadcast()
	net.Start("Auto-Reconnect")
	net.Broadcast()
end

timer.Create("Broadcast-Auto-Reconnect", 1, 0, Broadcast)

else
RunConsoleCommand(“cl_timeout”, tostring(reconnect_time + 5))
local time = 0
local open = false
local ui

local function OpenUI()
	ui = vgui.Create("DFrame")
	ui:SetSize(557, 493)
	ui:SetTitle("")
	ui:Center()
	ui:MakePopup()
	ui:ShowCloseButton(false)

	function ui.Paint(self)
		draw.RoundedBox(0, 0, 0, self:GetWide(), self:GetTall(), Color(90, 90, 90))
		local col = Color(255, 0, 0)

		for i = 0, 30 do
			surface.SetDrawColor(col)
			col.r = col.r - 2
			surface.DrawLine(0, i, 587, i)
		end

		draw.SimpleText("Auto-Reconnect", "Trebuchet24", self:GetWide() / 2, 14, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		draw.SimpleText(line1, "Trebuchet24", self:GetWide() / 2, 215, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		draw.SimpleText(line2, "Trebuchet24", self:GetWide() / 2, 235, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		draw.SimpleText(string.gsub(line3, "{time}", tostring((reconnect_time + 4) - time)), "Trebuchet24", self:GetWide() / 2, 255, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	end
end

local function AddTime()
	if time >= (reconnect_time + 4) then
		RunConsoleCommand('retry')
	end

	if time > 3 and !open then
		open = true
		OpenUI()
	end

	time = time + 1
end

local function RemoveTime()
	time = time - 1

	if open then
		time = 0
		open = false
		ui:Close()
		ui = nil
	end
end

timer.Create("Auto-Reconnect", 1, 0, AddTime)
net.Receive("Auto-Reconnect", RemoveTime)

end[/lua]

Changelog:
v1 - Created
v1.1 - Made the server broadcast the message every second instead of client to server ping pong every second, ping pong method lags like crazy in 40+ player servers
v1.1a - Localized variable reconnect_time
v1.2 - Customizable message! Use {time} variable on the 3rd line to get the time until reconnect!
v1.2a - Added something to force the client to update their timeout to something higher than the reconnect time.

If you find any bugs, or anything to make the code better, let me know!

Nice of you to think about the community. It’s good to see people who understand the value of scripts and what not

Thanks!

UPDATE:
v1.1 - Made the server broadcast the message every second instead of client to server ping pong every second, ping pong method lags like crazy in 40+ player servers

Nice job; I wouldn’t make reconnect_time global, though.

This code is useless, because Source Engine already check if the connection is lost and if this is the case, it will show a message for auto-disconnect in 30 seconds IIRC and you can’t prevent it.

The only things you are able to do is to change the “cl_timeout” to more than 30 seconds for prevent a auto-disconnect in 30 seconds. (And the Source Engine will automatically reconnect the client when the connection is done with server again during this time.)

I just felt like releasing this because there was one on scriptfodder for $3.99 and it takes about a minute to make it :v

But if the server restarts in that time, the game won’t reconnect. That timeout is for keeping the player in during high server lag points.

Update!
v1.2 - Customizable message! Use {time} variable on the 3rd line to get the time until reconnect!

If it’s true then it would be better to make the code like this:



if SERVER then
	util.AddNetworkString("Auto-Reconnect")
	
	local function Broadcast()
		net.Start("Auto-Reconnect")
		net.Broadcast()
	end
	
	timer.Create("Broadcast-Auto-Reconnect", 1, 0, Broadcast)
else
reconnect_time = 60 -- Reconnect in 1 minute. Just to be sure the server has crashed.
RunConsoleCommand("cl_timeout","70") -- Prevent the disconnect in 30 seconds. Wait 1 minute. Added 10 for safety.
	local time = 0
	local open = false
	local ui

	local function OpenUI()
		ui = vgui.Create("DFrame")
		ui:SetSize(557, 493)
		ui:SetTitle("")
		ui:Center()
		ui:MakePopup()
		ui:ShowCloseButton(false)

		function ui.Paint(self)
			draw.RoundedBox(0, 0, 0, self:GetWide(), self:GetTall(), Color(90, 90, 90))
			local col = Color(255, 0, 0)

			for i = 0, 30 do
				surface.SetDrawColor(col)
				col.r = col.r - 2
				surface.DrawLine(0, i, 587, i)
			end
    
			draw.SimpleText("Auto-Reconnect", "Trebuchet24", self:GetWide() / 2, 14, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			draw.SimpleText("Uh oh!", "Trebuchet24", self:GetWide() / 2, 215, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			draw.SimpleText("It looks like the server is experiencing some problems.", "Trebuchet24", self:GetWide() / 2, 235, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			draw.SimpleText("We will try to reconnect you in " .. tostring((reconnect_time + 4) - time) .. " seconds.", "Trebuchet24", self:GetWide() / 2, 255, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		end
	end

	local function AddTime()
		if time > reconnect_time then -- It's more than 1 minute. The server has probably crashed. Reconnect.
			RunConsoleCommand('retry')
		end
	
		if time > 3 and !open then
			open = true
			OpenUI()
		end
	
		time = time + 1
	end

	local function RemoveTime()
		time = time - 1
	
		if open then
			time = 0
			open = false
			ui:Close()
			ui = nil
		end
	end

	timer.Create("Auto-Reconnect", 1, 0, AddTime)
	net.Receive("Auto-Reconnect", RemoveTime)
end


Update!
v1.2a - Added something to force the client to update their timeout to something higher than the reconnect time!

I’ve seen this type of script on sf and can someone explain how it works? I don’t seem to understand very well cuz to me it just looks like it’s counting a number

Re-connects the client if possible as opposed to booting them after the 30(?) secs is up

the server pings each client to let them know that the server is still alive. the client counts the time since the last ping, and if it’s longer than a certain amount, it assumes that the server crashed

Sorry for the late reply, after the specified time in seconds (reconnect_time variable) after you lose connection to the server, it executes the command ‘retry’ and reconnects you to the server, aka when the server crashes after seconds it reconnects you. (You should set this to the time it takes your server to restart after crashes)

Is there a way to cancel it without having to disconnect from the server

No, should I add this?

(rate agree to vote to add, rate disagree to vote to not add)

Can a moderator please unban him for like 30 seconds so he can tell me if he will add it.

Here you go my friend,

[lua]local line1 = “Uh oh!”
local line2 = “It looks like the server is experiencing some problems.”
local line3 = “We will try to reconnect you in {time} seconds.”

local reconnect_time = 30
local reconnect_prevent = false

if SERVER then
util.AddNetworkString(“Auto-Reconnect”)

local function Broadcast()
	net.Start("Auto-Reconnect")
	net.Broadcast()
end

timer.Create("Broadcast-Auto-Reconnect", 1, 0, Broadcast)

else
RunConsoleCommand(“cl_timeout”, tostring(reconnect_time + 5))
local time = 0
local open = false
local ui
local reconnect_prevent

local function OpenUI()
	ui = vgui.Create("DFrame")
	ui:SetSize(557, 493)
	ui:SetTitle("")
	ui:Center()
	ui:MakePopup()
	ui:ShowCloseButton(true)

	function ui.OnClose(self)
		reconnect_prevent = true
	end

	function ui.Paint(self)
		draw.RoundedBox(0, 0, 0, self:GetWide(), self:GetTall(), Color(90, 90, 90))
		local col = Color(255, 0, 0)

		for i = 0, 30 do
			surface.SetDrawColor(col)
			col.r = col.r - 2
			surface.DrawLine(0, i, 587, i)
		end

		draw.SimpleText("Auto-Reconnect", "Trebuchet24", self:GetWide() / 2, 14, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		draw.SimpleText(line1, "Trebuchet24", self:GetWide() / 2, 215, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		draw.SimpleText(line2, "Trebuchet24", self:GetWide() / 2, 235, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		draw.SimpleText(string.gsub(line3, "{time}", tostring((reconnect_time + 4) - time)), "Trebuchet24", self:GetWide() / 2, 255, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	end
end

local function AddTime()
	if time >= (reconnect_time + 4) and (not reconnect_prevent) then
		RunConsoleCommand('retry')
	end

	if time > 3 and !open then
		open = true
		OpenUI()
	end

	time = time + 1
end

local function RemoveTime()
	time = time - 1

	if open then
		time = 0
		open = false
		reconnect_prevent = false

		ui:Remove()
		ui = nil
	end
end

timer.Create("Auto-Reconnect", 1, 0, AddTime)
net.Receive("Auto-Reconnect", RemoveTime)

end[/lua]

Should work, but it’s untested. (Close the window to prevent being reconnected)

When i put the script in the client file, i started the server and joined and it then ran the script and started to reconnect me automatically. Please help

It’s supposed to be in lua/autorun, not lua/autorun/client. The server side of the script isn’t running to check to see if the server’s crashed so the script is automatically enabling the menu. You must put it into a shared realm