From fc335d45378d5f5758770837750c1c9325cf3fb1 Mon Sep 17 00:00:00 2001 From: w Date: Fri, 4 Jul 2025 00:24:34 -0300 Subject: [PATCH] Add duel request functionality and update duel table schema --- bot/db_utils.py | 23 +++++++++++++++++++++++ bot/response.py | 6 ++++++ migrations/0007_duelv1.sql | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/bot/db_utils.py b/bot/db_utils.py index 45cac82..491c1ec 100644 --- a/bot/db_utils.py +++ b/bot/db_utils.py @@ -18,6 +18,7 @@ from random import choices import sqlite3 import config from custom_types import Card +from datetime import datetime DB_PATH = config.DB_PATH CONNECTION: sqlite3.Connection @@ -100,6 +101,28 @@ def get_player(username: str) -> int: return int(player[0]) return 0 +def insert_duel_request(username: str, target: str, duel_type: str) -> None: + '''Inserts a duel request into the database. + + Args: + username (str): The username of the player who initiated the duel. + target (str): The username of the player being challenged. + duel_type (str): The type of duel (e.g., casual, competitive). + + + ''' + player_1_1d = get_player(username) + 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 + last_round_dt = datetime.now().isoformat() + + CURSOR.execute( + 'INSERT INTO duels (player_1_id, player_2_id, attacker_id,last_round_dt) VALUES (?, ?, ?, ?)', + ) def insert_player(username: str) -> int: '''Insert a new player with default has_rolled = False and return their diff --git a/bot/response.py b/bot/response.py index 5c7f35f..0b3521b 100644 --- a/bot/response.py +++ b/bot/response.py @@ -196,6 +196,12 @@ def duel_request(author: str, args: list[str]) -> BotResponse: has not signed up, please sign up before challenging to a duel.', 'attachment_urls': None } + if target == author: + return { + 'message': f'{author} You can\'t duel yourself! \ + Try challenging someone else.', + 'attachment_urls': None + } duel_type = 'casual' if len(args) == 1 and args[0] == "competitive": duel_type = 'competitive' diff --git a/migrations/0007_duelv1.sql b/migrations/0007_duelv1.sql index 9a1c8e7..4ed9eb0 100644 --- a/migrations/0007_duelv1.sql +++ b/migrations/0007_duelv1.sql @@ -29,6 +29,6 @@ CREATE TABLE duels ( last_round_dt TEXT, is_finished BOOLEAN NOT NULL DEFAULT 0, points INTEGER NOT NULL DEFAULT 0, - winner_id INTEGER, + winner_id INTEGER DEFAULT NULL, accepted BOOLEAN NOT NULL DEFAULT 0 );