Skip to content

Commit

Permalink
Fix inconsistent rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
mitaa committed Sep 19, 2024
1 parent fb1f8d3 commit 2efbcf7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
7 changes: 2 additions & 5 deletions src/production_planner/cells/ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from ._cells import Cell
from ..core import smartround

from rich.style import Style

Expand All @@ -27,10 +28,6 @@ def text_postprocess(self, text: str, style: Style) -> (str, Style):
def get(self):
if self.access_guard():
value = self.data.node_main.ingredients[self.vispath]
truncated_value = int(value)
if value - truncated_value < 0.01:
return truncated_value
else:
return round(value, 2)
return smartround(value)
else:
return self.default_na
7 changes: 4 additions & 3 deletions src/production_planner/cells/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from .. import core
from .. core import ModuleFile
from ..core import CONFIG, PRODUCER_NAMES, PRODUCER_MAP, Recipe, Ingredient, all_recipes_producer, Node, NodeInstance
from .. core import MODULE_PRODUCER
from ..core import MODULE_PRODUCER
from ..core import smartround

import os
from enum import Enum
Expand Down Expand Up @@ -193,8 +194,8 @@ def ingredient_count(attr):
row += [ProducerCell(NodeInstance(Node(producer, Recipe.empty()))).get_styled() if producer else ""]
row += [recipe.name]
# FIXME: production per minute should somehow be included in `str(Ingredient)`, otherwise we can't filter for that
inputs = [Text(f"({round(ingr.count*rate_mult): >3}/min) {ingr.count: >3}x{ingr.name}", style="red") for ingr in recipe.inputs]
outputs = [Text(f"({round(ingr.count*rate_mult): >3}/min) {ingr.count: >3}x{ingr.name}", style="green") for ingr in recipe.outputs]
inputs = [Text(f"({smartround(ingr.count*rate_mult): >3}/min) {ingr.count: >3}x{ingr.name}", style="red") for ingr in recipe.inputs]
outputs = [Text(f"({smartround(ingr.count*rate_mult): >3}/min) {ingr.count: >3}x{ingr.name}", style="green") for ingr in recipe.outputs]
inputs += [""] * (max_input_count - len(inputs))
outputs += [""] * (max_output_count - len(outputs))
row += outputs
Expand Down
10 changes: 10 additions & 0 deletions src/production_planner/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
APP = None


# TODO: add some ~IGNORED_AMOUNT variable to CONFIG and use that instead of hardcoded 0.01
# use this also for column highlighting ...
def smartround(value: float | int):
truncated_value = int(value)
if abs(value - truncated_value) < 0.01:
return truncated_value
else:
return round(value, 2)


def get_path(obj, path):
paths = path.split(".", maxsplit=1)
primpath = paths[0]
Expand Down
1 change: 0 additions & 1 deletion src/production_planner/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def update(self):
rate_mult = 60 / self.recipe.cycle_rate

ingredient_mult = rate_mult * (self.clock_rate * self.count) / 100
# FIXME: use floats to improve accuracy and modify number formatting display elsewhere
for inp in self.recipe.inputs:
self.ingredients[inp.name] = inp.count * ingredient_mult * -1

Expand Down

0 comments on commit 2efbcf7

Please sign in to comment.