diff options
Diffstat (limited to 'api/game')
-rw-r--r-- | api/game/accept.py | 2 | ||||
-rw-r--r-- | api/game/cleanup.py | 6 | ||||
-rw-r--r-- | api/game/info.py | 68 | ||||
-rw-r--r-- | api/game/new.py | 20 | ||||
-rw-r--r-- | api/game/random.py | 8 | ||||
-rw-r--r-- | api/game/socket.py | 46 | ||||
-rw-r--r-- | api/game/voerbak_connector.py | 8 |
7 files changed, 79 insertions, 79 deletions
diff --git a/api/game/accept.py b/api/game/accept.py index a231d3a..5a09df2 100644 --- a/api/game/accept.py +++ b/api/game/accept.py @@ -18,7 +18,7 @@ join_game = Blueprint('game_accept', __name__) @auth_required("user") def index(game_id): if cursor.execute("select status from games where game_id = ?", - [game_id]).fetchone()[0] != "wait_for_opponent": + [game_id]).fetchone()[0] != "wait_for_opponent": return "", 403 start_game(game_id, user_id) diff --git a/api/game/cleanup.py b/api/game/cleanup.py index 5b705d2..cc0aab8 100644 --- a/api/game/cleanup.py +++ b/api/game/cleanup.py @@ -7,8 +7,8 @@ import time 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] + "select game_id from games where (status = \"wait_for_opponent\" or status = \"in_progress\") and last_activity < ?", + [now - 5 * 60 * 1e3] ).fetchall() for game_id in old_games: cursor.execute("delete from games where game_id = ?", [game_id[0]]) @@ -16,7 +16,7 @@ def cleanup(): def set_interval( - func, sec + func, sec ): # https://stackoverflow.com/questions/2697039/python-equivalent-of-setinterval def func_wrapper(): set_interval(func, sec) diff --git a/api/game/info.py b/api/game/info.py index 869a19c..71fed2e 100644 --- a/api/game/info.py +++ b/api/game/info.py @@ -8,25 +8,25 @@ from ruleset import resolve_ruleset def format_game(game_id, user_id=None): game = cursor.execute( - "select " + ", ".join( - [ - "game_id", # 0 - "parent_game", # 1 - "moves", # 2 - "player_1_id", # 3 - "player_2_id", # 4 - "outcome", # 5 - "created", # 6 - "started", # 7 - "duration", # 8 - "rating_delta_player_1", # 9 - "rating_delta_player_2", # 10 - "ruleset", # 11 - "status", # 12 - "private", # 13 - ] - ) + " from games where game_id = ?", - [game_id] + "select " + ", ".join( + [ + "game_id", # 0 + "parent_game", # 1 + "moves", # 2 + "player_1_id", # 3 + "player_2_id", # 4 + "outcome", # 5 + "created", # 6 + "started", # 7 + "duration", # 8 + "rating_delta_player_1", # 9 + "rating_delta_player_2", # 10 + "ruleset", # 11 + "status", # 12 + "private", # 13 + ] + ) + " from games where game_id = ?", + [game_id] ).fetchone() is_player_1 = game[4] != user_id @@ -36,30 +36,30 @@ def format_game(game_id, user_id=None): # parse moves into list and return empty list if moves string is empty moves = [] if len(game[2]) == 0 else [ - int(move) for move in str(game[2] + "0").split(",") + int(move) for move in str(game[2] + "0").split(",") ] return { - "id": game[0], - "parent": game[1], - "moves": moves, - "opponent": None if not opponent else format_user(opponent), - "outcome": None if not game[5] else outcome(game[5], is_player_1), - "created": game[6], - "started": game[7], - "duration": game[8], - "rating": game[9] if is_player_1 else game[10], - "rating_opponent": game[10] if is_player_1 else game[9], - "ruleset": resolve_ruleset(game[11]), - "status": game[12], - "private": bool(game[13]), + "id": game[0], + "parent": game[1], + "moves": moves, + "opponent": None if not opponent else format_user(opponent), + "outcome": None if not game[5] else outcome(game[5], is_player_1), + "created": game[6], + "started": game[7], + "duration": game[8], + "rating": game[9] if is_player_1 else game[10], + "rating_opponent": game[10] if is_player_1 else game[9], + "ruleset": resolve_ruleset(game[11]), + "status": game[12], + "private": bool(game[13]), } # check if game_id exists in database def valid_game_id(game_id): query = cursor.execute( - "select game_id from games where game_id = ?", [game_id] + "select game_id from games where game_id = ?", [game_id] ).fetchone() return bool(query) diff --git a/api/game/new.py b/api/game/new.py index 7f0862b..8c936de 100644 --- a/api/game/new.py +++ b/api/game/new.py @@ -13,8 +13,8 @@ def create_game(user_1_id, private=False, user_2_id=None): 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) + "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() @@ -25,25 +25,25 @@ 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] + "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) + "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) + "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] + "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]) diff --git a/api/game/random.py b/api/game/random.py index 2dfbe0b..559c9e5 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -17,7 +17,7 @@ random_game = Blueprint('random', __name__) def index(user_id): # get public_games (random opponent queue) public_games = cursor.execute( - "select game_id from games where private = FALSE and status = \"wait_for_opponent\"" + "select game_id from games where private = FALSE and status = \"wait_for_opponent\"" ).fetchall() game_started = False @@ -36,9 +36,9 @@ def index(user_id): game_started = True return { - "id": game_id, - "player_1": player_1, - "game_started": game_started + "id": game_id, + "player_1": player_1, + "game_started": game_started }, 200 diff --git a/api/game/socket.py b/api/game/socket.py index 63f680c..8bbc951 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -31,8 +31,8 @@ class game: now = int(time.time() * 1000) cursor.execute( - "update games set last_activity = ?, moves = moves || ? || ',' where game_id = ?", - [now, column, self.game_id] + "update games set last_activity = ?, moves = moves || ? || ',' where game_id = ?", + [now, column, self.game_id] ) connection.commit() @@ -42,11 +42,11 @@ class game: winner = self.board.board[int(self.board.win_positions[0][0])] outcome = "w" if winner == "2" else "l" io.emit( - "finish", { - "winPositions": self.board.win_positions, - "boardFull": self.board.board_full - }, - room=self.room + "finish", { + "winPositions": self.board.win_positions, + "boardFull": self.board.board_full + }, + room=self.room ) self.close("finished", outcome) return @@ -60,18 +60,18 @@ class game: def close(self, new_status, outcome): cursor.execute( - " ".join( - [ - "update games set", "moves = moves || '0',", - "duration = ?,", "status = ?,", "outcome = ?", - "where game_id = ?" - ] - ), [ - int(time.time() * 1000) - cursor.execute( - "select started from games where game_id = ?", - [self.game_id] - ).fetchone()[0], new_status, outcome, self.game_id - ] + " ".join( + [ + "update games set", "moves = moves || '0',", + "duration = ?,", "status = ?,", "outcome = ?", + "where game_id = ?" + ] + ), [ + int(time.time() * 1000) - cursor.execute( + "select started from games where game_id = ?", + [self.game_id] + ).fetchone()[0], new_status, outcome, self.game_id + ] ) connection.commit() @@ -81,8 +81,8 @@ class game: @io.on("newMove") def new_move(data): if not data["game_id"] or \ - not data["move"] or \ - not data["token"]: + not data["move"] or \ + not data["token"]: return if not data["game_id"] in games: return @@ -95,7 +95,7 @@ def new_move(data): @io.on("resign") def resign(data): if not data["game_id"] or \ - not request.cookies.get("token"): + not request.cookies.get("token"): return if not data["game_id"] in games: return @@ -103,7 +103,7 @@ def resign(data): if not user_id: return if games[data["game_id"]].player_1_id != user_id and \ - games[data["game_id"]].player_2_id != user_id: + games[data["game_id"]].player_2_id != user_id: return games[data["game_id"]].resign() diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py index 412a512..0b51bd5 100644 --- a/api/game/voerbak_connector.py +++ b/api/game/voerbak_connector.py @@ -21,10 +21,10 @@ class bord: self.board_full = False self.win_positions = [] self.process = subprocess.Popen( - [VOERBAK_LOCATION, f"-w {w}", f"-h {h}"], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=None + [VOERBAK_LOCATION, f"-w {w}", f"-h {h}"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=None ) self.process.stdin.flush() |