diff --git a/clashingcomrades/README.md b/clashingcomrades/README.md index e69de29..36daf98 100644 --- a/clashingcomrades/README.md +++ b/clashingcomrades/README.md @@ -0,0 +1,2 @@ +# Coexistence +A game coded in the script of java. \ No newline at end of file diff --git a/clashingcomrades/server.py b/clashingcomrades/server.py index 451d9cc..e04c789 100644 --- a/clashingcomrades/server.py +++ b/clashingcomrades/server.py @@ -1,6 +1,6 @@ from flask import Flask from flask import render_template, jsonify, request -from time import sleep +import time app = Flask(__name__) # Since only one game for now, this is the object that will hold the data for the game @@ -8,7 +8,8 @@ game = {} color = 0 vertical = 0 playersInGame = [] -maxPlayers = 10 +# Testing 1 Player for now +maxPlayers = 1 timeLeft = 11 # Renders client @@ -50,28 +51,32 @@ 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','COUNT']) +@app.route('/pregame', methods=['GET','POST','EXIT']) def update_players(): - if method.request == 'GET': + if request.method == 'GET': countdown() - return timeLeft + toReturn = {} + toReturn["timeLeft"] = timeLeft + return jsonify(toReturn) - if method.request == 'POST': + if request.method == '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: + if not uuid4 in playersInGame: playersInGame.append(uuid4) - - return jsonify(len(playersInGame)) + toReturn = {} + toReturn["playersInGame"] = len(playersInGame) + return jsonify(toReturn) - if method.request == 'EXIT': + if request.method == 'EXIT': #Define the data given by client. uuid4 = request.get_json(force=True) playersInGame.remove(uuid4) def countdown(): + timeLeft = 11 timeLeft = timeLeft - 1 time.sleep(1) if len(playersInGame) != maxPlayers: diff --git a/clashingcomrades/static/main.js b/clashingcomrades/static/main.js index cc7e8c6..bf9d672 100644 --- a/clashingcomrades/static/main.js +++ b/clashingcomrades/static/main.js @@ -44,7 +44,7 @@ document.getElementsByClassName('play')[0].onclick = function startGame() { createTable(); // Update score before creating player so scoreboard starts at 0 updateScore(); - waitForPlayers(); + waitForPlayers(username); } @@ -112,33 +112,39 @@ function serverTransfer(coordinate,team,turn,username) { } function waitForPlayers(uuid4) { - 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' + 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; - if (numberOfPlayers == '10') { - countdown(); - // 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. - waitForPlayers(); - ); - }, 3000); + 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 + } + ); + }, 3000); + } } function countdown() { @@ -148,7 +154,7 @@ function countdown() { type: 'GET', async: false, success: function(data) { - timeLeft = data; + timeLeft = data["timeLeft"]; //MAKE VISUAL STUFF HERE if(timeLeft == 0) { createPlayer();