diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-22 09:19:14 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-22 09:19:14 +0100 |
commit | 69a9ecfe7a7f5de1c59a066a83dee22d0788e24a (patch) | |
tree | 67b6b22a66086538d2febcb8e38751163fc341bf /api/game/new.py | |
parent | 2b56ef7c541f04d01d4ec60f1f6eb9f4a007046a (diff) |
generic create_game and start_game functions
Diffstat (limited to 'api/game/new.py')
-rw-r--r-- | api/game/new.py | 32 |
1 files changed, 32 insertions, 0 deletions
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) + + |