From 20ea8e66148495d42466d23be5c223fdceb8278b Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Wed, 29 Oct 2014 00:27:54 -0700 Subject: [PATCH] make games auto-reload if disconnected from server and then re-connected add GameServer.isReloaded() you can call to tell if your game is starting because of a reload --- public/hft/0.x.x/scripts/gameserver.js | 32 +++++++++++++++++++++--- public/hft/0.x.x/scripts/gamesupport.js | 23 +++++++++++++---- public/hft/0.x.x/scripts/misc/cookies.js | 26 ++++++++++++++----- 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/public/hft/0.x.x/scripts/gameserver.js b/public/hft/0.x.x/scripts/gameserver.js index ad3e3687..ad900d46 100644 --- a/public/hft/0.x.x/scripts/gameserver.js +++ b/public/hft/0.x.x/scripts/gameserver.js @@ -29,14 +29,14 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -(function() { - -var globalObject = this; +(function(globalObject) { define([ + './misc/cookies', './netplayer', './virtualsocket', ], function( + Cookie, NetPlayer, VirtualSocket) { @@ -77,6 +77,21 @@ define([ throw "you can't read the id before you've connected"; }, }; + var _reloaded = false; + + var readOldState = (function() { + var cookie = new Cookie("hft-state"); + var content = cookie.get(); + cookie.erase(); + + if (content) { + try { + var data = JSON.parse(content); + _reloaded = data.reload; + } catch (e) { + } + } + }()); if (!options.gameId) { var m = /games\/([^\/]+)\//.exec(window.location.href); @@ -235,6 +250,15 @@ define([ return _connected; }; + /** + * True if we were auto-reloaded after a disconnect. + * This happens if HFT stops running and then is re-started + * @return {Boolean} true if this is a reload. + */ + this.isReloaded = function() { + return _reloaded; + }; + var connect_ = function() { _socket = options.socket || new VirtualSocket(); _sendQueue = []; @@ -354,5 +378,5 @@ define([ return GameServer; }); -}()); +}(this)); diff --git a/public/hft/0.x.x/scripts/gamesupport.js b/public/hft/0.x.x/scripts/gamesupport.js index d549e809..7a2079c9 100644 --- a/public/hft/0.x.x/scripts/gamesupport.js +++ b/public/hft/0.x.x/scripts/gamesupport.js @@ -43,16 +43,20 @@ */ define([ '../../../3rdparty/stats/stats.min', + './misc/cookies', './misc/gameclock', './misc/logger', './misc/misc', './hft-splash', + './hft-system', ], function( StatsJS, + Cookie, GameClock, Logger, Misc, - HFTSplash) { + HFTSplash, + HFTSystem) { var $ = function(id) { return document.getElementById(id); @@ -93,17 +97,26 @@ define([ * */ var init = function(server, options) { - var showConnected = function() { + var handleConnected = function() { $('hft-disconnected').style.display = "none"; } - var showDisconnected = function() { + var handleDisconnected = function() { $('hft-disconnected').style.display = "block"; + + var hftSystem = new HFTSystem(); + hftSystem.on('connect', function() { + var cookie = new Cookie("hft-state"); + cookie.set(JSON.stringify({ + reload: true, + }), 1); + window.location.reload(); + }); } if (options.haveServer !== false && server) { - server.addEventListener('connect', showConnected); - server.addEventListener('disconnect', showDisconnected); + server.addEventListener('connect', handleConnected); + server.addEventListener('disconnect', handleDisconnected); } if (options.showGithub) { diff --git a/public/hft/0.x.x/scripts/misc/cookies.js b/public/hft/0.x.x/scripts/misc/cookies.js index 22aab811..75620564 100644 --- a/public/hft/0.x.x/scripts/misc/cookies.js +++ b/public/hft/0.x.x/scripts/misc/cookies.js @@ -28,9 +28,22 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -"use strict"; +(function(global) { define(function() { + + // If it node.js no cookies. + if (!global.document || !global.document.cookie) { + var noop = function() {}; + return function() { + return { + set: noop, + get: noop, + erase: noop, + }; + }; + } + /** * Represents a cookie. * @@ -60,11 +73,10 @@ define(function() { */ this.set = function(value, opt_days) { var expires = ""; - if (opt_days !== undefined) { - var date = new Date(); - date.setTime(Date.now() + (opt_days * 24 * 60 * 60 * 1000)); - expires = "; expires=" + date.toGMTString(); - } + opt_days = opt_days || 9999; + var date = new Date(); + date.setTime(Date.now() + (opt_days * 24 * 60 * 60 * 1000) | 0); + expires = "; expires=" + date.toGMTString(); var cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=" + path; document.cookie = cookie; }; @@ -98,4 +110,6 @@ define(function() { return Cookie; }); +}(this)); +