From cbc653ffd0ea9abf4360623dc7a7651e1a49cc61 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Sat, 25 Oct 2025 00:30:48 +0800 Subject: 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. --- examples/plugins/dice_analyzer_plugin.py | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/plugins/dice_analyzer_plugin.py (limited to 'examples/plugins/dice_analyzer_plugin.py') diff --git a/examples/plugins/dice_analyzer_plugin.py b/examples/plugins/dice_analyzer_plugin.py new file mode 100644 index 0000000..3ab2fb9 --- /dev/null +++ b/examples/plugins/dice_analyzer_plugin.py @@ -0,0 +1,90 @@ +import sys +import os +from pathlib import Path +import re +from typing import Any, Dict, List + +sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src")) + +from conventionalrp.plugins.base import AnalyzerPlugin + + +class DiceAnalyzerPlugin(AnalyzerPlugin): + """骰子投掷数据分析插件""" + def __init__(self): + super().__init__("DiceAnalyzer", "1.0.0") + self.dice_pattern = re.compile(r'd(\d+)') + + def initialize(self, config: Dict[str, Any] | None = None): + self.config = config or {} + self.logger.info("DiceAnalyzerPlugin initialized") + + def analyze(self, data: Any) -> Dict[str, Any]: + if not isinstance(data, list): + return {"error": "Input must be a list of tokens"} + + total_rolls = 0 + dice_types = {} + success_count = 0 + failure_count = 0 + critical_hits = 0 + critical_fails = 0 + + for token in data: + if not isinstance(token, dict): + continue + + token_type = token.get("type", "") + content = token.get("content", "") + + if token_type == "dice": + total_rolls += 1 + + match = self.dice_pattern.search(content) + if match: + dice_type = f"d{match.group(1)}" + dice_types[dice_type] = dice_types.get(dice_type, 0) + 1 + + if token_type == "success": + success_count += 1 + elif token_type == "failure": + failure_count += 1 + + if "critical" in content.lower(): + if "success" in token_type or "成功" in content: + critical_hits += 1 + elif "failure" in token_type or "失败" in content: + critical_fails += 1 + + result = { + "total_rolls": total_rolls, + "dice_types": dice_types, + "success_count": success_count, + "failure_count": failure_count, + "critical_hits": critical_hits, + "critical_fails": critical_fails, + "success_rate": success_count / total_rolls if total_rolls > 0 else 0, + "critical_rate": (critical_hits + critical_fails) / total_rolls if total_rolls > 0 else 0, + } + + self.logger.info(f"Analyzed {total_rolls} dice rolls") + return result + + +if __name__ == "__main__": + plugin = DiceAnalyzerPlugin() + plugin.initialize() + + test_data = [ + {"type": "dice", "content": "d20=15"}, + {"type": "success", "content": "检定成功"}, + {"type": "dice", "content": "d6=4"}, + {"type": "dice", "content": "d20=20"}, + {"type": "success", "content": "大成功!Critical hit!"}, + {"type": "dice", "content": "d20=1"}, + {"type": "failure", "content": "大失败..."}, + ] + + result = plugin.analyze(test_data) + for key, value in result.items(): + print(f" {key}: {value}") -- cgit v1.2.3-70-g09d2