aboutsummaryrefslogtreecommitdiff
path: root/api/game
diff options
context:
space:
mode:
Diffstat (limited to 'api/game')
-rw-r--r--api/game/new.py2
-rw-r--r--api/game/random.py31
-rw-r--r--api/game/voerbak_connector.py6
3 files changed, 38 insertions, 1 deletions
diff --git a/api/game/new.py b/api/game/new.py
index fd65019..1b63110 100644
--- a/api/game/new.py
+++ b/api/game/new.py
@@ -19,7 +19,7 @@ def index():
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)))
+ cursor.execute("insert into games values (?, NULL, NULL, ?, NULL, NULL, ?, NULL, NULL, NULL, \"wait_for_opponent\", ?, TRUE) ", (game_id, user_id, timestamp, json.dumps(game_settings)))
connection.commit()
return { "id": game_id }, 200
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
diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py
index 5ccaed8..65551ca 100644
--- a/api/game/voerbak_connector.py
+++ b/api/game/voerbak_connector.py
@@ -32,12 +32,17 @@ class bord:
def get_output(self):
return self.process.stdout.readline().decode()[:-1]
+ def kill_voerbak(self):
+ self.process.stdin.write(bytearray("0", "utf-8"))
+ self.process.stdin.flush()
+
def update_board(self):
buffer = self.get_output()
while not buffer.isdigit():
if buffer.startswith("w:"):
self.win_positions.append(buffer[2:].split("-"))
log.info(f"won: {buffer[2:].split('-')}")
+ self.kill_voerbak()
elif buffer.startswith("e:"):
log.warning(buffer[2:])
elif buffer.startswith("m:"):
@@ -45,6 +50,7 @@ class bord:
self.player_1 = True if substr == "true" else False
elif buffer.startswith("d:"):
self.board_full = True
+ self.kill_voerbak()
buffer = self.get_output()
self.board = buffer