From 459bef9c454973095cb0bef84c5871ebe213f6d4 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Fri, 2 Feb 2024 22:35:34 +0100 Subject: [PATCH] Fix ResourceFlow.amount_per_minute --- factorygame/data/models.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/factorygame/data/models.py b/factorygame/data/models.py index a294ae4..f561d5a 100644 --- a/factorygame/data/models.py +++ b/factorygame/data/models.py @@ -5,8 +5,8 @@ from typing import Optional from sqlalchemy import String, Select, select, Table, Column, ForeignKey from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship -amount_number = re.compile(r"^\d*(\.\d+)?") -time_number = re.compile(r"^(\d+) seconds?") +amount_per_step_regex = re.compile(r"^\d*(\.\d+)?") +time_per_step_regex = re.compile(r"^(\d+(\.\d+)?) seconds?") class Base(DeclarativeBase): @@ -83,11 +83,20 @@ class ResourceFlow(Base): time: Mapped[str] def amount_per_minute(self) -> float: - per_crafting_step_str = amount_number.match(self.amount).group(0) - per_crafting_step = float(per_crafting_step_str) - crafting_time_seconds_str = time_number.match(self.time).group(1) - crafting_time_seconds = float(crafting_time_seconds_str) - return per_crafting_step * (60.0 / crafting_time_seconds) + amount_per_step_match = amount_per_step_regex.match(self.amount) + assert amount_per_step_match, ( + f"Amount per crafting step of resource {self.resource.label} in crafting recipe " + f"{self.result_of or self.ingredient_in} could not be parsed" + ) + amount_per_step = float(amount_per_step_match.group(0)) + + time_per_step_match = time_per_step_regex.match(self.time) + assert time_per_step_match, ( + f"Time per crafting step of resource {self.resource.label} in crafting recipe " + f"{self.result_of or self.ingredient_in} could not be parsed" + ) + time_per_step = float(time_per_step_match.group(1)) + return amount_per_step * (60.0 / time_per_step) def describe(self) -> str: return f"{repr(str(self.resource.label))}x{self.amount}" @@ -120,4 +129,6 @@ class Recipe(Base): ) def __repr__(self): - return f"Recipe(id={self.id}, factories={self.factories}, ingredients={self.ingredients}, results={self.results})" + return ( + f"Recipe(id={self.id}, factories={self.factories}, ingredients={self.ingredients}, results={self.results})" + )