Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

94 improve exception strings #129

Merged
merged 10 commits into from
Jan 10, 2024
Merged
30 changes: 19 additions & 11 deletions game/common/items/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,46 +93,53 @@ def stack_size(self) -> int:
@durability.setter
def durability(self, durability: int | None) -> None:
if durability is not None and not isinstance(durability, int):
raise ValueError(f'{self.__class__.__name__}.durability must be an int or None.')
raise ValueError(
f'{self.__class__.__name__}.durability must be an int. It is a(n) {type(durability)} with the value of {durability}.')
if durability is not None and self.stack_size != 1:
raise ValueError(
f'{self.__class__.__name__}.durability must be set to None if stack_size is not equal to 1.')
f'{self.__class__.__name__}.durability must be set to None if stack_size is not equal to 1. {self.__class__.__name__}.durability has the value of {durability}.')
self.__durability = durability

@value.setter
def value(self, value: int) -> None:
if value is None or not isinstance(value, int):
raise ValueError(f'{self.__class__.__name__}.value must be an int.')
raise ValueError(
f'{self.__class__.__name__}.value must be an int. It is a(n) {type(value)} with the value of {value}.')
self.__value: int = value

@quantity.setter
def quantity(self, quantity: int) -> None:
if quantity is None or not isinstance(quantity, int):
raise ValueError(f'{self.__class__.__name__}.quantity must be an int.')
raise ValueError(
f'{self.__class__.__name__}.quantity must be an int. It is a(n) {type(quantity)} with the value of {quantity}.')
if quantity < 0:
raise ValueError(f'{self.__class__.__name__}.quantity must be greater than or equal to 0.')
raise ValueError(
f'{self.__class__.__name__}.quantity must be greater than or equal to 0. {self.__class__.__name__}.quantity has the value of {quantity}.')

# The self.quantity is set to the lower value between stack_size and the given quantity
# The remaining given quantity is returned if it's larger than self.quantity
if quantity > self.stack_size:
raise ValueError(f'{self.__class__.__name__}.quantity cannot be greater than '
f'{self.__class__.__name__}.stack_size')
f'{self.__class__.__name__}.stack_size. {self.__class__.__name__}.quantity has the value of {quantity}.')
self.__quantity: int = quantity

@stack_size.setter
def stack_size(self, stack_size: int) -> None:
if stack_size is None or not isinstance(stack_size, int):
raise ValueError(f'{self.__class__.__name__}.stack_size must be an int.')
raise ValueError(
f'{self.__class__.__name__}.stack_size must be an int. It is a(n) {type(stack_size)} with the value of {stack_size}.')
if self.durability is not None and stack_size != 1:
raise ValueError(f'{self.__class__.__name__}.stack_size must be 1 if {self.__class__.__name__}.durability '
f'is not None.')
f'is not None. {self.__class__.__name__}.stack_size has the value of {stack_size}.')
if self.__quantity is not None and stack_size < self.__quantity:
raise ValueError(f'{self.__class__.__name__}.stack_size must be greater than or equal to the quantity.')
raise ValueError(
f'{self.__class__.__name__}.stack_size must be greater than or equal to the quantity. {self.__class__.__name__}.stack_size has the value of {stack_size}.')
self.__stack_size: int = stack_size

def take(self, item: Self) -> Self | None:
if item is not None and not isinstance(item, Item):
raise ValueError(f'{item.__class__.__name__} is not of type Item.')
raise ValueError(
f'{item.__class__.__name__}.item must be an item. It is a(n) {type(item)} with the value of {item}.')

# If the item is None, just return None
if item is None:
Expand All @@ -156,7 +163,8 @@ def take(self, item: Self) -> Self | None:

def pick_up(self, item: Self) -> Self | None:
if item is not None and not isinstance(item, Item):
raise ValueError(f'{item.__class__.__name__} is not of type Item.')
raise ValueError(
f'{item.__class__.__name__}.item must be an item. It is a(n) {type(item)} with the value of {item}.')

# If the item is None, just return None
if item is None:
Expand Down
32 changes: 16 additions & 16 deletions game/common/map/game_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def seed(self, seed: int | None) -> None:
if self.game_map is not None:
raise RuntimeError(f'{self.__class__.__name__} variables cannot be changed once generate_map is run.')
if seed is not None and not isinstance(seed, int):
raise ValueError(f'{self.__class__.__name__}.seed must be an integer or None.')
raise ValueError(
f'{self.__class__.__name__}.seed must be an int. It is a(n) {type(seed)} with the value of {seed}.')
self.__seed = seed

