Compare commits

...

3 commits

3 changed files with 82 additions and 19 deletions

View file

@ -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. \
Reply with select <1,2 or 3> to select a card for the duel.',
text=f'{player_2_name} Your cards for the duel. You are defending!. \
Select a card with <1,2 or 3> and a stat with <1,2 or 3> for Power, Charm and Wit. Example: select 2 3 would select the second card and use its Wit stat.',
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]
)

View file

@ -257,8 +257,53 @@ def accept_duel(author: str, args: list[str]) -> BotResponse:
'attachment_urls': None
}
def update_duel(author: str, args: list[str]) -> BotResponse:
'''Updates the duel with the selected card and stat.'''
if len(args) != 2:
return {
'message': f'{author} Please specify a card number and a stat number.',
'attachment_urls': None
}
if not all(is_float(arg) for arg in args):
return {
'message': f'{author} Invalid arguments: both card number and stat number must be numbers.',
'attachment_urls': None
}
card_number = int(args[0]) - 1 # Convert to zero-based index
stat_number = int(args[1]) - 1 # Convert to zero-based index
duel = db.get_duel_by_player(author)
if not duel:
return {
'message': f'{author} You are not currently in a duel.',
'attachment_urls': None
}
player_id = db.get_player(author)
player_cards = db.get_player_cards(player_id)
if card_number < 0 or card_number >= len(player_cards):
return {
'message': f'{author} Invalid card number: {card_number + 1}.',
'attachment_urls': None
}
selected_card = player_cards[card_number]
if stat_number < 0 or stat_number >= len(selected_card['stats']):
return {
'message': f'{author} Invalid stat number: {stat_number + 1}.',
'attachment_urls': None
}
db.update_duel_selection(duel['id'], player_id, selected_card['id'], stat_number)
return {
'message': f'{author} You have selected {selected_card["name"]} with \
{selected_card["stats"][stat_number]} in {["Power", "Charm", "Wit"][stat_number]} for the duel.',
'attachment_urls': None]
}
def delete_account(author: str) -> BotResponse:
return {
@ -413,6 +458,11 @@ def generate_response(notification: ParsedNotification) -> BotResponse | None:
author,
notification['arguments']
)
case 'select':
res = update_duel(
author,
notification['arguments']
)
case _:
pass

View file

@ -41,3 +41,5 @@ CREATE TABLE duel_requests (
accepted BOOLEAN NOT NULL DEFAULT 0,
competitive BOOLEAN NOT NULL DEFAULT 0
);
ALTER TABLE players ADD COLUMN server_id text NOT NULL DEFAULT '0';