diff --git a/bot/db_utils.py b/bot/db_utils.py index 16e9610..b911b9b 100644 --- a/bot/db_utils.py +++ b/bot/db_utils.py @@ -88,7 +88,9 @@ def get_cards(card_ids: list[int]) -> list[tuple]: query = f'SELECT * FROM cards WHERE id IN ({placeholders})' CURSOR.execute(query, card_ids) - return CURSOR.fetchall() + + res = CURSOR.fetchall() + return res def get_player(username: str) -> int: '''Retrieve a player ID by username, or return None if not found.''' @@ -169,11 +171,11 @@ def take_cards(player_id: int, count: int) -> list[int]: list[int]: A list of card IDs taken from the player. ''' CURSOR.execute( - 'SELECT id FROM pulls WHERE player_id = ? ORDER BY RANDOM() LIMIT ?', + 'SELECT card_id FROM pulls WHERE player_id = ? ORDER BY RANDOM() LIMIT ?', (player_id, count) ) rows = CURSOR.fetchall() - return [row['id'] for row in rows] + return [row['card_id'] for row in rows] def create_duel(duel_request_id: int) -> None: '''Creates a duel from a duel request. @@ -245,37 +247,46 @@ def start_duel(duel_id: int, row: dict, player_1_cards: list[int], player_2_card else: duel_type = 'Casual' - player_1_name = CURSOR.execute( - 'SELECT username FROM players WHERE id = ?', + # Get both the player 1 name and the player 1 server_id + player_1 = CURSOR.execute( + 'SELECT username, server_id FROM players WHERE id = ?', (row['player_1_id'],) - ).fetchone()['username'] - player_2_name = CURSOR.execute( - 'SELECT username FROM players WHERE id = ?', - (row['player_2_id'],) - ).fetchone()['username'] + ).fetchone() + player_1_name = player_1[0] + player_1_server_id = player_1[1] + # Get both the player 2 name and the player 2 server_id + player_2_name = CURSOR.execute( + 'SELECT username, server_id FROM players WHERE id = ?', + (row['player_2_id'],) + ).fetchone() + player_2_name = player_2_name[0] + player_2_server_id = player_2_name[1] + + # Create a thread for the duel thread_id = fediverse_service.create_post( text=f'⚔️ A {duel_type} Duel started between {player_1_name} and {player_2_name}! ROUND 1 has begun!', visibility=Visibility.HOME ) # Update the duel with the thread ID - CURSOR.execute( + """CURSOR.execute( 'UPDATE duels SET thread_id = ? WHERE id = ?', (thread_id, duel_id) - ) + )""" - + print(player_1_cards) # Send the cards to the players # Player 1 # Get the card details for player 1 player_1_cards_details = get_cards(player_1_cards) + print(player_1_cards_details) # Send a notification to player 1 with their cards fediverse_service.create_post( - text=f'{player_1_name}Your cards for the duel. \ + text=f'{player_1_name} Your cards for the duel. \ Reply with select <1,2 or 3> to select a card for the duel.', visibility=Visibility.SPECIFIED, file_ids=[card['file_id'] for card in player_1_cards_details], - #visible_user_ids=player_1_name + visible_user_ids=[player_1_server_id] ) # Player 2 @@ -283,11 +294,11 @@ def start_duel(duel_id: int, row: dict, player_1_cards: list[int], player_2_card player_2_cards_details = get_cards(player_2_cards) # Send a notification to player 2 with their cards fediverse_service.create_post( - text=f'{player_2_name}Your cards for the duel. \ + text=f'{player_2_name} Your cards for the duel. \ Reply with select <1,2 or 3> to select a card for the duel.', visibility=Visibility.SPECIFIED, file_ids=[card['file_id'] for card in player_2_cards_details], - #visible_user_ids=player_2_name + visible_user_ids=[player_2_server_id] )