checkpoint
This commit is contained in:
278
main.py
278
main.py
@@ -34,7 +34,7 @@ class Production(BaseModel):
|
||||
quantity: float
|
||||
|
||||
class ProductionChain:
|
||||
def __init__(self):
|
||||
def __init__(self, preferred_recipes: Optional[Dict[Items, Recipe]] = None):
|
||||
self.item_to_recipies: Dict[Items, list[Recipes]] = defaultdict(list)
|
||||
for r in Recipes:
|
||||
for o in r.value.outputs:
|
||||
@@ -49,9 +49,11 @@ class ProductionChain:
|
||||
self.production_chain: Dict[str, float] = defaultdict(float)
|
||||
self.excess: Dict[Items, float] = defaultdict(float)
|
||||
self.byproduct: Dict[Items, float] = defaultdict(float)
|
||||
self.preferred_recipes = preferred_recipes or {}
|
||||
|
||||
def get_recipe(self, item: Items) -> Optional[Recipe]:
|
||||
# TODO: make logic for priority selection of recipes
|
||||
if item in self.preferred_recipes:
|
||||
return self.preferred_recipes[item]
|
||||
return self.item_to_recipies.get(item, (None,))[0]
|
||||
|
||||
def calculate_excess(self, production: Dict[Items, float], demand: Dict[Items, float], raw_resources: Dict[Items, float]):
|
||||
@@ -102,7 +104,12 @@ class ProductionChain:
|
||||
levels.append(target_quantity / quantity)
|
||||
|
||||
production_level = max(levels)
|
||||
if production_level == 0:
|
||||
continue
|
||||
production_chain[recipe.name] += production_level
|
||||
if production_chain[recipe.name] < 0:
|
||||
del(production_chain[recipe.name])
|
||||
production_level = 0
|
||||
for out, quantity in recipe.outputs.items():
|
||||
production[out] += production_level * quantity
|
||||
for byproduct, quantity in recipe.byproducts.items():
|
||||
@@ -153,13 +160,77 @@ def index():
|
||||
name_to_item = {i.value.name: i for i in Items}
|
||||
|
||||
result = None
|
||||
error = None
|
||||
selected_item = item_names[0] if item_names else ""
|
||||
selected_rate = 60.0
|
||||
error_msgs: List[str] = []
|
||||
|
||||
# Read from query parameters for bookmarkable URLs
|
||||
item_name = request.args.get("item") or selected_item
|
||||
rate_str = request.args.get("rate")
|
||||
# --------------------
|
||||
# Parse targets (support multiple products)
|
||||
# --------------------
|
||||
# Accept both legacy single `item`/`rate` and indexed `item_1`/`rate_1`, ...
|
||||
# Determine how many indexed rows we have
|
||||
indexed_pairs: List[Tuple[str, str]] = []
|
||||
max_idx = 0
|
||||
for key in request.args.keys():
|
||||
if key.startswith("item_"):
|
||||
try:
|
||||
idx = int(key.split("_", 1)[1])
|
||||
max_idx = max(max_idx, idx)
|
||||
except ValueError:
|
||||
pass
|
||||
for i in range(1, max_idx + 1):
|
||||
indexed_pairs.append((request.args.get(f"item_{i}"), request.args.get(f"rate_{i}")))
|
||||
|
||||
# Build targets_ui (for rendering the form) and targets dict (for compute)
|
||||
targets_ui: List[dict] = []
|
||||
targets: Dict[Items, float] = defaultdict(float)
|
||||
|
||||
def _add_target_row(item_name: Optional[str], rate_str: Optional[str]):
|
||||
nonlocal targets, targets_ui
|
||||
if not item_name and not rate_str:
|
||||
return
|
||||
item_name = (item_name or "").strip()
|
||||
rate_val: Optional[float] = None
|
||||
if rate_str is not None and rate_str != "":
|
||||
try:
|
||||
rate_val = float(rate_str)
|
||||
if rate_val < 0:
|
||||
raise ValueError
|
||||
except (TypeError, ValueError):
|
||||
error_msgs.append(f"Invalid rate for product '{item_name or '(missing)'}'. Enter a non-negative number.")
|
||||
rate_val = None
|
||||
if item_name:
|
||||
targets_ui.append({"item": item_name, "rate": rate_val if rate_val is not None else 0.0})
|
||||
if rate_val is not None:
|
||||
item_obj = name_to_item.get(item_name)
|
||||
if item_obj is None:
|
||||
error_msgs.append(f"Unknown item '{item_name}'.")
|
||||
else:
|
||||
targets[item_obj] += rate_val
|
||||
elif rate_val is not None:
|
||||
# Rate without item name – just keep it in UI
|
||||
targets_ui.append({"item": "", "rate": rate_val})
|
||||
|
||||
if indexed_pairs:
|
||||
for item_nm, rate_s in indexed_pairs:
|
||||
_add_target_row(item_nm, rate_s)
|
||||
else:
|
||||
# Legacy single controls
|
||||
default_item = item_names[0] if item_names else ""
|
||||
single_item = request.args.get("item") or default_item
|
||||
single_rate = request.args.get("rate")
|
||||
_add_target_row(single_item, single_rate)
|
||||
if not request.args:
|
||||
# Initial load: ensure at least one default row
|
||||
targets_ui = [{"item": default_item, "rate": 60.0}]
|
||||
if default_item:
|
||||
item_obj0 = name_to_item.get(default_item)
|
||||
if item_obj0:
|
||||
targets[item_obj0] += 60.0
|
||||
|
||||
# Selected defaults are taken from the first row (for compatibility with existing template pieces)
|
||||
selected_item = targets_ui[0]["item"] if targets_ui else (item_names[0] if item_names else "")
|
||||
selected_rate = targets_ui[0]["rate"] if targets_ui else 60.0
|
||||
|
||||
# Optional top-level recipe (applies only when exactly one target)
|
||||
selected_recipe = request.args.get("recipe") or ""
|
||||
|
||||
# Parse per-item recipe overrides from query params recipe_for_<slug(item)>
|
||||
@@ -184,23 +255,9 @@ def index():
|
||||
if value in candidates:
|
||||
overrides[item_enum] = value
|
||||
|
||||
rate = None
|
||||
if rate_str is not None and rate_str != "":
|
||||
try:
|
||||
rate = float(rate_str)
|
||||
if rate < 0:
|
||||
raise ValueError
|
||||
except (TypeError, ValueError):
|
||||
error = "Please enter a valid non-negative number for rate (items per minute)."
|
||||
rate = None
|
||||
|
||||
selected_item = item_name
|
||||
if rate is not None:
|
||||
selected_rate = rate
|
||||
|
||||
# Determine candidate recipes for the selected output item
|
||||
# Candidate recipes for the (first) selected item (single-target convenience)
|
||||
recipe_options: List[str] = []
|
||||
item_obj_for_options = name_to_item.get(selected_item)
|
||||
item_obj_for_options = name_to_item.get(selected_item) if len(targets) <= 1 else None
|
||||
if item_obj_for_options is not None:
|
||||
for r in Recipes:
|
||||
recipe = r.value
|
||||
@@ -224,61 +281,129 @@ def index():
|
||||
# Compute and also prepare per-item override options based on resulting chain
|
||||
overrides_ui: List[dict] = []
|
||||
|
||||
if not error and item_name and rate is not None:
|
||||
item_obj = name_to_item.get(item_name)
|
||||
if item_obj is None:
|
||||
error = "Unknown item selected."
|
||||
else:
|
||||
targets = {item_obj: rate}
|
||||
raw, steps, outputs, unused = compute_chain(targets, preferred_by_output=preferred)
|
||||
result = {
|
||||
"targets": {item_name: rate},
|
||||
"raw": raw,
|
||||
"steps": steps,
|
||||
"outputs": outputs,
|
||||
"unused": unused,
|
||||
}
|
||||
if targets:
|
||||
# Translate preferred mapping (Items -> recipe name) into objects and pass to ProductionChain
|
||||
preferred_recipes_obj: Optional[Dict[Items, Recipe]] = None
|
||||
if preferred:
|
||||
name_to_recipe = {r.value.name: r.value for r in Recipes}
|
||||
preferred_recipes_obj = {}
|
||||
for itm, rec_name in preferred.items():
|
||||
rec_obj = name_to_recipe.get(rec_name)
|
||||
if rec_obj:
|
||||
preferred_recipes_obj[itm] = rec_obj
|
||||
prod_chain = ProductionChain(preferred_recipes=preferred_recipes_obj)
|
||||
prod_chain.compute_chain(targets)
|
||||
|
||||
# Collect unique output items from steps
|
||||
unique_items = []
|
||||
seen = set()
|
||||
for s in steps:
|
||||
item_nm = s.get("item")
|
||||
if item_nm and item_nm not in seen:
|
||||
seen.add(item_nm)
|
||||
unique_items.append(item_nm)
|
||||
# For each item, compute candidate recipes and current selection
|
||||
for item_nm in unique_items:
|
||||
item_enum2 = name_to_item.get(item_nm)
|
||||
if not item_enum2:
|
||||
continue
|
||||
candidates = []
|
||||
for r in Recipes:
|
||||
rec = r.value
|
||||
if item_enum2 in rec.outputs:
|
||||
candidates.append({
|
||||
"name": rec.name,
|
||||
"building": rec.building.value.name,
|
||||
})
|
||||
if len(candidates) <= 1:
|
||||
continue # only show when alternates exist
|
||||
candidates.sort(key=lambda x: (x["name"]))
|
||||
sel = None
|
||||
if preferred and item_enum2 in preferred:
|
||||
sel = preferred[item_enum2]
|
||||
slug = _slugify(item_nm)
|
||||
overrides_ui.append({
|
||||
"item_name": item_nm,
|
||||
"slug": slug,
|
||||
"options": candidates,
|
||||
"selected": sel or "",
|
||||
# Build UI-facing structures from ProductionChain state
|
||||
raw = {itm.value.name: qty for itm, qty in prod_chain.raw_resources.items() if qty > 0}
|
||||
unused = {itm.value.name: qty for itm, qty in prod_chain.byproduct.items() if qty > 0}
|
||||
excess = {itm.value.name: qty for itm, qty in prod_chain.excess.items() if qty > 0}
|
||||
outputs = {itm.value.name: qty for itm, qty in prod_chain.production.items() if qty > 0}
|
||||
|
||||
# Steps per recipe
|
||||
steps = []
|
||||
for recipe_name, level in prod_chain.production_chain.items():
|
||||
rec = prod_chain.recipe_name_to_obj.get(recipe_name)
|
||||
if not rec:
|
||||
continue
|
||||
chosen_item = None
|
||||
if rec.outputs:
|
||||
demanded = sorted(rec.outputs.keys(), key=lambda it: prod_chain.demand.get(it, 0.0), reverse=True)
|
||||
chosen_item = demanded[0]
|
||||
if not chosen_item:
|
||||
continue
|
||||
per_building_output = rec.outputs[chosen_item]
|
||||
buildings_float = float(level)
|
||||
buildings = ceil(buildings_float) if buildings_float > 0 else 0
|
||||
utilization = (buildings_float / buildings) if buildings > 0 else 0.0
|
||||
target_rate_item = buildings_float * per_building_output
|
||||
# Compute per-step input item rates for this recipe at the computed level
|
||||
step_inputs = []
|
||||
for inp_item, qty_per_building in rec.inputs.items():
|
||||
rate = buildings_float * qty_per_building
|
||||
step_inputs.append({
|
||||
"item": inp_item.value.name,
|
||||
"rate": rate,
|
||||
})
|
||||
# Sort inputs by descending rate for a stable display
|
||||
step_inputs.sort(key=lambda x: x["rate"], reverse=True)
|
||||
steps.append({
|
||||
"item": chosen_item.value.name,
|
||||
"recipe": rec.name,
|
||||
"building": rec.building.value.name,
|
||||
"target_rate": target_rate_item,
|
||||
"per_building_output": per_building_output,
|
||||
"buildings_float": buildings_float,
|
||||
"buildings": buildings,
|
||||
"utilization": utilization,
|
||||
"inputs": step_inputs,
|
||||
})
|
||||
|
||||
# Build reset query (clear overrides)
|
||||
reset_query = f"?item={selected_item}&rate={selected_rate}"
|
||||
# Aggregate targets for display
|
||||
result_targets = {}
|
||||
for itm, qty in targets.items():
|
||||
if qty > 0:
|
||||
result_targets[itm.value.name] = qty
|
||||
|
||||
result = {
|
||||
"targets": result_targets,
|
||||
"raw": raw,
|
||||
"steps": steps,
|
||||
"outputs": outputs,
|
||||
"unused": unused,
|
||||
"excess": excess,
|
||||
}
|
||||
|
||||
# Collect unique output items from steps for override options
|
||||
unique_items = []
|
||||
seen = set()
|
||||
for s in steps:
|
||||
item_nm = s.get("item")
|
||||
if item_nm and item_nm not in seen:
|
||||
seen.add(item_nm)
|
||||
unique_items.append(item_nm)
|
||||
for item_nm in unique_items:
|
||||
item_enum2 = name_to_item.get(item_nm)
|
||||
if not item_enum2:
|
||||
continue
|
||||
candidates = []
|
||||
for r in Recipes:
|
||||
rec = r.value
|
||||
if item_enum2 in rec.outputs:
|
||||
candidates.append({
|
||||
"name": rec.name,
|
||||
"building": rec.building.value.name,
|
||||
})
|
||||
if len(candidates) <= 1:
|
||||
continue
|
||||
candidates.sort(key=lambda x: (x["name"]))
|
||||
sel = None
|
||||
if preferred and item_enum2 in preferred:
|
||||
sel = preferred[item_enum2]
|
||||
slug = _slugify(item_nm)
|
||||
overrides_ui.append({
|
||||
"item_name": item_nm,
|
||||
"slug": slug,
|
||||
"options": candidates,
|
||||
"selected": sel or "",
|
||||
})
|
||||
|
||||
# Build reset query (clear overrides) while preserving current targets
|
||||
if indexed_pairs or len(targets_ui) > 1:
|
||||
# Multi-target
|
||||
parts = []
|
||||
for idx, row in enumerate(targets_ui, start=1):
|
||||
parts.append(f"item_{idx}={row['item']}")
|
||||
parts.append(f"rate_{idx}={row['rate']}")
|
||||
reset_query = "?" + "&".join(parts)
|
||||
else:
|
||||
reset_query = f"?item={selected_item}&rate={selected_rate}"
|
||||
if selected_recipe:
|
||||
reset_query += f"&recipe={selected_recipe}"
|
||||
|
||||
# Combine error messages
|
||||
error = " ".join(error_msgs) if error_msgs else None
|
||||
|
||||
return render_template(
|
||||
"index.html",
|
||||
items=item_names,
|
||||
@@ -290,6 +415,7 @@ def index():
|
||||
selected_recipe=selected_recipe,
|
||||
overrides_ui=overrides_ui,
|
||||
reset_query=reset_query,
|
||||
targets_ui=targets_ui,
|
||||
)
|
||||
|
||||
|
||||
@@ -299,6 +425,6 @@ def create_app():
|
||||
|
||||
if __name__ == "__main__":
|
||||
# For local dev: python main.py
|
||||
# app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
prod_chain = ProductionChain()
|
||||
prod_chain.compute_chain({Items.ModularFrame: 1.5})
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
# prod_chain = ProductionChain()
|
||||
# prod_chain.compute_chain({Items.CateriumHeatsink: 10.0})
|
||||
Reference in New Issue
Block a user