diff options
-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 | ||||
-rw-r--r-- | api/main.py | 4 | ||||
-rw-r--r-- | database/init_db.sql | 5 |
6 files changed, 29 insertions, 32 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: diff --git a/api/main.py b/api/main.py index 15ead1e..e371ed2 100644 --- a/api/main.py +++ b/api/main.py @@ -5,7 +5,7 @@ from user.info import info from auth.signup import signup from auth.login import login from auth.login_token import token -from game.new import new_game +# from game.new import new_game from game.random import random_game app.register_blueprint(status, url_prefix='/') @@ -16,7 +16,7 @@ app.register_blueprint(signup, url_prefix='/auth') app.register_blueprint(login, url_prefix='/auth') app.register_blueprint(token, url_prefix='/auth') -app.register_blueprint(new_game, url_prefix='/game') +# app.register_blueprint(new_game, url_prefix='/game') app.register_blueprint(random_game, url_prefix='/game') from socket_io import io diff --git a/database/init_db.sql b/database/init_db.sql index 5b9e0a7..2ce8a1e 100644 --- a/database/init_db.sql +++ b/database/init_db.sql @@ -23,13 +23,16 @@ create table if not exists games ( player_1_id text not null, player_2_id text, outcome text, - timestamp integer, + created integer, + started integer, + last_activity integer, duration integer, rating_delta_player_1 integer, rating_delta_player_2 integer, status text not null, ruleset text not null, private boolean not null, + debug boolean not null, foreign key(player_1_id) references users(user_id), foreign key(player_2_id) references users(user_id) ); |