Spaces:
Sleeping
Sleeping
adityaverma977 commited on
Commit ·
263efa8
1
Parent(s): e0206f3
Fix place-fire range errors and improve error detail
Browse files- backend/app/main.py +35 -6
- frontend/lib/api.ts +4 -1
backend/app/main.py
CHANGED
|
@@ -36,6 +36,13 @@ app.add_middleware(
|
|
| 36 |
active_simulations: dict[str, SimulationState] = {}
|
| 37 |
START_TIME = time.time()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
class StartSimulationRequest(BaseModel):
|
| 40 |
model_names: list[str] = Field(..., min_length=2, max_length=6)
|
| 41 |
scenario: str = "fire"
|
|
@@ -103,16 +110,38 @@ def place_fire(req: PlaceFireRequest):
|
|
| 103 |
if sim.status != "waiting_for_scenario":
|
| 104 |
raise HTTPException(status_code=409, detail="Fire already placed or simulation finished.")
|
| 105 |
|
| 106 |
-
# Create fire at
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# Generate 3-5 water sources scattered around the map
|
| 110 |
num_sources = random.randint(3, 5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
for i in range(num_sources):
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
sim.water_sources.append(WaterSource(id=f"water_{i}", x=water_x, y=water_y))
|
| 117 |
|
| 118 |
sim.status = "running"
|
|
|
|
| 36 |
active_simulations: dict[str, SimulationState] = {}
|
| 37 |
START_TIME = time.time()
|
| 38 |
|
| 39 |
+
|
| 40 |
+
def _safe_randint(low: int, high: int) -> int:
|
| 41 |
+
"""Return a valid random int even if bounds are inverted."""
|
| 42 |
+
if low > high:
|
| 43 |
+
low, high = high, low
|
| 44 |
+
return random.randint(low, high)
|
| 45 |
+
|
| 46 |
class StartSimulationRequest(BaseModel):
|
| 47 |
model_names: list[str] = Field(..., min_length=2, max_length=6)
|
| 48 |
scenario: str = "fire"
|
|
|
|
| 110 |
if sim.status != "waiting_for_scenario":
|
| 111 |
raise HTTPException(status_code=409, detail="Fire already placed or simulation finished.")
|
| 112 |
|
| 113 |
+
# Create fire at a clamped location inside map bounds.
|
| 114 |
+
fire_x = max(0, min(req.x, sim.map_width))
|
| 115 |
+
fire_y = max(0, min(req.y, sim.map_height))
|
| 116 |
+
sim.fire = FireScenario(x=fire_x, y=fire_y)
|
| 117 |
|
| 118 |
# Generate 3-5 water sources scattered around the map
|
| 119 |
num_sources = random.randint(3, 5)
|
| 120 |
+
x_margin = 80
|
| 121 |
+
y_margin = 80
|
| 122 |
+
x_min = x_margin
|
| 123 |
+
x_max = max(x_margin, sim.map_width - x_margin)
|
| 124 |
+
y_min = y_margin
|
| 125 |
+
y_max = max(y_margin, sim.map_height - y_margin)
|
| 126 |
+
|
| 127 |
for i in range(num_sources):
|
| 128 |
+
# Prefer spawning wells to one side of the fire, but always keep ranges valid.
|
| 129 |
+
left_low = x_min
|
| 130 |
+
left_high = min(fire_x - 180, x_max)
|
| 131 |
+
right_low = max(fire_x + 180, x_min)
|
| 132 |
+
right_high = x_max
|
| 133 |
+
|
| 134 |
+
pick_left = random.random() < 0.5
|
| 135 |
+
if pick_left and left_low <= left_high:
|
| 136 |
+
water_x = _safe_randint(left_low, left_high)
|
| 137 |
+
elif right_low <= right_high:
|
| 138 |
+
water_x = _safe_randint(right_low, right_high)
|
| 139 |
+
elif left_low <= left_high:
|
| 140 |
+
water_x = _safe_randint(left_low, left_high)
|
| 141 |
+
else:
|
| 142 |
+
water_x = _safe_randint(x_min, x_max)
|
| 143 |
+
|
| 144 |
+
water_y = _safe_randint(y_min, y_max)
|
| 145 |
sim.water_sources.append(WaterSource(id=f"water_{i}", x=water_x, y=water_y))
|
| 146 |
|
| 147 |
sim.status = "running"
|
frontend/lib/api.ts
CHANGED
|
@@ -35,7 +35,10 @@ export async function placeVolcano(simulationId: string, x: number, y: number) {
|
|
| 35 |
headers: { "Content-Type": "application/json" },
|
| 36 |
body: JSON.stringify({ simulation_id: simulationId, x, y }),
|
| 37 |
})
|
| 38 |
-
if (!response.ok)
|
|
|
|
|
|
|
|
|
|
| 39 |
return response.json()
|
| 40 |
}
|
| 41 |
|
|
|
|
| 35 |
headers: { "Content-Type": "application/json" },
|
| 36 |
body: JSON.stringify({ simulation_id: simulationId, x, y }),
|
| 37 |
})
|
| 38 |
+
if (!response.ok) {
|
| 39 |
+
const details = await response.text()
|
| 40 |
+
throw new Error(`Failed to place fire (${response.status}): ${details}`)
|
| 41 |
+
}
|
| 42 |
return response.json()
|
| 43 |
}
|
| 44 |
|