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
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)

View File

@ -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