From 2a93fa7fc07cfb3ffda1b93ed7c632b3bdddc700 Mon Sep 17 00:00:00 2001 From: ksjdragon Date: Sat, 16 Jan 2016 22:20:59 -0500 Subject: [PATCH] merged both get and post requests --- clashingcomrades/server.py | 16 +++---- clashingcomrades/static/main.js | 81 +++++++++++++++------------------ 2 files changed, 44 insertions(+), 53 deletions(-) diff --git a/clashingcomrades/server.py b/clashingcomrades/server.py index 3c23e38..451d9cc 100644 --- a/clashingcomrades/server.py +++ b/clashingcomrades/server.py @@ -9,7 +9,7 @@ color = 0 vertical = 0 playersInGame = [] maxPlayers = 10 -timeLeft = 10 + 1 +timeLeft = 11 # Renders client @app.route("/") @@ -50,27 +50,27 @@ def update_game(): # Return the game with the information you added, in addition to everyone else return jsonify(game) -@app.route('/pregame', methods=['GET','POST','EXIT','COUNT']) +@app.route('/pregame', methods=['GET','POST','COUNT']) def update_players(): - + if method.request == 'GET': - return jsonify(len(playersInGame)) + countdown() + return timeLeft + if method.request == 'POST': #Define the data given by client. uuid4 = request.get_json(force=True) # If this client has not already registered with the server, register. if uuid4 not in playersInGame: playersInGame.append(uuid4) + + return jsonify(len(playersInGame)) if method.request == 'EXIT': #Define the data given by client. uuid4 = request.get_json(force=True) playersInGame.remove(uuid4) - if method.request == 'COUNT': - countdown() - return timeLeft - def countdown(): timeLeft = timeLeft - 1 time.sleep(1) diff --git a/clashingcomrades/static/main.js b/clashingcomrades/static/main.js index 022b488..cc7e8c6 100644 --- a/clashingcomrades/static/main.js +++ b/clashingcomrades/static/main.js @@ -45,7 +45,7 @@ document.getElementsByClassName('play')[0].onclick = function startGame() { // Update score before creating player so scoreboard starts at 0 updateScore(); waitForPlayers(); - document.onkeydown = movePlayer; + } function getInitial() { @@ -114,18 +114,19 @@ function serverTransfer(coordinate,team,turn,username) { function waitForPlayers(uuid4) { timer = setTimeout(function() { // Sending "I'm here." - $.ajax({ - url: 'http://127.0.0.1:5000/pregame', - type: 'GET', - async: false, - // data: '', + $.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) { - //called when successful - numberOfPlayers = data + numberOfPlayers = data; if (numberOfPlayers == '10') { countdown(); // try catch delete table for visuals later. - } }, error: function(e) { @@ -134,46 +135,36 @@ function waitForPlayers(uuid4) { } }) .then( - function sendhere() { - $.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' - }) - .then( - waitForPlayers(); - ); - } - ); - - }, 3000) + // Repeat this function until 10 players are here. + waitForPlayers(); + ); + }, 3000); } function countdown() { - $.ajax({ - url: 'http://127.0.0.1:5000/pregame', - type: 'COUNT', - async: false, - success: function(data) { - //called when successful - timeLeft = data; - //MAKE VISUAL STUFF HERE - countdown() - if(timeLeft == 0) { - createPlayer(); - autoScroll('start'); - } - - }, - error: function(e) { - //called when there is an error - //(e.message); - } - }) + timer = setTimeout(function() { + $.ajax({ + url: 'http://127.0.0.1:5000/pregame', + type: 'GET', + async: false, + success: function(data) { + timeLeft = data; + //MAKE VISUAL STUFF HERE + if(timeLeft == 0) { + createPlayer(); + autoScroll('start'); + document.onkeydown = movePlayer; + } + countdown(); + }, + error: function(e) { + //called when there is an error + //(e.message); + } + }) + }, 750); //Prevent too many requests } + // CREATION // Creation of Table