Compare commits
3 commits
614da6cd07
...
fc335d4537
Author | SHA1 | Date | |
---|---|---|---|
fc335d4537 | |||
5d150bfe23 | |||
e5c7196fa9 |
3 changed files with 62 additions and 2 deletions
|
@ -18,6 +18,7 @@ from random import choices
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import config
|
import config
|
||||||
from custom_types import Card
|
from custom_types import Card
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
DB_PATH = config.DB_PATH
|
DB_PATH = config.DB_PATH
|
||||||
CONNECTION: sqlite3.Connection
|
CONNECTION: sqlite3.Connection
|
||||||
|
@ -100,6 +101,28 @@ 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:
|
||||||
|
'''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:
|
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
|
||||||
|
|
|
@ -181,6 +181,37 @@ def do_help(author: str) -> BotResponse:
|
||||||
'attachment_urls': None
|
'attachment_urls': None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def duel_request(author: str, args: list[str]) -> BotResponse:
|
||||||
|
'''Sends a duel request to another user.'''
|
||||||
|
if len(args) == 0:
|
||||||
|
return {
|
||||||
|
'message': f'{author} Please specify a user to duel.',
|
||||||
|
'attachment_urls': None
|
||||||
|
}
|
||||||
|
|
||||||
|
target = args[0]
|
||||||
|
if not db.get_player(target):
|
||||||
|
return {
|
||||||
|
'message': f'{author} User {target} does not exist or \
|
||||||
|
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'
|
||||||
|
|
||||||
|
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` to accept the challenge.',
|
||||||
|
'attachment_urls': None
|
||||||
|
}
|
||||||
|
|
||||||
def delete_account(author: str) -> BotResponse:
|
def delete_account(author: str) -> BotResponse:
|
||||||
return {
|
return {
|
||||||
|
@ -325,6 +356,11 @@ def generate_response(notification: ParsedNotification) -> BotResponse | None:
|
||||||
res = delete_account(author)
|
res = delete_account(author)
|
||||||
case 'confirm_delete_account':
|
case 'confirm_delete_account':
|
||||||
res = confirm_delete(author)
|
res = confirm_delete(author)
|
||||||
|
case 'duel_request':
|
||||||
|
res = duel_request(
|
||||||
|
author,
|
||||||
|
notification['arguments']
|
||||||
|
)
|
||||||
case _:
|
case _:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see https://www.gnu.org/licenses/.
|
along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
*/
|
*/
|
||||||
CREATE TABLE duel (
|
CREATE TABLE duels (
|
||||||
duel_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
duel_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
player_1_id INTEGER NOT NULL,
|
player_1_id INTEGER NOT NULL,
|
||||||
player_2_id INTEGER NOT NULL,
|
player_2_id INTEGER NOT NULL,
|
||||||
|
@ -29,5 +29,6 @@ CREATE TABLE duel (
|
||||||
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
|
winner_id INTEGER DEFAULT NULL,
|
||||||
|
accepted BOOLEAN NOT NULL DEFAULT 0
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue