aboutsummaryrefslogtreecommitdiff
path: root/api/game/random.py
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-02-20 13:03:04 +0100
committerlonkaars <l.leblansch@gmail.com>2021-02-20 13:03:04 +0100
commite1978a9b80f3f7f5a36ca4af5f6df62f494a0d6d (patch)
tree27b2a142ad1c267f002e7c7e09ea337e4da688ac /api/game/random.py
parent16044fbf61b06f4d53e2ffcab67569721b3792e2 (diff)
/game/random endpoint
Diffstat (limited to 'api/game/random.py')
-rw-r--r--api/game/random.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/api/game/random.py b/api/game/random.py
new file mode 100644
index 0000000..bbddedc
--- /dev/null
+++ b/api/game/random.py
@@ -0,0 +1,31 @@
+from flask import Blueprint, request, make_response
+from db import cursor, connection
+from randid import new_uuid
+import time
+import json
+import random
+
+random_game = Blueprint('random', __name__)
+
+@random_game.route('/random', methods = ['POST'])
+def index():
+ data = request.get_json()
+
+ user_id = data.get("user_id") or ""
+ if not user_id:
+ print("a temporary user should be set up here")
+
+ public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall()
+ print(public_games)
+ if len(public_games) == 0:
+ game_id = new_uuid("games")
+
+ cursor.execute("insert into games values (?, NULL, NULL, ?, NULL, NULL, 0, NULL, NULL, NULL, \"wait_for_opponent\", \"default\", FALSE) ", (game_id, user_id))
+ connection.commit()
+ else:
+ game_id = random.choice(public_games)[0]
+ timestamp = int( time.time() * 1000 )
+ cursor.execute("update games set player_2_id = ?, status = \"in_progress\", timestamp = ? where game_id = ?", (user_id, timestamp, game_id))
+ connection.commit()
+
+ return { "id": game_id }, 200