100 lines
3.0 KiB
Python
Raw Normal View History

2015-07-06 22:32:53 -04:00
from flask import Flask
from flask import render_template, jsonify, request
2016-01-17 18:50:48 -05:00
import time
2015-07-06 22:32:53 -04:00
app = Flask(__name__)
2015-07-07 02:28:57 -04:00
# Since only one game for now, this is the object that will hold the data for the game
2015-07-06 22:32:53 -04:00
game = {}
color = 0
2015-08-26 11:20:37 -04:00
vertical = 0
playersInGame = []
2016-01-17 18:50:48 -05:00
# Testing 1 Player for now
maxPlayers = 1
2016-01-16 22:20:59 -05:00
timeLeft = 11
2015-07-07 02:28:57 -04:00
# Renders client
2015-07-06 22:32:53 -04:00
@app.route("/")
def initial():
return render_template('index.html')
2015-07-07 02:28:57 -04:00
# The URL where the data transfer takes place, the "backend"
2015-07-06 22:32:53 -04:00
@app.route('/game', methods=['GET', 'POST'])
def update_game():
2015-07-07 02:28:57 -04:00
# This is for if the Client Wants something
# Eventually will be used for initial team and coordinate
# Not currently being used anywhere
2015-07-06 22:32:53 -04:00
if request.method == 'GET':
global color
2015-08-26 11:20:37 -04:00
global vertical
color = color + 1
team = ["red", "blue"][color % 2]
2015-08-26 11:20:37 -04:00
horizontal = 10 + color % 2
vertical = vertical + 1
answer = {
"team": team,
2015-08-26 11:20:37 -04:00
"coordinate": [vertical, horizontal]
}
return jsonify(answer)
2015-07-07 02:28:57 -04:00
# What to do when the Client tells the server something
2015-07-06 22:32:53 -04:00
if request.method == 'POST':
2015-07-07 02:28:57 -04:00
# Define the data given by client
2015-07-06 22:32:53 -04:00
playerStatus = request.get_json(force=True)
2015-07-07 22:32:51 -04:00
# If the username that the player sent is already defined in game
if playerStatus["username"] in game:
game[playerStatus["username"]].append([playerStatus["turn"], playerStatus["coordinate"], playerStatus["team"]])
2015-07-06 22:32:53 -04:00
else:
2015-07-07 22:32:51 -04:00
game[playerStatus["username"]] = [[playerStatus["turn"], playerStatus["coordinate"], playerStatus["team"]]]
2015-07-07 02:28:57 -04:00
# Return the game with the information you added, in addition to everyone else
2015-07-06 22:32:53 -04:00
return jsonify(game)
2016-01-17 18:50:48 -05:00
@app.route('/pregame', methods=['GET','POST','EXIT'])
def update_players():
2016-01-16 22:20:59 -05:00
2016-01-17 18:50:48 -05:00
if request.method == 'GET':
2016-01-16 22:20:59 -05:00
countdown()
2016-01-17 18:50:48 -05:00
toReturn = {}
toReturn["timeLeft"] = timeLeft
return jsonify(toReturn)
2016-01-16 22:20:59 -05:00
2016-01-17 18:50:48 -05:00
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.
2016-01-17 18:50:48 -05:00
if not uuid4 in playersInGame:
playersInGame.append(uuid4)
2016-01-17 18:50:48 -05:00
toReturn = {}
toReturn["playersInGame"] = len(playersInGame)
return jsonify(toReturn)
2016-01-17 18:50:48 -05:00
if request.method == 'EXIT':
#Define the data given by client.
uuid4 = request.get_json(force=True)
playersInGame.remove(uuid4)
def countdown():
2016-01-17 18:50:48 -05:00
timeLeft = 11
timeLeft = timeLeft - 1
time.sleep(1)
if len(playersInGame) != maxPlayers:
timeLeft = 11
else:
countdown()
2015-07-06 22:32:53 -04:00
2015-07-07 02:28:57 -04:00
# Eventual more than one game can be played on website
2015-07-06 22:32:53 -04:00
# @app.route('/game/<int:game_id>', methods=['GET', 'POST'])
# def update_game(game_id):
# if request.method == 'GET':
# return jsonify(games[game_id])
# if request.method == 'POST':
# print request.form
# games[game_id] = {"state": request.form['state']}
# return jsonify(games[game_id])
if __name__ == "__main__":
app.run(debug=True)