Refactor duel creation: enhance start_duel function and add thread ID to duels table

This commit is contained in:
w 2025-07-07 00:49:59 -03:00
parent 59f88b8a4a
commit 4a0318ec10
2 changed files with 71 additions and 26 deletions

View file

@ -78,7 +78,7 @@ def get_random_card() -> Card | None:
def get_cards(card_ids: list[int]) -> list[tuple]: 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, ...) Returns a list of tuples: (id, name, rarity, file_id, power, charm, wit, ...)
''' '''
if not card_ids: if not card_ids:
@ -219,36 +219,80 @@ def create_duel(duel_request_id: int) -> None:
duel_id = CURSOR.lastrowid duel_id = CURSOR.lastrowid
if duel_id: if duel_id:
# Start duel # Start duel
# Initialize the duel by sending a notification to the players start_duel(duel_id, row, player_1_cards, player_2_cards)
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']
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: def insert_player(username: str) -> int:
'''Insert a new player with default has_rolled = False and return their '''Insert a new player with default has_rolled = False and return their

View file

@ -29,6 +29,7 @@ CREATE TABLE duels (
last_round_dt TEXT, last_round_dt TEXT,
is_finished BOOLEAN NOT NULL DEFAULT 0, is_finished BOOLEAN NOT NULL DEFAULT 0,
points INTEGER NOT NULL DEFAULT 0, points INTEGER NOT NULL DEFAULT 0,
thread_id TEXT DEFAULT NULL,
winner_id INTEGER DEFAULT NULL winner_id INTEGER DEFAULT NULL
); );