Fix ResourceFlow.amount_per_minute

This commit is contained in:
Ben 2024-02-02 22:35:34 +01:00
parent b0a9fecde5
commit 459bef9c45
Signed by: ben
GPG key ID: 0F54A7ED232D3319

View file

@ -5,8 +5,8 @@ from typing import Optional
from sqlalchemy import String, Select, select, Table, Column, ForeignKey from sqlalchemy import String, Select, select, Table, Column, ForeignKey
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
amount_number = re.compile(r"^\d*(\.\d+)?") amount_per_step_regex = re.compile(r"^\d*(\.\d+)?")
time_number = re.compile(r"^(\d+) seconds?") time_per_step_regex = re.compile(r"^(\d+(\.\d+)?) seconds?")
class Base(DeclarativeBase): class Base(DeclarativeBase):
@ -83,11 +83,20 @@ class ResourceFlow(Base):
time: Mapped[str] time: Mapped[str]
def amount_per_minute(self) -> float: def amount_per_minute(self) -> float:
per_crafting_step_str = amount_number.match(self.amount).group(0) amount_per_step_match = amount_per_step_regex.match(self.amount)
per_crafting_step = float(per_crafting_step_str) assert amount_per_step_match, (
crafting_time_seconds_str = time_number.match(self.time).group(1) f"Amount per crafting step of resource {self.resource.label} in crafting recipe "
crafting_time_seconds = float(crafting_time_seconds_str) f"{self.result_of or self.ingredient_in} could not be parsed"
return per_crafting_step * (60.0 / crafting_time_seconds) )
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: def describe(self) -> str:
return f"{repr(str(self.resource.label))}x{self.amount}" return f"{repr(str(self.resource.label))}x{self.amount}"
@ -120,4 +129,6 @@ class Recipe(Base):
) )
def __repr__(self): 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})"
)