From cc7c18f2f9ca1c62738868abe6ffbe3488532b44 Mon Sep 17 00:00:00 2001 From: mochi Date: Sun, 6 Sep 2020 13:47:28 +0200 Subject: [PATCH] Add auto-reconnect chat feature on server reboot --- code/game/world.dm | 2 + goon/browserassets/css/browserOutput-dark.css | 4 ++ goon/browserassets/css/browserOutput.css | 4 ++ goon/browserassets/js/browserOutput.js | 57 ++++++++++++++----- goon/code/datums/browserOutput.dm | 1 + 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/code/game/world.dm b/code/game/world.dm index 02c613e9ac8..c279fa24b54 100644 --- a/code/game/world.dm +++ b/code/game/world.dm @@ -340,6 +340,8 @@ GLOBAL_VAR_INIT(world_topic_spam_protect_time, world.timeofday) #endif for(var/client/C in GLOB.clients) + var/secs_before_auto_reconnect = 10 // TODO: make it higher if server is due for an update @AffectedArc07 + C << output(list2params(list(secs_before_auto_reconnect)), "browseroutput:reboot") if(config.server) //if you set a server location in config.txt, it sends you there instead of trying to reconnect to the same world address. -- NeoFite C << link("byond://[config.server]") diff --git a/goon/browserassets/css/browserOutput-dark.css b/goon/browserassets/css/browserOutput-dark.css index 8c177840a1b..529827ad369 100644 --- a/goon/browserassets/css/browserOutput-dark.css +++ b/goon/browserassets/css/browserOutput-dark.css @@ -401,6 +401,10 @@ h1.alert, h2.alert {color: #FFF;} .connectionClosed.restored {background: green;} .internal.boldnshit {color: #6685f5; font-weight: bold;} +.rebooting {background: #2979AF; color: white; padding: 5px;} +.rebooting a {color: white !important; text-decoration-color: white !important;} +#reconnectTimer {font-weight: bold;} + /* HELPER CLASSES */ .text-normal {font-weight: normal; font-style: normal;} .hidden {display: none; visibility: hidden;} diff --git a/goon/browserassets/css/browserOutput.css b/goon/browserassets/css/browserOutput.css index 0d7d987b0e7..31da7aa96f5 100644 --- a/goon/browserassets/css/browserOutput.css +++ b/goon/browserassets/css/browserOutput.css @@ -400,6 +400,10 @@ h1.alert, h2.alert {color: #000000;} .connectionClosed.restored {background: green;} .internal.boldnshit {color: blue; font-weight: bold;} +.rebooting {background: #2979AF; color: white; padding: 5px;} +.rebooting a {color: white !important; text-decoration-color: white !important;} +#reconnectTimer {font-weight: bold;} + /* HELPER CLASSES */ .text-normal {font-weight: normal; font-style: normal;} .hidden {display: none; visibility: hidden;} diff --git a/goon/browserassets/js/browserOutput.js b/goon/browserassets/js/browserOutput.js index 3cac81ca8e2..414a5d644b6 100644 --- a/goon/browserassets/js/browserOutput.js +++ b/goon/browserassets/js/browserOutput.js @@ -69,10 +69,13 @@ var opts = { 'macros': {}, // Emoji toggle - 'enableEmoji': true + 'enableEmoji': true, + + // Reboot message stuff + 'rebootIntervalHandler': null }; -var regexHasError = false; //variable to check if regex has excepted +var regexHasError = false; //variable to check if regex has excepted function outerHTML(el) { var wrap = document.createElement('div'); @@ -97,10 +100,10 @@ if (typeof String.prototype.trim !== 'function') { if (!String.prototype.includes) { String.prototype.includes = function(search, start) { 'use strict'; - + if (search instanceof RegExp) { throw TypeError('first argument must not be a RegExp'); - } + } if (start === undefined) { start = 0; } return this.indexOf(search, start) !== -1; }; @@ -124,7 +127,7 @@ function byondDecode(message) { // The replace for + is because FOR SOME REASON, BYOND replaces spaces with a + instead of %20, and a plus with %2b. // Marvelous. message = message.replace(/\+/g, "%20"); - try { + try { // This is a workaround for the above not always working when BYOND's shitty url encoding breaks. // Basically, sometimes BYOND's double encoding trick just arbitrarily produces something that makes decodeURIComponent // throw an "Invalid Encoding URI" URIError... the simplest way to work around this is to just ignore it and use unescape instead @@ -217,10 +220,10 @@ function highlightTerms(el) { ind = next_tag; } } - + element.innerHTML = s; } - + for (var i = 0; i < opts.highlightTerms.length; i++) { //Each highlight term if(opts.highlightTerms[i]) { if(!opts.highlightRegexEnable){ @@ -573,6 +576,34 @@ function toggleWasd(state) { opts.wasd = (state == 'on' ? true : false); } +function reboot(timeRaw) { + var timeLeftSecs = parseInt(timeRaw); + const intervalSecs = 1; // tick every 1 second + + rebootFinished(); + internalOutput('
The server is restarting. Reconnect (' + timeLeftSecs + ')
', 'internal'); + + opts.rebootIntervalHandler = setInterval(function() { + timeLeftSecs -= intervalSecs; + if (timeLeftSecs <= 0) { + $("#reconnectTimer").text('Reconnecting...'); + window.location.href = 'byond://winset?command=.reconnect'; + clearInterval(opts.rebootIntervalHandler) + opts.rebootIntervalHandler = null; + } else { + $("#reconnectTimer").text('Reconnect (' + timeLeftSecs + ')'); + } + }, intervalSecs * 1000); +} + +function rebootFinished() { + if (opts.rebootIntervalHandler != null) { + clearInterval(opts.rebootIntervalHandler) + } + $(" Reconnected automatically!").insertBefore("#reconnectTimer"); + $("#reconnectTimer").remove(); +} + /***************************************** * * MAKE MACRO DICTIONARY @@ -652,7 +683,7 @@ $(function() { 'shideSpam': getCookie('hidespam'), 'darkChat': getCookie('darkChat'), }; - + if (savedConfig.sfontSize) { $messages.css('font-size', savedConfig.sfontSize); internalOutput('Loaded font size setting of: '+savedConfig.sfontSize+'', 'internal'); @@ -1010,18 +1041,18 @@ $(function() { } else { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } - + // synchronous requests are depricated in modern browsers - xmlHttp.open('GET', 'browserOutput.css', true); + xmlHttp.open('GET', 'browserOutput.css', true); xmlHttp.onload = function (e) { if (xmlHttp.status === 200) { // request successful - + // Generate Log var saved = ''; saved += $messages.html(); saved = saved.replace(/&/g, '&'); saved = saved.replace(/