A library to roll dice in a simple way, but with advanced functionality.
Project description
rollplayerlib
This library provides a flexible and extensible way to handle complex dice roll expressions and formats. It supports a wide range of features, including basic dice rolls, bonuses, targeted bonuses, and various formatting options.
Installation
You can install the library through pip: pip install rollplayerlib
.
Usage
Basic Dice Roll
To roll a basic set of dice, you can use the UnifiedDice.new() method:
from rollplayerlib import UnifiedDice, SolveMode
# Roll 3 six-sided dice
dice = UnifiedDice.new("3d6")
result = dice.solve(SolveMode.RANDOM)
print(result)
This will output something like:
┏━━━━ 3d6 ━━━━
┃ 4, 2, 6
┃ sum: 12
Dice Rolls with Bonuses
You can apply bonuses to your dice rolls by using the +, -, *, and / operators:
# Roll 2d6 and add 3
dice = UnifiedDice.new("2d6+3")
result = dice.solve(SolveMode.RANDOM)
print(result)
This will output something like:
┏━━━━ 2d6+3 ━━━━
┃ 8, 7
┃ sum: 15
Note that the bonus applies to each roll individually.
Targeted Bonuses
You can apply targeted bonuses to specific dice in your roll using the i syntax:
# Roll 45d100, multiply the 1st die by 20
dice = UnifiedDice.new("45d100i1:*20")
result = dice.solve(SolveMode.RANDOM)
print(result)
This will output something like:
┏━━━━ 45d100i1:*20 ━━━━
┃ 1080, 17, 63, 39, 47, 58, 98, 93, 16, 54, 90, 89, 99, 63, 68, 30, 26, 66, 36, 96
┃ 8, 85, 47, 3, 79, 5, 46, 56, 41, 41, 67, 45, 83, 44, 14, 19, 31, 96, 82, 98
┃ 26, 36, 39, 51, 7
┃ sum: 3377
Formatting Options
You can control the formatting of the dice roll results using the Format class:
from rollplayerlib import UnifiedDice, SolveMode, Format, FormatType, ThresholdType
# Roll 4d6, keep the highest 3 rolls
dice = UnifiedDice.new("4d6")
formatting = Format(FormatType.FORMAT_LIST, threshold=Threshold(3, ThresholdType.GREATER))
result = dice.solve(SolveMode.RANDOM)
print(result.format(formatting))
This will output something like:
┏━━━━ 4d6 ━━━━
┃ **5**, **4**, **3**, 2
┃ sum: 14
API Reference
UnifiedDice
new(input_string: str) -> UnifiedDice: Constructs a UnifiedDice object from a dice roll expression.
solve(solve_mode: SolveMode) -> RollResult: Solves the dice roll expression and returns the RollResult.
RollResult
format(formatting: Format) -> List[tuple[str, str]]: Formats the dice roll result according to the provided Format object.
Format
__init__(format_type: FormatType, format_args=None, threshold: Threshold=None): Constructs a Format object with the specified parameters.
parse(expression: str) -> tuple[str, Format]: Parses a dice roll expression and returns the remaining text and the corresponding Format object.
Threshold
__init__(limit: int, threshold_type: ThresholdType): Constructs a Threshold object with the specified limit and type.
passing(numbers: list[int]) -> list[bool]: Checks which numbers in the provided list pass the threshold condition.
SolveMode
RANDOM, MAX, MIN: Enum values representing different solve modes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rollplayerlib-0.5.4.tar.gz
.
File metadata
- Download URL: rollplayerlib-0.5.4.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | afa2e4dbeb5d1db89d368b6970a44ed4b00c7b3658097d9da7947a2b8d310c61 |
|
MD5 | 2007e5a7b7680a5c9a400220b364a84c |
|
BLAKE2b-256 | 933a192d331b6a6421f1609ae3b5ceae76f0872385a7fce8da34272cfd77171c |
File details
Details for the file rollplayerlib-0.5.4-py3-none-any.whl
.
File metadata
- Download URL: rollplayerlib-0.5.4-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25f739de5fe1a27b7011b5a881858a7c86273ece60a91a583d8ce189c8e43a56 |
|
MD5 | 793f7a16ab3d322de62ba56553fb7f2a |
|
BLAKE2b-256 | 34325bcd20dfc1a3d459bc05e8d86a06c86fc3508bdb612731d1b28a61e9abf2 |