refactor relation strs

This commit is contained in:
bridge
2025-10-24 01:31:45 +08:00
parent 9e0911dd6c
commit c3f4f1c182
5 changed files with 321 additions and 65 deletions

View File

@@ -171,7 +171,9 @@ class Avatar:
获取 avatar 的信息,返回 dict根据 detailed 控制信息粒度。
"""
region = self.tile.region if self.tile is not None else None
relations_info = self._get_relations_summary_str()
from src.classes.relation import get_relations_strs
relation_lines = get_relations_strs(self, max_lines=8)
relations_info = "".join(relation_lines) if relation_lines else ""
magic_stone_info = str(self.magic_stone)
if detailed:
@@ -564,10 +566,11 @@ class Avatar:
if self.spirit_animal is not None:
add_kv(lines, "灵兽", self.spirit_animal.get_info())
# 关系
relations_list = [f"{other.name}({str(relation)})" for other, relation in getattr(self, "relations", {}).items()]
if relations_list:
add_section(lines, "关系", [f" {s}" for s in relations_list[:6]])
# 关系(从自身视角分组展示)
from src.classes.relation import get_relations_strs
relation_lines = get_relations_strs(self, max_lines=6)
if relation_lines:
add_section(lines, "关系", [f" {s}" for s in relation_lines])
else:
add_kv(lines, "关系", "")

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
from enum import Enum
from typing import TYPE_CHECKING, List
from collections import defaultdict
class Relation(Enum):
@@ -135,3 +136,35 @@ def get_possible_post_relations(from_avatar: "Avatar", to_avatar: "Avatar") -> L
return candidates
# ——— 悬浮提示:从“自身视角”格式化关系 ———
def _label_from_self_perspective(relation: Relation) -> str:
# 以“我”为参照:有向关系需要取对偶后再显示(如 MASTER -> 徒弟)。
counterpart = get_reciprocal(relation)
return relation_display_names.get(counterpart, str(counterpart))
def get_relations_strs(avatar: "Avatar", max_lines: int = 6) -> list[str]:
"""
以“我”的视角整理关系,输出若干行:
- 我的师傅AB
- 我的徒弟C
- 兄弟姐妹DE
等。
"""
relations = getattr(avatar, "relations", None)
if not relations:
return []
grouped: dict[str, list[str]] = defaultdict(list)
for other, rel in relations.items():
grouped[_label_from_self_perspective(rel)].append(other.name)
lines: list[str] = []
for key in sorted(grouped.keys()):
names = "".join(grouped[key])
lines.append(f"{key}为:{names}")
if len(lines) >= max_lines:
break
return lines