Fix: MapPlane.has_multiple_floors

This commit is contained in:
LmeSzinc 2023-07-30 19:33:54 +08:00
parent 7e778efa17
commit c71dc5296b

View File

@ -48,12 +48,13 @@ class MapPlane(Keyword):
@cached_property @cached_property
def has_multiple_floors(self): def has_multiple_floors(self):
return self.floors == ['F1'] return self.floors and self.floors != ['F1']
def convert_to_floor_index(self, floor: str) -> int: def convert_to_floor_index(self, floor: str | int) -> int:
""" """
Args: Args:
floor: Floor names in game, such as "B1", "F1", "F2" floor: int for floor index counted from bottom, such as 1, 2, 3
str for floor names in game, such as "B1", "F1", "F2"
Returns: Returns:
int: 1 to 3 counted from bottom. int: 1 to 3 counted from bottom.
@ -61,16 +62,28 @@ class MapPlane(Keyword):
Raises: Raises:
ScriptError: If floor doesn't exist ScriptError: If floor doesn't exist
""" """
floor = floor.upper() if isinstance(floor, int):
try: # Check exist
return self.floors.index(floor) + 1 try:
except IndexError: _ = self.floors[floor - 1]
raise ScriptError(f'Plane {self} does not have floor name {floor}') return floor
except IndexError:
raise ScriptError(f'Plane {self} does not have floor index {floor}')
elif isinstance(floor, str):
# Convert to floor index
floor = floor.upper()
try:
return self.floors.index(floor) + 1
except IndexError:
raise ScriptError(f'Plane {self} does not have floor name {floor}')
else:
raise ScriptError(f'Plane {self} does not have floor {floor}')
def convert_to_floor_name(self, floor: int) -> str: def convert_to_floor_name(self, floor: str | int) -> str:
""" """
Args: Args:
floor: Floor index counted from bottom, such as 1, 2, 3 floor: int for floor index counted from bottom, such as 1, 2, 3
str for floor names in game, such as "B1", "F1", "F2"
Returns: Returns:
str: Floor names in game, such as "B1", "F1", "F2" str: Floor names in game, such as "B1", "F1", "F2"
@ -78,7 +91,19 @@ class MapPlane(Keyword):
Raises: Raises:
ScriptError: If floor doesn't exist ScriptError: If floor doesn't exist
""" """
try: if isinstance(floor, int):
return self.floors[floor - 1] # Convert to floor index
except IndexError: try:
raise ScriptError(f'Plane {self} does not have floor index {floor}') return self.floors[floor - 1]
except IndexError:
raise ScriptError(f'Plane {self} does not have floor index {floor}')
elif isinstance(floor, str):
# Check exist
floor = floor.upper()
try:
_ = self.floors.index(floor) + 1
return floor
except IndexError:
raise ScriptError(f'Plane {self} does not have floor name {floor}')
else:
raise ScriptError(f'Plane {self} does not have floor {floor}')