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
|
2016-01-19 22:42:56 -05:00
|
|
|
import random
|
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 = {}
|
2015-08-25 22:32:21 -04:00
|
|
|
color = 0
|
2015-08-26 11:20:37 -04:00
|
|
|
vertical = 0
|
2016-01-16 21:26:37 -05:00
|
|
|
playersInGame = []
|
2016-01-17 20:46:58 -05:00
|
|
|
maxPlayers = 2
|
2016-01-18 16:37:52 -05:00
|
|
|
initialTime = 0
|
2016-01-19 22:42:56 -05:00
|
|
|
timeLeft = 3
|
|
|
|
|
|
2016-01-19 23:26:25 -05:00
|
|
|
def RandomMove(username):
|
2016-01-19 22:42:56 -05:00
|
|
|
x = [1,0]
|
|
|
|
|
random.shuffle(x)
|
2016-01-19 23:26:25 -05:00
|
|
|
game[username][0] = [x[0] + game[username][0][0], x[1] + game[username][0][1]]
|
2016-01-19 22:42:56 -05:00
|
|
|
|
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"
|
2016-01-19 22:42:56 -05:00
|
|
|
@app.route('/game', methods=['POST'])
|
2015-07-06 22:32:53 -04:00
|
|
|
def update_game():
|
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':
|
2016-01-19 23:26:25 -05:00
|
|
|
if "timeLeft" in game:
|
|
|
|
|
del game["timeLeft"]
|
|
|
|
|
noMove = game.keys()
|
2016-01-19 22:42:56 -05:00
|
|
|
global timeLeft
|
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)
|
2016-01-19 22:42:56 -05:00
|
|
|
|
|
|
|
|
if timeLeft == 3:
|
2016-01-19 23:26:25 -05:00
|
|
|
timeLeft = time.time()
|
2016-01-19 22:42:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(playerStatus[playerStatus.keys()[0]]) == 1:
|
|
|
|
|
if playerStatus[playerStatus.keys()[0]][0] == "death":
|
2016-01-20 20:31:50 -05:00
|
|
|
del game[playerStatus.keys()[0]]
|
|
|
|
|
del noMove[noMove.index(playerStatus.keys()[0])]
|
2016-01-19 22:42:56 -05:00
|
|
|
else:
|
|
|
|
|
global color
|
|
|
|
|
global vertical
|
|
|
|
|
color = color + 1
|
|
|
|
|
team = ["red", "blue"][color % 2]
|
|
|
|
|
horizontal = 10 + color % 2
|
|
|
|
|
vertical = vertical + 1
|
|
|
|
|
answer = {
|
|
|
|
|
"team": team,
|
|
|
|
|
"coordinate": [vertical, horizontal],
|
|
|
|
|
"max": maxPlayers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
game[playerStatus[playerStatus.keys()[0]][0]] = [[vertical, horizontal], team]
|
|
|
|
|
|
|
|
|
|
return jsonify(answer)
|
|
|
|
|
|
|
|
|
|
|
2015-07-07 22:32:51 -04:00
|
|
|
# If the username that the player sent is already defined in game
|
2016-01-19 22:42:56 -05:00
|
|
|
elif playerStatus["username"] in game:
|
2016-01-20 20:31:50 -05:00
|
|
|
elif abs(playerStatus["coordinate"][0]) + abs(playerStatus["coordinate"][1]) == 1:
|
|
|
|
|
if playerStatus["username"] in noMove:
|
|
|
|
|
coord1 = playerStatus["coordinate"][0] + game[playerStatus["username"]][0][0]
|
|
|
|
|
coord2 = playerStatus["coordinate"][1] + game[playerStatus["username"]][0][1]
|
|
|
|
|
game[playerStatus["username"]][0] = [coord1, coord2]
|
|
|
|
|
del noMove[noMove.index(playerStatus["username"])]
|
2016-01-19 22:42:56 -05:00
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print "Hax?"
|
2016-01-19 23:26:25 -05:00
|
|
|
RandomMove(playerStatus["username"])
|
|
|
|
|
del noMove[noMove.index(playerStatus["username"])]
|
2016-01-19 22:42:56 -05:00
|
|
|
|
|
|
|
|
|
2015-07-06 22:32:53 -04:00
|
|
|
else:
|
2016-01-19 22:42:56 -05:00
|
|
|
return "Hax"
|
2015-08-25 22:32:21 -04:00
|
|
|
|
2015-07-07 02:28:57 -04:00
|
|
|
# Return the game with the information you added, in addition to everyone else
|
2016-01-19 23:26:25 -05:00
|
|
|
while (time.time() - timeLeft) < 3:
|
2016-01-19 22:55:45 -05:00
|
|
|
time.sleep(0.1)
|
2016-01-19 23:26:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
for player in noMove:
|
|
|
|
|
RandomMove(player)
|
2016-01-19 22:55:45 -05:00
|
|
|
timeLeft = 3
|
2015-07-06 22:32:53 -04:00
|
|
|
return jsonify(game)
|
|
|
|
|
|
2016-01-19 22:42:56 -05:00
|
|
|
@app.route('/pregame', methods=['GET','POST'])
|
2016-01-16 21:26:37 -05:00
|
|
|
def update_players():
|
2016-01-18 16:37:52 -05:00
|
|
|
global initialTime
|
|
|
|
|
global maxPlayers
|
|
|
|
|
|
2016-01-17 18:50:48 -05:00
|
|
|
if request.method == 'GET':
|
2016-01-18 16:37:52 -05:00
|
|
|
after = time.time()
|
|
|
|
|
timeSince = after - initialTime
|
|
|
|
|
timeLeft = 10 - timeSince
|
|
|
|
|
print timeLeft
|
2016-01-19 22:42:56 -05:00
|
|
|
game["timeLeft"] = timeLeft
|
2016-01-17 20:21:22 -05:00
|
|
|
|
2016-01-19 22:42:56 -05:00
|
|
|
return jsonify(game)
|
2016-01-16 22:20:59 -05:00
|
|
|
|
2016-01-17 18:50:48 -05:00
|
|
|
if request.method == 'POST':
|
2016-01-16 21:26:37 -05:00
|
|
|
#Define the data given by client.
|
2016-01-18 16:37:52 -05:00
|
|
|
username = request.get_json(force=True)
|
|
|
|
|
print username
|
|
|
|
|
|
2016-01-16 21:26:37 -05:00
|
|
|
# If this client has not already registered with the server, register.
|
2016-01-18 16:37:52 -05:00
|
|
|
if not username["username"] in playersInGame:
|
|
|
|
|
playersInGame.append(username["username"])
|
|
|
|
|
|
|
|
|
|
numberofplayers = len(playersInGame)
|
|
|
|
|
if numberofplayers == maxPlayers:
|
|
|
|
|
initialTime = time.time()
|
|
|
|
|
|
|
|
|
|
print numberofplayers
|
|
|
|
|
|
2016-01-17 18:50:48 -05:00
|
|
|
toReturn = {}
|
2016-01-18 16:37:52 -05:00
|
|
|
toReturn["playersInGame"] = numberofplayers
|
|
|
|
|
|
2016-01-17 18:50:48 -05:00
|
|
|
return jsonify(toReturn)
|
2016-01-16 21:26:37 -05:00
|
|
|
|
2016-01-18 16:37:52 -05:00
|
|
|
# if request.method == 'EXIT':
|
|
|
|
|
# #Define the data given by client.
|
|
|
|
|
# uuid4 = request.get_json(force=True)
|
|
|
|
|
# playersInGame.remove(uuid4)
|
2016-01-16 21:26:37 -05: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__":
|
2016-01-19 22:42:56 -05:00
|
|
|
# app.run(host='0.0.0.0')
|
|
|
|
|
app.run(debug=True, threaded=True)
|