WIP: dueling_v1 #64
2 changed files with 131 additions and 17 deletions
103
bot/db_utils.py
103
bot/db_utils.py
|
@ -125,9 +125,110 @@ def insert_duel_request(username: str, target: str, duel_type: str) -> int:
|
||||||
(player_1_1d, player_2_id, competitive)
|
(player_1_1d, player_2_id, competitive)
|
||||||
)
|
)
|
||||||
duel_id = CURSOR.lastrowid
|
duel_id = CURSOR.lastrowid
|
||||||
|
|
||||||
return duel_id
|
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(
|
||||||
|
'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)
|
||||||
|
if not player_1_cards or not player_2_cards:
|
||||||
|
raise ValueError("One of the players has no cards to duel with.")
|
||||||
|
|
||||||
|
# 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
|
||||||
|
#start_duel(duel_id)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##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
|
||||||
player ID.'''
|
player ID.'''
|
||||||
|
|
|
@ -217,31 +217,44 @@ def duel_request(author: str, args: list[str]) -> BotResponse:
|
||||||
}
|
}
|
||||||
|
|
||||||
def accept_duel(author: str, args: list[str]) -> BotResponse:
|
def accept_duel(author: str, args: list[str]) -> BotResponse:
|
||||||
'''Accepts a duel request from another user.'''
|
'''Accepts a duel request with an id.'''
|
||||||
if len(args) == 0:
|
if len(args) == 0:
|
||||||
return {
|
return {
|
||||||
'message': f'{author} Please specify a user to accept the duel from.',
|
'message': f'{author} Please specify a duel ID.',
|
||||||
'attachment_urls': None
|
'attachment_urls': None
|
||||||
}
|
}
|
||||||
|
|
||||||
target = args[0]
|
duel_request_id = args[0]
|
||||||
if not db.get_player(target):
|
duel_request = db.get_duel_request(duel_request_id)
|
||||||
|
if not duel_request:
|
||||||
return {
|
return {
|
||||||
'message': f'{author} User {target} does not exist or \
|
'message': f'{author} Duel request with ID {duel_request_id} does not exist.',
|
||||||
has not signed up, please sign up before accepting a duel.',
|
|
||||||
'attachment_urls': None
|
'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
|
||||||
|
}
|
||||||
|
|
||||||
if db.accept_duel_request(author, target):
|
|
||||||
return {
|
|
||||||
'message': f'{author} You have accepted the duel request from {target}.',
|
|
||||||
'attachment_urls': None
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
return {
|
|
||||||
'message': f'{author} No duel request found from {target}.',
|
|
||||||
'attachment_urls': None
|
|
||||||
}
|
|
||||||
|
|
||||||
def delete_account(author: str) -> BotResponse:
|
def delete_account(author: str) -> BotResponse:
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Add table
Reference in a new issue