diff --git a/clashingcomrades/server.py b/clashingcomrades/server.py index e04c789..6369fdd 100644 --- a/clashingcomrades/server.py +++ b/clashingcomrades/server.py @@ -10,7 +10,7 @@ vertical = 0 playersInGame = [] # Testing 1 Player for now maxPlayers = 1 -timeLeft = 11 +timeLeft = 11.0 # Renders client @app.route("/") @@ -53,11 +53,16 @@ def update_game(): @app.route('/pregame', methods=['GET','POST','EXIT']) def update_players(): - + global timeLeft if request.method == 'GET': - countdown() + before = time.clock() + time.sleep(0.99) + after = time.clock() + timeTaken = after - before + timeLeft -= timeTaken + toReturn = {} - toReturn["timeLeft"] = timeLeft + toReturn["timeLeft"] = int(timeLeft) return jsonify(toReturn) if request.method == 'POST': @@ -75,15 +80,6 @@ def update_players(): uuid4 = request.get_json(force=True) playersInGame.remove(uuid4) -def countdown(): - timeLeft = 11 - timeLeft = timeLeft - 1 - time.sleep(1) - if len(playersInGame) != maxPlayers: - timeLeft = 11 - else: - countdown() - # Eventual more than one game can be played on website # @app.route('/game/', methods=['GET', 'POST']) diff --git a/clashingcomrades/static/main.js b/clashingcomrades/static/main.js index bf9d672..f9e43db 100644 --- a/clashingcomrades/static/main.js +++ b/clashingcomrades/static/main.js @@ -50,7 +50,7 @@ document.getElementsByClassName('play')[0].onclick = function startGame() { function getInitial() { $.ajax({ - url: 'http://127.0.0.1:5000/pregame', + url: 'http://localhost:5000/game', type: 'GET', async: false, // data: '', @@ -74,7 +74,7 @@ function serverTransfer(coordinate,team,turn,username) { username: username }; // Sending Data - $.ajax('http://127.0.0.1:5000/game', { + $.ajax('http://localhost:5000/game', { method: 'POST', type : "POST", data: JSON.stringify(move, null, '\t'), @@ -112,56 +112,53 @@ function serverTransfer(coordinate,team,turn,username) { } function waitForPlayers(uuid4) { - repeat = true; - while (repeat == true) { - timer = setTimeout(function() { - // Sending "I'm here." - $.ajax('http://127.0.0.1:5000/pregame', { - method: 'POST', - type : "POST", - data: JSON.stringify(uuid4, null, '\t'), - async: false, - dataType: "json", - contentType: 'application/json;charset=UTF-8', - - success: function(data) { - numberOfPlayers = data["playersInGame"]; - if (numberOfPlayers == 1) { - countdown(); - repeat = false - // try catch delete table for visuals later. - } - }, - error: function(e) { - //called when there is an error - //(e.message); - } - }) - .then( - // Repeat this function until 10 players are here. - function success (data) { - repeat = true + timer = setTimeout(function() { + // Sending "I'm here." + $.ajax('http://localhost:5000/pregame', { + method: 'POST', + type : "POST", + data: JSON.stringify(uuid4, null, '\t'), + async: false, + dataType: "json", + contentType: 'application/json;charset=UTF-8', + }) + .then( + function success(data) { + numberOfPlayers = data["playersInGame"]; + if (numberOfPlayers == 1) { + countdown(); + // try catch delete table for visuals later. + } else { + waitForPlayers(); } - ); - }, 3000); - } + }, + function error(e) { + //called when there is an error + //(e.message); + } + // Repeat this function until 10 players are here. + ); + }, 3000); } function countdown() { timer = setTimeout(function() { $.ajax({ - url: 'http://127.0.0.1:5000/pregame', + url: 'http://localhost:5000/pregame', type: 'GET', async: false, success: function(data) { timeLeft = data["timeLeft"]; + console.log(data) //MAKE VISUAL STUFF HERE if(timeLeft == 0) { + canMove = true; createPlayer(); autoScroll('start'); document.onkeydown = movePlayer; - } - countdown(); + } else { + countdown(); + } }, error: function(e) { //called when there is an error