Skip to content

Commit

Permalink
Don't start polling before the new player is created
Browse files Browse the repository at this point in the history
  • Loading branch information
Batiste Bieler committed Nov 27, 2009
1 parent a4c9fe0 commit e955714
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 57 deletions.
55 changes: 0 additions & 55 deletions static/chat.js
Expand Up @@ -14,62 +14,7 @@

$(document).ready(function() {

var updater = {
errorSleepTime: 500,
cursor: null,

poll: function() {
var args = {"_xsrf": getCookie("_xsrf")};
if (updater.cursor) args.cursor = updater.cursor;
$.ajax({
url: "/a/room/updates",
type: "POST",
dataType: "text",
data: $.param(args),
success: updater.onSuccess,
error: updater.onError
});
},

onSuccess: function(response) {
//try {
var mapdata = $.evalJSON(response);
$.receive_data(mapdata)
//var pos = mapdata['players'][0]['position'].split(',');
//$.player_target_position[0] = parseInt(pos[0])
//$.player_target_position[1] = parseInt(pos[1])

//} catch (e) {
// updater.onError();
// return;
//}
updater.errorSleepTime = 500;
window.setTimeout(updater.poll, 0);
},

onError: function(response) {
updater.errorSleepTime *= 2;
console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
window.setTimeout(updater.poll, updater.errorSleepTime);
}
};

updater.poll();


function getCookie(name) {
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
return r ? r[1] : undefined;
}

jQuery.fn.formToDict = function() {
var fields = this.serializeArray();
var json = {}
for (var i = 0; i < fields.length; i++) {
json[fields[i].name] = fields[i].value;
}
if (json.next) delete json.next;
return json;
};

});
46 changes: 44 additions & 2 deletions static/game.js
Expand Up @@ -8,6 +8,47 @@ $(function() {
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function() {};

// polling update
var updater = {
errorSleepTime: 500,
cursor: null,

poll: function() {
var args = {"_xsrf": getCookie("_xsrf")};
if (updater.cursor) args.cursor = updater.cursor;
$.ajax({
url: "/a/room/updates",
type: "POST",
dataType: "text",
data: $.param(args),
success: updater.onSuccess,
error: updater.onError
});
},

onSuccess: function(response) {
//try {
var mapdata = $.evalJSON(response);
$.receive_data(mapdata)
//var pos = mapdata['players'][0]['position'].split(',');
//$.player_target_position[0] = parseInt(pos[0])
//$.player_target_position[1] = parseInt(pos[1])

//} catch (e) {
// updater.onError();
// return;
//}
updater.errorSleepTime = 500;
window.setTimeout(updater.poll, 0);
},

onError: function(response) {
updater.errorSleepTime *= 2;
console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
window.setTimeout(updater.poll, updater.errorSleepTime);
}
};

jQuery.postJSON = function(url, args, callback) {
args._xsrf = getCookie("_xsrf");
$.ajax({
Expand Down Expand Up @@ -231,15 +272,16 @@ $(function() {
};

var personnal_key = false
/*var = prompt("Choose your username");*/
var player_name = "toto";
var player_name = prompt("Choose your hero name");
$.postJSON("/a/player/new", {'body':player_name}, function(response) {
response = $.evalJSON(response);
personnal_key = response["new_player"]["key"];
});

var me = new player([30, 100], personnal_key, player_name);
me.init_position();
// start polling events
updater.poll();

$("#messageform").submit(function(e) {
e.preventDefault();
Expand Down

0 comments on commit e955714

Please sign in to comment.