add emotion

This commit is contained in:
bridge
2026-01-04 22:49:20 +08:00
parent 441f8c8e3a
commit b74014f9f2
10 changed files with 71 additions and 0 deletions

View File

@@ -99,6 +99,16 @@ class LLMAI(AI):
avatar_thinking = r.get("avatar_thinking", r.get("thinking", ""))
short_term_objective = r.get("short_term_objective", "")
# 更新情绪
from src.classes.emotions import EmotionType
raw_emotion = r.get("current_emotion", "平静")
try:
# 尝试通过 value (中文) 获取枚举
avatar.emotion = EmotionType(raw_emotion)
except ValueError:
avatar.emotion = EmotionType.CALM
results[avatar] = (pairs, avatar_thinking, short_term_objective)
return results

View File

@@ -37,6 +37,7 @@ from src.classes.appearance import Appearance, get_random_appearance
from src.classes.spirit_animal import SpiritAnimal
from src.classes.long_term_objective import LongTermObjective
from src.classes.nickname_data import Nickname
from src.classes.emotions import EmotionType
from src.utils.config import CONFIG
# Mixin 导入
@@ -106,6 +107,7 @@ class Avatar(
auxiliary: Optional[Auxiliary] = None
spirit_animal: Optional[SpiritAnimal] = None
nickname: Optional[Nickname] = None
emotion: EmotionType = EmotionType.CALM
custom_pic_id: Optional[int] = None
is_dead: bool = False

View File

@@ -12,6 +12,7 @@ if TYPE_CHECKING:
from src.classes.battle import get_base_strength
from src.classes.relation import get_relation_label
from src.classes.emotions import EMOTION_EMOJIS, EmotionType
from src.utils.config import CONFIG
@@ -84,6 +85,7 @@ def get_avatar_info(avatar: "Avatar", detailed: bool = False) -> dict:
"外貌": appearance_info,
"兵器": weapon_info,
"辅助装备": auxiliary_info,
"情绪": avatar.emotion.value,
}
if detailed:
@@ -109,6 +111,8 @@ def get_avatar_structured_info(avatar: "Avatar") -> dict:
获取结构化的角色信息,用于前端展示和交互。
"""
# 基础信息
emoji = EMOTION_EMOJIS.get(avatar.emotion, EMOTION_EMOJIS[EmotionType.CALM])
info = {
"id": avatar.id,
"name": avatar.name,
@@ -121,6 +125,11 @@ def get_avatar_structured_info(avatar: "Avatar") -> dict:
"alignment": str(avatar.alignment) if avatar.alignment else "未知",
"magic_stone": avatar.magic_stone.value,
"base_battle_strength": int(get_base_strength(avatar)),
"emotion": {
"name": avatar.emotion.value,
"emoji": emoji,
"desc": avatar.emotion.value
},
"thinking": avatar.thinking,
"short_term_objective": avatar.short_term_objective,
"long_term_objective": avatar.long_term_objective.content if avatar.long_term_objective else "",

27
src/classes/emotions.py Normal file
View File

@@ -0,0 +1,27 @@
from enum import Enum
class EmotionType(Enum):
CALM = "平静"
HAPPY = "开心"
ANGRY = "愤怒"
SAD = "悲伤"
FEARFUL = "恐惧"
SURPRISED = "惊讶"
ANTICIPATING = "期待"
DISGUSTED = "厌恶"
CONFUSED = "疑惑"
TIRED = "疲惫"
# 情绪对应的 Emoji 配置
EMOTION_EMOJIS = {
EmotionType.CALM: "😌",
EmotionType.HAPPY: "😄",
EmotionType.ANGRY: "😡",
EmotionType.SAD: "😢",
EmotionType.FEARFUL: "😨",
EmotionType.SURPRISED: "😲",
EmotionType.ANTICIPATING: "🤩",
EmotionType.DISGUSTED: "🤢",
EmotionType.CONFUSED: "😕",
EmotionType.TIRED: "😫",
}

View File

@@ -155,6 +155,14 @@ class AvatarLoadMixin:
from src.classes.nickname_data import Nickname
avatar.nickname = Nickname.from_dict(data.get("nickname"))
# 恢复情绪
from src.classes.emotions import EmotionType
emotion_str = data.get("emotion", "平静")
try:
avatar.emotion = EmotionType(emotion_str)
except ValueError:
avatar.emotion = EmotionType.CALM
# 恢复死亡状态
avatar.is_dead = data.get("is_dead", False)
avatar.death_info = data.get("death_info")

View File

@@ -91,6 +91,7 @@ class AvatarSaveMixin:
"persona_ids": [p.id for p in self.personas] if self.personas else [],
"appearance": self.appearance.level,
"nickname": self.nickname.to_dict() if self.nickname else None,
"emotion": self.emotion.value,
"is_dead": self.is_dead,
"death_info": self.death_info,