diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-04-16 16:57:26 +0200 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-04-16 16:57:26 +0200 |
commit | 07c2b124e4348b15f1e5ec18c6cdfd77248c6bc8 (patch) | |
tree | e4a29123d3ebedc1d25500390c904c66b3b02489 /api/game | |
parent | aa2c999702dadba2afbcf2be9f597f890aafcc87 (diff) |
spaces > tabs in python :(
Diffstat (limited to 'api/game')
-rw-r--r-- | api/game/accept.py | 12 | ||||
-rw-r--r-- | api/game/cleanup.py | 30 | ||||
-rw-r--r-- | api/game/info.py | 90 | ||||
-rw-r--r-- | api/game/new.py | 76 | ||||
-rw-r--r-- | api/game/random.py | 50 | ||||
-rw-r--r-- | api/game/socket.py | 174 | ||||
-rw-r--r-- | api/game/voerbak_connector.py | 132 |
7 files changed, 282 insertions, 282 deletions
diff --git a/api/game/accept.py b/api/game/accept.py index 3510ffd..e0681c8 100644 --- a/api/game/accept.py +++ b/api/game/accept.py @@ -16,14 +16,14 @@ join_game = Blueprint('game_accept', __name__) @join_game.route('/accept', methods=['POST']) @game_id_with_viewer def index(game_id, user_id): - if not user_id: return "", 400 - if cursor.execute("select status from games where game_id = ?", - [game_id]).fetchone()[0] != "wait_for_opponent": - return "", 403 + if not user_id: return "", 400 + if cursor.execute("select status from games where game_id = ?", + [game_id]).fetchone()[0] != "wait_for_opponent": + return "", 403 - start_game(game_id, user_id) + start_game(game_id, user_id) - return {"id": game_id, "player_1": False, "game_started": True}, 200 + return {"id": game_id, "player_1": False, "game_started": True}, 200 dynamic_route = ["/game", join_game] diff --git a/api/game/cleanup.py b/api/game/cleanup.py index cc0aab8..8f7266d 100644 --- a/api/game/cleanup.py +++ b/api/game/cleanup.py @@ -5,26 +5,26 @@ import time # cleanup function that's ran every five minutes 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] - ).fetchall() - for game_id in old_games: - cursor.execute("delete from games where game_id = ?", [game_id[0]]) - connection.commit() + 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] + ).fetchall() + for game_id in old_games: + cursor.execute("delete from games where game_id = ?", [game_id[0]]) + connection.commit() def set_interval( - func, sec + func, sec ): # https://stackoverflow.com/questions/2697039/python-equivalent-of-setinterval - def func_wrapper(): - set_interval(func, sec) - func() + def func_wrapper(): + set_interval(func, sec) + func() - t = threading.Timer(sec, func_wrapper) - t.start() - return t + t = threading.Timer(sec, func_wrapper) + t.start() + return t # run every five minutes diff --git a/api/game/info.py b/api/game/info.py index b150dbc..fa23616 100644 --- a/api/game/info.py +++ b/api/game/info.py @@ -9,54 +9,54 @@ import valid 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] - ).fetchone() + 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] + ).fetchone() - is_player_1 = game[4] != user_id + is_player_1 = game[4] != user_id - # get opponent from perspective of `user_id` - #TODO: return .players as array of player_1 and player_2 but format_user()'d - opponent = game[4] if is_player_1 else game[3] + # get opponent from perspective of `user_id` + #TODO: return .players as array of player_1 and player_2 but format_user()'d + opponent = game[4] if is_player_1 else game[3] - # 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(",") - ] + # 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(",") + ] - 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]), - } + 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]), + } game_info = Blueprint('game_info', __name__) @@ -65,7 +65,7 @@ game_info = Blueprint('game_info', __name__) @game_info.route('/info', methods=['POST']) @game_id_with_viewer def index(game_id, viewer): - return format_game(game_id, viewer), 200 + return format_game(game_id, viewer), 200 dynamic_route = ["/game", game_info] diff --git a/api/game/new.py b/api/game/new.py index 8c936de..edc7f52 100644 --- a/api/game/new.py +++ b/api/game/new.py @@ -8,46 +8,46 @@ from hierarchy import auth_required def create_game(user_1_id, private=False, user_2_id=None): - timestamp = int(time.time() * 1000) + timestamp = int(time.time() * 1000) - game_id = new_uuid("games") + 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() + 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 + 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) + 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) new_game = Blueprint('new_game', __name__) @@ -56,10 +56,10 @@ new_game = Blueprint('new_game', __name__) @new_game.route('/new', methods=["GET", "POST"]) @auth_required("user") def index(user_id): - # create a new private game (join by link) - #TODO: friend invites + notifications - game_id = create_game(user_id, True) - return {"id": game_id}, 200 + # create a new private game (join by link) + #TODO: friend invites + notifications + game_id = create_game(user_id, True) + return {"id": game_id}, 200 dynamic_route = ["/game", new_game] diff --git a/api/game/random.py b/api/game/random.py index 559c9e5..aa5383c 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -15,31 +15,31 @@ random_game = Blueprint('random', __name__) @random_game.route('/random') @auth_required("user") 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\"" - ).fetchall() - - game_started = False - - # create a new public game if the queue is empty - if len(public_games) == 0: - game_id = create_game(user_id) - player_1 = True - # otherwise join a random public game - else: - game_id = random.choice(public_games)[0] - - start_game(game_id, user_id) - - player_1 = False - game_started = True - - return { - "id": game_id, - "player_1": player_1, - "game_started": game_started - }, 200 + # get public_games (random opponent queue) + public_games = cursor.execute( + "select game_id from games where private = FALSE and status = \"wait_for_opponent\"" + ).fetchall() + + game_started = False + + # create a new public game if the queue is empty + if len(public_games) == 0: + game_id = create_game(user_id) + player_1 = True + # otherwise join a random public game + else: + game_id = random.choice(public_games)[0] + + start_game(game_id, user_id) + + player_1 = False + game_started = True + + return { + "id": game_id, + "player_1": player_1, + "game_started": game_started + }, 200 dynamic_route = ["/game", random_game] diff --git a/api/game/socket.py b/api/game/socket.py index ca28346..587ad7d 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -11,121 +11,121 @@ games = {} def participants_only(func): - ''' + ''' listener should have two parameters: listener(data: socket.io.data, user_id: str, game: game) listener should only be executed if the request comes from one of the game participants (player_1_id | player_2_id) ''' - def wrapper(data, user_id): - game_id = data["game_id"] + def wrapper(data, user_id): + game_id = data["game_id"] - if not game_id or \ - not game_id in games: - return + if not game_id or \ + not game_id in games: + return - game = games[game_id] - if game.player_1_id != user_id and \ - game.player_2_id != user_id: - return + game = games[game_id] + if game.player_1_id != user_id and \ + game.player_2_id != user_id: + return - return func(data, user_id, game) + return func(data, user_id, game) - wrapper.__name__ = func.__name__ - return wrapper + wrapper.__name__ = func.__name__ + return wrapper class game: - def __init__(self, game_id, io, player_1_id, player_2_id): - self.game_id = game_id - self.room = "game-" + game_id - self.board = bord(7, 6) - self.io = io - self.player_1_id = player_1_id - self.player_2_id = player_2_id - - # drop a disc in `column` - def move(self, user_id, column): - if len(self.board.win_positions) > 0: return - if self.board.board_full: return - - move = self.player_1_id if self.board.player_1 else self.player_2_id - if user_id != move: return - - self.board.drop_fisje(column) - - io.emit("fieldUpdate", {"field": self.board.board}, room=self.room) - - 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 len(self.board.win_positions) > 0 or self.board.board_full: - outcome = "d" - if not self.board.board_full: - 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 - ) - self.close("finished", outcome) - return - - io.emit("turnUpdate", {"player1": self.board.player_1}, room=self.room) - - def resign(self): - self.board.kill_voerbak() - io.emit("resign", room=self.room) - self.close("resign", "d") - - 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 - ] - ) - connection.commit() - - games.pop(self.game_id) + def __init__(self, game_id, io, player_1_id, player_2_id): + self.game_id = game_id + self.room = "game-" + game_id + self.board = bord(7, 6) + self.io = io + self.player_1_id = player_1_id + self.player_2_id = player_2_id + + # drop a disc in `column` + def move(self, user_id, column): + if len(self.board.win_positions) > 0: return + if self.board.board_full: return + + move = self.player_1_id if self.board.player_1 else self.player_2_id + if user_id != move: return + + self.board.drop_fisje(column) + + io.emit("fieldUpdate", {"field": self.board.board}, room=self.room) + + 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 len(self.board.win_positions) > 0 or self.board.board_full: + outcome = "d" + if not self.board.board_full: + 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 + ) + self.close("finished", outcome) + return + + io.emit("turnUpdate", {"player1": self.board.player_1}, room=self.room) + + def resign(self): + self.board.kill_voerbak() + io.emit("resign", room=self.room) + self.close("resign", "d") + + 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 + ] + ) + connection.commit() + + games.pop(self.game_id) @io.on("newMove") @io_auth_required("none") @participants_only def new_move(data, user_id, game): - move = data.get("move") - if not move: return + move = data.get("move") + if not move: return - game.move(user_id, move) + game.move(user_id, move) @io.on("resign") @io_auth_required("none") @participants_only def resign(data, user_id, game): - game.resign() + game.resign() @io.on("registerGameListener") def register_game_listener(data): - game_id = data.get("game_id") - if not game_id: return + game_id = data.get("game_id") + if not game_id: return - join_room("game-" + game_id) + join_room("game-" + game_id) diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py index 0b51bd5..54f5fa6 100644 --- a/api/game/voerbak_connector.py +++ b/api/game/voerbak_connector.py @@ -13,81 +13,81 @@ if os.name == "nt": VOERBAK_LOCATION += ".exe" class bord: - def __init__(self, w, h): - self.width = w - self.height = h - self.player_1 = True - self.board = "0" * (w * h) - 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 - ) - self.process.stdin.flush() + def __init__(self, w, h): + self.width = w + self.height = h + self.player_1 = True + self.board = "0" * (w * h) + 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 + ) + self.process.stdin.flush() - # get output from voerbak without trailing newline character (this might break on windows because crlf) - def get_output(self): - return self.process.stdout.readline().decode()[:-1] + # get output from voerbak without trailing newline character (this might break on windows because crlf) + def get_output(self): + return self.process.stdout.readline().decode()[:-1] - def kill_voerbak(self): - self.process.stdin.write(bytearray("0", "utf-8")) - self.process.stdin.flush() + def kill_voerbak(self): + self.process.stdin.write(bytearray("0", "utf-8")) + self.process.stdin.flush() - # read messages from voerbak - def update_board(self): - buffer = self.get_output() - while not buffer.isdigit(): - # win message - if buffer.startswith("w:"): - self.win_positions.append(buffer[2:].split("-")) - log.info(f"won: {buffer[2:].split('-')}") - self.kill_voerbak() - # error message - elif buffer.startswith("e:"): - log.warning(buffer[2:]) - # turn update message - elif buffer.startswith("m:"): - substr = buffer[2:] - self.player_1 = True if substr == "true" else False - # draw game message - elif buffer.startswith("d:"): - self.board_full = True - self.kill_voerbak() - buffer = self.get_output() - self.board = buffer + # read messages from voerbak + def update_board(self): + buffer = self.get_output() + while not buffer.isdigit(): + # win message + if buffer.startswith("w:"): + self.win_positions.append(buffer[2:].split("-")) + log.info(f"won: {buffer[2:].split('-')}") + self.kill_voerbak() + # error message + elif buffer.startswith("e:"): + log.warning(buffer[2:]) + # turn update message + elif buffer.startswith("m:"): + substr = buffer[2:] + self.player_1 = True if substr == "true" else False + # draw game message + elif buffer.startswith("d:"): + self.board_full = True + self.kill_voerbak() + buffer = self.get_output() + self.board = buffer - # debug board print function - def print(self): - for y in range(self.height - 1, -1, -1): - for x in range(self.width): - state = self.board[x + y * self.width] - char = [EMPTY, DISC_A, DISC_B] - print(char[int(state)], end=" ") - print("\n", end="") + # debug board print function + def print(self): + for y in range(self.height - 1, -1, -1): + for x in range(self.width): + state = self.board[x + y * self.width] + char = [EMPTY, DISC_A, DISC_B] + print(char[int(state)], end=" ") + print("\n", end="") - def drop_fisje(self, column): - self.process.stdin.write(bytearray(f"{column}\n", "utf-8")) - self.process.stdin.flush() - self.update_board() + def drop_fisje(self, column): + self.process.stdin.write(bytearray(f"{column}\n", "utf-8")) + self.process.stdin.flush() + self.update_board() # debug game def main(): - gert = bord(7, 6) - while True: - print(gert.player_1) - if len(gert.win_positions) > 0: - print(f"won: {gert.win_positions}") - exit(0) - gert.print() - column = int(input("column?: ")) - 1 - if column not in range(gert.width): - continue - gert.drop_fisje(column + 1) + gert = bord(7, 6) + while True: + print(gert.player_1) + if len(gert.win_positions) > 0: + print(f"won: {gert.win_positions}") + exit(0) + gert.print() + column = int(input("column?: ")) - 1 + if column not in range(gert.width): + continue + gert.drop_fisje(column + 1) if __name__ == "__main__": - main() + main() |