SeaWolf-AI commited on
Commit
184d290
Β·
verified Β·
1 Parent(s): ffa2a34

arcade: app

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # -*- coding: utf-8 -*-
2
- """κ΄€λ¬Έ ν…ŒνŠΈλ¦¬μŠ€ Space β€” 정적 νŽ˜μ΄μ§€ + λˆ„μ  전적(프라이빗 데이터셋) 기둝
3
 
4
  πŸ”΄ 전적은 Space λ””μŠ€ν¬κ°€ μ•„λ‹ˆλΌ **데이터셋 repo** 에 μ“΄λ‹€. Space λŠ” μž¬μ‹œμž‘ν•˜λ©΄ λ””μŠ€ν¬κ°€ λ‚ μ•„κ°„λ‹€.
5
  """
@@ -12,7 +12,8 @@ DS = os.environ.get("RECORD_DATASET", "FINAL-Bench/gate-tetris-record")
12
  TOK = os.environ.get("HF_TOKEN")
13
  api = HfApi(token=TOK)
14
  LOCK = threading.Lock()
15
- BLANK = {"matches": 0, "wins": {"plain": 0, "jev": 0, "ztc": 0}, "saved": 0.0, "recent": []}
 
16
 
17
  app = FastAPI()
18
  HTML = open(os.path.join(os.path.dirname(__file__), "index.html"), encoding="utf-8").read()
@@ -24,13 +25,15 @@ def load():
24
  force_download=True)
25
  with open(p, encoding="utf-8") as f:
26
  t = json.load(f)
27
- for k, v in BLANK.items():
28
- t.setdefault(k, v if not isinstance(v, dict) else dict(v))
29
- for k in ("plain", "jev", "ztc"):
30
- t["wins"].setdefault(k, 0)
31
- return t
32
  except Exception:
33
- return json.loads(json.dumps(BLANK))
 
 
 
 
 
 
 
34
 
35
 
36
  def save(t):
@@ -56,23 +59,25 @@ async def post_record(req: Request):
56
  win = body.get("winner")
57
  if win not in ("plain", "jev", "ztc"):
58
  return JSONResponse({"error": "bad winner"}, status_code=400)
59
- lines = body.get("lines") or {}
 
60
  mode = "human" if body.get("mode") == "human" else "auto"
61
- jc = float(body.get("jevCost") or 0)
62
- zc = float(body.get("ztcCost") or 0)
63
  with LOCK:
64
  t = load()
65
- t["matches"] = int(t.get("matches", 0)) + 1
66
- t["wins"][win] = int(t["wins"].get(win, 0)) + 1
67
- t["saved"] = float(t.get("saved", 0)) + max(0.0, jc - zc)
68
  row = {"mode": mode, "winner": win,
69
- "lines": {k: int(lines.get(k, 0)) for k in ("plain", "jev", "ztc")},
70
- "jevCost": round(jc, 1), "ztcCost": round(zc, 2)}
71
- t["recent"] = ([row] + list(t.get("recent", [])))[:50]
 
72
  try:
73
  save(t)
74
  except Exception as e:
75
- return JSONResponse({"error": "save failed: %s" % e, **t}, status_code=200)
 
76
  return JSONResponse(t)
77
 
78
 
 
1
  # -*- coding: utf-8 -*-
2
+ """행동 κ΄€λ¬Έ μ•„μΌ€μ΄λ“œ Space β€” 정적 νŽ˜μ΄μ§€ + λˆ„μ  전적(프라이빗 데이터셋)
3
 
4
  πŸ”΄ 전적은 Space λ””μŠ€ν¬κ°€ μ•„λ‹ˆλΌ **데이터셋 repo** 에 μ“΄λ‹€. Space λŠ” μž¬μ‹œμž‘ν•˜λ©΄ λ””μŠ€ν¬κ°€ λ‚ μ•„κ°„λ‹€.
5
  """
 
12
  TOK = os.environ.get("HF_TOKEN")
13
  api = HfApi(token=TOK)
14
  LOCK = threading.Lock()
15
+ BLANK = {"matches": 0, "wins": {"plain": 0, "jev": 0, "ztc": 0},
16
+ "blocked": 0, "recent": []}
17
 
18
  app = FastAPI()
19
  HTML = open(os.path.join(os.path.dirname(__file__), "index.html"), encoding="utf-8").read()
 
25
  force_download=True)
26
  with open(p, encoding="utf-8") as f:
27
  t = json.load(f)
 
 
 
 
 
28
  except Exception:
29
+ t = {}
30
+ out = json.loads(json.dumps(BLANK))
31
+ out["matches"] = int(t.get("matches", 0) or 0)
32
+ for k in ("plain", "jev", "ztc"):
33
+ out["wins"][k] = int((t.get("wins") or {}).get(k, 0) or 0)
34
+ out["blocked"] = int(t.get("blocked", 0) or 0)
35
+ out["recent"] = list(t.get("recent") or [])
36
+ return out
37
 
38
 
39
  def save(t):
 
59
  win = body.get("winner")
60
  if win not in ("plain", "jev", "ztc"):
61
  return JSONResponse({"error": "bad winner"}, status_code=400)
62
+ sc = body.get("score") or {}
63
+ ac = body.get("acted") or {}
64
  mode = "human" if body.get("mode") == "human" else "auto"
65
+ blocked = int(body.get("blocked") or 0)
 
66
  with LOCK:
67
  t = load()
68
+ t["matches"] += 1
69
+ t["wins"][win] += 1
70
+ t["blocked"] += max(0, blocked)
71
  row = {"mode": mode, "winner": win,
72
+ "score": {k: int(sc.get(k, 0) or 0) for k in ("plain", "jev", "ztc")},
73
+ "acted": {k: int(ac.get(k, 0) or 0) for k in ("plain", "jev", "ztc")},
74
+ "blocked": max(0, blocked)}
75
+ t["recent"] = ([row] + t["recent"])[:50]
76
  try:
77
  save(t)
78
  except Exception as e:
79
+ out = dict(t); out["error"] = "save failed: %s" % e
80
+ return JSONResponse(out, status_code=200)
81
  return JSONResponse(t)
82
 
83