party
party
The party module contains the Party class and functions related to managing a party of player characters (collection of PlayerCharacter).
CharacterAlreadyInPartyError
Bases: Exception
Raised when attempting to add a player character to a party that already has that character as a member.
Example:
CharacterNotInPartyError
Bases: Exception
Raised when attempting to remove a character from a party when that character isn't in the party.
Example:
NoMembersInPartyError
Bases: Exception
Raised when attempting an operation on a party member when the party has no members.
Party
Party(name: str, max_party_members: int = 8, characters: List[PlayerCharacter] = None)
Manages a collection of player characters (PCs) that comprise an adventuring party.
If the party has been added to an adventure and the adventure has been started, you can't modify the party roster by adding or removing characters.
Parameters:
-
name
(str
) –The name of the party.
-
max_party_members
(int
, default:8
) –The maximum number of characters allowed in the party.
-
characters
(List[PlayerCharacter]
, default:None
) –The characters in the party.
Attributes:
-
name
(str
) –The name of the party.
-
max_party_members
(int
) –The maximum number of characters allowed in the party.
-
characters
(List[PlayerCharacter]
) –The characters in the party.
-
active_character
(PlayerCharacter
) –The currently active, or selected, character in the party.
-
is_adventuring
(bool
) –Whether the party has been added to an Adventure that has been started.
-
current_adventure
(Adventure
) –The adventure the party is currently in, or None if the party is not in an adventure.
is_adventuring
property
is_adventuring: bool
Returns True if the party has been added to an adventure that has been started.
Returns:
-
bool
(bool
) –True if the party has been added to an adventure that has been started.
is_alive
property
is_alive: bool
Returns True if any character in the party is alive.
Returns:
-
bool
(bool
) –True if any character in the party is alive.
num_characters
property
num_characters: int
Get the number of characters in the party.
Returns:
-
int
(int
) –The number of characters in the party.
add_character
add_character(character: PlayerCharacter, set_as_active_character: bool = True) -> PlayerCharacter
Add a character to the party.
A character can be added to a party only once, and a party has a maximum number of characters.
Example:
# Add a character to a party and allow them to be set as the active character
fighter = PlayerCharacter("Sckricko", character_classes.CharacterClassType.FIGHTER, 1)
thief = PlayerCharacter("Slick", character_classes.CharacterClassType.THIEF, 1)
party.add_character(fighter) # sets the character as active for the party character by default
party.add_character(thief, set_as_active_character=False) # don't set the character as active for the party
if party.active_character == fighter:
print(f"Character '{character.name}' is the active character in the party.")
Parameters:
-
character
(PlayerCharacter
) –The PC to add to the party.
Raises:
-
CharacterAlreadyInPartyError
–If the character is already in the party.
-
PartyAtCapacityError
–If the party already has the maximum number of characters.
Returns:
-
PlayerCharacter
(PlayerCharacter
) –The character that was added to the party.
create_character
create_character(name: str, character_class: CharacterClassType, level: int = 1) -> PlayerCharacter
Initialize a new character, add them to the party, set as the active character for the party, and return the character.
Example:
# Create a new character and add them to the party
party.create_character("Sckricko", character_classes.CharacterClassType.FIGHTER, 1)
Parameters:
-
name
(str
) –The name of the character.
-
character_class
(CharacterClassType
) –The character's class.
-
level
(int
, default:1
) –The character's level. Defaults to 1.
Returns:
-
PlayerCharacter
(PlayerCharacter
) –The character that was created and added to the party.
Raises:
-
PartyInStartedAdventureError
–If the party is already adventuring, can't create a new character in the party.
from_dict
classmethod
Deserializes a dictionary representation of an Exit
object. Typically done after getting the dictionary from persistent storage.
get_character_by_index
get_character_by_index(index: int) -> PlayerCharacter
Get a character from the party by index, or None if there's no character at that index.
Example:
if len(party.members) > 0:
# Get the first character in the party
first_character = party.get_character_by_index(0)
# Get the last character in the party
last_character = party.get_character_by_index(-1)
Parameters:
-
index
(int
) –The index of the character to return.
Returns:
-
PlayerCharacter
(PlayerCharacter
) –The character with the given index, or None if the index is out of range.
get_character_by_name
get_character_by_name(name: str) -> PlayerCharacter
Get a character from the party by name or None if the character is not in the party.
Example:
character = party.get_character_by_name("Sckricko")
if character is not None:
print(f"Character '{character.name}' has {character.hit_points} hit points.")
Parameters:
-
name
(str
) –The name of the character to return.
Returns:
-
PlayerCharacter
(PlayerCharacter
) –The character with the given name or None if the character is not in the party.
get_character_index
get_character_index(character: PlayerCharacter) -> int
Get the index of a character in the party.
Example:
Get the index of a character in the party without checking whether the character is in the party (not recommended):
character = party.get_character_by_name("Sckricko")
if party.is_member(character):
index = party.get_character_index(character)
print(f"Character '{character.name}' is number {index + 1} in the party's marching order.")
Parameters:
-
character
(PlayerCharacter
) –The character to get the index of
Returns:
-
int
(int
) –The index of the character in the party.
Raises:
-
CharacterNotInPartyError
–If the character is not in the party.
get_characters_by_class
get_characters_by_class(character_class_type: CharacterClassType) -> List[PlayerCharacter]
Get all characters in the party of the given class.
Example:
fighters = party.get_characters_by_class(character_classes.CharacterClassType.FIGHTER)
for fighter in fighters:
print(fighter.name)
Parameters:
-
character_class_type
(CharacterClassType
) –The class of characters to return.
Returns:
-
List[PlayerCharacter]
–List[PlayerCharacter]: A list of all characters in the party of the given class.
get_default_party
staticmethod
Get a party of first-level characters of each class: a Fighter, Elf, Cleric, Dwarf, Thief, Halfling, and Magic User.
Returns:
-
Party
(Party
) –A party of first-level player characters in each character class.
get_living_members
get_living_members() -> List[PlayerCharacter]
Returns a list of all living members of the party.
Returns:
-
List[PlayerCharacter]
–List[PlayerCharacter]: A list of all living members of the party.
get_surprise_roll
get_surprise_roll() -> int
Rolls a 1d6 and returns the result for the party's surprise roll.
grant_xp
grant_xp(xp: int)
Divide and award experience points to the living characters in the party.
Example:
Parameters:
-
xp
(int
) –The number of experience points to award to each character in the party.
is_member
is_member(character: PlayerCharacter) -> bool
Returns True
if the character is in the party.
Example:
# Check whether a character is in the party
if party.is_member(some_player_character):
print(f"{some_name} is in the party.")
else:
print(f"{some_name} is not in the party.")
Parameters:
-
character
(PlayerCharacter
) –The character to check for.
Returns:
-
bool
(bool
) –True
if the character is in the party, otherwiseFalse
.
move_character_to_index
move_character_to_index(character: PlayerCharacter, index: int)
Moves a character to a new slot in the in party's marching order.
Use this method to adjust the marching order of the party.
Example:
# Move a character from fourth in line (index 3) to the
# front of the party at index 0.
character = party.get_character_by_name("Sckricko")
if party.is_member(character):
party.move_character_to_index(character, 0)
Parameters:
-
character
(PlayerCharacter
) –The character to move.
-
index
(int
) –The index to move the character to.
Raises:
-
CharacterNotInPartyError
–If the character is not in the party.
-
IndexError
–If the index is out of range.
remove_character
remove_character(character: PlayerCharacter)
Removes a character from the party and sets the next character in the party as active.
If the character is the last in the party, the party's active character is set to None.
Example:
try:
party.remove_character(character)
except CharacterNotInPartyError:
print(f"Character '{character.name}' wasn't in the party and thus couldn't be removed from it.")
Parameters:
-
character
(PlayerCharacter
) –The PC to remove from the party.
set_active_character
set_active_character(character: PlayerCharacter)
Sets the given character as the active, or "selected," character in the party.
The character must be a member of the party before you can set them as the active character.
Parameters:
-
character
(PlayerCharacter
) –The party member to set as the active or currently selected character.
Raises:
-
CharacterNotInPartyError
–Raised if the character is not in the party.
set_next_character_as_active
to_dict
to_dict() -> dict
Serializes the Party
to a dictionary, typically in preparation for writing it to persistent storage in a downstream operation.
PartyAtCapacityError
Bases: Exception
Raised when attempting to add a player character to a party that already has the maximum number of members.
PartyInStartedAdventureError
Bases: Exception
Raised when attempting to modify a party's roster when the party is in an adventure that's already been started.