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/json_renderer.py | |
| 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/json_renderer.py')
| -rw-r--r-- | src/conventionalrp/renderers/json_renderer.py | 77 |
1 files changed, 72 insertions, 5 deletions
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"] + |
