merged both get and post requests

This commit is contained in:
ksjdragon 2016-01-16 22:20:59 -05:00
parent 7d58256e28
commit 2a93fa7fc0
2 changed files with 44 additions and 53 deletions

View File

@ -9,7 +9,7 @@ color = 0
vertical = 0 vertical = 0
playersInGame = [] playersInGame = []
maxPlayers = 10 maxPlayers = 10
timeLeft = 10 + 1 timeLeft = 11
# Renders client # Renders client
@app.route("/") @app.route("/")
@ -50,11 +50,13 @@ def update_game():
# Return the game with the information you added, in addition to everyone else # Return the game with the information you added, in addition to everyone else
return jsonify(game) return jsonify(game)
@app.route('/pregame', methods=['GET','POST','EXIT','COUNT']) @app.route('/pregame', methods=['GET','POST','COUNT'])
def update_players(): def update_players():
if method.request == 'GET': if method.request == 'GET':
return jsonify(len(playersInGame)) countdown()
return timeLeft
if method.request == 'POST': if method.request == 'POST':
#Define the data given by client. #Define the data given by client.
uuid4 = request.get_json(force=True) uuid4 = request.get_json(force=True)
@ -62,15 +64,13 @@ def update_players():
if uuid4 not in playersInGame: if uuid4 not in playersInGame:
playersInGame.append(uuid4) playersInGame.append(uuid4)
return jsonify(len(playersInGame))
if method.request == 'EXIT': if method.request == 'EXIT':
#Define the data given by client. #Define the data given by client.
uuid4 = request.get_json(force=True) uuid4 = request.get_json(force=True)
playersInGame.remove(uuid4) playersInGame.remove(uuid4)
if method.request == 'COUNT':
countdown()
return timeLeft
def countdown(): def countdown():
timeLeft = timeLeft - 1 timeLeft = timeLeft - 1
time.sleep(1) time.sleep(1)

View File

@ -45,7 +45,7 @@ document.getElementsByClassName('play')[0].onclick = function startGame() {
// Update score before creating player so scoreboard starts at 0 // Update score before creating player so scoreboard starts at 0
updateScore(); updateScore();
waitForPlayers(); waitForPlayers();
document.onkeydown = movePlayer;
} }
function getInitial() { function getInitial() {
@ -114,27 +114,6 @@ function serverTransfer(coordinate,team,turn,username) {
function waitForPlayers(uuid4) { function waitForPlayers(uuid4) {
timer = setTimeout(function() { timer = setTimeout(function() {
// Sending "I'm here." // Sending "I'm here."
$.ajax({
url: 'http://127.0.0.1:5000/pregame',
type: 'GET',
async: false,
// data: '',
success: function(data) {
//called when successful
numberOfPlayers = data
if (numberOfPlayers == '10') {
countdown();
// try catch delete table for visuals later.
}
},
error: function(e) {
//called when there is an error
//(e.message);
}
})
.then(
function sendhere() {
$.ajax('http://127.0.0.1:5000/pregame', { $.ajax('http://127.0.0.1:5000/pregame', {
method: 'POST', method: 'POST',
type : "POST", type : "POST",
@ -142,38 +121,50 @@ function waitForPlayers(uuid4) {
async: false, async: false,
dataType: "json", dataType: "json",
contentType: 'application/json;charset=UTF-8' contentType: 'application/json;charset=UTF-8'
})
.then(
waitForPlayers();
);
}
);
}, 3000)
}
function countdown() {
$.ajax({
url: 'http://127.0.0.1:5000/pregame',
type: 'COUNT',
async: false,
success: function(data) { success: function(data) {
//called when successful numberOfPlayers = data;
timeLeft = data; if (numberOfPlayers == '10') {
//MAKE VISUAL STUFF HERE countdown();
countdown() // try catch delete table for visuals later.
if(timeLeft == 0) {
createPlayer();
autoScroll('start');
} }
}, },
error: function(e) { error: function(e) {
//called when there is an error //called when there is an error
//(e.message); //(e.message);
} }
}) })
.then(
// Repeat this function until 10 players are here.
waitForPlayers();
);
}, 3000);
} }
function countdown() {
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
// Creation of Table // Creation of Table