codryn/phpdice
Composer 安装命令:
composer require codryn/phpdice
包简介
A comprehensive PHP library for parsing and rolling dice expressions for tabletop RPG systems
README 文档
README
PHP library for parsing and rolling dice expressions for tabletop RPG systems.
PHPDice is a powerful and flexible library that allows developers to parse and roll complex dice expressions used in various tabletop role-playing games (RPGs). It supports a wide range of dice mechanics, including advantage/disadvantage, success counting, rerolls, exploding dice, critical hits, and more.
Features
- 🎲 Universal Dice Notation: Support for all major RPG systems (D&D 5e, Pathfinder, Shadowrun, World of Darkness, FATE, Savage Worlds, etc.)
- ⚡ Advanced Mechanics: Advantage/disadvantage, success counting, rerolls, exploding dice, critical detection, DC comparisons
- 📊 Statistical Analysis: Pre-calculated min/max/expected values for any expression
- 🔧 Placeholder Variables: Character sheet integration with
$variable$syntax - ➗ Complex Arithmetic: Full expression evaluation with operator precedence and parentheses
- ❗ Error Handling: Clear, specific error messages with location information
- 🚀 High Performance: Parse <100ms, Roll <50ms for complex expressions
- 🔒 Type Safe: Full PHP 8.1+ type declarations and strict mode
- ⚡ Zero Dependencies: Pure PHP 8.1+ implementation using only stdlib and ctype extension
- 🧪 Well Tested: Unit tests with comprehensive coverage
- 📦 PSR-12 Compliant: Modern PHP coding standards
Requirements
- PHP 8.1 or higher
- ctype PHP extension enabled
Installation
composer require codryn/phpdice
Quick Start and Usage
<?php require 'vendor/autoload.php'; use Codryn\PHPDice\PHPDice; // Create instance $dice = new PHPDice(); // Roll dice directly $result = $dice->roll("3d6"); echo "Rolled: " . $result->total . "\n"; echo "Dice: " . implode(", ", $result->diceValues) . "\n"; // Example output: // Rolled: 14 // Dice: 5, 6, 3
See docs/quickstart.md for a 10-minute tutorial.
Dice Expressions
See docs/expressions.md for all supported dice expressions.
Game System Support
| System | Example | Features |
|---|---|---|
| D&D 5e | 1d20 auto 20 crit 20 +5 dc >= 15 |
Auto success, criticals, modifiers, comparisons |
| Pathfinder | 3d6+2 |
Basic dice, modifiers |
| Shadowrun 5e | 12d6 edge count >=5 |
Success counting, edge (Rule of Six) |
| World of Darkness | 10d10 >=8 |
Success counting |
| FATE | 4dF+2 |
Fudge dice, modifiers |
| Savage Worlds | 1d6 explode + 1d8 explode |
Exploding dice |
| Call of Cthulhu | d% |
Percentile dice |
| Even/Odd Counting | 6d6 count even or 12d4 count odd |
Count even or odd results |
API Overview
See docs/api.md for complete reference.
Documentation
- Quick Start Guide - 10-minute tutorial
- Dice Expressions - All supported dice expressions
- API Documentation - Complete API reference with examples
- Examples - Game system specific examples
Development
See CONTRIBUTING.md for development guidelines.
Architecture
PHPDice is structured into several key components:
- Parser: Converts dice expression strings into an Abstract Syntax Tree (AST) using a recursive descent parser.
- Model: Represents the AST nodes, dice expressions, roll results, and related data structures.
- Roller: Evaluates the AST to perform dice rolls, applying game mechanics
Performance
PHPDice is optimized for real-time use:
- Parsing: <100ms for complex expressions
- Rolling: <50ms for typical rolls
- Memory: <1MB per operation
Tips for best performance:
- Reuse parsed
DiceExpressionobjects for repeated rolls - Set reasonable explosion/reroll limits (default 100 is safe)
- Use
getStatistics()for probability analysis instead of Monte Carlo simulation
Quality Standards
- ✅ PHPStan Level 10: Strictest static analysis level from PHPSTan 2.1
- ✅ PSR-12: PHP coding standards compliance
- ✅ Strict Types:
declare(strict_types=1)in all files - ✅ TDD: Test-driven development methodology
- ✅ Type Hints: Full type declarations on all methods
- ✅ PHPDoc: Complete documentation blocks
Contributing
Contributions are welcome! See CONTRIBUTING.md for:
- Development workflow
- Coding standards
- Testing requirements
- Pull request process
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for version history and migration guides.
Support
- 📖 Documentation
- 🐛 Issue Tracker
- 💬 Discussions
Credits
Created and maintained by Marco for Codryn.
Special thanks to:
- The PHP community
- PHPUnit, PHPStan, and PHP-CS-Fixer maintainers
Game Systems Copyright
This library implements dice roll mechanics from various tabletop RPG systems for non-commercial use. All game system names, mechanics, and related intellectual property remain the property of their respective copyright holders. See GAME_SYSTEMS_COPYRIGHT.md for detailed copyright notices and attributions.
Built for the tabletop RPG community 🎲
codryn/phpdice 适用场景与选型建议
codryn/phpdice 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「gaming」 「rpg」 「dice」 「d20」 「roller」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codryn/phpdice 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codryn/phpdice 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codryn/phpdice 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Laravel PHP Facade/Wrapper for the IGDB API
An MT940 bank statement parser for PHP
Laravel integration for the jsonapi.org parser
Gaming payments done right - Tebex is the monetization platform designed to grow your gaming revenue streams
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 32
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-06