database 接入
This commit is contained in:
@ -18,11 +18,14 @@ from nonebot_plugin_alconna import (
|
||||
on_alconna,
|
||||
)
|
||||
|
||||
from konabot.common.database import DatabaseManager
|
||||
from konabot.common.longtask import DepLongTaskTarget
|
||||
from konabot.common.path import ASSETS_PATH
|
||||
|
||||
from konabot.common.llm import get_llm
|
||||
|
||||
ROOT_PATH = Path(__file__).resolve().parent
|
||||
|
||||
DATA_DIR = Path(__file__).parent.parent.parent.parent / "data"
|
||||
|
||||
DATA_FILE_PATH = (
|
||||
@ -94,18 +97,19 @@ class IdiomGameLLM:
|
||||
|
||||
@classmethod
|
||||
async def storage_idiom(cls, idiom: str):
|
||||
# 将 idiom 存入本地文件以备后续分析
|
||||
with open(DATA_DIR / "idiom_llm_storage.txt", "a", encoding="utf-8") as f:
|
||||
f.write(idiom + "\n")
|
||||
IdiomGame.append_into_word_list(idiom)
|
||||
# 将 idiom 存入数据库
|
||||
DatabaseManager.execute_by_sql_file(
|
||||
ROOT_PATH / "sql" / "insert_custom_word.sql",
|
||||
(idiom,)
|
||||
)
|
||||
|
||||
|
||||
class IdiomGame:
|
||||
ALL_WORDS = [] # 所有四字词语
|
||||
ALL_IDIOMS = [] # 所有成语
|
||||
# ALL_WORDS = [] # 所有四字词语
|
||||
# ALL_IDIOMS = [] # 所有成语
|
||||
INSTANCE_LIST: dict[str, "IdiomGame"] = {} # 群号对应的游戏实例
|
||||
IDIOM_FIRST_CHAR = {} # 所有成语包括词语的首字字典
|
||||
AVALIABLE_IDIOM_FIRST_CHAR = {} # 真正有效的成语首字字典
|
||||
# IDIOM_FIRST_CHAR = {} # 所有成语包括词语的首字字典
|
||||
# AVALIABLE_IDIOM_FIRST_CHAR = {} # 真正有效的成语首字字典
|
||||
|
||||
__inited = False
|
||||
|
||||
@ -130,11 +134,10 @@ class IdiomGame:
|
||||
'''
|
||||
将一个新词加入到词语列表中
|
||||
'''
|
||||
if word not in cls.ALL_WORDS:
|
||||
cls.ALL_WORDS.append(word)
|
||||
if word[0] not in cls.IDIOM_FIRST_CHAR:
|
||||
cls.IDIOM_FIRST_CHAR[word[0]] = []
|
||||
cls.IDIOM_FIRST_CHAR[word[0]].append(word)
|
||||
DatabaseManager.execute_by_sql_file(
|
||||
ROOT_PATH / "sql" / "insert_custom_word.sql",
|
||||
(word,)
|
||||
)
|
||||
|
||||
def be_able_to_play(self) -> bool:
|
||||
if self.last_play_date != datetime.date.today():
|
||||
@ -145,11 +148,17 @@ class IdiomGame:
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def random_idiom() -> str:
|
||||
return DatabaseManager.query_by_sql_file(
|
||||
ROOT_PATH / "sql" / "random_choose_idiom.sql"
|
||||
)[0]["idiom"]
|
||||
|
||||
def choose_start_idiom(self) -> str:
|
||||
"""
|
||||
随机选择一个成语作为起始成语
|
||||
"""
|
||||
self.last_idiom = secrets.choice(IdiomGame.ALL_IDIOMS)
|
||||
self.last_idiom = IdiomGame.random_idiom()
|
||||
self.last_char = self.last_idiom[-1]
|
||||
if not self.is_nextable(self.last_char):
|
||||
self.choose_start_idiom()
|
||||
@ -208,7 +217,7 @@ class IdiomGame:
|
||||
return self.last_idiom
|
||||
|
||||
def _skip_idiom_async(self) -> str:
|
||||
self.last_idiom = secrets.choice(IdiomGame.ALL_IDIOMS)
|
||||
self.last_idiom = IdiomGame.random_idiom()
|
||||
self.last_char = self.last_idiom[-1]
|
||||
if not self.is_nextable(self.last_char):
|
||||
self._skip_idiom_async()
|
||||
@ -228,8 +237,11 @@ class IdiomGame:
|
||||
"""
|
||||
判断是否有成语可以接
|
||||
"""
|
||||
return last_char in IdiomGame.AVALIABLE_IDIOM_FIRST_CHAR
|
||||
|
||||
return DatabaseManager.query_by_sql_file(
|
||||
ROOT_PATH / "sql" / "is_nextable.sql",
|
||||
(last_char,)
|
||||
)[0]["DEED"] == 1
|
||||
|
||||
def add_already_idiom(self, idiom: str):
|
||||
if idiom in self.already_idioms:
|
||||
self.already_idioms[idiom] += 1
|
||||
@ -259,7 +271,12 @@ class IdiomGame:
|
||||
if idiom[0] != self.last_char:
|
||||
state.append(TryVerifyState.WRONG_FIRST_CHAR)
|
||||
return state
|
||||
if idiom not in IdiomGame.ALL_IDIOMS and idiom not in IdiomGame.ALL_WORDS:
|
||||
# 成语是否存在
|
||||
result = DatabaseManager.query_by_sql_file(
|
||||
ROOT_PATH / "sql" / "query_idiom.sql",
|
||||
(idiom, idiom, idiom)
|
||||
)[0]["status"]
|
||||
if result == -1:
|
||||
logger.info(f"用户 {user_id} 发送了未知词语 {idiom},正在使用 LLM 进行验证")
|
||||
try:
|
||||
if not await IdiomGameLLM.verify_idiom_with_llm(idiom):
|
||||
@ -281,7 +298,7 @@ class IdiomGame:
|
||||
self.last_idiom = idiom
|
||||
self.last_char = idiom[-1]
|
||||
self.add_score(user_id, 1 * score_k) # 先加 1 分
|
||||
if idiom in IdiomGame.ALL_IDIOMS:
|
||||
if result == 1:
|
||||
state.append(TryVerifyState.VERIFIED_AND_REAL)
|
||||
self.add_score(user_id, 4 * score_k) # 再加 4 分
|
||||
self.remain_rounds -= 1
|
||||
@ -319,14 +336,21 @@ class IdiomGame:
|
||||
@classmethod
|
||||
def random_idiom_starting_with(cls, first_char: str) -> Optional[str]:
|
||||
cls.init_lexicon()
|
||||
if first_char not in cls.AVALIABLE_IDIOM_FIRST_CHAR:
|
||||
result = DatabaseManager.query_by_sql_file(
|
||||
ROOT_PATH / "sql" / "query_idiom_start_with.sql",
|
||||
(first_char,)
|
||||
)
|
||||
if len(result) == 0:
|
||||
return None
|
||||
return secrets.choice(cls.AVALIABLE_IDIOM_FIRST_CHAR[first_char])
|
||||
return result[0]["idiom"]
|
||||
|
||||
@classmethod
|
||||
def init_lexicon(cls):
|
||||
if cls.__inited:
|
||||
return
|
||||
DatabaseManager.execute_by_sql_file(
|
||||
ROOT_PATH / "sql" / "create_table.sql"
|
||||
) # 确保数据库初始化
|
||||
cls.__inited = True
|
||||
|
||||
# 成语大表
|
||||
@ -334,11 +358,12 @@ class IdiomGame:
|
||||
ALL_IDIOMS_INFOS = json.load(f)
|
||||
|
||||
# 词语大表
|
||||
ALL_WORDS = []
|
||||
with open(ASSETS_PATH / "lexicon" / "ci.json", "r", encoding="utf-8") as f:
|
||||
jsonData = json.load(f)
|
||||
cls.ALL_WORDS = [item["ci"] for item in jsonData]
|
||||
logger.debug(f"Loaded {len(cls.ALL_WORDS)} words from ci.json")
|
||||
logger.debug(f"Sample words: {cls.ALL_WORDS[:5]}")
|
||||
ALL_WORDS = [item["ci"] for item in jsonData]
|
||||
logger.debug(f"Loaded {len(ALL_WORDS)} words from ci.json")
|
||||
logger.debug(f"Sample words: {ALL_WORDS[:5]}")
|
||||
|
||||
COMMON_WORDS = []
|
||||
# 读取 COMMON 词语大表
|
||||
@ -389,29 +414,44 @@ class IdiomGame:
|
||||
logger.debug(f"Loaded additional {len(LOCAL_LLM_WORDS)} words from idiom_llm_storage.txt")
|
||||
|
||||
# 只有成语的大表
|
||||
cls.ALL_IDIOMS = [idiom["word"] for idiom in ALL_IDIOMS_INFOS] + THUOCL_IDIOMS
|
||||
cls.ALL_IDIOMS = list(set(cls.ALL_IDIOMS)) # 去重
|
||||
ALL_IDIOMS = [idiom["word"] for idiom in ALL_IDIOMS_INFOS] + THUOCL_IDIOMS
|
||||
ALL_IDIOMS = list(set(ALL_IDIOMS)) # 去重
|
||||
# 批量插入数据库
|
||||
DatabaseManager.execute_many_values_by_sql_file(
|
||||
ROOT_PATH / "sql" / "insert_idiom.sql",
|
||||
[(idiom,) for idiom in ALL_IDIOMS]
|
||||
)
|
||||
|
||||
|
||||
# 其他四字词语表,仅表示可以有这个词
|
||||
cls.ALL_WORDS = (
|
||||
[word for word in cls.ALL_WORDS if len(word) == 4]
|
||||
ALL_WORDS = (
|
||||
[word for word in ALL_WORDS if len(word) == 4]
|
||||
+ THUOCL_WORDS
|
||||
+ COMMON_WORDS
|
||||
+ LOCAL_LLM_WORDS
|
||||
)
|
||||
cls.ALL_WORDS = list(set(cls.ALL_WORDS)) # 去重
|
||||
# 插入数据库
|
||||
DatabaseManager.execute_many_values_by_sql_file(
|
||||
ROOT_PATH / "sql" / "insert_word.sql",
|
||||
[(word,) for word in ALL_WORDS]
|
||||
)
|
||||
|
||||
# 根据成语大表,划分出成语首字字典
|
||||
for idiom in cls.ALL_IDIOMS + cls.ALL_WORDS:
|
||||
if idiom[0] not in cls.IDIOM_FIRST_CHAR:
|
||||
cls.IDIOM_FIRST_CHAR[idiom[0]] = []
|
||||
cls.IDIOM_FIRST_CHAR[idiom[0]].append(idiom)
|
||||
# 自定义词语 LOCAL_LLM_WORDS 插入数据库,兼容用
|
||||
DatabaseManager.execute_many_values_by_sql_file(
|
||||
ROOT_PATH / "sql" / "insert_custom_word.sql",
|
||||
[(word,) for word in LOCAL_LLM_WORDS]
|
||||
)
|
||||
|
||||
# 根据真正的成语大表,划分出有效成语首字字典
|
||||
for idiom in cls.ALL_IDIOMS:
|
||||
if idiom[0] not in cls.AVALIABLE_IDIOM_FIRST_CHAR:
|
||||
cls.AVALIABLE_IDIOM_FIRST_CHAR[idiom[0]] = []
|
||||
cls.AVALIABLE_IDIOM_FIRST_CHAR[idiom[0]].append(idiom)
|
||||
# # 根据成语大表,划分出成语首字字典
|
||||
# for idiom in cls.ALL_IDIOMS + cls.ALL_WORDS:
|
||||
# if idiom[0] not in cls.IDIOM_FIRST_CHAR:
|
||||
# cls.IDIOM_FIRST_CHAR[idiom[0]] = []
|
||||
# cls.IDIOM_FIRST_CHAR[idiom[0]].append(idiom)
|
||||
|
||||
# # 根据真正的成语大表,划分出有效成语首字字典
|
||||
# for idiom in cls.ALL_IDIOMS:
|
||||
# if idiom[0] not in cls.AVALIABLE_IDIOM_FIRST_CHAR:
|
||||
# cls.AVALIABLE_IDIOM_FIRST_CHAR[idiom[0]] = []
|
||||
# cls.AVALIABLE_IDIOM_FIRST_CHAR[idiom[0]].append(idiom)
|
||||
|
||||
|
||||
evt = on_alconna(
|
||||
@ -514,7 +554,9 @@ async def end_game(event: BaseEvent, group_id: str):
|
||||
for line in history_lines:
|
||||
result_text += line + "\n"
|
||||
await evt.send(await result_text.export())
|
||||
instance.clear_score_board()
|
||||
# instance.clear_score_board()
|
||||
# 将实例删除
|
||||
del IdiomGame.INSTANCE_LIST[group_id]
|
||||
|
||||
|
||||
evt = on_alconna(
|
||||
|
||||
Reference in New Issue
Block a user