Update app.py
Browse files
app.py
CHANGED
|
@@ -388,4 +388,42 @@ async def game_loop():
|
|
| 388 |
|
| 389 |
@app.on_event("startup")
|
| 390 |
async def on_startup():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
asyncio.create_task(game_loop())
|
|
|
|
|
|
| 388 |
|
| 389 |
@app.on_event("startup")
|
| 390 |
async def on_startup():
|
| 391 |
+
global MARKET, EVENTS, CURRENT_DAY, CURRENT_VOL
|
| 392 |
+
|
| 393 |
+
# ---- Load default scenario file (Option B) ----
|
| 394 |
+
scenario_path = os.getenv("DEFAULT_SCENARIO_FILE", "default_scenario.json")
|
| 395 |
+
if os.path.exists(scenario_path):
|
| 396 |
+
with open(scenario_path, "r", encoding="utf-8") as f:
|
| 397 |
+
scn = json.load(f)
|
| 398 |
+
|
| 399 |
+
start_day = int(scn.get("startDay", 0))
|
| 400 |
+
base_price = float(scn.get("basePrice", START_PRICE))
|
| 401 |
+
default_vol = float(scn.get("defaultVolatility", DEFAULT_VOLATILITY))
|
| 402 |
+
evs_raw = scn.get("events", [])
|
| 403 |
+
if not isinstance(evs_raw, list):
|
| 404 |
+
evs_raw = []
|
| 405 |
+
|
| 406 |
+
evs = [parse_event(e) for e in evs_raw]
|
| 407 |
+
max_day_in_scenario = max((ev.day for ev in evs), default=0)
|
| 408 |
+
|
| 409 |
+
# If scenario provides marketLength, use it as a hint; always ensure it's big enough.
|
| 410 |
+
requested_len = scn.get("marketLength", None)
|
| 411 |
+
if requested_len is None or str(requested_len).strip() == "":
|
| 412 |
+
desired_len = max(MIN_MARKET_LENGTH, MARKET_LENGTH, max_day_in_scenario + 1)
|
| 413 |
+
else:
|
| 414 |
+
desired_len = max(MIN_MARKET_LENGTH, int(requested_len), max_day_in_scenario + 1)
|
| 415 |
+
|
| 416 |
+
async with STATE_LOCK:
|
| 417 |
+
CURRENT_VOL = default_vol
|
| 418 |
+
MARKET = regen_market(length=desired_len, start_price=base_price, vol=default_vol)
|
| 419 |
+
|
| 420 |
+
EVENTS.clear()
|
| 421 |
+
for ev in evs:
|
| 422 |
+
EVENTS.setdefault(ev.day, []).append(ev)
|
| 423 |
+
|
| 424 |
+
async with DAY_LOCK:
|
| 425 |
+
CURRENT_DAY = max(0, min(start_day, len(MARKET) - 1))
|
| 426 |
+
|
| 427 |
+
# ---- Start the tick loop ----
|
| 428 |
asyncio.create_task(game_loop())
|
| 429 |
+
|