debug 游戏命令
This commit is contained in:
@@ -1,46 +1,59 @@
|
||||
-- 创建数据库
|
||||
CREATE DATABASE message_archive CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
USE message_archive;
|
||||
|
||||
-- 创建群聊表
|
||||
CREATE TABLE t_encyclopedia_groups (
|
||||
group_id INT PRIMARY KEY,
|
||||
group_id VARCHAR(50) PRIMARY KEY,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||
|
||||
-- 创建玩家表
|
||||
CREATE TABLE t_encyclopedia_players (
|
||||
player_id INT NOT NULL,
|
||||
group_id INT NOT NULL,
|
||||
player_id VARCHAR(50) NOT NULL,
|
||||
group_id VARCHAR(50) NOT NULL,
|
||||
player_name VARCHAR(50) NOT NULL,
|
||||
points INT DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (player_id, group_id),
|
||||
FOREIGN KEY (group_id) REFERENCES t_encyclopedia_groups(group_id)
|
||||
PRIMARY KEY (player_id, group_id)
|
||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||
|
||||
-- 创建活跃任务表
|
||||
CREATE TABLE t_encyclopedia_active_tasks (
|
||||
active_task_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
group_id INT NOT NULL,
|
||||
group_id VARCHAR(50) NOT NULL,
|
||||
question VARCHAR(255) NOT NULL,
|
||||
answer VARCHAR(100) NOT NULL,
|
||||
score INT NOT NULL,
|
||||
description TEXT,
|
||||
holder_id INT NOT NULL,
|
||||
holder_id VARCHAR(50) NOT NULL,
|
||||
assigned_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
status ENUM('pending', 'completed') DEFAULT 'pending',
|
||||
FOREIGN KEY (group_id) REFERENCES t_encyclopedia_groups(group_id),
|
||||
FOREIGN KEY (holder_id, group_id) REFERENCES t_encyclopedia_players(player_id, group_id),
|
||||
INDEX (group_id)
|
||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||
|
||||
-- 创建任务历史表
|
||||
CREATE TABLE t_encyclopedia_task_history (
|
||||
history_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
group_id INT NOT NULL,
|
||||
group_id VARCHAR(50) NOT NULL,
|
||||
active_task_id INT NOT NULL,
|
||||
player_id INT NOT NULL,
|
||||
player_id VARCHAR(50) NOT NULL,
|
||||
answer VARCHAR(100) NOT NULL,
|
||||
is_correct BOOLEAN DEFAULT FALSE,
|
||||
points_earned INT DEFAULT 0,
|
||||
completed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (group_id) REFERENCES t_encyclopedia_groups(group_id),
|
||||
FOREIGN KEY (active_task_id) REFERENCES t_encyclopedia_active_tasks(active_task_id),
|
||||
FOREIGN KEY (player_id, group_id) REFERENCES t_encyclopedia_players(player_id, group_id),
|
||||
INDEX (group_id)
|
||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||
|
||||
-- 初始化数据
|
||||
-- 添加群聊
|
||||
INSERT INTO t_encyclopedia_groups (group_id) VALUES
|
||||
('group1'),
|
||||
('group2');
|
||||
|
||||
-- 添加玩家
|
||||
INSERT INTO t_encyclopedia_players (player_id, group_id, player_name) VALUES
|
||||
('player1001', 'group1', '玩家1'),
|
||||
('player1002', 'group1', '玩家2'),
|
||||
('player2001', 'group2', '玩家A'),
|
||||
('player2002', 'group2', '玩家B');
|
||||
@@ -23,7 +23,7 @@ def get_db_connection():
|
||||
|
||||
|
||||
# 添加群聊
|
||||
def add_group(group_id):
|
||||
def add_group(group_id, player_id):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -124,7 +124,7 @@ def assign_random_task(group_id):
|
||||
conn.close()
|
||||
|
||||
|
||||
# 提交答案并计分
|
||||
# 提交答案并计分(错误扣1分)
|
||||
def submit_answer(group_id, player_id, task_id, answer):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
@@ -148,7 +148,7 @@ def submit_answer(group_id, player_id, task_id, answer):
|
||||
return f"群 {group_id} 中此任务不存在或已完成!"
|
||||
|
||||
question, correct_answer_db, top_score, holder_id, _ = task_data['question'], task_data['answer'].lower(), \
|
||||
task_data['score'], task_data['holder_id'], task_data['status']
|
||||
task_data['score'], task_data['holder_id'], task_data['status']
|
||||
|
||||
# 获取持有者名称
|
||||
cursor.execute("SELECT player_name FROM t_encyclopedia_players WHERE group_id = %s AND player_id = %s",
|
||||
@@ -164,39 +164,44 @@ def submit_answer(group_id, player_id, task_id, answer):
|
||||
description = result["description"]
|
||||
is_correct = points > 0
|
||||
|
||||
# 记录历史
|
||||
cursor.execute(
|
||||
"INSERT INTO t_encyclopedia_task_history (group_id, active_task_id, player_id, answer, is_correct, points_earned) VALUES (%s, %s, %s, %s, %s, %s)",
|
||||
(group_id, active_task_id, player_id, answer, is_correct, points)
|
||||
)
|
||||
|
||||
# 记录历史并更新积分
|
||||
if is_correct:
|
||||
# 更新玩家积分
|
||||
cursor.execute(
|
||||
"UPDATE t_encyclopedia_players SET points = points + %s WHERE group_id = %s AND player_id = %s",
|
||||
(points, group_id, player_id)
|
||||
)
|
||||
# 标记任务完成
|
||||
cursor.execute(
|
||||
"UPDATE t_encyclopedia_active_tasks SET status = 'completed' WHERE group_id = %s AND active_task_id = %s",
|
||||
(group_id, active_task_id)
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
if player_id == holder_id:
|
||||
return f"{player_name} (ID: {player_id}) 回答正确!任务:{question}\n获得 {points} 分\n描述:{description}"
|
||||
else:
|
||||
return f"{player_name} (ID: {player_id}) 抢答成功!任务:{question}(原持有者:{holder_name} (ID: {holder_id}))\n获得 {points} 分\n描述:{description}"
|
||||
message = (
|
||||
f"{player_name} (ID: {player_id}) 回答正确!任务:{question}\n获得 {points} 分\n描述:{description}"
|
||||
if player_id == holder_id
|
||||
else f"{player_name} (ID: {player_id}) 抢答成功!任务:{question}(原持有者:{holder_name} (ID: {holder_id}))\n获得 {points} 分\n描述:{description}"
|
||||
)
|
||||
else:
|
||||
conn.commit()
|
||||
return f"{player_name} (ID: {player_id}) 回答错误!任务:{question}\n你的答案:{answer},正确答案:{correct_answer}\n描述:{description}"
|
||||
# 错误扣1分,确保积分不低于0
|
||||
cursor.execute(
|
||||
"UPDATE t_encyclopedia_players SET points = GREATEST(points - 1, 0) WHERE group_id = %s AND player_id = %s",
|
||||
(group_id, player_id)
|
||||
)
|
||||
points = -1 # 记录惩罚分
|
||||
message = f"{player_name} (ID: {player_id}) 回答错误!任务:{question}\n你的答案:{answer},正确答案:{correct_answer}\n扣除 1 分\n描述:{description}"
|
||||
|
||||
cursor.execute(
|
||||
"INSERT INTO t_encyclopedia_task_history (group_id, active_task_id, player_id, answer, is_correct, points_earned) VALUES (%s, %s, %s, %s, %s, %s)",
|
||||
(group_id, active_task_id, player_id, answer, is_correct, points)
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
return message
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
# 显示排行榜
|
||||
def show_rank(group_id):
|
||||
def show_rank(group_id, player_id):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -217,7 +222,7 @@ def show_rank(group_id):
|
||||
|
||||
|
||||
# 显示当前活跃任务
|
||||
def show_active_tasks(group_id):
|
||||
def show_active_tasks(group_id, player_id):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -240,7 +245,7 @@ def show_active_tasks(group_id):
|
||||
|
||||
|
||||
# 列举所有未完成任务及其所属者
|
||||
def list_uncompleted_tasks(group_id):
|
||||
def list_uncompleted_tasks(group_id, player_id):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -277,9 +282,9 @@ def game_process_message(group_id, player_id, message, player_name="未知玩家
|
||||
if message == "/start":
|
||||
return start_game(group_id, player_id, player_name)
|
||||
elif message == "/tasks":
|
||||
return show_active_tasks(group_id)
|
||||
return show_active_tasks(group_id, player_id)
|
||||
elif message == "/list":
|
||||
return list_uncompleted_tasks(group_id)
|
||||
return list_uncompleted_tasks(group_id, player_id)
|
||||
elif message.startswith("/answer"):
|
||||
parts = message.split(" ", 2)
|
||||
if len(parts) < 3:
|
||||
@@ -287,9 +292,9 @@ def game_process_message(group_id, player_id, message, player_name="未知玩家
|
||||
task_id, answer = parts[1], parts[2]
|
||||
return submit_answer(group_id, player_id, task_id, answer)
|
||||
elif message == "/rank":
|
||||
return show_rank(group_id)
|
||||
return show_rank(group_id, player_id)
|
||||
elif message.startswith("/addgroup"):
|
||||
return add_group(group_id)
|
||||
return add_group(group_id, player_id)
|
||||
else:
|
||||
return "无效命令!可用:/start, /tasks, /list, /answer [任务ID] [答案], /addgroup, /rank"
|
||||
|
||||
@@ -304,18 +309,18 @@ def setup_schedule():
|
||||
# 主程序
|
||||
if __name__ == "__main__":
|
||||
# 初始化群聊
|
||||
print(add_group(1))
|
||||
print(add_group(2))
|
||||
print(add_group("group1", "admin1"))
|
||||
print(add_group("group2", "admin1"))
|
||||
|
||||
# 初始化玩家
|
||||
print(process_message(1, 1001, "/start", "玩家1"))
|
||||
print(process_message(1, 1002, "/start", "玩家2"))
|
||||
print(process_message(2, 2001, "/start", "玩家A"))
|
||||
print(process_message(2, 2002, "/start", "玩家B"))
|
||||
print(game_process_message("group1", "player1001", "/start", "玩家1"))
|
||||
print(game_process_message("group1", "player1002", "/start", "玩家2"))
|
||||
print(game_process_message("group2", "player2001", "/start", "玩家A"))
|
||||
print(game_process_message("group2", "player2002", "/start", "玩家B"))
|
||||
|
||||
# 设置调度
|
||||
setup_schedule()
|
||||
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
time.sleep(1)
|
||||
time.sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user