Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2062f917b0 | |||
| 01c7176b1c | |||
| 1ee963ce2e | |||
| 351cac7734 | |||
| 79dccde963 |
@@ -32,8 +32,12 @@ htmlcov/
|
||||
# IDE and editor files
|
||||
.idea/
|
||||
.vscode/
|
||||
.codex/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# debug resources
|
||||
debug/
|
||||
|
||||
# Node dependencies for browser automation tooling
|
||||
node_modules/
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# Repository Guidelines
|
||||
|
||||
## Project Structure & Module Organization
|
||||
|
||||
Core poker service code lives in `texas_holdem/`. Important modules include `engine.py` for Texas Hold'em rules, `service.py` for game management, `server.py` for the HTTP API, `agents.py` for local/HTTP agents, and `ai_client.py` / `human_client.py` for standalone agents. Prompt templates live in `texas_holdem/prompts/`.
|
||||
|
||||
## Build, Test, and Development Commands
|
||||
|
||||
- `python -m unittest discover -v` runs the full test suite.
|
||||
- `python -m texas_holdem.server --host 127.0.0.1 --port 8000` starts the game service.
|
||||
- `python -m texas_holdem.human_client --port 9001 --keep-history` starts an interactive human HTTP agent.
|
||||
- `python -m texas_holdem.ai_client --port 9101 --api-key "$OPENAI_API_KEY" --model gpt-4o-mini` starts an OpenAI-compatible AI agent.
|
||||
|
||||
## Coding Style & Naming Conventions
|
||||
|
||||
Use Python 3.11+ standard-library APIs unless a dependency is intentionally added to `pyproject.toml`. Keep modules focused and prefer explicit dataclasses for wire/state models. Use 4-space indentation, type hints, `snake_case` for functions and variables, `PascalCase` for classes, and concise comments only where logic is non-obvious.
|
||||
|
||||
## Testing Guidelines
|
||||
|
||||
Use `unittest`. Add tests near the behavior changed: engine rules in `tests/test_engine.py`, HTTP/service behavior in `tests/test_service.py`, agent transport in `tests/test_agents.py`. New bug fixes should include a regression test. Avoid tests that require external network access or real LLM calls.
|
||||
|
||||
## Commit & Pull Request Guidelines
|
||||
|
||||
History uses short Conventional Commit-style subjects, for example `feat: add server and agent client` and `fix: game service api block when a game is running`. Keep commits scoped to one behavior change. Pull requests should include a short summary, test commands run, linked issue or motivation, and screenshots only for visible terminal-output changes.
|
||||
|
||||
## Security & Configuration Tips
|
||||
|
||||
Do not commit API keys. Pass LLM credentials through `OPENAI_API_KEY` or CLI flags in local shells only. HTTP Agent endpoints are exclusive per active game; preserve this invariant when changing service concurrency.
|
||||
@@ -11,6 +11,7 @@
|
||||
- 支持盲注、四条街下注、弃牌、过牌、跟注、下注、加注、全下、边池和摊牌结算。
|
||||
- 支持本地 Agent 和 HTTP Agent。
|
||||
- 支持 Human Agent 和 OpenAI-compatible AI Agent 的终端过程输出。
|
||||
- 游戏运行中可以并发查询状态;查询返回上一手完成后的稳定快照。
|
||||
|
||||
## 运行服务
|
||||
|
||||
@@ -51,6 +52,12 @@ curl -X POST http://127.0.0.1:8000/games/demo/hands/run \
|
||||
curl http://127.0.0.1:8000/games/demo
|
||||
```
|
||||
|
||||
也可以使用单数别名:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8000/game/demo
|
||||
```
|
||||
|
||||
## HTTP Agent 协议
|
||||
|
||||
玩家配置可以使用远程 HTTP Agent:
|
||||
@@ -62,12 +69,19 @@ curl http://127.0.0.1:8000/games/demo
|
||||
"agent": {
|
||||
"type": "http",
|
||||
"endpoint": "http://127.0.0.1:9101",
|
||||
"timeout_seconds": 10
|
||||
"timeout_seconds": 10,
|
||||
"game_update_timeout_seconds": 3,
|
||||
"retries": 2,
|
||||
"retry_backoff_seconds": 0.25
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
服务会向 `endpoint` 发送当前行动玩家的观察 JSON。Agent 返回:
|
||||
服务会向 `endpoint + /game` 推送每手开始时的游戏快照,向 `endpoint + /act` 发送当前行动玩家的观察 JSON。`endpoint` 也可以传入历史形式的 `/act` 或 `/game` 后缀,服务会归一化为 base URL。
|
||||
|
||||
同一个 HTTP Agent endpoint 不能同时被不同游戏占用;后创建的游戏会返回错误。服务会给 HTTP Agent 请求自动重试,`/act` 重试仍失败时,规则引擎会按 `check > call > fold` 选择默认动作,避免整桌卡死。
|
||||
|
||||
Agent 返回:
|
||||
|
||||
```json
|
||||
{"action": "call"}
|
||||
@@ -112,3 +126,22 @@ AI Agent 会在终端输出:
|
||||
```bash
|
||||
python -m unittest discover -v
|
||||
```
|
||||
|
||||
## Web 回放与控制台
|
||||
|
||||
启动核心游戏服务后,可以单独启动 Web 回放服务:
|
||||
|
||||
```bash
|
||||
python -m texas_holdem_replay.server --host 127.0.0.1 --port 8088
|
||||
```
|
||||
|
||||
打开 `http://127.0.0.1:8088`。页面通过自身的代理接口访问核心服务,
|
||||
避免浏览器跨域限制;它不会导入或耦合 `texas_holdem.engine` 内部代码。
|
||||
|
||||
页面支持:
|
||||
|
||||
- 拉取 `GET /games/{game_id}` 快照并按 `hands[].actions` 生成逐帧回放。
|
||||
- 通过代理调用核心服务运行指定数量手牌。
|
||||
- 可选覆盖下一批手牌的大小盲。
|
||||
- 上传或粘贴静态 JSON 快照进行离线回放。
|
||||
- 自动轮询正在运行的游戏,保留当前历史查看位置。
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from urllib.error import URLError
|
||||
|
||||
from texas_holdem.agents import HttpAgent, normalise_http_agent_endpoint
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, payload: dict[str, object]) -> None:
|
||||
self.payload = payload
|
||||
|
||||
def read(self) -> bytes:
|
||||
return json.dumps(self.payload).encode("utf-8")
|
||||
|
||||
def __enter__(self) -> "FakeResponse":
|
||||
return self
|
||||
|
||||
def __exit__(self, *args: object) -> None:
|
||||
return None
|
||||
|
||||
|
||||
class AgentTests(unittest.TestCase):
|
||||
def test_normalise_http_agent_endpoint_accepts_action_or_game_paths(self) -> None:
|
||||
self.assertEqual(
|
||||
normalise_http_agent_endpoint("http://127.0.0.1:9101/act"),
|
||||
"http://127.0.0.1:9101",
|
||||
)
|
||||
self.assertEqual(
|
||||
normalise_http_agent_endpoint("http://127.0.0.1:9101/game/"),
|
||||
"http://127.0.0.1:9101",
|
||||
)
|
||||
|
||||
def test_http_agent_post_retries_and_sets_player_header(self) -> None:
|
||||
calls = []
|
||||
|
||||
def fake_urlopen(request, timeout): # type: ignore[no-untyped-def]
|
||||
calls.append((request, timeout))
|
||||
if len(calls) == 1:
|
||||
raise URLError("temporary")
|
||||
return FakeResponse({"ok": True})
|
||||
|
||||
agent = HttpAgent(
|
||||
"http://agent.test/act",
|
||||
player_id="p1",
|
||||
retries=1,
|
||||
retry_backoff_seconds=0,
|
||||
)
|
||||
|
||||
with patch("texas_holdem.agents.urlopen", fake_urlopen):
|
||||
payload = agent._post_json("/game", {"game_id": "g1"}, timeout_seconds=2)
|
||||
|
||||
self.assertEqual(payload, {"ok": True})
|
||||
self.assertEqual(len(calls), 2)
|
||||
self.assertEqual(calls[1][0].headers["X-player-id"], "p1")
|
||||
self.assertEqual(calls[1][1], 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from texas_holdem_replay.server import build_game_url
|
||||
from texas_holdem_replay.server import build_core_url, build_game_url
|
||||
|
||||
|
||||
class ReplayServerTests(unittest.TestCase):
|
||||
@@ -22,7 +22,12 @@ class ReplayServerTests(unittest.TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
build_game_url({"url": ["file:///tmp/game.json"]})
|
||||
|
||||
def test_build_core_url_preserves_base_path(self) -> None:
|
||||
self.assertEqual(
|
||||
build_core_url("http://127.0.0.1:8000/api/", "/games/demo/hands/run"),
|
||||
"http://127.0.0.1:8000/api/games/demo/hands/run",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
+74
-1
@@ -1,8 +1,26 @@
|
||||
import unittest
|
||||
from threading import Event, Thread
|
||||
|
||||
from texas_holdem.agents import PokerAgent
|
||||
from texas_holdem.models import Observation, PlayerAction
|
||||
from texas_holdem.service import GameManager
|
||||
|
||||
|
||||
class BlockingAgent(PokerAgent):
|
||||
def __init__(self, entered: Event, release: Event) -> None:
|
||||
self.entered = entered
|
||||
self.release = release
|
||||
|
||||
def decide(self, observation: Observation) -> PlayerAction:
|
||||
self.entered.set()
|
||||
if not self.release.wait(timeout=5):
|
||||
raise RuntimeError("test timed out waiting to release blocking agent")
|
||||
for action in observation.legal_actions:
|
||||
if action["action"] == "check":
|
||||
return PlayerAction("check")
|
||||
return PlayerAction("call")
|
||||
|
||||
|
||||
class ServiceTests(unittest.TestCase):
|
||||
def test_create_and_run_game(self) -> None:
|
||||
manager = GameManager()
|
||||
@@ -23,7 +41,62 @@ class ServiceTests(unittest.TestCase):
|
||||
hands = manager.run_hands(game.game_id, count=1)
|
||||
|
||||
self.assertEqual(len(hands), 1)
|
||||
self.assertEqual(manager.get_game("demo").to_dict()["hand_number"], 1)
|
||||
self.assertEqual(manager.get_game_state("demo")["hand_number"], 1)
|
||||
|
||||
def test_get_game_state_does_not_block_during_run(self) -> None:
|
||||
manager = GameManager()
|
||||
entered = Event()
|
||||
release = Event()
|
||||
game = manager.create_game(
|
||||
{
|
||||
"game_id": "blocking",
|
||||
"seed": 13,
|
||||
"starting_stack": 200,
|
||||
"small_blind": 5,
|
||||
"big_blind": 10,
|
||||
"players": [
|
||||
{"id": "a", "type": "calling"},
|
||||
{"id": "b", "type": "calling"},
|
||||
],
|
||||
}
|
||||
)
|
||||
manager.run_hands("blocking", count=1)
|
||||
game.agents["a"] = BlockingAgent(entered, release)
|
||||
|
||||
thread = Thread(target=lambda: manager.run_hands("blocking", count=1))
|
||||
thread.start()
|
||||
self.assertTrue(entered.wait(timeout=2))
|
||||
|
||||
state = manager.get_game_state("blocking")
|
||||
|
||||
release.set()
|
||||
thread.join(timeout=2)
|
||||
self.assertFalse(thread.is_alive())
|
||||
self.assertEqual(state["hand_number"], 1)
|
||||
self.assertEqual(len(state["hands"]), 1)
|
||||
|
||||
def test_duplicate_http_agent_endpoint_is_rejected_across_active_games(self) -> None:
|
||||
manager = GameManager()
|
||||
payload = {
|
||||
"starting_stack": 200,
|
||||
"small_blind": 5,
|
||||
"big_blind": 10,
|
||||
"players": [
|
||||
{
|
||||
"id": "ai",
|
||||
"agent": {
|
||||
"type": "http",
|
||||
"endpoint": "http://127.0.0.1:9101/act",
|
||||
},
|
||||
},
|
||||
{"id": "b", "type": "calling"},
|
||||
],
|
||||
}
|
||||
|
||||
manager.create_game({"game_id": "g1", **payload})
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "already belongs to game g1"):
|
||||
manager.create_game({"game_id": "g2", **payload})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+106
-31
@@ -2,10 +2,11 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
from abc import ABC, abstractmethod
|
||||
from random import Random
|
||||
from typing import IO, Any
|
||||
from urllib.error import URLError
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from texas_holdem.human_io import clear_screen, prompt_action, render_observation
|
||||
@@ -54,6 +55,27 @@ class CallingStationAgent(PokerAgent):
|
||||
return PlayerAction("fold")
|
||||
|
||||
|
||||
def normalise_http_agent_endpoint(raw: str) -> str:
|
||||
"""Return the canonical base URL for an HTTP agent endpoint."""
|
||||
url = raw.rstrip("/")
|
||||
if url.endswith("/act"):
|
||||
url = url[: -len("/act")]
|
||||
if url.endswith("/game"):
|
||||
url = url[: -len("/game")]
|
||||
return url
|
||||
|
||||
|
||||
def http_agent_endpoint_from_spec(spec: dict[str, Any]) -> str | None:
|
||||
"""Extract the canonical HTTP endpoint from an agent spec, if present."""
|
||||
agent_type = str(spec.get("type", "calling")).lower()
|
||||
if agent_type != "http":
|
||||
return None
|
||||
endpoint = spec.get("endpoint")
|
||||
if not endpoint:
|
||||
raise ValueError("http agent requires an endpoint")
|
||||
return normalise_http_agent_endpoint(str(endpoint))
|
||||
|
||||
|
||||
class HttpAgent(PokerAgent):
|
||||
"""Remote agent that talks to a base URL exposing ``/act`` and ``/game``.
|
||||
|
||||
@@ -66,28 +88,36 @@ class HttpAgent(PokerAgent):
|
||||
ACT_PATH = "/act"
|
||||
GAME_PATH = "/game"
|
||||
|
||||
def __init__(self, endpoint: str, timeout_seconds: float = 10.0) -> None:
|
||||
self.base_url = self._normalise_base_url(endpoint)
|
||||
def __init__(
|
||||
self,
|
||||
endpoint: str,
|
||||
timeout_seconds: float = 10.0,
|
||||
player_id: str | None = None,
|
||||
game_update_timeout_seconds: float | None = None,
|
||||
retries: int = 2,
|
||||
retry_backoff_seconds: float = 0.25,
|
||||
) -> None:
|
||||
self.base_url = normalise_http_agent_endpoint(endpoint)
|
||||
self.timeout_seconds = timeout_seconds
|
||||
|
||||
@staticmethod
|
||||
def _normalise_base_url(raw: str) -> str:
|
||||
"""Strip a trailing slash so URL joins do not produce double slashes.
|
||||
|
||||
Centralising this also tolerates the legacy "endpoint already points
|
||||
at /act" mistake by chopping off a redundant ``/act`` suffix.
|
||||
"""
|
||||
url = raw.rstrip("/")
|
||||
if url.endswith("/act"):
|
||||
url = url[: -len("/act")]
|
||||
return url
|
||||
self.player_id = player_id
|
||||
self.game_update_timeout_seconds = (
|
||||
float(game_update_timeout_seconds)
|
||||
if game_update_timeout_seconds is not None
|
||||
else min(timeout_seconds, 3.0)
|
||||
)
|
||||
self.retries = max(0, retries)
|
||||
self.retry_backoff_seconds = max(0.0, retry_backoff_seconds)
|
||||
|
||||
def _url(self, path: str) -> str:
|
||||
"""Compose a full URL by joining the base with a path component."""
|
||||
return f"{self.base_url}{path}"
|
||||
|
||||
def decide(self, observation: Observation) -> PlayerAction:
|
||||
payload = self._post_json(self.ACT_PATH, observation.to_dict())
|
||||
payload = self._post_json(
|
||||
self.ACT_PATH,
|
||||
observation.to_dict(),
|
||||
timeout_seconds=self.timeout_seconds,
|
||||
)
|
||||
if not isinstance(payload, dict):
|
||||
raise RuntimeError("agent endpoint must return a JSON object")
|
||||
return PlayerAction.from_dict(payload)
|
||||
@@ -100,30 +130,54 @@ class HttpAgent(PokerAgent):
|
||||
only by way of the raised exception bubbling to the engine guard.
|
||||
"""
|
||||
try:
|
||||
self._post_json(self.GAME_PATH, game_state)
|
||||
self._post_json(
|
||||
self.GAME_PATH,
|
||||
game_state,
|
||||
timeout_seconds=self.game_update_timeout_seconds,
|
||||
)
|
||||
except RuntimeError:
|
||||
# ``/game`` is informational; treat any HTTP error as a benign
|
||||
# drop rather than reraising and aborting the hand loop.
|
||||
return None
|
||||
|
||||
def _post_json(self, path: str, payload: dict[str, Any]) -> Any:
|
||||
def _post_json(
|
||||
self,
|
||||
path: str,
|
||||
payload: dict[str, Any],
|
||||
timeout_seconds: float,
|
||||
) -> Any:
|
||||
"""POST ``payload`` as JSON to ``base_url + path`` and return parsed body.
|
||||
|
||||
Extracted as a tiny helper so ``decide`` and ``on_game_update`` share
|
||||
identical transport semantics (timeout, error wrapping, content-type).
|
||||
"""
|
||||
body = json.dumps(payload).encode("utf-8")
|
||||
request = Request(
|
||||
self._url(path),
|
||||
data=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urlopen(request, timeout=self.timeout_seconds) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
except (OSError, URLError) as exc:
|
||||
raise RuntimeError(f"agent endpoint failed: {self._url(path)}") from exc
|
||||
last_error: BaseException | None = None
|
||||
raw = ""
|
||||
for attempt in range(self.retries + 1):
|
||||
request = Request(
|
||||
self._url(path),
|
||||
data=body,
|
||||
headers=self._headers(),
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urlopen(request, timeout=timeout_seconds) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
break
|
||||
except HTTPError as exc:
|
||||
detail = exc.read().decode("utf-8", errors="replace")
|
||||
last_error = RuntimeError(
|
||||
f"agent endpoint failed with HTTP {exc.code}: "
|
||||
f"{self._url(path)} {detail}"
|
||||
)
|
||||
except (OSError, URLError) as exc:
|
||||
last_error = exc
|
||||
if attempt < self.retries and self.retry_backoff_seconds > 0:
|
||||
time.sleep(self.retry_backoff_seconds * (2**attempt))
|
||||
else:
|
||||
raise RuntimeError(f"agent endpoint failed: {self._url(path)}") from last_error
|
||||
|
||||
if not raw:
|
||||
return None
|
||||
try:
|
||||
@@ -133,6 +187,12 @@ class HttpAgent(PokerAgent):
|
||||
f"agent endpoint returned invalid JSON: {self._url(path)}"
|
||||
) from exc
|
||||
|
||||
def _headers(self) -> dict[str, str]:
|
||||
headers = {"Content-Type": "application/json", "Connection": "close"}
|
||||
if self.player_id:
|
||||
headers["X-Player-Id"] = self.player_id
|
||||
return headers
|
||||
|
||||
|
||||
class HumanAgent(PokerAgent):
|
||||
"""Interactive CLI agent for debugging and manual play.
|
||||
@@ -189,7 +249,11 @@ class HumanAgent(PokerAgent):
|
||||
return line.rstrip("\n")
|
||||
|
||||
|
||||
def build_agent(spec: dict[str, Any], rng: Random | None = None) -> PokerAgent:
|
||||
def build_agent(
|
||||
spec: dict[str, Any],
|
||||
rng: Random | None = None,
|
||||
player_id: str | None = None,
|
||||
) -> PokerAgent:
|
||||
agent_type = str(spec.get("type", "calling")).lower()
|
||||
if agent_type == "random":
|
||||
return RandomAgent(rng)
|
||||
@@ -199,7 +263,18 @@ def build_agent(spec: dict[str, Any], rng: Random | None = None) -> PokerAgent:
|
||||
endpoint = spec.get("endpoint")
|
||||
if not endpoint:
|
||||
raise ValueError("http agent requires an endpoint")
|
||||
return HttpAgent(str(endpoint), float(spec.get("timeout_seconds", 10.0)))
|
||||
return HttpAgent(
|
||||
str(endpoint),
|
||||
timeout_seconds=float(spec.get("timeout_seconds", 10.0)),
|
||||
player_id=player_id,
|
||||
game_update_timeout_seconds=(
|
||||
float(spec["game_update_timeout_seconds"])
|
||||
if "game_update_timeout_seconds" in spec
|
||||
else None
|
||||
),
|
||||
retries=int(spec.get("retries", 2)),
|
||||
retry_backoff_seconds=float(spec.get("retry_backoff_seconds", 0.25)),
|
||||
)
|
||||
if agent_type in {"human", "cli", "interactive"}:
|
||||
return HumanAgent()
|
||||
raise ValueError(f"unknown agent type: {agent_type}")
|
||||
|
||||
+63
-18
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from random import Random
|
||||
from threading import RLock
|
||||
from time import time
|
||||
|
||||
from texas_holdem.agents import PokerAgent
|
||||
@@ -54,6 +56,7 @@ class TableGame:
|
||||
self.small_blind = small_blind
|
||||
self.big_blind = big_blind
|
||||
self.rng = rng or Random()
|
||||
self.lock = RLock()
|
||||
self.hand_number = 0
|
||||
self.button_index: int | None = None
|
||||
self.board = []
|
||||
@@ -64,6 +67,7 @@ class TableGame:
|
||||
# first hand that played under those stakes, which makes it trivial
|
||||
# to reconstruct the schedule from the outside.
|
||||
self.blind_history: list[BlindLevel] = []
|
||||
self._completed_snapshot: dict[str, object] = self._to_dict_unlocked()
|
||||
|
||||
@property
|
||||
def is_complete(self) -> bool:
|
||||
@@ -83,6 +87,14 @@ class TableGame:
|
||||
:attr:`blind_history` whenever it changes (including the very first
|
||||
hand) so external observers can replay the schedule.
|
||||
"""
|
||||
with self.lock:
|
||||
return self._run_hand_locked(small_blind=small_blind, big_blind=big_blind)
|
||||
|
||||
def _run_hand_locked(
|
||||
self,
|
||||
small_blind: int | None = None,
|
||||
big_blind: int | None = None,
|
||||
) -> HandSummary:
|
||||
if self.is_complete:
|
||||
raise GameComplete("game is complete")
|
||||
|
||||
@@ -151,6 +163,7 @@ class TableGame:
|
||||
finished_at=time(),
|
||||
)
|
||||
self.hand_summaries.append(summary)
|
||||
self._completed_snapshot = deepcopy(self._to_dict_unlocked())
|
||||
return summary
|
||||
|
||||
def run_hands(
|
||||
@@ -166,24 +179,47 @@ class TableGame:
|
||||
with the first hand of this call; subsequent calls can raise them
|
||||
again. Leaving them ``None`` keeps the current level unchanged.
|
||||
"""
|
||||
if max_hands <= 0:
|
||||
raise ValueError("max_hands must be positive")
|
||||
summaries = []
|
||||
for _ in range(max_hands):
|
||||
if self.is_complete:
|
||||
break
|
||||
# Only the first hand of the batch needs to apply the blind
|
||||
# override; after that the engine reuses the stored values.
|
||||
summaries.append(
|
||||
self.run_hand(small_blind=small_blind, big_blind=big_blind)
|
||||
)
|
||||
small_blind = None
|
||||
big_blind = None
|
||||
if until_one_left and self.is_complete:
|
||||
break
|
||||
return summaries
|
||||
with self.lock:
|
||||
if max_hands <= 0:
|
||||
raise ValueError("max_hands must be positive")
|
||||
summaries = []
|
||||
for _ in range(max_hands):
|
||||
if self.is_complete:
|
||||
break
|
||||
# Only the first hand of the batch needs to apply the blind
|
||||
# override; after that the engine reuses the stored values.
|
||||
summaries.append(
|
||||
self._run_hand_locked(
|
||||
small_blind=small_blind,
|
||||
big_blind=big_blind,
|
||||
)
|
||||
)
|
||||
small_blind = None
|
||||
big_blind = None
|
||||
if until_one_left and self.is_complete:
|
||||
break
|
||||
return summaries
|
||||
|
||||
def to_dict(self) -> dict[str, object]:
|
||||
with self.lock:
|
||||
return self._to_dict_unlocked()
|
||||
|
||||
def snapshot_completed(self) -> dict[str, object]:
|
||||
"""Return a stable snapshot from the latest completed hand boundary.
|
||||
|
||||
If a hand is currently running under ``self.lock``, this method does
|
||||
not block. It returns the most recent completed hand summary and
|
||||
stacks captured in memory, which is exactly what status endpoints
|
||||
need while a long-running HTTP-agent decision is in progress.
|
||||
"""
|
||||
if self.lock.acquire(blocking=False):
|
||||
try:
|
||||
return deepcopy(self._to_dict_unlocked())
|
||||
finally:
|
||||
self.lock.release()
|
||||
return deepcopy(self._completed_snapshot)
|
||||
|
||||
def _to_dict_unlocked(self) -> dict[str, object]:
|
||||
return {
|
||||
"game_id": self.game_id,
|
||||
"status": "complete" if self.is_complete else "running",
|
||||
@@ -449,9 +485,18 @@ class TableGame:
|
||||
try:
|
||||
requested = agent.decide(observation)
|
||||
except Exception:
|
||||
requested = PlayerAction("fold")
|
||||
requested = self._default_action(observation.legal_actions)
|
||||
return self._coerce_action(requested, observation.legal_actions)
|
||||
|
||||
def _default_action(self, legal_actions: list[dict[str, object]]) -> PlayerAction:
|
||||
by_action = {str(action["action"]): action for action in legal_actions}
|
||||
for action_type in ("check", "call", "fold"):
|
||||
if action_type in by_action:
|
||||
legal = by_action[action_type]
|
||||
return PlayerAction(action_type, int(legal.get("amount") or 0))
|
||||
legal = legal_actions[0]
|
||||
return PlayerAction(str(legal["action"]), int(legal.get("amount") or 0))
|
||||
|
||||
def _coerce_action(
|
||||
self,
|
||||
requested: PlayerAction,
|
||||
@@ -581,7 +626,7 @@ class TableGame:
|
||||
swallow individual exceptions so a flaky remote endpoint cannot
|
||||
break the table flow.
|
||||
"""
|
||||
snapshot = self.to_dict()
|
||||
snapshot = self._to_dict_unlocked()
|
||||
for agent in self.agents.values():
|
||||
try:
|
||||
agent.on_game_update(snapshot)
|
||||
|
||||
@@ -25,8 +25,8 @@ class PokerRequestHandler(BaseHTTPRequestHandler):
|
||||
if path == ["games"]:
|
||||
self._json({"games": MANAGER.list_games()})
|
||||
return
|
||||
if len(path) == 2 and path[0] == "games":
|
||||
self._json(MANAGER.get_game(path[1]).to_dict())
|
||||
if len(path) == 2 and path[0] in {"game", "games"}:
|
||||
self._json(MANAGER.get_game_state(path[1]))
|
||||
return
|
||||
self._json({"error": "not found"}, HTTPStatus.NOT_FOUND)
|
||||
except KeyError as exc:
|
||||
@@ -35,11 +35,11 @@ class PokerRequestHandler(BaseHTTPRequestHandler):
|
||||
def do_POST(self) -> None:
|
||||
path = self._path_parts()
|
||||
try:
|
||||
if path == ["games"]:
|
||||
if path in (["game"], ["games"]):
|
||||
game = MANAGER.create_game(self._read_json())
|
||||
self._json(game.to_dict(), HTTPStatus.CREATED)
|
||||
self._json(game.snapshot_completed(), HTTPStatus.CREATED)
|
||||
return
|
||||
if len(path) == 3 and path[0] == "games" and path[2] == "hands":
|
||||
if len(path) == 3 and path[0] in {"game", "games"} and path[2] == "hands":
|
||||
body = self._read_json()
|
||||
count = int(body.get("count", 1))
|
||||
until_one_left = bool(body.get("until_one_left", False))
|
||||
@@ -51,9 +51,9 @@ class PokerRequestHandler(BaseHTTPRequestHandler):
|
||||
small_blind=small_blind,
|
||||
big_blind=big_blind,
|
||||
)
|
||||
self._json({"hands": summaries, "game": MANAGER.get_game(path[1]).to_dict()})
|
||||
self._json({"hands": summaries, "game": MANAGER.get_game_state(path[1])})
|
||||
return
|
||||
if len(path) == 4 and path[0] == "games" and path[2] == "hands" and path[3] == "run":
|
||||
if len(path) == 4 and path[0] in {"game", "games"} and path[2] == "hands" and path[3] == "run":
|
||||
body = self._read_json()
|
||||
count = int(body.get("count", 1))
|
||||
until_one_left = bool(body.get("until_one_left", False))
|
||||
@@ -65,7 +65,7 @@ class PokerRequestHandler(BaseHTTPRequestHandler):
|
||||
small_blind=small_blind,
|
||||
big_blind=big_blind,
|
||||
)
|
||||
self._json({"hands": summaries, "game": MANAGER.get_game(path[1]).to_dict()})
|
||||
self._json({"hands": summaries, "game": MANAGER.get_game_state(path[1])})
|
||||
return
|
||||
self._json({"error": "not found"}, HTTPStatus.NOT_FOUND)
|
||||
except KeyError as exc:
|
||||
|
||||
+51
-13
@@ -5,13 +5,14 @@ from threading import RLock
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from texas_holdem.agents import build_agent
|
||||
from texas_holdem.agents import build_agent, http_agent_endpoint_from_spec
|
||||
from texas_holdem.engine import TableGame
|
||||
|
||||
|
||||
class GameManager:
|
||||
def __init__(self) -> None:
|
||||
self._games: dict[str, TableGame] = {}
|
||||
self._http_endpoint_owners: dict[str, str] = {}
|
||||
self._lock = RLock()
|
||||
|
||||
def create_game(self, payload: dict[str, Any]) -> TableGame:
|
||||
@@ -29,12 +30,19 @@ class GameManager:
|
||||
big_blind = int(payload.get("big_blind", 10))
|
||||
|
||||
specs = []
|
||||
http_endpoints: set[str] = set()
|
||||
for seat, raw_spec in enumerate(players):
|
||||
if not isinstance(raw_spec, dict):
|
||||
raise ValueError("each player must be an object")
|
||||
player_id = str(raw_spec.get("id") or raw_spec.get("player_id") or f"p{seat + 1}")
|
||||
name = str(raw_spec.get("name") or player_id)
|
||||
agent = build_agent(raw_spec.get("agent", raw_spec), rng)
|
||||
agent_spec = raw_spec.get("agent", raw_spec)
|
||||
if not isinstance(agent_spec, dict):
|
||||
raise ValueError("agent spec must be an object")
|
||||
endpoint = http_agent_endpoint_from_spec(agent_spec)
|
||||
if endpoint is not None:
|
||||
http_endpoints.add(endpoint)
|
||||
agent = build_agent(agent_spec, rng, player_id=player_id)
|
||||
specs.append((player_id, name, agent))
|
||||
|
||||
game = TableGame(
|
||||
@@ -46,9 +54,18 @@ class GameManager:
|
||||
rng=rng,
|
||||
)
|
||||
with self._lock:
|
||||
self._release_completed_http_endpoints_locked()
|
||||
if game_id in self._games:
|
||||
raise ValueError(f"game already exists: {game_id}")
|
||||
for endpoint in http_endpoints:
|
||||
owner = self._http_endpoint_owners.get(endpoint)
|
||||
if owner is not None and owner != game_id:
|
||||
raise ValueError(
|
||||
f"http agent endpoint already belongs to game {owner}: {endpoint}"
|
||||
)
|
||||
self._games[game_id] = game
|
||||
for endpoint in http_endpoints:
|
||||
self._http_endpoint_owners[endpoint] = game_id
|
||||
return game
|
||||
|
||||
def get_game(self, game_id: str) -> TableGame:
|
||||
@@ -58,9 +75,13 @@ class GameManager:
|
||||
except KeyError as exc:
|
||||
raise KeyError(f"game not found: {game_id}") from exc
|
||||
|
||||
def get_game_state(self, game_id: str) -> dict[str, object]:
|
||||
return self.get_game(game_id).snapshot_completed()
|
||||
|
||||
def list_games(self) -> list[dict[str, object]]:
|
||||
with self._lock:
|
||||
return [game.to_dict() for game in self._games.values()]
|
||||
games = list(self._games.values())
|
||||
return [game.snapshot_completed() for game in games]
|
||||
|
||||
def run_hands(
|
||||
self,
|
||||
@@ -78,13 +99,30 @@ class GameManager:
|
||||
no-argument behaviour.
|
||||
"""
|
||||
game = self.get_game(game_id)
|
||||
with self._lock:
|
||||
return [
|
||||
summary.to_dict()
|
||||
for summary in game.run_hands(
|
||||
count,
|
||||
until_one_left=until_one_left,
|
||||
small_blind=small_blind,
|
||||
big_blind=big_blind,
|
||||
)
|
||||
]
|
||||
summaries = [
|
||||
summary.to_dict()
|
||||
for summary in game.run_hands(
|
||||
count,
|
||||
until_one_left=until_one_left,
|
||||
small_blind=small_blind,
|
||||
big_blind=big_blind,
|
||||
)
|
||||
]
|
||||
if game.is_complete:
|
||||
with self._lock:
|
||||
self._release_http_endpoints_for_game_locked(game_id)
|
||||
return summaries
|
||||
|
||||
def _release_completed_http_endpoints_locked(self) -> None:
|
||||
for game_id, game in list(self._games.items()):
|
||||
if game.lock.acquire(blocking=False):
|
||||
try:
|
||||
if game.is_complete:
|
||||
self._release_http_endpoints_for_game_locked(game_id)
|
||||
finally:
|
||||
game.lock.release()
|
||||
|
||||
def _release_http_endpoints_for_game_locked(self, game_id: str) -> None:
|
||||
for endpoint, owner in list(self._http_endpoint_owners.items()):
|
||||
if owner == game_id:
|
||||
del self._http_endpoint_owners[endpoint]
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
"""Standalone web replay viewer for Texas Hold X game snapshots."""
|
||||
"""Standalone web replay viewer for Texas Hold X."""
|
||||
|
||||
|
||||
+149
-80
@@ -8,26 +8,55 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import quote, unquote, urlparse, parse_qs
|
||||
from urllib.request import Request, urlopen
|
||||
from urllib.parse import parse_qs, quote, urlencode, urlparse
|
||||
from urllib.request import ProxyHandler, Request, build_opener
|
||||
|
||||
STATIC_DIR = Path(__file__).resolve().parent / "static"
|
||||
DEFAULT_CORE_BASE_URL = "http://127.0.0.1:8000"
|
||||
NO_PROXY_OPENER = build_opener(ProxyHandler({}))
|
||||
|
||||
|
||||
STATIC_DIR = Path(__file__).with_name("static")
|
||||
def build_game_url(params: dict[str, list[str]]) -> str:
|
||||
"""Build a core-service game URL from query parameters.
|
||||
|
||||
|
||||
def build_game_url(query: dict[str, list[str]]) -> str:
|
||||
raw_url = _first(query, "url")
|
||||
Supported forms:
|
||||
- ``url=https://host/games/demo`` for callers that already have a full URL.
|
||||
- ``base_url=http://host&game_id=demo`` for the common local service case.
|
||||
"""
|
||||
raw_url = _first(params, "url")
|
||||
if raw_url:
|
||||
return _validate_http_url(raw_url)
|
||||
|
||||
base_url = _first(query, "base_url")
|
||||
game_id = _first(query, "game_id")
|
||||
if not base_url or not game_id:
|
||||
raise ValueError("provide either url or both base_url and game_id")
|
||||
base_url = _first(params, "base_url") or DEFAULT_CORE_BASE_URL
|
||||
game_id = _first(params, "game_id")
|
||||
if not game_id:
|
||||
raise ValueError("game_id is required when url is not provided")
|
||||
|
||||
base = _validate_http_url(base_url).rstrip("/")
|
||||
safe_game_id = quote(game_id.strip("/"), safe="")
|
||||
return f"{base}/games/{safe_game_id}"
|
||||
parsed = urlparse(_validate_http_url(base_url.rstrip("/")))
|
||||
base_path = parsed.path.rstrip("/")
|
||||
game_path = f"{base_path}/games/{quote(game_id, safe='')}"
|
||||
return parsed._replace(path=game_path, query="", fragment="").geturl()
|
||||
|
||||
|
||||
def build_core_url(base_url: str, path: str) -> str:
|
||||
parsed = urlparse(_validate_http_url(base_url.rstrip("/")))
|
||||
base_path = parsed.path.rstrip("/")
|
||||
target_path = f"{base_path}/{path.lstrip('/')}"
|
||||
return parsed._replace(path=target_path, query="", fragment="").geturl()
|
||||
|
||||
|
||||
def _first(params: dict[str, list[str]], key: str) -> str | None:
|
||||
values = params.get(key)
|
||||
if not values:
|
||||
return None
|
||||
return values[0]
|
||||
|
||||
|
||||
def _validate_http_url(value: str) -> str:
|
||||
parsed = urlparse(value)
|
||||
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
||||
raise ValueError("url must be an absolute http(s) URL")
|
||||
return value
|
||||
|
||||
|
||||
class ReplayRequestHandler(BaseHTTPRequestHandler):
|
||||
@@ -35,97 +64,152 @@ class ReplayRequestHandler(BaseHTTPRequestHandler):
|
||||
|
||||
def do_GET(self) -> None:
|
||||
parsed = urlparse(self.path)
|
||||
if parsed.path == "/api/health":
|
||||
self._json({"ok": True, "service": "texas-holdem-replay"})
|
||||
if parsed.path == "/health":
|
||||
self._json({"ok": True})
|
||||
return
|
||||
if parsed.path == "/api/fetch-game":
|
||||
self._handle_fetch_game(parsed.query)
|
||||
self._handle_fetch_game(parse_qs(parsed.query))
|
||||
return
|
||||
self._serve_static(parsed.path)
|
||||
|
||||
def do_OPTIONS(self) -> None:
|
||||
self.send_response(HTTPStatus.NO_CONTENT)
|
||||
self._cors_headers()
|
||||
self.end_headers()
|
||||
def do_POST(self) -> None:
|
||||
parsed = urlparse(self.path)
|
||||
if parsed.path == "/api/create-game":
|
||||
self._proxy_json_request("POST", "/games")
|
||||
return
|
||||
if parsed.path == "/api/run-hands":
|
||||
try:
|
||||
payload = self._read_json()
|
||||
base_url = str(payload.pop("base_url", DEFAULT_CORE_BASE_URL))
|
||||
game_id = str(payload.pop("game_id", "")).strip()
|
||||
if not game_id:
|
||||
raise ValueError("game_id is required")
|
||||
path = f"/games/{quote(game_id, safe='')}/hands/run"
|
||||
self._proxy_json_request("POST", path, base_url=base_url, payload=payload)
|
||||
except ValueError as exc:
|
||||
self._json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
|
||||
return
|
||||
self._json({"error": "not found"}, HTTPStatus.NOT_FOUND)
|
||||
|
||||
def log_message(self, format: str, *args: Any) -> None:
|
||||
return
|
||||
|
||||
def _handle_fetch_game(self, raw_query: str) -> None:
|
||||
query = parse_qs(raw_query)
|
||||
def _handle_fetch_game(self, params: dict[str, list[str]]) -> None:
|
||||
try:
|
||||
target = build_game_url(query)
|
||||
timeout = float(_first(query, "timeout") or 8)
|
||||
request = Request(target, headers={"Accept": "application/json"})
|
||||
with urlopen(request, timeout=max(1, min(timeout, 30))) as response:
|
||||
payload = response.read()
|
||||
content_type = response.headers.get("Content-Type", "")
|
||||
try:
|
||||
data = json.loads(payload.decode("utf-8"))
|
||||
except json.JSONDecodeError as exc:
|
||||
raise ValueError("target did not return valid JSON") from exc
|
||||
if not isinstance(data, dict):
|
||||
raise ValueError("target JSON must be an object")
|
||||
self._json({"source": target, "content_type": content_type, "game": data})
|
||||
except HTTPError as exc:
|
||||
body = exc.read().decode("utf-8", errors="replace")[:600]
|
||||
self._json(
|
||||
{"error": f"upstream returned HTTP {exc.code}", "detail": body},
|
||||
HTTPStatus.BAD_GATEWAY,
|
||||
)
|
||||
except (URLError, TimeoutError) as exc:
|
||||
self._json({"error": "failed to reach upstream", "detail": str(exc)}, HTTPStatus.BAD_GATEWAY)
|
||||
target_url = build_game_url(params)
|
||||
payload, status = self._request_json("GET", target_url)
|
||||
self._json(payload, HTTPStatus(status))
|
||||
except ValueError as exc:
|
||||
self._json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
|
||||
except RuntimeError as exc:
|
||||
self._json({"error": str(exc)}, HTTPStatus.BAD_GATEWAY)
|
||||
|
||||
def _serve_static(self, raw_path: str) -> None:
|
||||
relative = unquote(raw_path.lstrip("/")) or "index.html"
|
||||
if relative.endswith("/"):
|
||||
relative += "index.html"
|
||||
candidate = (STATIC_DIR / relative).resolve()
|
||||
root = STATIC_DIR.resolve()
|
||||
if root not in candidate.parents and candidate != root:
|
||||
def _proxy_json_request(
|
||||
self,
|
||||
method: str,
|
||||
path: str,
|
||||
base_url: str | None = None,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
try:
|
||||
if payload is None:
|
||||
payload = self._read_json()
|
||||
base_url = str(payload.pop("base_url", DEFAULT_CORE_BASE_URL))
|
||||
target_url = build_core_url(base_url or DEFAULT_CORE_BASE_URL, path)
|
||||
response_payload, status = self._request_json(method, target_url, payload)
|
||||
self._json(response_payload, HTTPStatus(status))
|
||||
except ValueError as exc:
|
||||
self._json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
|
||||
except RuntimeError as exc:
|
||||
self._json({"error": str(exc)}, HTTPStatus.BAD_GATEWAY)
|
||||
|
||||
def _request_json(
|
||||
self,
|
||||
method: str,
|
||||
url: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> tuple[dict[str, Any], int]:
|
||||
data = None
|
||||
headers = {"Accept": "application/json"}
|
||||
if payload is not None:
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
request = Request(url, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with NO_PROXY_OPENER.open(request, timeout=20) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
status = response.status
|
||||
except HTTPError as exc:
|
||||
raw = exc.read().decode("utf-8", errors="replace")
|
||||
status = exc.code
|
||||
except (OSError, URLError) as exc:
|
||||
raise RuntimeError(f"core service request failed: {url}") from exc
|
||||
try:
|
||||
parsed = json.loads(raw) if raw else {}
|
||||
except json.JSONDecodeError as exc:
|
||||
raise RuntimeError(f"core service returned invalid JSON: {url}") from exc
|
||||
if not isinstance(parsed, dict):
|
||||
raise RuntimeError("core service response must be a JSON object")
|
||||
return parsed, status
|
||||
|
||||
def _serve_static(self, path: str) -> None:
|
||||
relative = "index.html" if path in {"", "/"} else path.lstrip("/")
|
||||
if relative.startswith("static/"):
|
||||
relative = relative[len("static/") :]
|
||||
if "/" in relative:
|
||||
safe_parts = [part for part in relative.split("/") if part not in {"", ".", ".."}]
|
||||
relative = "/".join(safe_parts)
|
||||
target = STATIC_DIR / relative
|
||||
try:
|
||||
target.relative_to(STATIC_DIR)
|
||||
except ValueError:
|
||||
self._json({"error": "not found"}, HTTPStatus.NOT_FOUND)
|
||||
return
|
||||
if not candidate.is_file():
|
||||
candidate = STATIC_DIR / "index.html"
|
||||
body = candidate.read_bytes()
|
||||
content_type = mimetypes.guess_type(candidate.name)[0] or "application/octet-stream"
|
||||
if not target.is_file():
|
||||
self._json({"error": "not found"}, HTTPStatus.NOT_FOUND)
|
||||
return
|
||||
content_type = mimetypes.guess_type(str(target))[0] or "application/octet-stream"
|
||||
body = target.read_bytes()
|
||||
self.send_response(HTTPStatus.OK)
|
||||
self._cors_headers()
|
||||
self.send_header("Content-Type", content_type)
|
||||
self.send_header("Cache-Control", "no-store" if candidate.name == "index.html" else "public, max-age=60")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def _read_json(self) -> dict[str, Any]:
|
||||
length = int(self.headers.get("Content-Length", "0"))
|
||||
if length <= 0:
|
||||
return {}
|
||||
try:
|
||||
payload = json.loads(self.rfile.read(length).decode("utf-8"))
|
||||
except json.JSONDecodeError as exc:
|
||||
raise ValueError("request body must be valid JSON") from exc
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError("request body must be a JSON object")
|
||||
return payload
|
||||
|
||||
def _json(self, payload: dict[str, Any], status: HTTPStatus = HTTPStatus.OK) -> None:
|
||||
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
||||
self.send_response(status)
|
||||
self._cors_headers()
|
||||
self.send_header("Content-Type", "application/json; charset=utf-8")
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def _cors_headers(self) -> None:
|
||||
self.send_header("Access-Control-Allow-Origin", "*")
|
||||
self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
|
||||
self.send_header("Access-Control-Allow-Headers", "Content-Type")
|
||||
|
||||
|
||||
def create_server(host: str, port: int) -> ThreadingHTTPServer:
|
||||
return ThreadingHTTPServer((host, port), ReplayRequestHandler)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Run the standalone Texas Hold X replay web viewer.")
|
||||
parser = argparse.ArgumentParser(description="Run the Texas Hold X web replay viewer.")
|
||||
parser.add_argument("--host", default="127.0.0.1")
|
||||
parser.add_argument("--port", default=8088, type=int)
|
||||
args = parser.parse_args()
|
||||
|
||||
server = create_server(args.host, args.port)
|
||||
print(f"Texas Hold X replay viewer listening on http://{args.host}:{args.port}")
|
||||
query = urlencode({"base_url": DEFAULT_CORE_BASE_URL})
|
||||
print(f"Texas Hold X replay viewer listening on http://{args.host}:{args.port}/?{query}")
|
||||
try:
|
||||
server.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
@@ -134,20 +218,5 @@ def main() -> None:
|
||||
server.server_close()
|
||||
|
||||
|
||||
def _first(query: dict[str, list[str]], key: str) -> str | None:
|
||||
values = query.get(key)
|
||||
if not values:
|
||||
return None
|
||||
value = values[0].strip()
|
||||
return value or None
|
||||
|
||||
|
||||
def _validate_http_url(value: str) -> str:
|
||||
parsed = urlparse(value.strip())
|
||||
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
||||
raise ValueError("url must be an absolute http(s) URL")
|
||||
return value.strip()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
+749
-785
File diff suppressed because it is too large
Load Diff
@@ -1,132 +1,144 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Texas Hold X Replay</title>
|
||||
<link rel="stylesheet" href="/styles.css" />
|
||||
<link rel="stylesheet" href="/styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="app-shell">
|
||||
<header class="topbar">
|
||||
<div class="brand-lockup">
|
||||
<div class="chip-mark" aria-hidden="true">TX</div>
|
||||
<div class="brand-meta">
|
||||
<h1>Texas Hold X Replay</h1>
|
||||
<p id="subtitle">等待加载游戏数据</p>
|
||||
</div>
|
||||
<main class="shell">
|
||||
<header class="topbar" aria-label="Application header">
|
||||
<div>
|
||||
<p class="eyebrow">Texas Hold X</p>
|
||||
<h1>Game Replay Control</h1>
|
||||
</div>
|
||||
<div class="status-strip" aria-live="polite">
|
||||
<span id="sourceBadge" class="badge badge-gold">No Data</span>
|
||||
<span id="pollBadge" class="badge badge-blue">Auto Off</span>
|
||||
<span id="connectionStatus" class="pill neutral">Idle</span>
|
||||
<span id="gameStatus" class="pill">No game</span>
|
||||
<span id="handCounter" class="metric">Hand 0</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="layout-grid">
|
||||
<!-- Stage zone: pure visualization (table, seats, animations).
|
||||
Placed first in DOM so mobile/tablet layouts keep it on top. -->
|
||||
<section class="stage-zone" aria-label="牌桌动画回放">
|
||||
<div class="stage-head">
|
||||
<div class="stage-head-left">
|
||||
<span id="handBadge" class="badge badge-gold">Hand -</span>
|
||||
<strong id="streetLabel">未加载</strong>
|
||||
<section class="workspace">
|
||||
<aside class="panel controls" aria-label="Game controls">
|
||||
<div class="panel-header">
|
||||
<h2>Source</h2>
|
||||
</div>
|
||||
|
||||
<label class="field">
|
||||
<span>Core service</span>
|
||||
<input id="baseUrlInput" type="url" value="http://127.0.0.1:8000" spellcheck="false">
|
||||
</label>
|
||||
<label class="field">
|
||||
<span>Game ID</span>
|
||||
<input id="gameIdInput" type="text" value="demo" spellcheck="false">
|
||||
</label>
|
||||
<div class="button-row">
|
||||
<button id="fetchButton" type="button" class="primary">Fetch</button>
|
||||
<button id="togglePollButton" type="button">Auto</button>
|
||||
</div>
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
<div class="panel-header compact">
|
||||
<h2>Run Hands</h2>
|
||||
</div>
|
||||
<div class="form-grid">
|
||||
<label class="field">
|
||||
<span>Count</span>
|
||||
<input id="runCountInput" type="number" min="1" max="100" value="1">
|
||||
</label>
|
||||
<label class="field">
|
||||
<span>Poll sec</span>
|
||||
<input id="pollSecondsInput" type="number" min="1" max="60" value="3">
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-grid">
|
||||
<label class="field">
|
||||
<span>Small blind</span>
|
||||
<input id="smallBlindInput" type="number" min="1" placeholder="keep">
|
||||
</label>
|
||||
<label class="field">
|
||||
<span>Big blind</span>
|
||||
<input id="bigBlindInput" type="number" min="1" placeholder="keep">
|
||||
</label>
|
||||
</div>
|
||||
<label class="check-field">
|
||||
<input id="untilOneLeftInput" type="checkbox">
|
||||
<span>Run until one player remains</span>
|
||||
</label>
|
||||
<button id="runButton" type="button" class="wide">Run</button>
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
<div class="panel-header compact">
|
||||
<h2>Create Game</h2>
|
||||
</div>
|
||||
<textarea id="createGameInput" rows="10" spellcheck="false"></textarea>
|
||||
<button id="createGameButton" type="button" class="wide">Create</button>
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
<div class="panel-header compact">
|
||||
<h2>Load JSON</h2>
|
||||
</div>
|
||||
<input id="fileInput" class="file-input" type="file" accept="application/json,.json">
|
||||
<textarea id="jsonInput" rows="8" spellcheck="false" placeholder='Paste a GET /games/{id} response'></textarea>
|
||||
<button id="loadJsonButton" type="button" class="wide">Load Snapshot</button>
|
||||
</aside>
|
||||
|
||||
<section class="table-zone" aria-label="Poker table replay">
|
||||
<div class="table-toolbar">
|
||||
<div class="select-wrap">
|
||||
<label for="handSelect">Hand</label>
|
||||
<select id="handSelect"></select>
|
||||
</div>
|
||||
<div class="stage-head-right">
|
||||
<span id="potLabel" class="badge badge-gold">Pot 0</span>
|
||||
<div class="select-wrap">
|
||||
<label for="speedInput">Pace</label>
|
||||
<input id="speedInput" type="range" min="0.5" max="2" step="0.1" value="1">
|
||||
</div>
|
||||
</div>
|
||||
<div id="table" class="poker-table">
|
||||
<!-- felt-shell encapsulates the rounded green felt with overflow:hidden,
|
||||
so player speech bubbles drawn in seat-layer can overflow freely
|
||||
above and below the table without being clipped. -->
|
||||
<div class="felt-shell" aria-hidden="true">
|
||||
<div class="felt-rail"></div>
|
||||
<div class="felt-surface">
|
||||
<div class="felt-grid"></div>
|
||||
<div class="felt-glow"></div>
|
||||
<div class="felt-mark">TX</div>
|
||||
|
||||
<div class="felt-stage">
|
||||
<div class="table-felt" id="tableFelt">
|
||||
<div id="seatLayer" class="seat-layer"></div>
|
||||
<div class="board-zone">
|
||||
<div id="potDisplay" class="pot-display">Pot 0</div>
|
||||
<div id="boardCards" class="cards board-cards"></div>
|
||||
<div id="frameCaption" class="frame-caption">Load a game snapshot</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="community-area">
|
||||
<div id="boardCards" class="card-row board"></div>
|
||||
<div id="tableMessage" class="table-message">上传 JSON 或从游戏服务获取快照</div>
|
||||
</div>
|
||||
<div id="seatLayer" class="seat-layer"></div>
|
||||
</div>
|
||||
|
||||
<div class="transport" aria-label="Replay transport">
|
||||
<button id="resetButton" type="button" title="Reset">|<</button>
|
||||
<button id="prevButton" type="button" title="Previous"><</button>
|
||||
<button id="playButton" type="button" class="primary">Play</button>
|
||||
<button id="nextButton" type="button" title="Next">></button>
|
||||
<span id="frameCounter" class="metric">0 / 0</span>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Interaction zone: data source + replay controls + summary. -->
|
||||
<section class="control-panel" aria-label="数据与播放控制">
|
||||
<div class="panel-section">
|
||||
<h2>数据源</h2>
|
||||
<label>
|
||||
<span>游戏服务</span>
|
||||
<input id="serverUrl" type="url" value="http://127.0.0.1:8000" placeholder="http://127.0.0.1:8000" />
|
||||
</label>
|
||||
<label>
|
||||
<span>Game ID</span>
|
||||
<input id="gameId" type="text" value="game1" placeholder="game1" />
|
||||
</label>
|
||||
<div class="button-row">
|
||||
<button id="fetchBtn" class="primary-btn" type="button">获取</button>
|
||||
<label class="file-btn">
|
||||
上传 JSON
|
||||
<input id="fileInput" type="file" accept="application/json,.json" />
|
||||
</label>
|
||||
</div>
|
||||
<div class="auto-grid">
|
||||
<label class="toggle-line">
|
||||
<input id="autoPoll" type="checkbox" />
|
||||
<span>自动获取</span>
|
||||
</label>
|
||||
<label>
|
||||
<span>间隔秒</span>
|
||||
<input id="pollSeconds" type="number" min="5" max="300" value="12" />
|
||||
</label>
|
||||
</div>
|
||||
<aside class="panel log-panel" aria-label="Game details">
|
||||
<div class="panel-header">
|
||||
<h2>Table State</h2>
|
||||
</div>
|
||||
<div id="tableStats" class="stats-grid"></div>
|
||||
|
||||
<div class="panel-section">
|
||||
<h2>回放</h2>
|
||||
<label>
|
||||
<span>手牌</span>
|
||||
<select id="handSelect"></select>
|
||||
</label>
|
||||
<label>
|
||||
<span>节奏</span>
|
||||
<input id="pace" type="range" min="0.75" max="1.8" step="0.05" value="1" />
|
||||
</label>
|
||||
<div class="transport-row">
|
||||
<button id="prevBtn" type="button" title="上一帧">◀</button>
|
||||
<button id="playBtn" class="primary-btn" type="button" title="播放/暂停">▶</button>
|
||||
<button id="nextBtn" type="button" title="下一帧">▶</button>
|
||||
<button id="resetBtn" type="button" title="重置">↺</button>
|
||||
</div>
|
||||
<div class="progress-wrap">
|
||||
<div id="progressBar"></div>
|
||||
</div>
|
||||
<div class="panel-header compact">
|
||||
<h2>Players</h2>
|
||||
</div>
|
||||
<div id="playerList" class="player-list"></div>
|
||||
|
||||
<div class="panel-section dense">
|
||||
<h2>牌局摘要</h2>
|
||||
<dl class="stat-list">
|
||||
<div><dt>状态</dt><dd id="gameStatus">-</dd></div>
|
||||
<div><dt>玩家</dt><dd id="playerCount">-</dd></div>
|
||||
<div><dt>总手数</dt><dd id="handCount">-</dd></div>
|
||||
<div><dt>盲注</dt><dd id="blindLevel">-</dd></div>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<aside class="event-panel" aria-label="事件日志">
|
||||
<div class="panel-section">
|
||||
<h2>事件</h2>
|
||||
<ol id="eventLog" class="event-log"></ol>
|
||||
<div class="panel-header compact">
|
||||
<h2>Timeline</h2>
|
||||
</div>
|
||||
<ol id="eventLog" class="event-log"></ol>
|
||||
</aside>
|
||||
</main>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<script src="/app.js" type="module"></script>
|
||||
<script src="/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user