diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-02-22 12:48:38 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-02-22 12:48:38 +0100 |
commit | 32aaaf92f2c06214abdc93e3cede07e067df6b88 (patch) | |
tree | 1b98d9e7605278311aa76880614ba17361f69285 /api/game | |
parent | ae4e74ccde82388cca5e874539bb132e23fc1e92 (diff) |
added columns to games + cleanup function
Diffstat (limited to 'api/game')
-rw-r--r-- | api/game/cleanup.py | 17 | ||||
-rw-r--r-- | api/game/new.py | 25 | ||||
-rw-r--r-- | api/game/random.py | 7 | ||||
-rw-r--r-- | api/game/socket.py | 3 |
4 files changed, 23 insertions, 29 deletions
diff --git a/api/game/cleanup.py b/api/game/cleanup.py new file mode 100644 index 0000000..2b09142 --- /dev/null +++ b/api/game/cleanup.py @@ -0,0 +1,17 @@ +from db import cursor, connection +import threading + +def cleanup(): + now = int( time.time() * 1000 ) + old_games = cursor.execute("select game_id from games where (status = \"wait_for_opponent\" or status = \"in_progress\") and last_activity < ?", [now - 5 * 60 * 1e3]) + print(old_games) + +def set_interval(func, sec): # https://stackoverflow.com/questions/2697039/python-equivalent-of-setinterval + def func_wrapper(): + set_interval(func, sec) + func() + t = threading.Timer(sec, func_wrapper) + t.start() + return t + +set_interval(cleanup, 5 * 60) diff --git a/api/game/new.py b/api/game/new.py deleted file mode 100644 index 1b63110..0000000 --- a/api/game/new.py +++ /dev/null @@ -1,25 +0,0 @@ -from flask import Blueprint, request, make_response -from db 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\", ?, 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 index 52a6d1f..ab4323d 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -23,16 +23,17 @@ def index(): public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall() + timestamp = int( time.time() * 1000 ) + if len(public_games) == 0: game_id = new_uuid("games") - cursor.execute("insert into games values (?, NULL, \"\", ?, NULL, NULL, 0, NULL, NULL, NULL, \"wait_for_opponent\", \"default\", FALSE) ", (game_id, user_id)) + 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() player_1 = True 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)) + 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() players = cursor.execute("select player_1_id, player_2_id from games where game_id = ?", [game_id]).fetchone() diff --git a/api/game/socket.py b/api/game/socket.py index f1a591e..4d6c0de 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -31,7 +31,8 @@ class game: "boardFull": self.board.board_full }) - cursor.execute("update games set moves = moves || ? || ',' where game_id = ?", [column, self.game_id]) + now = int( time.time() * 1000 ) + cursor.execute("update games set last_activity = ?, moves = moves || ? || ',' where game_id = ?", [now, column, self.game_id]) connection.commit() if self.board.board_full: |