Compare commits

...

6 commits

3 changed files with 197 additions and 11 deletions

View file

@ -101,7 +101,7 @@ def get_player(username: str) -> int:
return int(player[0]) return int(player[0])
return 0 return 0
def insert_duel_request(username: str, target: str, duel_type: str) -> None: def insert_duel_request(username: str, target: str, duel_type: str) -> int:
'''Inserts a duel request into the database. '''Inserts a duel request into the database.
Args: Args:
@ -111,18 +111,144 @@ def insert_duel_request(username: str, target: str, duel_type: str) -> None:
''' '''
# get the player ids
player_1_1d = get_player(username) player_1_1d = get_player(username)
player_2_id = get_player(target) player_2_id = get_player(target)
# picks a random attacker
attacker_id = choices([player_1_1d, player_2_id], k=1)[0]
# sets the last round date to the current time competitive = duel_type
last_round_dt = datetime.now().isoformat()
# insert the duel request and get the duel ID
CURSOR.execute(
'INSERT INTO duel_requests (player_1_id, player_2_id, competitive) VALUES (?, ?, ?)',
(player_1_1d, player_2_id, competitive)
)
duel_id = CURSOR.lastrowid
return duel_id
def get_duel_request(duel_request_id: int) -> dict | None:
'''Retrieves a duel request by its ID.
Args:
duel_id (int): The ID of the duel request.
Returns:
dict: A dictionary containing the duel request details, or None if not found.
'''
CURSOR.execute(
'SELECT * FROM duel_requests WHERE duel_request_id = ?',
(duel_request_id,)
)
row = CURSOR.fetchone()
if row:
return dict(row)
return None
def accept_duel_request(duel_request_id: int) -> bool:
''' Sets up the duel request to an actual duel.
Args:
duel_request_id (int): The ID of the duel request to begin.
'''
create_duel(duel_request_id)
CURSOR.execute( CURSOR.execute(
'INSERT INTO duels (player_1_id, player_2_id, attacker_id,last_round_dt) VALUES (?, ?, ?, ?)', 'UPDATE duel_requests SET accepted = 1 WHERE duel_request_id = ?',
(duel_request_id,)
) )
def take_cards(player_id: int, count: int) -> list[int]:
'''Takes a specified number of random cards from a player.
Args:
player_id (int): The ID of the player to take cards from.
count (int): The number of cards to take.
Returns:
list[int]: A list of card IDs taken from the player.
'''
CURSOR.execute(
'SELECT id FROM pulls WHERE player_id = ? ORDER BY RANDOM() LIMIT ?',
(player_id, count)
)
rows = CURSOR.fetchall()
return [row['id'] for row in rows]
def create_duel(duel_request_id: int) -> None:
'''Creates a duel from a duel request.
Args:
duel_request_id (int): The ID of the duel request to create a duel from.
'''
CURSOR.execute(
'SELECT player_1_id, player_2_id, competitive FROM duel_requests WHERE duel_request_id = ?',
(duel_request_id,)
)
row = CURSOR.fetchone()
# Select a random player to set as attacker
attacker_id = choices([row['player_1_id'], row['player_2_id']])[0]
# Set the last round date to now
last_round_dt = datetime.now().isoformat()
# take 3 random cards from each player
player_1_cards = take_cards(row['player_1_id'], 3)
player_2_cards = take_cards(row['player_2_id'], 3)
# Insert the duel into the database, only players ids, attacker id, cards, last round date and competitive flag
CURSOR.execute(
'''
INSERT INTO duels (player_1_id, player_2_id, attacker_id, player_1_cards, player_2_cards, last_round_dt, is_finished, competitive)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''',
(
row['player_1_id'],
row['player_2_id'],
attacker_id,
','.join(map(str, player_1_cards)),
','.join(map(str, player_2_cards)),
last_round_dt,
False,
row['competitive']
)
)
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)
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 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

@ -181,6 +181,8 @@ def do_help(author: str) -> BotResponse:
'attachment_urls': None 'attachment_urls': None
} }
# Dueling
def duel_request(author: str, args: list[str]) -> BotResponse: def duel_request(author: str, args: list[str]) -> BotResponse:
'''Sends a duel request to another user.''' '''Sends a duel request to another user.'''
if len(args) == 0: if len(args) == 0:
@ -202,17 +204,62 @@ def duel_request(author: str, args: list[str]) -> BotResponse:
Try challenging someone else.', Try challenging someone else.',
'attachment_urls': None 'attachment_urls': None
} }
# Check if both the users have enough cards to duel
# TODO
duel_type = 'casual' duel_type = 'casual'
if len(args) == 1 and args[0] == "competitive": if len(args) == 1 and args[0] == "competitive":
duel_type = 'competitive' duel_type = 'competitive'
db.insert_duel_request(author, target, duel_type) duel_id = db.insert_duel_request(author, target, duel_type)
return { return {
'message': f'{target} You have been challenged to a {duel_type} duel by \ 'message': f'{target} You have been challenged to a {duel_type} duel by {author}! \
{author}! Reply with `accept_duel` to accept the challenge.', Reply with `accept_duel {duel_id}` to accept the challenge. Duel ID:{duel_id}.',
'attachment_urls': None 'attachment_urls': None
} }
def accept_duel(author: str, args: list[str]) -> BotResponse:
'''Accepts a duel request with an id.'''
if len(args) == 0:
return {
'message': f'{author} Please specify a duel ID.',
'attachment_urls': None
}
duel_request_id = args[0]
duel_request = db.get_duel_request(duel_request_id)
if not duel_request:
return {
'message': f'{author} Duel request with ID {duel_request_id} does not exist.',
'attachment_urls': None
}
# Check if the author is the target of the duel request
if duel_request['player_2_id'] != db.get_player(author):
return {
'message': f'{author} You cannot accept this duel request. It is not \
addressed to you.',
'attachment_urls': None
}
# Check if the duel was already accepted
if duel_request['accepted']:
return {
'message': f'{author} This duel request has already been accepted.',
'attachment_urls': None
}
# Accept the duel request
db.accept_duel_request(duel_request_id)
return {
'message': f'{author} You have accepted the duel request with ID {duel_request_id}. \
The duel will shortly begin.',
'attachment_urls': None
}
def delete_account(author: str) -> BotResponse: def delete_account(author: str) -> BotResponse:
return { return {
'message': f'{author} ⚠️ This will permanently delete your account \ 'message': f'{author} ⚠️ This will permanently delete your account \
@ -361,6 +408,11 @@ def generate_response(notification: ParsedNotification) -> BotResponse | None:
author, author,
notification['arguments'] notification['arguments']
) )
case 'accept_duel':
res = accept_duel(
author,
notification['arguments']
)
case _: case _:
pass pass

View file

@ -29,6 +29,14 @@ 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,
winner_id INTEGER DEFAULT NULL, winner_id INTEGER DEFAULT NULL
accepted BOOLEAN NOT NULL DEFAULT 0 );
CREATE TABLE duel_requests (
duel_request_id INTEGER PRIMARY KEY AUTOINCREMENT,
player_1_id INTEGER NOT NULL,
player_2_id INTEGER NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
accepted BOOLEAN NOT NULL DEFAULT 0,
competitive BOOLEAN NOT NULL DEFAULT 0
); );