From 4a0318ec103f88a46906d064e9dd0b6207394009 Mon Sep 17 00:00:00 2001 From: w Date: Mon, 7 Jul 2025 00:49:59 -0300 Subject: [PATCH] Refactor duel creation: enhance start_duel function and add thread ID to duels table --- bot/db_utils.py | 96 +++++++++++++++++++++++++++----------- migrations/0007_duelv1.sql | 1 + 2 files changed, 71 insertions(+), 26 deletions(-) diff --git a/bot/db_utils.py b/bot/db_utils.py index 3667a93..16e9610 100644 --- a/bot/db_utils.py +++ b/bot/db_utils.py @@ -78,7 +78,7 @@ def get_random_card() -> Card | None: def get_cards(card_ids: list[int]) -> list[tuple]: ''' - Retrieves stats for a list of card IDs. + Retrieves information about cards from the database by their IDs. Returns a list of tuples: (id, name, rarity, file_id, power, charm, wit, ...) ''' if not card_ids: @@ -219,36 +219,80 @@ def create_duel(duel_request_id: int) -> None: duel_id = CURSOR.lastrowid if duel_id: # Start duel - # Initialize the duel by sending a notification to the players - from fediverse_factory import get_fediverse_service - from fediverse_types import Visibility - - fediverse_service = get_fediverse_service(config.INSTANCE_TYPE) + start_duel(duel_id, row, player_1_cards, player_2_cards) - - if row['competitive']==1: - duel_type = 'Competitive' - else: - duel_type = 'Casual' - - player_1_name = CURSOR.execute( - 'SELECT username 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'] - - fediverse_service.create_post( - text=f'⚔️ A {duel_type} Duel started between {player_1_name} and {player_2_name}!', - visibility=Visibility.HOME - ) -##def start_duel(duel_id: int) -> None: +def start_duel(duel_id: int, row: dict, player_1_cards: list[int], player_2_cards: list[int]) -> None: + '''Starts a duel by sending a notification to the players and sending the cards. + Args: + duel_id (int): The ID of the duel. + row (dict): The row containing duel request details. + player_1_cards (list[int]): List of card IDs for player 1. + player_2_cards (list[int]): List of card IDs for player 2. + ''' + # Initialize the duel thread by sending a notification to the players + from fediverse_factory import get_fediverse_service + from fediverse_types import Visibility + + fediverse_service = get_fediverse_service(config.INSTANCE_TYPE) + + + if row['competitive']==1: + duel_type = 'Competitive' + else: + duel_type = 'Casual' + + player_1_name = CURSOR.execute( + 'SELECT username 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'] + + 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( + 'UPDATE duels SET thread_id = ? WHERE id = ?', + (thread_id, duel_id) + ) + + + # Send the cards to the players + # Player 1 + # Get the card details for player 1 + player_1_cards_details = get_cards(player_1_cards) + # Send a notification to player 1 with their cards + fediverse_service.create_post( + 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 + ) + + # Player 2 + # Get the card details for player 2 + 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. \ + 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 + ) + + + + def insert_player(username: str) -> int: '''Insert a new player with default has_rolled = False and return their diff --git a/migrations/0007_duelv1.sql b/migrations/0007_duelv1.sql index be62148..a55d757 100644 --- a/migrations/0007_duelv1.sql +++ b/migrations/0007_duelv1.sql @@ -29,6 +29,7 @@ CREATE TABLE duels ( last_round_dt TEXT, is_finished BOOLEAN NOT NULL DEFAULT 0, points INTEGER NOT NULL DEFAULT 0, + thread_id TEXT DEFAULT NULL, winner_id INTEGER DEFAULT NULL );