Skip to content

Commit

Permalink
make games auto-reload if disconnected from server and then re-connected
Browse files Browse the repository at this point in the history
add GameServer.isReloaded() you can call to tell if your game is starting
because of a reload
  • Loading branch information
greggman committed Oct 29, 2014
1 parent fa0c77e commit 20ea8e6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
32 changes: 28 additions & 4 deletions public/hft/0.x.x/scripts/gameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -354,5 +378,5 @@ define([
return GameServer;
});

}());
}(this));

23 changes: 18 additions & 5 deletions public/hft/0.x.x/scripts/gamesupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 20 additions & 6 deletions public/hft/0.x.x/scripts/misc/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -98,4 +110,6 @@ define(function() {
return Cookie;
});

}(this));


0 comments on commit 20ea8e6

Please sign in to comment.