Compare commits
	
		
			No commits in common. "59f88b8a4a36e3397948ed3b5020824d496a2a0c" and "fc335d45378d5f5758770837750c1c9325cf3fb1" have entirely different histories.
		
	
	
		
			59f88b8a4a
			...
			fc335d4537
		
	
		
					 3 changed files with 10 additions and 196 deletions
				
			
		
							
								
								
									
										136
									
								
								bot/db_utils.py
									
										
									
									
									
								
							
							
						
						
									
										136
									
								
								bot/db_utils.py
									
										
									
									
									
								
							|  | @ -101,7 +101,7 @@ def get_player(username: str) -> int: | |||
|         return int(player[0]) | ||||
|     return 0 | ||||
| 
 | ||||
| def insert_duel_request(username: str, target: str, duel_type: str) -> int: | ||||
| def insert_duel_request(username: str, target: str, duel_type: str) -> None: | ||||
|     '''Inserts a duel request into the database. | ||||
|      | ||||
|     Args: | ||||
|  | @ -111,144 +111,18 @@ def insert_duel_request(username: str, target: str, duel_type: str) -> int: | |||
|      | ||||
|      | ||||
|     ''' | ||||
| 
 | ||||
|     # get the player ids | ||||
|     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] | ||||
| 
 | ||||
|     competitive = duel_type | ||||
| 
 | ||||
|     # 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( | ||||
|         '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 | ||||
|     # sets the last round date to the current time | ||||
|     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'] | ||||
|         ) | ||||
|         'INSERT INTO duels (player_1_id, player_2_id, attacker_id,last_round_dt) VALUES (?, ?, ?, ?)', | ||||
|     ) | ||||
|     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: | ||||
|     '''Insert a new player with default has_rolled = False and return their | ||||
|  |  | |||
|  | @ -181,8 +181,6 @@ def do_help(author: str) -> BotResponse: | |||
|         'attachment_urls': None | ||||
|     } | ||||
| 
 | ||||
| # Dueling | ||||
| 
 | ||||
| def duel_request(author: str, args: list[str]) -> BotResponse: | ||||
|     '''Sends a duel request to another user.''' | ||||
|     if len(args) == 0: | ||||
|  | @ -204,62 +202,17 @@ def duel_request(author: str, args: list[str]) -> BotResponse: | |||
|             Try challenging someone else.', | ||||
|             'attachment_urls': None | ||||
|         } | ||||
|      | ||||
|     # Check if both the users have enough cards to duel | ||||
|     # TODO | ||||
| 
 | ||||
|     duel_type = 'casual' | ||||
|     if len(args) == 1 and args[0] == "competitive": | ||||
|         duel_type = 'competitive' | ||||
| 
 | ||||
|     duel_id = db.insert_duel_request(author, target, duel_type) | ||||
| 
 | ||||
|     db.insert_duel_request(author, target, duel_type) | ||||
|     return { | ||||
|         'message': f'{target} You have been challenged to a {duel_type} duel by {author}! \ | ||||
|         Reply with `accept_duel {duel_id}` to accept the challenge. Duel ID:{duel_id}.', | ||||
|         'message': f'{target} You have been challenged to a {duel_type} duel by \ | ||||
|         {author}! Reply with `accept_duel` to accept the challenge.', | ||||
|         '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: | ||||
|     return { | ||||
|         'message': f'{author} ⚠️ This will permanently delete your account \ | ||||
|  | @ -408,11 +361,6 @@ def generate_response(notification: ParsedNotification) -> BotResponse | None: | |||
|                 author, | ||||
|                 notification['arguments'] | ||||
|             ) | ||||
|         case 'accept_duel': | ||||
|             res = accept_duel( | ||||
|                 author, | ||||
|                 notification['arguments'] | ||||
|             ) | ||||
|         case _: | ||||
|             pass | ||||
| 
 | ||||
|  |  | |||
|  | @ -29,14 +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 DEFAULT NULL | ||||
| ); | ||||
| 
 | ||||
| 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 | ||||
|     winner_id INTEGER DEFAULT NULL, | ||||
|     accepted BOOLEAN NOT NULL DEFAULT 0 | ||||
| ); | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue