diff options
| author | 2025-10-25 00:30:48 +0800 | |
|---|---|---|
| committer | 2025-10-25 00:30:48 +0800 | |
| commit | cbc653ffd0ea9abf4360623dc7a7651e1a49cc61 (patch) | |
| tree | ea3c396148158077bae3e77eaa9341f8c1990636 /src/conventionalrp/renderers | |
| parent | 08299b37dfda86e56e4f2b442f68ccd2da7a82e3 (diff) | |
| download | conventional_role_play-cbc653ffd0ea9abf4360623dc7a7651e1a49cc61.tar.gz conventional_role_play-cbc653ffd0ea9abf4360623dc7a7651e1a49cc61.zip | |
feat: Implement plugin system with combat tracker and dice analyzer
- Added `plugin_system_demo.py` to demonstrate basic plugin usage, processing, and analysis.
- Created `CombatTrackerPlugin` for tracking combat statistics including damage and healing.
- Developed `DiceAnalyzerPlugin` for analyzing dice rolls and calculating success rates.
- Introduced `renderer_demo.py` for rendering output in HTML, Markdown, and JSON formats.
- Implemented `rule_system_demo.py` to showcase rule engine capabilities with various examples.
- Established core rule engine functionality in `rules.py` with support for conditions and actions.
- Enhanced base plugin structure in `base.py` to support different plugin types (Processor, Renderer, Analyzer).
- Added custom exception handling in `exceptions.py` for better error management.
- Configured logging setup in `logging_config.py` for improved logging capabilities.
- Created unit tests in `test_rust_core.py` to validate core functionalities and performance.
Diffstat (limited to 'src/conventionalrp/renderers')
| -rw-r--r-- | src/conventionalrp/renderers/__init__.py | 3 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/html_renderer.py | 433 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/json_renderer.py | 77 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/markdown_renderer.py | 176 |
4 files changed, 628 insertions, 61 deletions
diff --git a/src/conventionalrp/renderers/__init__.py b/src/conventionalrp/renderers/__init__.py index 6d5ac7a..e69de29 100644 --- a/src/conventionalrp/renderers/__init__.py +++ b/src/conventionalrp/renderers/__init__.py @@ -1,3 +0,0 @@ -""" -This file initializes the renderers module. -""" diff --git a/src/conventionalrp/renderers/html_renderer.py b/src/conventionalrp/renderers/html_renderer.py index 75efcd3..d435fc9 100644 --- a/src/conventionalrp/renderers/html_renderer.py +++ b/src/conventionalrp/renderers/html_renderer.py @@ -1,22 +1,423 @@ +from typing import Any, Dict, List, Optional from .base import BaseRenderer class HTMLRenderer(BaseRenderer): - def __init__(self): + THEMES = { + "light": { + "bg_color": "#ffffff", + "text_color": "#333333", + "header_bg": "#f5f5f5", + "border_color": "#e0e0e0", + "dialogue_color": "#4CAF50", + "dice_color": "#2196F3", + "narration_color": "#FF9800", + "system_color": "#9E9E9E", + "success_color": "#43a047", + "failure_color": "#e53935", + "code_bg": "#f5f5f5", + }, + "dark": { + "bg_color": "#1e1e1e", + "text_color": "#d4d4d4", + "header_bg": "#2d2d30", + "border_color": "#3e3e42", + "dialogue_color": "#6adb8d", + "dice_color": "#5fb3f5", + "narration_color": "#ffb74d", + "system_color": "#bdbdbd", + "success_color": "#66bb6a", + "failure_color": "#ef5350", + "code_bg": "#2d2d30", + }, + "fantasy": { + "bg_color": "#f9f6f1", + "text_color": "#3e2723", + "header_bg": "#d7ccc8", + "border_color": "#bcaaa4", + "dialogue_color": "#8d6e63", + "dice_color": "#5d4037", + "narration_color": "#795548", + "system_color": "#a1887f", + "success_color": "#7cb342", + "failure_color": "#c62828", + "code_bg": "#efebe9", + }, + } + + def __init__(self, theme: str = "light", custom_css: Optional[str] = None): super().__init__() - self.title = "TRPG Log Output" - - def render(self, data): - html_content = f"<html><head><title>{self.title}</title></head><body>" - html_content += "<h1>TRPG Log Output</h1>" - html_content += "<ul>" - - for entry in data: - html_content += f"<li>{entry}</li>" - - html_content += "</ul></body></html>" - return html_content - + self.title = "Rendered Log" + self.theme = theme if theme in self.THEMES else "light" + self.custom_css = custom_css + + def _get_css(self) -> str: + colors = self.THEMES[self.theme] + + css = f""" + * {{ + margin: 0; + padding: 0; + box-sizing: border-box; + }} + + body {{ + font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; + background-color: {colors['bg_color']}; + color: {colors['text_color']}; + line-height: 1.6; + padding: 20px; + max-width: 1200px; + margin: 0 auto; + }} + + header {{ + background: linear-gradient(135deg, {colors['header_bg']} 0%, {colors['border_color']} 100%); + padding: 30px; + border-radius: 10px; + margin-bottom: 30px; + box-shadow: 0 4px 6px rgba(0,0,0,0.1); + }} + + h1 {{ + font-size: 2.5em; + font-weight: 700; + margin-bottom: 10px; + color: {colors['text_color']}; + }} + + .subtitle {{ + font-size: 1.1em; + opacity: 0.8; + }} + + .stats {{ + display: flex; + gap: 20px; + margin-top: 20px; + flex-wrap: wrap; + }} + + .stat-item {{ + background: {colors['bg_color']}; + padding: 10px 20px; + border-radius: 5px; + font-size: 0.9em; + border: 1px solid {colors['border_color']}; + }} + + .stat-label {{ + opacity: 0.7; + margin-right: 5px; + }} + + .stat-value {{ + font-weight: bold; + }} + + .content-wrapper {{ + background: {colors['bg_color']}; + border-radius: 10px; + padding: 20px; + box-shadow: 0 2px 4px rgba(0,0,0,0.05); + }} + + .token {{ + margin: 15px 0; + padding: 15px; + border-left: 4px solid {colors['border_color']}; + border-radius: 5px; + background: {colors['header_bg']}; + transition: transform 0.2s, box-shadow 0.2s; + }} + + .token:hover {{ + transform: translateX(5px); + box-shadow: 0 4px 8px rgba(0,0,0,0.1); + }} + + .token.dialogue {{ + border-left-color: {colors['dialogue_color']}; + }} + + .token.dice {{ + border-left-color: {colors['dice_color']}; + }} + + .token.narration {{ + border-left-color: {colors['narration_color']}; + }} + + .token.system {{ + border-left-color: {colors['system_color']}; + }} + + .token.success {{ + border-left-color: {colors['success_color']}; + background: {colors['success_color']}15; + }} + + .token.failure {{ + border-left-color: {colors['failure_color']}; + background: {colors['failure_color']}15; + }} + + .token-header {{ + display: flex; + align-items: center; + gap: 10px; + margin-bottom: 8px; + }} + + .type-badge {{ + display: inline-block; + padding: 3px 10px; + border-radius: 12px; + font-size: 0.75em; + font-weight: 600; + text-transform: uppercase; + letter-spacing: 0.5px; + }} + + .type-badge.dialogue {{ + background: {colors['dialogue_color']}; + color: white; + }} + + .type-badge.dice {{ + background: {colors['dice_color']}; + color: white; + }} + + .type-badge.narration {{ + background: {colors['narration_color']}; + color: white; + }} + + .type-badge.system {{ + background: {colors['system_color']}; + color: white; + }} + + .speaker {{ + font-weight: 700; + font-size: 1.1em; + color: {colors['text_color']}; + }} + + .content {{ + margin-top: 8px; + line-height: 1.8; + font-size: 1em; + }} + + .metadata {{ + display: flex; + gap: 15px; + margin-top: 10px; + padding-top: 10px; + border-top: 1px solid {colors['border_color']}; + font-size: 0.85em; + opacity: 0.7; + flex-wrap: wrap; + }} + + .metadata-item {{ + display: flex; + align-items: center; + gap: 5px; + }} + + .tags {{ + display: flex; + gap: 5px; + flex-wrap: wrap; + }} + + .tag {{ + background: {colors['code_bg']}; + padding: 2px 8px; + border-radius: 3px; + font-size: 0.85em; + border: 1px solid {colors['border_color']}; + }} + + code {{ + background: {colors['code_bg']}; + padding: 2px 6px; + border-radius: 3px; + font-family: 'Courier New', monospace; + font-size: 0.9em; + }} + + .dice-result {{ + display: inline-block; + background: {colors['dice_color']}; + color: white; + padding: 3px 10px; + border-radius: 5px; + font-weight: bold; + margin-left: 5px; + }} + + footer {{ + margin-top: 40px; + padding: 20px; + text-align: center; + opacity: 0.6; + font-size: 0.9em; + }} + + @media (max-width: 768px) {{ + body {{ + padding: 10px; + }} + + h1 {{ + font-size: 1.8em; + }} + + .token {{ + padding: 10px; + }} + }} + """ + + if self.custom_css: + css += "\n" + self.custom_css + + return css + + def render(self, data: List[Dict[str, Any]]) -> str: + if data and not isinstance(data[0], dict): + data = [{"type": "text", "content": str(item)} for item in data] + + stats = self._calculate_stats(data) + + html = f"""<!DOCTYPE html> +<html lang="zh-CN"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>{self.title}</title> + <style>{self._get_css()}</style> +</head> +<body> + <header> + <h1>🎲 {self.title}</h1> + <div class="subtitle">完整的游戏日志与数据分析</div> + <div class="stats"> + <div class="stat-item"> + <span class="stat-label">总条目:</span> + <span class="stat-value">{stats['total']}</span> + </div> + <div class="stat-item"> + <span class="stat-label">对话:</span> + <span class="stat-value">{stats['dialogue']}</span> + </div> + <div class="stat-item"> + <span class="stat-label">骰子:</span> + <span class="stat-value">{stats['dice']}</span> + </div> + <div class="stat-item"> + <span class="stat-label">旁白:</span> + <span class="stat-value">{stats['narration']}</span> + </div> + <div class="stat-item"> + <span class="stat-label">系统:</span> + <span class="stat-value">{stats['system']}</span> + </div> + </div> + </header> + + <div class="content-wrapper"> +""" + + for item in data: + html += self._render_token(item) + + html += """ </div> + + <footer> + <p>Generated by ConventionalRP HTML Renderer</p> + <p>Theme: """ + self.theme.capitalize() + """</p> + </footer> +</body> +</html>""" + + return html + + def _render_token(self, token: Dict[str, Any]) -> str: + token_type = token.get("type", "unknown") + speaker = token.get("speaker", "") + content = str(token.get("content", "")) + + content = (content + .replace("&", "&") + .replace("<", "<") + .replace(">", ">") + .replace('"', """)) + + if token_type == "dice" and "result" in token: + content += f' <span class="dice-result">{token["result"]}</span>' + + html = f' <div class="token {token_type}">\n' + html += ' <div class="token-header">\n' + html += f' <span class="type-badge {token_type}">{token_type}</span>\n' + + if speaker: + html += f' <span class="speaker">{speaker}</span>\n' + + html += ' </div>\n' + html += f' <div class="content">{content}</div>\n' + + metadata_items = [] + + if "timestamp" in token: + metadata_items.append(f'⏰ {token["timestamp"]}') + + if "tags" in token and token["tags"]: + tags_html = '<div class="tags">' + for tag in token["tags"]: + tags_html += f'<span class="tag">{tag}</span>' + tags_html += '</div>' + metadata_items.append(tags_html) + + if "combat_data" in token: + combat = token["combat_data"] + if combat.get("type") == "damage": + metadata_items.append(f'⚔️ 伤害: {combat["amount"]}') + elif combat.get("type") == "healing": + metadata_items.append(f'💚 治疗: {combat["amount"]}') + + if metadata_items: + html += ' <div class="metadata">\n' + for item in metadata_items: + html += f' <div class="metadata-item">{item}</div>\n' + html += ' </div>\n' + + html += ' </div>\n' + + return html + + def _calculate_stats(self, data: List[Dict[str, Any]]) -> Dict[str, int]: + stats = { + "total": len(data), + "dialogue": 0, + "dice": 0, + "narration": 0, + "system": 0, + } + + for item in data: + token_type = item.get("type", "unknown") + if token_type in stats: + stats[token_type] += 1 + + return stats + def set_style(self, style): - # Implement style setting if needed - pass + """设置样式(向后兼容)""" + if style in self.THEMES: + self.theme = style + diff --git a/src/conventionalrp/renderers/json_renderer.py b/src/conventionalrp/renderers/json_renderer.py index 8dcdd6d..42d5fd4 100644 --- a/src/conventionalrp/renderers/json_renderer.py +++ b/src/conventionalrp/renderers/json_renderer.py @@ -1,11 +1,78 @@ +import json +from typing import Any, Dict, List from .base import BaseRenderer class JSONRenderer(BaseRenderer): - def render(self, data): - import json - - return json.dumps(data, ensure_ascii=False, indent=4) + def __init__(self, pretty: bool = True, indent: int = 2, sort_keys: bool = False): + super().__init__() + self.pretty = pretty + self.indent = indent if pretty else None + self.sort_keys = sort_keys + self.style = {} + + def render(self, data: Any) -> str: + if self.pretty and isinstance(data, list): + output = { + "metadata": { + "total_entries": len(data), + "renderer": "ConventionalRP JSONRenderer", + "version": "1.0.0", + }, + "statistics": self._calculate_stats(data), + "data": data, + } + else: + output = data + + return json.dumps( + output, + ensure_ascii=False, + indent=self.indent, + sort_keys=self.sort_keys, + ) + + def _calculate_stats(self, data: List[Dict[str, Any]]) -> Dict[str, Any]: + stats = { + "dialogue": 0, + "dice": 0, + "narration": 0, + "system": 0, + "success": 0, + "failure": 0, + "other": 0, + } + + speakers = set() + + for item in data: + if not isinstance(item, dict): + continue + + token_type = item.get("type", "unknown") + if token_type in stats: + stats[token_type] += 1 + else: + stats["other"] += 1 + + if "speaker" in item and item["speaker"]: + speakers.add(item["speaker"]) + + stats["unique_speakers"] = len(speakers) + stats["speakers"] = sorted(list(speakers)) + + return stats def set_style(self, style): - self.style = style # Placeholder for potential styling options in the future + self.style = style + + # 从 style 中提取设置 + if isinstance(style, dict): + if "pretty" in style: + self.pretty = style["pretty"] + self.indent = 2 if self.pretty else None + if "indent" in style: + self.indent = style["indent"] + if "sort_keys" in style: + self.sort_keys = style["sort_keys"] + diff --git a/src/conventionalrp/renderers/markdown_renderer.py b/src/conventionalrp/renderers/markdown_renderer.py index 9df59a2..7080913 100644 --- a/src/conventionalrp/renderers/markdown_renderer.py +++ b/src/conventionalrp/renderers/markdown_renderer.py @@ -1,18 +1,19 @@ +""" +Markdown 渲染器 +""" + +from typing import Any, Dict, List, Union from .base import BaseRenderer -from typing import List, Dict, Any, Union class MarkdownRenderer(BaseRenderer): + def __init__(self, enable_syntax_hints: bool = True, enable_emoji: bool = True): + super().__init__() + self.enable_syntax_hints = enable_syntax_hints + self.enable_emoji = enable_emoji + self.style = {} + def render(self, data: Union[List[Dict[str, Any]], Dict[str, Any]]) -> str: - """ - Renders the given data in Markdown format. - - Args: - data: The data to render (can be list or dict). - - Returns: - str: The rendered Markdown string. - """ if isinstance(data, list): return self._render_list(data) elif isinstance(data, dict): @@ -21,40 +22,141 @@ class MarkdownRenderer(BaseRenderer): return str(data) def _render_list(self, data: List[Dict[str, Any]]) -> str: - """渲染列表数据为 Markdown""" - markdown_output = "# TRPG Log\n\n" - - for i, entry in enumerate(data, 1): - if entry.get("type") == "metadata": - markdown_output += f"## Entry {i}\n\n" - markdown_output += f"**Timestamp**: {entry.get('timestamp', 'N/A')} \n" - markdown_output += f"**Speaker**: {entry.get('speaker', 'N/A')} \n\n" - - content_items = entry.get("content", []) - if content_items: - markdown_output += "**Content**:\n\n" - for content in content_items: - content_type = content.get("type", "unknown") - content_text = content.get("content", "") - markdown_output += f"- [{content_type}] {content_text}\n" - markdown_output += "\n" - else: - markdown_output += f"- {entry}\n" + if data and not isinstance(data[0], dict): + data = [{"type": "text", "content": str(item)} for item in data] - return markdown_output + stats = self._calculate_stats(data) + + emoji_prefix = "🎲 " if self.enable_emoji else "" + md = f"# {emoji_prefix}\n\n" + + md += "## Statistics\n\n" + md += "| Type | Count |\n" + md += "|------|------|\n" + md += f"| Total | {stats['total']} |\n" + md += f"| Dialogue | {stats['dialogue']} |\n" + md += f"| Dice | {stats['dice']} |\n" + md += f"| Narration | {stats['narration']} |\n" + md += f"| System | {stats['system']} |\n" + md += f"| Metadata | {stats['metadata']} |\n\n" + + md += "---\n\n" + md += "## Detailed Log\n\n" + + # Render each token + for i, item in enumerate(data, 1): + md += self._render_token(item, i) + md += "\n" + + md += "---\n\n" + md += "_Generated by ConventionalRP Markdown Renderer_\n" + + return md + + def _render_token(self, token: Dict[str, Any], index: int) -> str: + token_type = token.get("type", "unknown") + + # 处理元数据类型(向后兼容) + if token_type == "metadata": + return self._render_metadata_token(token, index) + + speaker = token.get("speaker", "") + content = str(token.get("content", "")) + + type_emojis = { + "dialogue": "💬", + "dice": "🎲", + "narration": "📖", + "system": "⚙️", + "success": "✅", + "failure": "❌", + "text": "📄", + "unknown": "❓", + } + + emoji = type_emojis.get(token_type, "•") if self.enable_emoji else "-" + + md = f"### {emoji} [{index}] {token_type.upper()}\n\n" + + if speaker: + md += f"**说话者:** {speaker}\n\n" + + if token_type == "dice" and self.enable_syntax_hints: + md += f"```dice\n{content}\n```\n\n" + + if "result" in token: + md += f"**结果:** `{token['result']}`\n\n" + else: + md += f"{content}\n\n" + + metadata_lines = [] + + if "timestamp" in token: + metadata_lines.append(f"Timestamp: `{token['timestamp']}`") + + if "tags" in token and token["tags"]: + tags_str = " ".join([f"`{tag}`" for tag in token["tags"]]) + metadata_lines.append(f"Tags: {tags_str}") + + if "combat_data" in token: + combat = token["combat_data"] + if combat.get("type") == "damage": + metadata_lines.append(f"Damage: **{combat['amount']}** (Total: {combat.get('total_damage', '?')})") + elif combat.get("type") == "healing": + metadata_lines.append(f"Healing: **{combat['amount']}** (Total: {combat.get('total_healing', '?')})") + + if metadata_lines: + md += "> " + "\n> ".join(metadata_lines) + "\n\n" + + return md + + def _render_metadata_token(self, token: Dict[str, Any], index: int) -> str: + """渲染元数据类型的 token(向后兼容)""" + md = f"### Entry {index}\n\n" + md += f"**Timestamp**: {token.get('timestamp', 'N/A')} \n" + md += f"**Speaker**: {token.get('speaker', 'N/A')} \n\n" + + content_items = token.get("content", []) + if content_items: + md += "**Content**:\n\n" + for content in content_items: + content_type = content.get("type", "unknown") + content_text = content.get("content", "") + md += f"- [{content_type}] {content_text}\n" + md += "\n" + + return md def _render_dict(self, data: Dict[str, Any]) -> str: - """渲染字典数据为 Markdown""" markdown_output = "" for key, value in data.items(): markdown_output += f"## {key}\n\n{value}\n\n" return markdown_output + + def _calculate_stats(self, data: List[Dict[str, Any]]) -> Dict[str, int]: + stats = { + "total": len(data), + "dialogue": 0, + "dice": 0, + "narration": 0, + "system": 0, + "metadata": 0, + } + + for item in data: + token_type = item.get("type", "unknown") + if token_type in stats: + stats[token_type] += 1 + + return stats def set_style(self, style): - """ - Sets the style for the Markdown renderer. + self.style = style + + # 从 style 中提取设置 + if isinstance(style, dict): + if "emoji" in style: + self.enable_emoji = style["emoji"] + if "syntax_hints" in style: + self.enable_syntax_hints = style["syntax_hints"] - Args: - style (dict): A dictionary of style options. - """ - self.style = style # Currently, Markdown does not support styling, but this can be extended. |
