Spaces:
Sleeping
Sleeping
Update api/server.py
Browse files- api/server.py +34 -16
api/server.py
CHANGED
|
@@ -1,22 +1,40 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import tempfile
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import subprocess
|
| 4 |
import tempfile
|
| 5 |
+
import json
|
| 6 |
+
import random
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
+
WORLD = {"tick": 0, "agents": []}
|
| 10 |
|
| 11 |
+
class WorldInit(BaseModel):
|
| 12 |
+
agents: int
|
| 13 |
+
|
| 14 |
+
@app.get("/")
|
| 15 |
+
def root():
|
| 16 |
+
return {"status": "BCN Civilization Engine Running"}
|
| 17 |
+
|
| 18 |
+
@app.post("/world/init")
|
| 19 |
+
def init_world(cfg: WorldInit):
|
| 20 |
+
WORLD["tick"] = 0
|
| 21 |
+
WORLD["agents"] = [
|
| 22 |
+
{"id": f"node-{i}", "trust": 100, "credit": 0, "flow": 0}
|
| 23 |
+
for i in range(cfg.agents)
|
| 24 |
+
]
|
| 25 |
+
return {"status": "world initialized", "agents": cfg.agents}
|
| 26 |
+
|
| 27 |
+
@app.post("/world/step")
|
| 28 |
+
def step_world():
|
| 29 |
+
WORLD["tick"] += 1
|
| 30 |
+
|
| 31 |
+
for a in WORLD["agents"]:
|
| 32 |
+
a["trust"] += random.randint(-5, 5)
|
| 33 |
+
a["credit"] = max(0, a["credit"] + random.randint(-3, 3))
|
| 34 |
+
a["flow"] += random.randint(0, 20)
|
| 35 |
+
|
| 36 |
+
return {"tick": WORLD["tick"], "agents": WORLD["agents"]}
|
| 37 |
+
|
| 38 |
+
@app.get("/world/state")
|
| 39 |
+
def world_state():
|
| 40 |
+
return WORLD
|