Skip to content

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
  1. Validate that the character is eligible to level up
  2. Determine the next level
  3. Calculate hit point gain
  4. Retrieve updated progression data (attack bonus, saving throws, etc.)
  5. Construct a new Character instance
  6. Build a LevelUpResult summary
  7. 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
class LevelUpError(Exception):
    """Raised when a character is not eligible to level up."""

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
def level_up(
    character: Character, 
    *, 
    rng: DiceRoller
) -> tuple[Character, LevelUpResult]:
    """
    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.

    Args:
        character:
            The character to level up.
        rng:
            A DiceRoller instance used for hit point calculation.

    Returns:
        tuple[Character, LevelUpResult]:
            - A new Character instance reflecting the updated state
            - A LevelUpResult describing the outcome of the level-up

    Raises:
        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.
    """
    _validate_can_level_up(character)

    old_level = character.level
    new_level = character.level + 1

    hp_gained = roll_hp_gain(character, rng)
    new_hp_total = character.hp + hp_gained

    new_attack_bonus = get_attack_bonus(character.class_name, new_level)

    saving_throws = get_saving_throws(
        character.class_name, character.race, new_level)

    new_spell_slots = get_spell_slots(character.class_name, new_level)
    thief_skills = get_thief_skills(character.class_name, new_level)
    turn_undead = get_turn_undead(character.class_name, new_level)

    new_character = _build_new_character(
        character=character,
        new_level=new_level,
        new_hp_total=new_hp_total,
        new_attack_bonus=new_attack_bonus,
        saving_throws=saving_throws,
        new_spell_slots=new_spell_slots,
        thief_skills=thief_skills,
        turn_undead=turn_undead,
    )

    level_up_result = LevelUpResult(
        class_name=character.class_name,
        race=character.race,
        old_level=old_level,
        new_level=new_level,
        hp_gained=hp_gained,
        new_hp_total=new_hp_total,
        new_attack_bonus=new_attack_bonus,
        saving_throws=saving_throws,
        new_spell_slots=new_spell_slots,
        thief_skills=thief_skills,
        turn_undead=turn_undead,
    )

    return new_character, level_up_result