refactor: 将多个方法修改为静态方法以提高代码可读性和一致性
This commit is contained in:
@@ -72,7 +72,8 @@ class PersonInfoManager:
|
||||
self.person_name_list[doc["person_id"]] = doc["person_name"]
|
||||
logger.debug(f"已加载 {len(self.person_name_list)} 个用户名称")
|
||||
|
||||
def get_person_id(self, platform: str, user_id: int):
|
||||
@staticmethod
|
||||
def get_person_id(platform: str, user_id: int):
|
||||
"""获取唯一id"""
|
||||
# 如果platform中存在-,就截取-后面的部分
|
||||
if "-" in platform:
|
||||
@@ -91,7 +92,8 @@ class PersonInfoManager:
|
||||
else:
|
||||
return False
|
||||
|
||||
async def create_person_info(self, person_id: str, data: dict = None):
|
||||
@staticmethod
|
||||
async def create_person_info(person_id: str, data: dict = None):
|
||||
"""创建一个项"""
|
||||
if not person_id:
|
||||
logger.debug("创建失败,personid不存在")
|
||||
@@ -131,7 +133,8 @@ class PersonInfoManager:
|
||||
else:
|
||||
return False
|
||||
|
||||
def _extract_json_from_text(self, text: str) -> dict:
|
||||
@staticmethod
|
||||
def _extract_json_from_text(text: str) -> dict:
|
||||
"""从文本中提取JSON数据的高容错方法"""
|
||||
try:
|
||||
# 尝试直接解析
|
||||
@@ -225,7 +228,8 @@ class PersonInfoManager:
|
||||
logger.error(f"在{max_retries}次尝试后仍未能生成唯一昵称")
|
||||
return None
|
||||
|
||||
async def del_one_document(self, person_id: str):
|
||||
@staticmethod
|
||||
async def del_one_document(person_id: str):
|
||||
"""删除指定 person_id 的文档"""
|
||||
if not person_id:
|
||||
logger.debug("删除失败:person_id 不能为空")
|
||||
@@ -237,7 +241,8 @@ class PersonInfoManager:
|
||||
else:
|
||||
logger.debug(f"删除失败:未找到 person_id={person_id}")
|
||||
|
||||
async def get_value(self, person_id: str, field_name: str):
|
||||
@staticmethod
|
||||
async def get_value(person_id: str, field_name: str):
|
||||
"""获取指定person_id文档的字段值,若不存在该字段,则返回该字段的全局默认值"""
|
||||
if not person_id:
|
||||
logger.debug("get_value获取失败:person_id不能为空")
|
||||
@@ -256,7 +261,8 @@ class PersonInfoManager:
|
||||
logger.trace(f"获取{person_id}的{field_name}失败,已返回默认值{default_value}")
|
||||
return default_value
|
||||
|
||||
async def get_values(self, person_id: str, field_names: list) -> dict:
|
||||
@staticmethod
|
||||
async def get_values(person_id: str, field_names: list) -> dict:
|
||||
"""获取指定person_id文档的多个字段值,若不存在该字段,则返回该字段的全局默认值"""
|
||||
if not person_id:
|
||||
logger.debug("get_values获取失败:person_id不能为空")
|
||||
@@ -281,7 +287,8 @@ class PersonInfoManager:
|
||||
|
||||
return result
|
||||
|
||||
async def del_all_undefined_field(self):
|
||||
@staticmethod
|
||||
async def del_all_undefined_field():
|
||||
"""删除所有项里的未定义字段"""
|
||||
# 获取所有已定义的字段名
|
||||
defined_fields = set(person_info_default.keys())
|
||||
@@ -307,9 +314,9 @@ class PersonInfoManager:
|
||||
logger.error(f"清理未定义字段时出错: {e}")
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
async def get_specific_value_list(
|
||||
self,
|
||||
field_name: str,
|
||||
field_name: str,
|
||||
way: Callable[[Any], bool], # 接受任意类型值
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
|
||||
@@ -62,7 +62,7 @@ class RelationshipManager:
|
||||
def mood_feedback(self, value):
|
||||
"""情绪反馈"""
|
||||
mood_manager = self.mood_manager
|
||||
mood_gain = (mood_manager.get_current_mood().valence) ** 2 * math.copysign(
|
||||
mood_gain = mood_manager.get_current_mood().valence ** 2 * math.copysign(
|
||||
1, value * mood_manager.get_current_mood().valence
|
||||
)
|
||||
value += value * mood_gain
|
||||
@@ -77,24 +77,27 @@ class RelationshipManager:
|
||||
else:
|
||||
return mood_value / coefficient
|
||||
|
||||
async def is_known_some_one(self, platform, user_id):
|
||||
@staticmethod
|
||||
async def is_known_some_one(platform, user_id):
|
||||
"""判断是否认识某人"""
|
||||
is_known = person_info_manager.is_person_known(platform, user_id)
|
||||
return is_known
|
||||
|
||||
async def is_qved_name(self, platform, user_id):
|
||||
@staticmethod
|
||||
async def is_qved_name(platform, user_id):
|
||||
"""判断是否认识某人"""
|
||||
person_id = person_info_manager.get_person_id(platform, user_id)
|
||||
is_qved = await person_info_manager.has_one_field(person_id, "person_name")
|
||||
old_name = await person_info_manager.get_value(person_id, "person_name")
|
||||
print(f"old_name: {old_name}")
|
||||
print(f"is_qved: {is_qved}")
|
||||
if is_qved and old_name != None:
|
||||
if is_qved and old_name is not None:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
async def first_knowing_some_one(self, platform, user_id, user_nickname, user_cardname, user_avatar):
|
||||
@staticmethod
|
||||
async def first_knowing_some_one(platform, user_id, user_nickname, user_cardname, user_avatar):
|
||||
"""判断是否认识某人"""
|
||||
person_id = person_info_manager.get_person_id(platform, user_id)
|
||||
await person_info_manager.update_one_field(person_id, "nickname", user_nickname)
|
||||
@@ -102,7 +105,8 @@ class RelationshipManager:
|
||||
# await person_info_manager.update_one_field(person_id, "user_avatar", user_avatar)
|
||||
await person_info_manager.qv_person_name(person_id, user_nickname, user_cardname, user_avatar)
|
||||
|
||||
async def convert_all_person_sign_to_person_name(self, input_text: str):
|
||||
@staticmethod
|
||||
async def convert_all_person_sign_to_person_name(input_text: str):
|
||||
"""将所有人的<platform:user_id:nickname:cardname>格式转换为person_name"""
|
||||
try:
|
||||
# 使用正则表达式匹配<platform:user_id:nickname:cardname>格式
|
||||
@@ -119,7 +123,7 @@ class RelationshipManager:
|
||||
person_name = nickname.strip() if nickname.strip() else cardname.strip()
|
||||
|
||||
if person_id in all_person:
|
||||
if all_person[person_id] != None:
|
||||
if all_person[person_id] is not None:
|
||||
person_name = all_person[person_id]
|
||||
|
||||
print(f"将<{platform}:{user_id}:{nickname}:{cardname}>替换为{person_name}")
|
||||
@@ -326,7 +330,8 @@ class RelationshipManager:
|
||||
f"回复态度为{relation_prompt2_list[level_num]},关系等级为{level_num}。"
|
||||
)
|
||||
|
||||
def calculate_level_num(self, relationship_value) -> int:
|
||||
@staticmethod
|
||||
def calculate_level_num(relationship_value) -> int:
|
||||
"""关系等级计算"""
|
||||
if -1000 <= relationship_value < -227:
|
||||
level_num = 0
|
||||
@@ -344,7 +349,8 @@ class RelationshipManager:
|
||||
level_num = 5 if relationship_value > 1000 else 0
|
||||
return level_num
|
||||
|
||||
def ensure_float(self, value, person_id):
|
||||
@staticmethod
|
||||
def ensure_float(value, person_id):
|
||||
"""确保返回浮点数,转换失败返回0.0"""
|
||||
if isinstance(value, float):
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user