You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.1 KiB
111 lines
3.1 KiB
#!flask/bin/python
|
|
import sqlite3
|
|
import secrets
|
|
from flask import Flask, abort, request, jsonify
|
|
|
|
START_PORT = 8128
|
|
|
|
try:
|
|
with sqlite3.connect("database.db") as con:
|
|
cur = con.cursor()
|
|
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()
|
|
finally:
|
|
con.close()
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
@app.route('/client/game/create', methods=['POST'])
|
|
def create_game():
|
|
# TODO: block multiple request from same game
|
|
try:
|
|
with sqlite3.connect("database.db") as con:
|
|
port = START_PORT
|
|
cur = con.cursor()
|
|
cur.execute("SELECT * FROM games WHERE port=?", (port,))
|
|
rows = cur.fetchall()
|
|
while rows:
|
|
port += 1
|
|
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()
|
|
token = secrets.token_hex(32)
|
|
except:
|
|
con.rollback()
|
|
finally:
|
|
con.close()
|
|
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)
|