diff --git a/static/chat.js b/static/chat.js index a213134..2e907b7 100644 --- a/static/chat.js +++ b/static/chat.js @@ -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; -}; }); \ No newline at end of file diff --git a/static/game.js b/static/game.js index f0dbae6..1ff1fd6 100644 --- a/static/game.js +++ b/static/game.js @@ -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({ @@ -231,8 +272,7 @@ $(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"]; @@ -240,6 +280,8 @@ $(function() { var me = new player([30, 100], personnal_key, player_name); me.init_position(); + // start polling events + updater.poll(); $("#messageform").submit(function(e) { e.preventDefault();