debug 游戏命令
This commit is contained in:
@@ -1,46 +1,59 @@
|
|||||||
|
-- 创建数据库
|
||||||
|
CREATE DATABASE message_archive CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
USE message_archive;
|
USE message_archive;
|
||||||
|
|
||||||
|
-- 创建群聊表
|
||||||
CREATE TABLE t_encyclopedia_groups (
|
CREATE TABLE t_encyclopedia_groups (
|
||||||
group_id INT PRIMARY KEY,
|
group_id VARCHAR(50) PRIMARY KEY,
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||||
|
|
||||||
|
-- 创建玩家表
|
||||||
CREATE TABLE t_encyclopedia_players (
|
CREATE TABLE t_encyclopedia_players (
|
||||||
player_id INT NOT NULL,
|
player_id VARCHAR(50) NOT NULL,
|
||||||
group_id INT NOT NULL,
|
group_id VARCHAR(50) NOT NULL,
|
||||||
player_name VARCHAR(50) NOT NULL,
|
player_name VARCHAR(50) NOT NULL,
|
||||||
points INT DEFAULT 0,
|
points INT DEFAULT 0,
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (player_id, group_id),
|
PRIMARY KEY (player_id, group_id)
|
||||||
FOREIGN KEY (group_id) REFERENCES t_encyclopedia_groups(group_id)
|
|
||||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||||
|
|
||||||
|
-- 创建活跃任务表
|
||||||
CREATE TABLE t_encyclopedia_active_tasks (
|
CREATE TABLE t_encyclopedia_active_tasks (
|
||||||
active_task_id INT AUTO_INCREMENT PRIMARY KEY,
|
active_task_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
group_id INT NOT NULL,
|
group_id VARCHAR(50) NOT NULL,
|
||||||
question VARCHAR(255) NOT NULL,
|
question VARCHAR(255) NOT NULL,
|
||||||
answer VARCHAR(100) NOT NULL,
|
answer VARCHAR(100) NOT NULL,
|
||||||
score INT NOT NULL,
|
score INT NOT NULL,
|
||||||
description TEXT,
|
description TEXT,
|
||||||
holder_id INT NOT NULL,
|
holder_id VARCHAR(50) NOT NULL,
|
||||||
assigned_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
assigned_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
status ENUM('pending', 'completed') DEFAULT 'pending',
|
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)
|
INDEX (group_id)
|
||||||
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
) ENGINE=InnoDB CHARACTER SET utf8mb4;
|
||||||
|
|
||||||
|
-- 创建任务历史表
|
||||||
CREATE TABLE t_encyclopedia_task_history (
|
CREATE TABLE t_encyclopedia_task_history (
|
||||||
history_id INT AUTO_INCREMENT PRIMARY KEY,
|
history_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
group_id INT NOT NULL,
|
group_id VARCHAR(50) NOT NULL,
|
||||||
active_task_id INT NOT NULL,
|
active_task_id INT NOT NULL,
|
||||||
player_id INT NOT NULL,
|
player_id VARCHAR(50) NOT NULL,
|
||||||
answer VARCHAR(100) NOT NULL,
|
answer VARCHAR(100) NOT NULL,
|
||||||
is_correct BOOLEAN DEFAULT FALSE,
|
is_correct BOOLEAN DEFAULT FALSE,
|
||||||
points_earned INT DEFAULT 0,
|
points_earned INT DEFAULT 0,
|
||||||
completed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
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)
|
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()
|
conn = get_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
try:
|
try:
|
||||||
@@ -124,7 +124,7 @@ def assign_random_task(group_id):
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
# 提交答案并计分
|
# 提交答案并计分(错误扣1分)
|
||||||
def submit_answer(group_id, player_id, task_id, answer):
|
def submit_answer(group_id, player_id, task_id, answer):
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
@@ -164,39 +164,44 @@ def submit_answer(group_id, player_id, task_id, answer):
|
|||||||
description = result["description"]
|
description = result["description"]
|
||||||
is_correct = points > 0
|
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:
|
if is_correct:
|
||||||
# 更新玩家积分
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"UPDATE t_encyclopedia_players SET points = points + %s WHERE group_id = %s AND player_id = %s",
|
"UPDATE t_encyclopedia_players SET points = points + %s WHERE group_id = %s AND player_id = %s",
|
||||||
(points, group_id, player_id)
|
(points, group_id, player_id)
|
||||||
)
|
)
|
||||||
# 标记任务完成
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"UPDATE t_encyclopedia_active_tasks SET status = 'completed' WHERE group_id = %s AND active_task_id = %s",
|
"UPDATE t_encyclopedia_active_tasks SET status = 'completed' WHERE group_id = %s AND active_task_id = %s",
|
||||||
(group_id, active_task_id)
|
(group_id, active_task_id)
|
||||||
)
|
)
|
||||||
|
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:
|
||||||
|
# 错误扣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()
|
conn.commit()
|
||||||
|
|
||||||
if player_id == holder_id:
|
return message
|
||||||
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}"
|
|
||||||
else:
|
|
||||||
conn.commit()
|
|
||||||
return f"{player_name} (ID: {player_id}) 回答错误!任务:{question}\n你的答案:{answer},正确答案:{correct_answer}\n描述:{description}"
|
|
||||||
finally:
|
finally:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
# 显示排行榜
|
# 显示排行榜
|
||||||
def show_rank(group_id):
|
def show_rank(group_id, player_id):
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
try:
|
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()
|
conn = get_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
try:
|
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()
|
conn = get_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
try:
|
try:
|
||||||
@@ -277,9 +282,9 @@ def game_process_message(group_id, player_id, message, player_name="未知玩家
|
|||||||
if message == "/start":
|
if message == "/start":
|
||||||
return start_game(group_id, player_id, player_name)
|
return start_game(group_id, player_id, player_name)
|
||||||
elif message == "/tasks":
|
elif message == "/tasks":
|
||||||
return show_active_tasks(group_id)
|
return show_active_tasks(group_id, player_id)
|
||||||
elif message == "/list":
|
elif message == "/list":
|
||||||
return list_uncompleted_tasks(group_id)
|
return list_uncompleted_tasks(group_id, player_id)
|
||||||
elif message.startswith("/answer"):
|
elif message.startswith("/answer"):
|
||||||
parts = message.split(" ", 2)
|
parts = message.split(" ", 2)
|
||||||
if len(parts) < 3:
|
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]
|
task_id, answer = parts[1], parts[2]
|
||||||
return submit_answer(group_id, player_id, task_id, answer)
|
return submit_answer(group_id, player_id, task_id, answer)
|
||||||
elif message == "/rank":
|
elif message == "/rank":
|
||||||
return show_rank(group_id)
|
return show_rank(group_id, player_id)
|
||||||
elif message.startswith("/addgroup"):
|
elif message.startswith("/addgroup"):
|
||||||
return add_group(group_id)
|
return add_group(group_id, player_id)
|
||||||
else:
|
else:
|
||||||
return "无效命令!可用:/start, /tasks, /list, /answer [任务ID] [答案], /addgroup, /rank"
|
return "无效命令!可用:/start, /tasks, /list, /answer [任务ID] [答案], /addgroup, /rank"
|
||||||
|
|
||||||
@@ -304,14 +309,14 @@ def setup_schedule():
|
|||||||
# 主程序
|
# 主程序
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 初始化群聊
|
# 初始化群聊
|
||||||
print(add_group(1))
|
print(add_group("group1", "admin1"))
|
||||||
print(add_group(2))
|
print(add_group("group2", "admin1"))
|
||||||
|
|
||||||
# 初始化玩家
|
# 初始化玩家
|
||||||
print(process_message(1, 1001, "/start", "玩家1"))
|
print(game_process_message("group1", "player1001", "/start", "玩家1"))
|
||||||
print(process_message(1, 1002, "/start", "玩家2"))
|
print(game_process_message("group1", "player1002", "/start", "玩家2"))
|
||||||
print(process_message(2, 2001, "/start", "玩家A"))
|
print(game_process_message("group2", "player2001", "/start", "玩家A"))
|
||||||
print(process_message(2, 2002, "/start", "玩家B"))
|
print(game_process_message("group2", "player2002", "/start", "玩家B"))
|
||||||
|
|
||||||
# 设置调度
|
# 设置调度
|
||||||
setup_schedule()
|
setup_schedule()
|
||||||
|
|||||||
Reference in New Issue
Block a user