From 69a9ecfe7a7f5de1c59a066a83dee22d0788e24a Mon Sep 17 00:00:00 2001
From: lonkaars <l.leblansch@gmail.com>
Date: Mon, 22 Mar 2021 09:19:14 +0100
Subject: generic create_game and start_game functions

---
 api/game/new.py    | 32 ++++++++++++++++++++++++++++++++
 api/game/random.py | 13 ++++++-------
 2 files changed, 38 insertions(+), 7 deletions(-)
 create mode 100644 api/game/new.py

(limited to 'api')

diff --git a/api/game/new.py b/api/game/new.py
new file mode 100644
index 0000000..80d0cae
--- /dev/null
+++ b/api/game/new.py
@@ -0,0 +1,32 @@
+import time
+from db import cursor, connection
+from socket_io import io
+from randid import new_uuid
+from game.socket import games, game
+
+def create_game(user_1_id, private = False, user_2_id = None):
+    timestamp = int( time.time() * 1000 )
+
+    game_id = new_uuid("games")
+
+    cursor.execute("insert into games values (?, NULL, \"\", ?, ?, NULL, ?, NULL, ?, NULL, NULL, NULL, \"wait_for_opponent\", \"default\", ?, FALSE) ", (game_id, user_1_id, user_2_id, timestamp, timestamp, private))
+    connection.commit()
+
+    return game_id
+
+def start_game(game_id, user_2_id):
+    timestamp = int( time.time() * 1000 )
+
+    db_game = cursor.execute("select player_2_id, status, private from games where game_id = ?", [game_id]).fetchone()
+    if db_game[1] != "wait_for_opponent": return False
+
+    if db_game[0] == None: cursor.execute("update games set player_2_id = ? where game_id = ?", (user_2_id, game_id))
+    cursor.execute("update games set status = \"in_progress\", started = ?, last_activity = ? where game_id = ?", (timestamp, timestamp, game_id))
+    connection.commit()
+
+    players = cursor.execute("select player_1_id, player_2_id from games where game_id = ?", [game_id]).fetchone()
+    games[game_id] = game(game_id, io, players[0], players[1])
+
+    io.emit("gameStart", room=games[game_id].room)
+
+
diff --git a/api/game/random.py b/api/game/random.py
index 54ab2a8..ae7dc95 100644
--- a/api/game/random.py
+++ b/api/game/random.py
@@ -4,8 +4,10 @@ from randid import new_uuid
 import time
 import json
 import random
-from game.socket import io, game, games
+from game.socket import game, games
+from game.new import create_game, start_game
 from auth.login_token import token_login
+from socket_io import io
 
 random_game = Blueprint('random', __name__)
 
@@ -28,15 +30,12 @@ def index():
     game_started = False
 
     if len(public_games) == 0:
-        game_id = new_uuid("games")
-
-        cursor.execute("insert into games values (?, NULL, \"\", ?, NULL, NULL, ?, NULL, ?, NULL, NULL, NULL, \"wait_for_opponent\", \"default\", FALSE, FALSE) ", (game_id, user_id, timestamp, timestamp))
-        connection.commit()
+        game_id = create_game(user_id)
         player_1 = True
     else:
         game_id = random.choice(public_games)[0]
-        cursor.execute("update games set player_2_id = ?, status = \"in_progress\", started = ?, last_activity = ? where game_id = ?", (user_id, timestamp, timestamp, game_id))
-        connection.commit()
+
+        start_game(game_id, user_id)
 
         players = cursor.execute("select player_1_id, player_2_id from games where game_id = ?", [game_id]).fetchone()
         games[game_id] = game(game_id, io, players[0], players[1])
-- 
cgit v1.2.3