|
|
@ -1,14 +1,15 @@ |
|
|
|
#!flask/bin/python |
|
|
|
import sqlite3 as sql |
|
|
|
from flask import Flask,abort,request,jsonify |
|
|
|
|
|
|
|
START_PORT = 8127 |
|
|
|
import sqlite3 |
|
|
|
import secrets |
|
|
|
from flask import Flask, abort, request, jsonify |
|
|
|
|
|
|
|
START_PORT = 8128 |
|
|
|
|
|
|
|
try: |
|
|
|
with sql.connect("database.db") as con: |
|
|
|
with sqlite3.connect("database.db") as con: |
|
|
|
cur = con.cursor() |
|
|
|
cur.execute('CREATE TABLE IF NOT EXISTS games (id INTEGER PRIMARY KEY, ip TEXT, running INTEGER, port INTEGER, player_count INTEGER)') |
|
|
|
cur.execute( |
|
|
|
'CREATE TABLE IF NOT EXISTS games (id INTEGER PRIMARY KEY, secret TEXT, ip TEXT, running INTEGER, port INTEGER, player_count INTEGER)') |
|
|
|
con.commit() |
|
|
|
except: |
|
|
|
con.rollback() |
|
|
@ -17,23 +18,94 @@ finally: |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
@app.route('/create',methods = ['POST']) |
|
|
|
def create(): |
|
|
|
|
|
|
|
@app.route('/client/game/create', methods=['POST']) |
|
|
|
def create_game(): |
|
|
|
# TODO: block multiple request from same game |
|
|
|
try: |
|
|
|
with sql.connect("database.db") as con: |
|
|
|
with sqlite3.connect("database.db") as con: |
|
|
|
port = START_PORT |
|
|
|
cur = con.cursor() |
|
|
|
rows = True |
|
|
|
cur.execute("SELECT * FROM games WHERE port=?", (port,)) |
|
|
|
rows = cur.fetchall() |
|
|
|
while rows: |
|
|
|
port += 1 |
|
|
|
cur.execute("SELECT * FROM games WHERE port = ?", (port,)) |
|
|
|
cur.execute("SELECT * FROM games WHERE port=?", (port,)) |
|
|
|
rows = cur.fetchall() |
|
|
|
token = secrets.token_hex(32) |
|
|
|
cur.execute("INSERT INTO games (secret, ip,port) VALUES (?,?,?)", |
|
|
|
(token, request.remote_addr, port)) |
|
|
|
except: |
|
|
|
con.rollback() |
|
|
|
finally: |
|
|
|
con.close() |
|
|
|
return jsonify({'port': port}) |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/client/games', methods=['GET']) |
|
|
|
def get_games(): |
|
|
|
try: |
|
|
|
with sqlite3.connect("database.db") as con: |
|
|
|
cur = con.cursor() |
|
|
|
query = "SELECT * FROM games" |
|
|
|
if 'open' in request.args: |
|
|
|
query = "SELECT * FROM games WHERE running=0" |
|
|
|
cur.execute(query) |
|
|
|
rows = cur.fetchall() |
|
|
|
cur.execute("INSERT INTO games (ip,port) VALUES (?,?)", (request.remote_addr, port)) |
|
|
|
token = secrets.token_hex(32) |
|
|
|
except: |
|
|
|
con.rollback() |
|
|
|
finally: |
|
|
|
con.close() |
|
|
|
return jsonify({'ip' : request.remote_addr, 'port' : port}) |
|
|
|
result = [] |
|
|
|
for row in rows: |
|
|
|
result.append( |
|
|
|
{'player_count': row[5], 'running': (row[3] == 1), 'port': row[4]}) |
|
|
|
return jsonify(result) |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/game', methods=['DELETE']) |
|
|
|
def close_game(): |
|
|
|
if not request.json: |
|
|
|
abort(400) |
|
|
|
if not 'token' in request.json: |
|
|
|
abort(400) |
|
|
|
|
|
|
|
token = request.json.get('token') |
|
|
|
try: |
|
|
|
with sqlite3.connect("database.db") as con: |
|
|
|
cur = con.cursor() |
|
|
|
cur.execute("DELETE FROM games WHERE secret=?", (token,)) |
|
|
|
except: |
|
|
|
con.rollback() |
|
|
|
return jsonify(False) |
|
|
|
finally: |
|
|
|
con.close() |
|
|
|
return jsonify(True) |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/game', methods=['PUT']) |
|
|
|
def update_game(): |
|
|
|
if not request.json: |
|
|
|
abort(400) |
|
|
|
if not 'token' in request.json or not 'player_count' in request.json or not 'running' in request.json: |
|
|
|
abort(400) |
|
|
|
|
|
|
|
token = request.json.get('token') |
|
|
|
player_count = request.json.get('player_count') |
|
|
|
running = request.json.get('running') |
|
|
|
try: |
|
|
|
with sqlite3.connect("database.db") as con: |
|
|
|
cur = con.cursor() |
|
|
|
cur.execute("UPDATE games SET player_count=?,running=? WHERE secret=?", |
|
|
|
(player_count, running, token,)) |
|
|
|
except: |
|
|
|
con.rollback() |
|
|
|
return jsonify(False) |
|
|
|
finally: |
|
|
|
con.close() |
|
|
|
return jsonify(True) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
app.run(debug=True) |