Leveler
leveler
Level-up engine for Basic Fantasy RPG.
This module orchestrates the process of advancing a character by one level. It coordinates validation, progression lookups, hit point calculation, and construction of the updated character state.
The level-up process is intentionally implemented as a pure transformation
- The input Character is never modified
- A new Character instance is returned
- A LevelUpResult object summarizes the updated state
Flow
- Validate that the character is eligible to level up
- Determine the next level
- Calculate hit point gain
- Retrieve updated progression data (attack bonus, saving throws, etc.)
- Construct a new Character instance
- Build a LevelUpResult summary
- Return both the updated character and result
Design principles
- No mutation: all updates produce new objects
- No rule logic: this module delegates all game mechanics
- Uniform data model: all progression fields are always populated, even for classes where the values are neutral (e.g., zero spell slots)
Delegation
- advancement.py: Determines level-up eligibility based on XP and rules
- hit_points.py: Calculates hit point gains using a DiceRoller
- rpgleveler.data: Provides progression table lookups
This separation keeps the engine modular, testable, and easy to extend.
LevelUpError
Bases: Exception
Raised when a character is not eligible to level up.
Source code in src/rpgleveler/engine/leveler.py
61 62 | |
level_up(character, *, rng)
Apply a single level-up to a character.
This function performs a complete level-up operation, returning both the updated character state and a structured summary of the result.
All progression data is retrieved through dedicated modules, ensuring that this function remains a pure orchestration layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
character
|
Character
|
The character to level up. |
required |
rng
|
DiceRoller
|
A DiceRoller instance used for hit point calculation. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Character, LevelUpResult]
|
tuple[Character, LevelUpResult]: - A new Character instance reflecting the updated state - A LevelUpResult describing the outcome of the level-up |
Raises:
| Type | Description |
|---|---|
LevelUpError
|
If the character is not eligible to level up. |
Notes
- The input character is never modified.
- All progression fields are always populated.
- Class-specific features (e.g., spell slots, thief skills) are represented using neutral/default values when not applicable.
Source code in src/rpgleveler/engine/leveler.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |