diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-02-12 18:56:52 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-02-12 18:56:52 +0100 |
commit | 5f391aadbcc818ab7fa13312fb2ee04ed02983c6 (patch) | |
tree | 59a50473839a26370b6d1a51cb430adb49b26536 /api | |
parent | b8f3a658253bb5991a3d034733b685cc7d543704 (diff) |
base /api/game/new
Diffstat (limited to 'api')
-rw-r--r-- | api/auth/login.py | 2 | ||||
-rw-r--r-- | api/auth/signup.py | 2 | ||||
-rw-r--r-- | api/game/new.py | 25 | ||||
-rw-r--r-- | api/main.py | 2 | ||||
-rw-r--r-- | api/randid.py | 14 | ||||
-rwxr-xr-x | api/tests.sh | 20 |
6 files changed, 58 insertions, 7 deletions
diff --git a/api/auth/login.py b/api/auth/login.py index 8eb0eea..e686b67 100644 --- a/api/auth/login.py +++ b/api/auth/login.py @@ -15,7 +15,7 @@ def index(): if not email or \ not password: return "", 400 - + user_id = None user_id = user_id or cursor.execute("select user_id from users where email = ?", [email]).fetchone() user_id = user_id or cursor.execute("select user_id from users where username = ?", [email]).fetchone() diff --git a/api/auth/signup.py b/api/auth/signup.py index a9e155c..e77f82d 100644 --- a/api/auth/signup.py +++ b/api/auth/signup.py @@ -26,7 +26,7 @@ def index(): if cursor.execute("select email from users where email = ?", [email]).fetchone(): return {"error": "email_taken"}, 403 - user_id = new_uuid() + user_id = new_uuid("users") password_hash = passwords.password_hash(password) registered = int( time.time() * 1000 ) diff --git a/api/game/new.py b/api/game/new.py new file mode 100644 index 0000000..6f777e3 --- /dev/null +++ b/api/game/new.py @@ -0,0 +1,25 @@ +from flask import Blueprint, request, make_response +from main import cursor, connection +from randid import new_uuid +import time +import json + +new_game = Blueprint('new', __name__) + +@new_game.route('/new', methods = ['POST']) +def index(): + data = request.get_json() + + user_id = data.get("user_id") or "" # maybe set up a temporary user here? + game_settings = data.get("settings") or "" + + if not user_id: + print("a temporary user should be set up here") + + game_id = new_uuid("games") + timestamp = int( time.time() * 1000 ) + + cursor.execute("insert into games values (?, NULL, NULL, ?, NULL, NULL, ?, NULL, NULL, NULL, \"wait_for_opponent\", ?) ", (game_id, user_id, timestamp, json.dumps(game_settings))) + connection.commit() + + return { "id": game_id }, 200 diff --git a/api/main.py b/api/main.py index 0d16cbc..b9900f8 100644 --- a/api/main.py +++ b/api/main.py @@ -14,10 +14,12 @@ from user.info import info from auth.signup import signup from auth.login import login from auth.login_token import token +from game.new import new_game app.register_blueprint(status, url_prefix='/') app.register_blueprint(info, url_prefix='/user') app.register_blueprint(signup, url_prefix='/auth') app.register_blueprint(login, url_prefix='/auth') app.register_blueprint(token, url_prefix='/auth') +app.register_blueprint(new_game, url_prefix='/game') diff --git a/api/randid.py b/api/randid.py index b9292b6..645d0a0 100644 --- a/api/randid.py +++ b/api/randid.py @@ -1,11 +1,17 @@ from main import cursor import uuid -def new_uuid(): +tables = { + "users": "user_id", + "games": "game_id" + } + +def new_uuid(table_name): temp_uuid = str(uuid.uuid4()) - # check if user_id is already taken - if cursor.execute("select user_id from users where user_id = ?", [temp_uuid]).fetchone(): - return new_uuid() + column_name = tables[table_name] + # check if id is already taken + if cursor.execute(f"select {column_name} from {table_name} where {column_name} = ?", [temp_uuid]).fetchone(): + return new_uuid(table_name) else: return temp_uuid diff --git a/api/tests.sh b/api/tests.sh index 002f85f..3882699 100755 --- a/api/tests.sh +++ b/api/tests.sh @@ -54,5 +54,23 @@ user_info () { # localhost:5000/api/auth/token # } -user_info +new_game () { + curl -X POST \ + -H "Content-Type: application/json" \ + -d '{ + "user_id": "4577c119-c768-4ad5-afec-b53a5c19baf4", + "settings": { + "ranked": true, + "timelimit": { + "minutes": 5, + "seconds": 30, + "enabled": true, + "shared": "false" + } + } + }' \ + localhost:5000/game/new +} + +new_game |