@property
Expand All @@ -149,7 +150,8 @@ def game_map(self, game_map: list[list[Tile]]) -> None:
any(map(lambda l: not isinstance(l, list), game_map)) or
any([any(map(lambda g: not isinstance(g, Tile), tile_list))
for tile_list in game_map])):
raise ValueError(f'{self.__class__.__name__}.game_map must be a list[list[Tile]].')
raise ValueError(
f'{self.__class__.__name__}.game_map must be a list[list[Tile]]. It is a(n) {type(game_map)} with the value of {game_map}.')
self.__game_map = game_map

@property
Expand All @@ -161,7 +163,8 @@ def map_size(self, map_size: Vector) -> None:
if self.game_map is not None:
raise RuntimeError(f'{self.__class__.__name__} variables cannot be changed once generate_map is run.')
if map_size is None or not isinstance(map_size, Vector):
raise ValueError(f'{self.__class__.__name__}.map_size must be a Vector.')
raise ValueError(
f'{self.__class__.__name__}.map_size must be a Vector. It is a(n) {type(map_size)} with the value of {map_size}.')
self.__map_size = map_size

@property
Expand All @@ -173,13 +176,8 @@ def locations(self, locations: dict[tuple[Vector]:list[GameObject]] | None) -> N
if self.game_map is not None:
raise RuntimeError(f'{self.__class__.__name__} variables cannot be changed once generate_map is run.')
if locations is not None and not isinstance(locations, dict):
raise ValueError("Locations must be a dict. The key must be a tuple of Vector Objects, and the "
"value a list of GameObject.")
# if locations is not None:
# for k, v in locations.items():
# if len(k) != len(v):
# raise ValueError("Cannot set the locations for the game_board. A key has a different "
# "length than its key.")
raise ValueError(
f'Locations must be a dict. The key must be a tuple of Vector Objects, and the value a list of GameObject. It is a(n) {type(locations)} with the value of {locations}.')

self.__locations = locations

Expand All @@ -192,7 +190,8 @@ def walled(self, walled: bool) -> None:
if self.game_map is not None:
raise RuntimeError(f'{self.__class__.__name__} variables cannot be changed once generate_map is run.')
if walled is None or not isinstance(walled, bool):
raise ValueError(f'{self.__class__.__name__}.walled must be a bool.')
raise ValueError(
f'{self.__class__.__name__}.walled must be a bool. It is a(n) {type(walled)} with the value of {walled}.')

self.__walled = walled

Expand All @@ -213,7 +212,8 @@ def generate_map(self) -> None:
def __populate_map(self) -> None:
for k, v in self.locations.items():
if len(k) == 0 or len(v) == 0: # Key-Value lengths must be > 0 and equal
raise ValueError("A key-value pair from game_board.locations has a length of 0. ")
raise ValueError(
f'A key-value pair from game_board.locations has a length of 0. The length of the keys is {len(k)} and the length of the values is {len(v)}.')

# random.sample returns a randomized list which is used in __help_populate()
j = random.sample(k, k=len(k))
Expand Down Expand Up @@ -253,7 +253,8 @@ def __help_populate(self, vector_list: list[Vector], game_object_list: list[Game
temp_tile = temp_tile.occupied_by

if temp_tile.occupied_by is not None:
raise ValueError("Last item on the given tile doesn't have the 'occupied_by' attribute.")
raise ValueError(
f'Last item on the given tile doesn\'t have the \'occupied_by\' attribute. It is a(n) {type(temp_tile.occupied_by)}.')

temp_tile.occupied_by = game_object

Expand All @@ -268,7 +269,7 @@ def __help_populate(self, vector_list: list[Vector], game_object_list: list[Game

for game_object in remaining_objects:
if not hasattr(temp_tile, 'occupied_by') or temp_tile.occupied_by is not None:
raise ValueError("Last item on the given tile doesn't have the 'occupied_by' attribute.")
raise ValueError(f'Last item on the given tile doesn\'t have the \'occupied_by\' attribute. It is a(n) {type(temp_tile.occupied_by)}.')
temp_tile.occupied_by = game_object
temp_tile = temp_tile.occupied_by

Expand Down Expand Up @@ -327,8 +328,7 @@ def __from_json_helper(self, data: dict) -> GameObject:
return Avatar().from_json(data)
# If adding more ObjectTypes that can be placed on the game_board, specify here
case _:
raise ValueError(f'The location (dict) must have a valid key (tuple of vectors) and a valid value ('
f'list of GameObjects).')
raise ValueError(f'The object type of the object is not handled properly. The object type passed in is {temp}.')

def from_json(self, data: dict) -> Self:
super().from_json(data)
Expand Down
Loading