codryn / phpdice
A comprehensive PHP library for parsing and rolling dice expressions for tabletop RPG systems
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 5
pkg:composer/codryn/phpdice
Requires
- php: >=8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5
- dev-main
- 0.3.0
- 0.2.1
- 0.2.0
- dev-develop
- dev-copilot/add-docu-for-switch-case-expressions
- dev-copilot/fix-statistics-for-groups
- dev-copilot/fix-math-only-roll-statistics
- dev-feature/fix-main-merge-issue
- dev-copilot/add-performance-tests
- dev-copilot/add-switch-case-expression
- dev-feature/php-demands
- dev-copilot/fix-issue-47-update-tests
- dev-copilot/fix-namespace-for-package
- dev-copilot/add-coin-flip-dice
- dev-copilot/add-count-odd-even-mechanic
- dev-feature/improve-ci
- dev-copilot/add-tags-to-parser
- dev-copilot/add-roll-groups-feature
- dev-copilot/add-roll-comments-feature
- dev-copilot/allow-dice-expression-in-math
- dev-copilot/add-shadowrun-edge-feature
This package is auto-updated.
Last update: 2026-01-12 16:08:09 UTC
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
- ๐ง Email
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 ๐ฒ