Compare commits
66
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54cc5e9b2e | ||
|
|
9b79d18201 | ||
|
|
45ca961064 | ||
|
|
69bdd94ef2 | ||
|
|
8fbebc21f3 | ||
|
|
7cfdecb2c3 | ||
|
|
38f3837f5a | ||
|
|
681888adcd | ||
|
|
88c0d94d39 | ||
|
|
4fdddf332e | ||
|
|
213b9dc32d | ||
|
|
2af1d58104 | ||
|
|
547a84d8c2 | ||
|
|
71b86b74eb | ||
|
|
d00093897a | ||
|
|
0b01f1c467 | ||
|
|
65873555bc | ||
|
|
8995d743af | ||
|
|
14ff543135 | ||
|
|
ea23f97b9c | ||
|
|
a23fdc55a4 | ||
|
|
4cae97ab44 | ||
|
|
d308e8f413 | ||
|
|
44123f7622 | ||
|
|
9eaa41a4ae | ||
|
|
409aba26f4 | ||
|
|
30c3ab5a89 | ||
|
|
b7f9c119cb | ||
|
|
bbb7f7a966 | ||
|
|
ef739e1f49 | ||
|
|
920d7d9787 | ||
|
|
52d51ebd5e | ||
|
|
db3675aa6a | ||
|
|
99044342c2 | ||
|
|
d5fd648139 | ||
|
|
022f89478d | ||
|
|
336f913d4b | ||
|
|
661847a4d8 | ||
|
|
e710fd5e03 | ||
|
|
85af163123 | ||
|
|
f2e87feb4e | ||
|
|
948f8e0f8f | ||
|
|
84d56bf901 | ||
|
|
74d24d606c | ||
|
|
98e1092ebc | ||
|
|
9338ac881b | ||
|
|
c3ff9f8888 | ||
|
|
e4f35a2d8b | ||
|
|
084742a56a | ||
|
|
45a413837b | ||
|
|
018852c92d | ||
|
|
4ca21e5e29 | ||
|
|
28665bf663 | ||
|
|
83d05294c8 | ||
|
|
e8f8766d59 | ||
|
|
a69b273f5f | ||
|
|
e55ac9aebf | ||
|
|
d2b86ef2ed | ||
|
|
081dd66121 | ||
|
|
6d89d5bb08 | ||
|
|
f29a5a31b5 | ||
|
|
370d4cd92f | ||
|
|
e90efe1654 | ||
|
|
bf701397b0 | ||
|
|
7750b43494 | ||
|
|
fa58463cde |
No files matched your search
+8
-3
@@ -8,15 +8,20 @@ XIMILAR_API_KEY=your-ximilar-key-here
|
|||||||
|
|
||||||
# ── Forge JAR Path ────────────────────────────────────────────────
|
# ── Forge JAR Path ────────────────────────────────────────────────
|
||||||
# Point this to your Forge GUI desktop fat JAR.
|
# Point this to your Forge GUI desktop fat JAR.
|
||||||
FORGE_JAR_PATH=D:/ForgeCommander/forge-repo/forge-gui-desktop/target/forge-gui-desktop-2.0.12-SNAPSHOT-jar-with-dependencies.jar
|
FORGE_JAR_PATH=D:/ForgeCommander/Forge-Repo/forge-gui-desktop/target/forge-gui-desktop-2.0.12-SNAPSHOT-jar-with-dependencies.jar
|
||||||
|
|
||||||
# IMPORTANT: Forge MUST be launched from the forge-gui directory (not forge-gui-desktop/target).
|
# IMPORTANT: Forge MUST be launched from the forge-gui directory (not forge-gui-desktop/target).
|
||||||
# The JAR looks for res/ relative to its working directory. Without this, you get
|
# The JAR looks for res/ relative to its working directory. Without this, you get
|
||||||
# ExceptionInInitializerError on startup.
|
# ExceptionInInitializerError on startup.
|
||||||
FORGE_WORKING_DIR=D:/ForgeCommander/forge-repo/forge-gui
|
FORGE_WORKING_DIR=D:/ForgeCommander/Forge-Repo/forge-gui
|
||||||
|
|
||||||
# ── Deck Paths ────────────────────────────────────────────────────
|
# ── Deck Paths ────────────────────────────────────────────────────
|
||||||
# Directory containing your precon .dck files.
|
# Directory containing your precon .dck files.
|
||||||
# Forge reads deck files directly from this folder.
|
# Forge reads deck files directly from this folder.
|
||||||
|
#
|
||||||
|
# IMPORTANT: lab_api.py reads PRECON_DIR (not PRECON_DECKS_DIR).
|
||||||
|
# PRECON_DECKS_DIR is kept below for backwards compat but is NOT used by the server.
|
||||||
|
PRECON_DIR=D:\ForgeCommander\commander-ai-lab\precon-decks
|
||||||
PRECON_DECKS_DIR=D:\ForgeCommander\commander-ai-lab\precon-decks
|
PRECON_DECKS_DIR=D:\ForgeCommander\commander-ai-lab\precon-decks
|
||||||
FORGE_DECKS_DIR=D:\ForgeCommander\commander-ai-lab\precon-decks
|
USER_DECKS_DIR=D:\ForgeCommander\commander-ai-lab\user-decks
|
||||||
|
FORGE_DECKS_DIR=D:\ForgeCommander\commander-ai-lab\user-decks
|
||||||
+15
@@ -32,9 +32,24 @@ embeddings/
|
|||||||
checkpoints/
|
checkpoints/
|
||||||
imported-decks/
|
imported-decks/
|
||||||
|
|
||||||
|
# User-created decks (generated at runtime, not committed)
|
||||||
|
user-decks/*.dck
|
||||||
|
|
||||||
# OS
|
# OS
|
||||||
.DS_Store
|
.DS_Store
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
*.log
|
*.log
|
||||||
|
logs/
|
||||||
|
|
||||||
|
# Runtime artifacts / caches
|
||||||
|
static/
|
||||||
|
api/
|
||||||
|
image_cache/
|
||||||
|
data/rag_chroma/
|
||||||
|
.claude/
|
||||||
|
dependency-reduced-pom.xml
|
||||||
|
|
||||||
|
# Precon decks (generated / imported at runtime)
|
||||||
|
precon-decks/
|
||||||
@@ -1,67 +1,212 @@
|
|||||||
# Commander AI Lab
|
# Commander AI Lab
|
||||||
|
|
||||||
A full-stack MTG Commander AI research platform. Simulate hundreds of games overnight, train ML models on the results, and let simulation data automatically shape the AI deck builder — all from a web UI or CLI.
|
A full-stack Magic: The Gathering Commander research platform. Run thousands of simulated games, train neural networks on the outcomes, build decks with AI that learns from simulation data, get LLM-powered coaching, and play live 4-player battles — all from a single unified system.
|
||||||
|
|
||||||
## What It Does
|
---
|
||||||
|
|
||||||
| Layer | What it is |
|
## Overview
|
||||||
|---|---|
|
|
||||||
| **Monte Carlo Engine** | Pure-Python headless sim — runs thousands of games in minutes, no JVM needed |
|
Commander AI Lab connects a rules-complete Forge engine, a fast Monte Carlo simulator, an LLM deck builder, an ML training pipeline, and multiple game clients (web, LAN, Unity) into one closed-loop platform. Simulation results feed back into deck building and AI decision-making, so the system improves the more it plays.
|
||||||
| **Forge Engine** | Java/Forge rules-complete sim for ground-truth validation |
|
|
||||||
| **Overnight Runner** | Unattended batch runner — runs sims, updates sim weights, trains neural net |
|
### Key Capabilities
|
||||||
| **ML Pipeline** | Outcome-weighted RL nudges card scoring weights; neural net trains on decision logs |
|
|
||||||
| **Deck Builder** | 7-step LLM pipeline (EDHrec → Scryfall → Ollama) now injected with sim-learned insights |
|
- **Batch Simulation** — Run hundreds or thousands of Commander games unattended using Forge (rules-complete) or a headless Python Monte Carlo engine
|
||||||
| **AI Coach** | LLM-powered coaching sessions (Anthropic/Ollama) with streaming support |
|
- **ML Training Pipeline** — Reinforcement learning (PPO) trains a neural net on game decision logs; outcome-weighted scoring nudges card weights automatically
|
||||||
| **Web UI** | React + FastAPI dashboard for decks, sims, training, and deck building |
|
- **AI Deck Builder** — 7-step LLM pipeline (EDHrec → Scryfall → Ollama) with simulation-learned insights injected into prompts
|
||||||
|
- **LLM Coach** — Streaming coaching sessions powered by Anthropic Claude or local Ollama models, enriched with RAG-retrieved card knowledge
|
||||||
|
- **RAG Card Knowledge** — ChromaDB vector store over 30,000+ Scryfall cards with semantic search and auto-staleness monitoring
|
||||||
|
- **LAN Battlefield** — Browser-based 4-player Commander game client with ML-powered AI opponents and precon deck support
|
||||||
|
- **Unity Client** — Full Unity application with battleground scene, lobby system, WebSocket game state sync, and cross-platform builds
|
||||||
|
- **Card Scanner** — OCR pipeline for physical card recognition via Ximilar API
|
||||||
|
- **163 Precon Decks** — Complete preconstructed deck library in `.dck` format
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
```
|
```
|
||||||
┌─────────────────────────────────────────────────────────────────┐
|
┌─────────────────────────────────────────────────────────────────────┐
|
||||||
│ Web UI (React/TypeScript — frontend/) │
|
│ Clients │
|
||||||
│ Deck Builder · Sim Dashboard · Training · Coach │
|
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────────────────┐ │
|
||||||
└────────────────────────┬────────────────────────────────────────┘
|
│ │ Web UI │ │ LAN Client │ │ Unity Client │ │
|
||||||
│ HTTP/REST + WebSocket
|
│ │ (React/TS) │ │ (HTML/JS) │ │ (C# / URP) │ │
|
||||||
┌────────────────────────┴────────────────────────────────────────┐
|
│ │ /ui │ │ /lan │ │ /unity-client │ │
|
||||||
│ FastAPI Server (lab_api.py / routes/ — port 8080) │
|
│ └──────┬───────┘ └──────┬───────┘ └──────┬───────────────────────┘ │
|
||||||
│ /api/lab/* /api/ml/* /api/coach/* /api/deck-builder/* │
|
│ └────────────────┼────────────────┘ │
|
||||||
└────────┬──────────────────┬──────────────────┬──────────────────┘
|
│ │ HTTP/REST + WebSocket │
|
||||||
│ │ │
|
│ ┌───────────────────────┴─────────────────────────────────────────┐│
|
||||||
Java subprocess Python sim engine Ollama (local LLM)
|
│ │ FastAPI Backend (lab_api.py — port 8080) ││
|
||||||
(Forge rules) (Monte Carlo) qwen2.5:7b + others
|
│ │ ││
|
||||||
│ │
|
│ │ /api/lab/* Batch simulation (Forge + Monte Carlo) ││
|
||||||
┌────────┴───────┐ ┌───────┴────────────────────────────────────┐
|
│ │ /api/deck-builder/* 7-step LLM deck construction ││
|
||||||
│ Forge Engine │ │ src/commander_ai_lab/ │
|
│ │ /api/coach/* Streaming LLM coaching sessions ││
|
||||||
│ (rules- │ │ sim/ — engine, models, rules │
|
│ │ /api/ml/* Neural net training + policy server ││
|
||||||
│ complete) │ │ deck_builder/ — 7-step LLM pipeline │
|
│ │ /api/rag/* ChromaDB vector search ││
|
||||||
└────────────────┘ │ coach/ — streaming LLM coach │
|
│ │ /api/game/* Live game sessions (Forge IPC) ││
|
||||||
└────────────────────────────────────────────┘
|
│ │ /api/policy/* ML policy decisions for AI players ││
|
||||||
|
│ │ /ws/game/{id} WebSocket game state streaming ││
|
||||||
|
│ └───────┬──────────────┬──────────────┬──────────────┬────────────┘│
|
||||||
|
│ │ │ │ │ │
|
||||||
|
│ ┌──────┴──────┐ ┌────┴─────┐ ┌──────┴──────┐ ┌────┴──────┐ │
|
||||||
|
│ │ Forge │ │ Monte │ │ Ollama │ │ ChromaDB │ │
|
||||||
|
│ │ Engine │ │ Carlo │ │ (local LLM) │ │ (vector │ │
|
||||||
|
│ │ (Java/JVM) │ │ Engine │ │ qwen2.5:7b │ │ store) │ │
|
||||||
|
│ └─────────────┘ │ (Python) │ └─────────────┘ └───────────┘ │
|
||||||
|
│ └──────────┘ │
|
||||||
|
│ ┌──────────────────────────────────────────────────────────────────┐│
|
||||||
|
│ │ ML Pipeline ││
|
||||||
|
│ │ decisions_*.jsonl → update_weights.py → learned_weights.json ││
|
||||||
|
│ │ → /api/ml/train → neural net checkpoints ││
|
||||||
|
│ └──────────────────────────────────────────────────────────────────┘│
|
||||||
|
└─────────────────────────────────────────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
## Quick Start (Windows)
|
---
|
||||||
|
|
||||||
**Prerequisites:** Java 17+, Python 3.10+, Maven 3.8+, [Forge](https://github.com/Card-Forge/forge) built at `D:\ForgeCommander\forge-repo`
|
## Features
|
||||||
|
|
||||||
|
### Simulation Engines
|
||||||
|
|
||||||
|
| Engine | Description | Speed |
|
||||||
|
|---|---|---|
|
||||||
|
| **Forge** | Rules-complete Java engine — handles the stack, instants, triggered abilities, and all Commander rules. Each game spawns a JVM subprocess. | ~0.04 sims/sec (single-threaded) |
|
||||||
|
| **Monte Carlo** | Headless Python engine — models combat keywords (flying, trample, deathtouch, lifelink, menace, reach, haste), summoning sickness, tapped state, commander zone, multiplayer targeting, and board wipes. No JVM required. | Thousands of games in minutes |
|
||||||
|
|
||||||
|
Both engines feed structured results into the same ML pipeline.
|
||||||
|
|
||||||
|
### Deck Builder
|
||||||
|
|
||||||
|
The `POST /api/deck-builder/build` endpoint runs a 7-step pipeline:
|
||||||
|
|
||||||
|
1. Resolve commander via Scryfall
|
||||||
|
2. Fetch EDHrec recommendations
|
||||||
|
3. Fetch Scryfall candidates by category (ramp, removal, draw, lands)
|
||||||
|
4. Ollama suggests synergy/protection/wincon cards — **simulation insights injected here**
|
||||||
|
5. Filter by color identity, collection, and ban list
|
||||||
|
6. Ollama enforces ratios (trim/expand to 99) — **simulation insights injected here**
|
||||||
|
7. Assemble final deck with smart substitution (guarantees 99-card output)
|
||||||
|
|
||||||
|
### AI Coach
|
||||||
|
|
||||||
|
LLM-powered coaching with two modes:
|
||||||
|
|
||||||
|
- **Full Session** — Deep analysis including removal coverage, ramp quality, draw engine profiling, win conditions, anti-synergy detection, pod meta evaluation, and tempo assessment
|
||||||
|
- **Quick Digest** — Fast ~5-second summary for rapid feedback
|
||||||
|
|
||||||
|
Coaching prompts are enriched with semantically relevant cards from the ChromaDB RAG store (15 cards for full sessions, 8 for quick digests). Supports both Anthropic Claude and local Ollama models with streaming responses.
|
||||||
|
|
||||||
|
### RAG Card Knowledge
|
||||||
|
|
||||||
|
- **SQLite FTS5** bulk card database from Scryfall data
|
||||||
|
- **ChromaDB** vector index with natural-language card embeddings
|
||||||
|
- Automatic staleness monitoring (checks every 6 hours, auto-rebuilds if >14 days old)
|
||||||
|
- Graceful fallback — RAG failures never block coaching or deck building
|
||||||
|
|
||||||
|
### ML Training Pipeline
|
||||||
|
|
||||||
|
```
|
||||||
|
Overnight sims → decisions_*.jsonl
|
||||||
|
↓
|
||||||
|
update_weights.py → learned_weights.json (feeds deck builder)
|
||||||
|
↓
|
||||||
|
/api/ml/train → neural net training on decision dataset
|
||||||
|
↓
|
||||||
|
/api/policy/decide → ML policy serves AI decisions in live games
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Outcome-weighted RL** nudges card scoring weights from game results
|
||||||
|
- **Neural net** trains on the full decision log dataset
|
||||||
|
- **Distillation loop** — self-play → collect data → retrain → repeat
|
||||||
|
- **ELO tracking** and model tournaments for comparing AI versions
|
||||||
|
- Weights clamped to `[-20.0, 20.0]`, stored in `learned_weights.json`
|
||||||
|
|
||||||
|
### Win Condition Tracking
|
||||||
|
|
||||||
|
Games are classified by win type — not just "Combat" vs "Other":
|
||||||
|
|
||||||
|
- **Combat** — standard creature damage wins
|
||||||
|
- **Combo** — Approach of the Second Sun, Felidar Sovereign, Happily Ever After, and 30+ known alternate-win cards
|
||||||
|
- **Drain** — life drain effects
|
||||||
|
- **Mill** — library depletion
|
||||||
|
- **Poison** — infect/poison counter wins
|
||||||
|
|
||||||
|
Draw games (timeout) are analyzed separately with per-player life-at-timeout snapshots and likely cause identification.
|
||||||
|
|
||||||
|
### LAN Battlefield Client
|
||||||
|
|
||||||
|
Browser-based 4-player Commander game at `/lan`:
|
||||||
|
|
||||||
|
- **AI Bridge** — REST + WebSocket connectivity with ML policy decisions as Priority 1 (falls back to local heuristic AI)
|
||||||
|
- **Deck System** — 3-tab selector: Precons (163 decks, grouped by set), EDHREC search, and custom deck import
|
||||||
|
- **Player Panels** — Life counters, commander damage bars, poison/energy/experience/rad counters, emblem management
|
||||||
|
- **Backend Detection** — Auto-detects the Commander AI Lab backend; works offline with local precon data
|
||||||
|
|
||||||
|
### Unity Client
|
||||||
|
|
||||||
|
Full Unity application (URP, .NET Standard 2.1) with:
|
||||||
|
|
||||||
|
- **Battleground Scene** — 4-seat layout with rotated perspectives, life/poison/commander damage widgets, phase tracker, turn order bar
|
||||||
|
- **Card System** — Hand, battlefield, graveyard, exile zones with card art, tapped rotation, face-down state, summoning sickness indicators
|
||||||
|
- **Stack & Mana** — LIFO stack display and 6-pip mana pool
|
||||||
|
- **Lobby System** — Configure 4 seats (human/AI), POST to backend, WebSocket game state sync
|
||||||
|
- **Elimination** — Grayscale shader overlay on eliminated players
|
||||||
|
- **Token System** — Create, tap, add counters, delete tokens
|
||||||
|
- **Cross-Platform** — Windows, WebGL, Android, iOS build targets
|
||||||
|
- **Scenes** — MainMenu, Collection, DeckBuilder, DeckGenerator, Simulator, Coach, Scanner, Training, Precon, Battleground
|
||||||
|
|
||||||
|
### Card Scanner
|
||||||
|
|
||||||
|
OCR pipeline for physical Magic cards using the Ximilar API. Scan cards from photos and match them to the Scryfall database.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
| Tool | Version |
|
||||||
|
|---|---|
|
||||||
|
| Python | 3.10+ |
|
||||||
|
| Java | 17+ |
|
||||||
|
| Maven | 3.8+ |
|
||||||
|
| Node.js | 18+ (for LAN client scripts) |
|
||||||
|
| Ollama | Latest (for local LLM inference) |
|
||||||
|
| [Forge](https://github.com/KoalaTrapLord/Forge-Repo) | Built at `D:\ForgeCommander\Forge-Repo` |
|
||||||
|
|
||||||
|
### Setup (Windows)
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
# 1. Build Forge (one-time)
|
# 1. Build Forge (one-time)
|
||||||
cd D:\ForgeCommander\forge-repo
|
cd D:\ForgeCommander\Forge-Repo
|
||||||
mvn package -pl forge-gui-desktop "-DskipTests" "-Drevision=2.0.12-SNAPSHOT" -am
|
mvn package -pl forge-gui-desktop "-DskipTests" "-Drevision=2.0.12-SNAPSHOT" -am
|
||||||
|
|
||||||
# 2. Build Commander AI Lab
|
# 2. Build Commander AI Lab
|
||||||
cd D:\ForgeCommander\commander-ai-lab
|
cd D:\ForgeCommander\commander-ai-lab
|
||||||
mvn package "-DskipTests"
|
mvn package "-DskipTests"
|
||||||
|
|
||||||
# 3. Install Python deps
|
# 3. Install Python dependencies
|
||||||
pip install -e .
|
pip install -e ".[rag,dev]"
|
||||||
|
|
||||||
# 4. Launch
|
# 4. Pull an Ollama model for the deck builder
|
||||||
|
ollama pull qwen2.5:7b
|
||||||
|
|
||||||
|
# 5. Launch
|
||||||
.\start-lab.bat
|
.\start-lab.bat
|
||||||
# → open http://localhost:8080
|
# → Web UI: http://localhost:8080
|
||||||
|
# → LAN Client: http://localhost:8080/lan/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Or use the automated installer:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
.\install-commander-ai-lab.ps1
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Overnight AFK Runner
|
## Overnight AFK Runner
|
||||||
|
|
||||||
Runs batch sims unattended, then updates card weights and trains the neural net:
|
Run batch simulations unattended, then automatically update card weights and train the neural net:
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
python overnight-run.py # default: 11.5h DeepSeek sims
|
python overnight-run.py # default: 11.5h DeepSeek sims
|
||||||
@@ -74,9 +219,9 @@ python overnight-run.py --no-train # skip neural net training
|
|||||||
1. `update_weights.py` — nudges `learned_weights.json` from decision logs (feeds deck builder)
|
1. `update_weights.py` — nudges `learned_weights.json` from decision logs (feeds deck builder)
|
||||||
2. `/api/ml/train` — trains the neural net on the full decision dataset
|
2. `/api/ml/train` — trains the neural net on the full decision dataset
|
||||||
|
|
||||||
## Sim → Deck Builder Feedback Loop
|
---
|
||||||
|
|
||||||
Simulation outcomes automatically improve deck recommendations:
|
## Simulation → Deck Builder Feedback Loop
|
||||||
|
|
||||||
```
|
```
|
||||||
Overnight sims → decisions_*.jsonl
|
Overnight sims → decisions_*.jsonl
|
||||||
@@ -90,24 +235,63 @@ Ollama prompts (suggest_cards + enforce_deck_ratios)
|
|||||||
Better card recommendations
|
Better card recommendations
|
||||||
```
|
```
|
||||||
|
|
||||||
When active, `BuildResult.sources_consulted` includes `"sim_weights"`. No-op until real sim data exists.
|
When active, `BuildResult.sources_consulted` includes `"sim_weights"`. The loop is a no-op until real sim data exists.
|
||||||
|
|
||||||
## Deck Builder Pipeline
|
---
|
||||||
|
|
||||||
The `POST /api/deck-builder/build` endpoint runs 7 steps:
|
## API Reference
|
||||||
|
|
||||||
1. Resolve commander via Scryfall
|
### Simulation
|
||||||
2. Fetch EDHrec recommendations
|
|
||||||
3. Fetch Scryfall candidates by category (ramp, removal, draw, lands)
|
|
||||||
4. Ollama suggests synergy/protection/wincon cards ← **sim insights injected here**
|
|
||||||
5. Filter by color identity, collection, ban list
|
|
||||||
6. Ollama enforces ratios (trim/expand to 99) ← **sim insights injected here**
|
|
||||||
7. Assemble final deck
|
|
||||||
|
|
||||||
## ML Pipeline
|
| Endpoint | Method | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `/api/lab/decks` | GET | List available Commander decks |
|
||||||
|
| `/api/lab/start` | POST | Start Forge batch (returns batchId) |
|
||||||
|
| `/api/lab/start-deepseek` | POST | Start Python/DeepSeek batch |
|
||||||
|
| `/api/lab/status?batchId=X` | GET | Poll batch progress |
|
||||||
|
| `/api/lab/result?batchId=X` | GET | Get completed batch JSON |
|
||||||
|
| `/api/lab/history` | GET | List past batch runs |
|
||||||
|
| `/api/lab/precons/deck` | GET | Fetch precon `.dck` file content |
|
||||||
|
|
||||||
|
### Deck Builder & Coach
|
||||||
|
|
||||||
|
| Endpoint | Method | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `/api/deck-builder/build` | POST | Build a 99-card Commander deck |
|
||||||
|
| `/api/coach/chat` | POST | Full coaching session (streaming) |
|
||||||
|
| `/api/coach/quick-digest` | POST | Fast ~5s coaching summary |
|
||||||
|
|
||||||
|
### ML & Policy
|
||||||
|
|
||||||
|
| Endpoint | Method | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `/api/ml/train` | POST | Start neural net training |
|
||||||
|
| `/api/ml/train/status` | GET | Poll training progress |
|
||||||
|
| `/api/policy/decide` | POST | Get ML policy decision for game state |
|
||||||
|
|
||||||
|
### RAG
|
||||||
|
|
||||||
|
| Endpoint | Method | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `/api/rag/build` | POST | Build/rebuild ChromaDB index |
|
||||||
|
| `/api/rag/search` | POST | Semantic card search |
|
||||||
|
| `/api/rag/status` | GET | Index health and staleness |
|
||||||
|
|
||||||
|
### Live Game
|
||||||
|
|
||||||
|
| Endpoint | Method | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `/api/game/start` | POST | Create a live game session |
|
||||||
|
| `/api/game/action` | POST | Submit a player action |
|
||||||
|
| `/api/game/{id}/actions` | GET | Poll Forge IPC actions |
|
||||||
|
| `/ws/game/{game_id}` | WS | Real-time game state streaming |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ML Scripts
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
# Update sim card weights from decision logs (runs automatically in overnight-run.py)
|
# Update card weights from decision logs
|
||||||
python -m ml.scripts.update_weights --min-games 10 --lr 0.05 --dry-run
|
python -m ml.scripts.update_weights --min-games 10 --lr 0.05 --dry-run
|
||||||
|
|
||||||
# Reset weights to defaults
|
# Reset weights to defaults
|
||||||
@@ -117,7 +301,7 @@ python -m ml.scripts.update_weights --reset
|
|||||||
python ml/scripts/generate_synthetic_data.py
|
python ml/scripts/generate_synthetic_data.py
|
||||||
```
|
```
|
||||||
|
|
||||||
Learned weights are clamped to `[-20.0, 20.0]` and stored in `src/commander_ai_lab/sim/learned_weights.json`.
|
---
|
||||||
|
|
||||||
## Python Simulator API
|
## Python Simulator API
|
||||||
|
|
||||||
@@ -129,52 +313,122 @@ stats = run_simulation(deck, num_games=100, name_a="My Deck")
|
|||||||
print(f"Win rate: {stats['win_rate']:.1%}, Avg turns: {stats['avg_turns']:.1f}")
|
print(f"Win rate: {stats['win_rate']:.1%}, Avg turns: {stats['avg_turns']:.1f}")
|
||||||
```
|
```
|
||||||
|
|
||||||
The Monte Carlo engine models: combat keywords (flying, trample, deathtouch, lifelink, menace, reach, haste), summoning sickness, tapped/untap state, commander zone, multiplayer attack targeting, and board wipes. It does **not** model the stack, instants, or continuous type-changing effects.
|
---
|
||||||
|
|
||||||
## API Reference
|
|
||||||
|
|
||||||
| Endpoint | Method | Description |
|
|
||||||
|---|---|---|
|
|
||||||
| `/api/lab/decks` | GET | List available Commander decks |
|
|
||||||
| `/api/lab/start` | POST | Start Forge batch (returns batchId) |
|
|
||||||
| `/api/lab/start-deepseek` | POST | Start Python/DeepSeek batch |
|
|
||||||
| `/api/lab/status?batchId=X` | GET | Poll batch progress |
|
|
||||||
| `/api/lab/result?batchId=X` | GET | Get completed batch JSON |
|
|
||||||
| `/api/lab/history` | GET | List past batch runs |
|
|
||||||
| `/api/ml/train` | POST | Start neural net training |
|
|
||||||
| `/api/ml/train/status` | GET | Poll training progress |
|
|
||||||
| `/api/coach/chat` | POST | LLM coaching session (streaming) |
|
|
||||||
| `/api/deck-builder/build` | POST | Build a 99-card Commander deck |
|
|
||||||
|
|
||||||
## Project Structure
|
## Project Structure
|
||||||
|
|
||||||
```
|
```
|
||||||
commander-ai-lab/
|
commander-ai-lab/
|
||||||
|
├── lab_api.py # FastAPI server entry point
|
||||||
├── overnight-run.py # AFK batch runner
|
├── overnight-run.py # AFK batch runner
|
||||||
├── start-lab.bat # Windows one-click launcher
|
├── start-lab.bat # Windows one-click launcher
|
||||||
├── lab_api.py # FastAPI server entry point
|
├── install-commander-ai-lab.ps1 # Automated setup script
|
||||||
├── frontend/ # React + TypeScript web UI
|
├── pom.xml # Maven build (Java/Forge components)
|
||||||
├── ui/ # Legacy static HTML UI
|
├── pyproject.toml # Python package config
|
||||||
|
│
|
||||||
|
├── routes/ # FastAPI route modules
|
||||||
|
│ ├── lab.py # Batch simulation
|
||||||
|
│ ├── deckbuilder.py # Deck builder pipeline
|
||||||
|
│ ├── coach.py # LLM coaching
|
||||||
|
│ ├── ml.py # ML training
|
||||||
|
│ ├── rag.py # ChromaDB vector search
|
||||||
|
│ ├── game.py # Live game sessions
|
||||||
|
│ ├── policy.py # ML policy server
|
||||||
|
│ ├── precon.py # Precon deck serving
|
||||||
|
│ ├── scanner.py # Card OCR
|
||||||
|
│ └── ws_game.py # WebSocket game state
|
||||||
|
│
|
||||||
|
├── services/ # Core service layer
|
||||||
|
│ ├── forge_runner.py # Forge subprocess management
|
||||||
|
│ ├── draw_game_analyzer.py # Draw/timeout analysis
|
||||||
|
│ ├── rag_store.py # ChromaDB vector store
|
||||||
|
│ └── card_text.py # Shared card text builder
|
||||||
|
│
|
||||||
├── src/commander_ai_lab/
|
├── src/commander_ai_lab/
|
||||||
│ ├── sim/ # Monte Carlo engine (engine, models, rules)
|
│ ├── sim/ # Monte Carlo engine
|
||||||
│ ├── deck_builder/ # LLM deck builder pipeline
|
│ │ ├── engine.py # Game loop
|
||||||
│ │ ├── api/ # Scryfall, EDHrec, Ollama, sim_insights
|
│ │ ├── models.py # GameResult, Card, Player
|
||||||
│ │ ├── core/ # Models, rules engine, assembler
|
│ │ ├── rules.py # Combat, keywords, commander rules
|
||||||
│ │ └── pipeline/ # build_deck.py (7-step orchestrator)
|
│ │ ├── win_condition_parser.py # Alternate win detection
|
||||||
│ └── coach/ # LLM coaching (Anthropic + Ollama)
|
│ │ └── learned_weights.json # ML-learned card weights
|
||||||
├── ml/
|
│ ├── deck_builder/ # 7-step LLM pipeline
|
||||||
│ ├── scripts/ # update_weights.py, ml_cli.py
|
│ │ ├── api/ # Scryfall, EDHrec, Ollama, sim_insights
|
||||||
│ ├── training/ # Neural net trainer
|
│ │ ├── core/ # Models, rules engine, assembler
|
||||||
│ ├── encoder/ # Feature encoding
|
│ │ └── pipeline/ # build_deck.py orchestrator
|
||||||
│ └── models/ # Saved model checkpoints
|
│ └── coach/ # LLM coaching
|
||||||
├── scanner/ # Card OCR pipeline
|
│ ├── coach_service.py # Session + quick digest
|
||||||
├── logs/decisions/ # ML decision logs (gitignored)
|
│ ├── prompt_template.py # System/user prompts
|
||||||
└── src/main/java/commanderailab/ # Java/Forge batch runner
|
│ └── embeddings.py # Card embedding utilities
|
||||||
|
│
|
||||||
|
├── ml/ # ML training pipeline
|
||||||
|
│ ├── scripts/ # update_weights.py, ml_cli.py
|
||||||
|
│ ├── training/ # Neural net trainer
|
||||||
|
│ ├── encoder/ # Feature encoding
|
||||||
|
│ └── models/ # Saved checkpoints
|
||||||
|
│
|
||||||
|
├── lan-client/ # Browser-based LAN battlefield
|
||||||
|
│ ├── index.html # Entry point
|
||||||
|
│ ├── engine.js # Game engine + ML AI turns
|
||||||
|
│ ├── ai-bridge.js # Backend connector + policy bridge
|
||||||
|
│ ├── player-panels.js # Enhanced player panels
|
||||||
|
│ ├── ui.js # 3-tab deck selector + setup
|
||||||
|
│ └── precons.js # 163 precon deck data
|
||||||
|
│
|
||||||
|
├── unity-client/ # Unity game client
|
||||||
|
│ └── Assets/
|
||||||
|
│ ├── Scenes/ # 11 scenes (MainMenu → Battleground)
|
||||||
|
│ ├── Scripts/ # C# controllers, models, services
|
||||||
|
│ ├── Prefabs/ # Card, deck slot, chat bubble
|
||||||
|
│ └── Materials/ # URP materials + grayscale shader
|
||||||
|
│
|
||||||
|
├── scanner/ # Card OCR pipeline (Ximilar)
|
||||||
|
├── precon-decks/ # 163 preconstructed decks (.dck)
|
||||||
|
├── sample-decks/ # Sample deck files
|
||||||
|
├── data/ # Runtime data (ChromaDB, bulk DB)
|
||||||
|
├── logs/ # Simulation + decision logs
|
||||||
|
├── docs/ # Architecture docs + schemas
|
||||||
|
├── tests/ # Test suite
|
||||||
|
├── scripts/ # Utility scripts
|
||||||
|
├── .github/workflows/ # CI + Unity build automation
|
||||||
|
└── src/main/java/ # Java/Forge batch runner
|
||||||
|
└── commanderailab/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Tech Stack
|
||||||
|
|
||||||
|
| Layer | Technology |
|
||||||
|
|---|---|
|
||||||
|
| Backend | Python 3.10+, FastAPI, Uvicorn |
|
||||||
|
| Simulation (rules-complete) | Java 17, Maven, Forge |
|
||||||
|
| Simulation (fast) | Python Monte Carlo engine |
|
||||||
|
| LLM Inference | Ollama (qwen2.5:7b), Anthropic Claude |
|
||||||
|
| Vector Store | ChromaDB with all-MiniLM-L6-v2 embeddings |
|
||||||
|
| Card Database | SQLite FTS5 (Scryfall bulk data) |
|
||||||
|
| ML Training | PyTorch, PPO reinforcement learning |
|
||||||
|
| Web Client | HTML/CSS/JS |
|
||||||
|
| Unity Client | Unity 2022 LTS, URP, C# |
|
||||||
|
| CI/CD | GitHub Actions |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Known Limitations
|
## Known Limitations
|
||||||
|
|
||||||
- **Monte Carlo engine** — no stack, no instants, no continuous type changes (animated lands don't attack)
|
- **Monte Carlo engine** — no stack, no instants, no continuous type-changing effects (animated lands don't attack)
|
||||||
- **Forge engine** — each game spawns a new JVM (~2-3s overhead), ~0.04 sims/sec single-threaded
|
- **Forge engine** — each game spawns a new JVM (~2–3s overhead); ~0.04 sims/sec single-threaded
|
||||||
- **Forge winner life total** not captured (quiet mode only reports losers)
|
- **Forge** — winner life total not captured in quiet mode (only reports losers)
|
||||||
- **Deck builder** — Ollama suggestions are only as good as the local model; `qwen2.5:7b` works well
|
- **Deck builder** — Ollama suggestions depend on the local model quality; `qwen2.5:7b` works well
|
||||||
|
- **LAN client** — EDHREC commander search is a stub (backend-powered, not yet fully wired)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Commander AI Lab is an active research project. Check the [open issues](https://github.com/KoalaTrapLord/commander-ai-lab/issues) for areas where contributions are welcome.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
See repository for license details.
|
||||||
@@ -0,0 +1,257 @@
|
|||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Smoke-tests the Commander AI Lab policy server from PowerShell.
|
||||||
|
Checks: server reachable -> /api/policy/health -> /api/policy/decide.
|
||||||
|
Prints a plain-English pass/fail for every step.
|
||||||
|
|
||||||
|
.USAGE
|
||||||
|
# From repo root (server already running in another window):
|
||||||
|
.\Test-PolicyServer.ps1
|
||||||
|
|
||||||
|
# Custom host/port:
|
||||||
|
.\Test-PolicyServer.ps1 -BaseUrl http://localhost:9000
|
||||||
|
|
||||||
|
# Pretty-print the full decide response:
|
||||||
|
.\Test-PolicyServer.ps1 -Verbose
|
||||||
|
#>
|
||||||
|
param(
|
||||||
|
[string]$BaseUrl = "http://localhost:8080",
|
||||||
|
[switch]$Verbose
|
||||||
|
)
|
||||||
|
|
||||||
|
$ErrorActionPreference = "Stop"
|
||||||
|
|
||||||
|
# ---- helpers ----------------------------------------------------------------
|
||||||
|
|
||||||
|
function Write-Pass { param([string]$msg) Write-Host " [PASS] $msg" -ForegroundColor Green }
|
||||||
|
function Write-Fail { param([string]$msg) Write-Host " [FAIL] $msg" -ForegroundColor Red }
|
||||||
|
function Write-Info { param([string]$msg) Write-Host " [INFO] $msg" -ForegroundColor Cyan }
|
||||||
|
function Write-Warn { param([string]$msg) Write-Host " [WARN] $msg" -ForegroundColor Yellow }
|
||||||
|
function Write-Step { param([string]$msg) Write-Host "`n-- $msg" -ForegroundColor White }
|
||||||
|
|
||||||
|
$allPassed = $true
|
||||||
|
|
||||||
|
function Invoke-Step {
|
||||||
|
param(
|
||||||
|
[string] $Name,
|
||||||
|
[scriptblock] $Block
|
||||||
|
)
|
||||||
|
Write-Step $Name
|
||||||
|
try { & $Block }
|
||||||
|
catch {
|
||||||
|
Write-Fail $_.Exception.Message
|
||||||
|
$script:allPassed = $false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---- Step 1: server reachable -----------------------------------------------
|
||||||
|
|
||||||
|
Invoke-Step "Step 1 -- Server reachable at $BaseUrl" {
|
||||||
|
$r1 = $null
|
||||||
|
try { $r1 = Invoke-WebRequest -Uri "$BaseUrl/docs" -UseBasicParsing -TimeoutSec 5 } catch {}
|
||||||
|
if ($r1 -and $r1.StatusCode -eq 200) {
|
||||||
|
Write-Pass "FastAPI server is up (GET /docs -> 200)"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
$r2 = $null
|
||||||
|
try { $r2 = Invoke-WebRequest -Uri "$BaseUrl/" -UseBasicParsing -TimeoutSec 5 } catch {}
|
||||||
|
if ($r2 -and $r2.StatusCode -lt 500) {
|
||||||
|
Write-Pass "FastAPI server is up (GET / -> $($r2.StatusCode))"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
throw "Cannot reach $BaseUrl -- is lab_api.py / uvicorn running?"
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---- Step 2: /api/policy/health ---------------------------------------------
|
||||||
|
|
||||||
|
$modelLoaded = $false
|
||||||
|
|
||||||
|
Invoke-Step "Step 2 -- GET $BaseUrl/api/policy/health" {
|
||||||
|
$resp = Invoke-RestMethod -Uri "$BaseUrl/api/policy/health" -Method Get -TimeoutSec 10
|
||||||
|
if ($Verbose) { $resp | ConvertTo-Json -Depth 5 | Write-Host }
|
||||||
|
|
||||||
|
if ($resp.ready -eq $true) {
|
||||||
|
Write-Pass "Model is loaded and ready (ready=true)"
|
||||||
|
$script:modelLoaded = $true
|
||||||
|
} elseif ($resp.status -eq "degraded") {
|
||||||
|
Write-Fail "Health returned status=degraded / ready=false"
|
||||||
|
Write-Warn " -> Policy model is NOT loaded."
|
||||||
|
Write-Warn " -> Start the server with --load-policy, or call POST /api/ml/load_model first."
|
||||||
|
Write-Warn " -> Confirm models/ contains a valid .pt checkpoint."
|
||||||
|
$script:allPassed = $false
|
||||||
|
} else {
|
||||||
|
Write-Warn "Unexpected health response -- dumping:"
|
||||||
|
$resp | ConvertTo-Json -Depth 5 | Write-Host
|
||||||
|
$script:allPassed = $false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---- Step 3: /api/policy/decide (synthetic snapshot) ------------------------
|
||||||
|
|
||||||
|
Invoke-Step "Step 3 -- POST $BaseUrl/api/policy/decide (synthetic 4-player snapshot)" {
|
||||||
|
|
||||||
|
if (-not $script:modelLoaded) {
|
||||||
|
Write-Warn "Skipping /decide -- model not loaded (Step 2 failed)."
|
||||||
|
Write-Warn "Fix Step 2 first, then re-run."
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = [ordered]@{
|
||||||
|
game_id = "ps1-smoke-test"
|
||||||
|
turn = 3
|
||||||
|
phase = "MAIN1"
|
||||||
|
active_player = 0
|
||||||
|
playstyle = "midrange"
|
||||||
|
temperature = 1.0
|
||||||
|
greedy = $false
|
||||||
|
players = @(
|
||||||
|
[ordered]@{
|
||||||
|
seat = 0
|
||||||
|
name = "TestPlayer0"
|
||||||
|
life = 40
|
||||||
|
manaAvailable = 3
|
||||||
|
landCount = 3
|
||||||
|
creaturesOnField = 1
|
||||||
|
totalPowerOnBoard = 2
|
||||||
|
hand = @("Cultivate", "Solemn Simulacrum", "Sol Ring")
|
||||||
|
battlefield = @("Forest", "Forest", "Forest", "Birds of Paradise")
|
||||||
|
graveyard = @()
|
||||||
|
commandZone = @("Atraxa, Praetors Voice")
|
||||||
|
},
|
||||||
|
[ordered]@{
|
||||||
|
seat = 1
|
||||||
|
name = "TestPlayer1"
|
||||||
|
life = 38
|
||||||
|
manaAvailable = 2
|
||||||
|
landCount = 2
|
||||||
|
creaturesOnField = 0
|
||||||
|
totalPowerOnBoard = 0
|
||||||
|
handCount = 4
|
||||||
|
hand = @()
|
||||||
|
battlefield = @("Island", "Swamp")
|
||||||
|
graveyard = @()
|
||||||
|
commandZone = @("Ur-Dragon")
|
||||||
|
},
|
||||||
|
[ordered]@{
|
||||||
|
seat = 2
|
||||||
|
name = "TestPlayer2"
|
||||||
|
life = 35
|
||||||
|
manaAvailable = 4
|
||||||
|
landCount = 4
|
||||||
|
creaturesOnField = 2
|
||||||
|
totalPowerOnBoard = 5
|
||||||
|
handCount = 3
|
||||||
|
hand = @()
|
||||||
|
battlefield = @("Mountain", "Mountain", "Mountain", "Mountain", "Goblin Guide", "Monastery Swiftspear")
|
||||||
|
graveyard = @()
|
||||||
|
commandZone = @("Krenko, Mob Boss")
|
||||||
|
},
|
||||||
|
[ordered]@{
|
||||||
|
seat = 3
|
||||||
|
name = "TestPlayer3"
|
||||||
|
life = 40
|
||||||
|
manaAvailable = 0
|
||||||
|
landCount = 0
|
||||||
|
creaturesOnField = 0
|
||||||
|
totalPowerOnBoard = 0
|
||||||
|
handCount = 7
|
||||||
|
hand = @()
|
||||||
|
battlefield = @()
|
||||||
|
graveyard = @()
|
||||||
|
commandZone = @("Prossh, Skyraider of Kher")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} | ConvertTo-Json -Depth 10
|
||||||
|
|
||||||
|
$resp = Invoke-RestMethod `
|
||||||
|
-Uri "$BaseUrl/api/policy/decide" `
|
||||||
|
-Method Post `
|
||||||
|
-Body $body `
|
||||||
|
-ContentType "application/json" `
|
||||||
|
-TimeoutSec 30
|
||||||
|
|
||||||
|
if ($Verbose) {
|
||||||
|
Write-Host ""
|
||||||
|
Write-Host " Full /decide response:" -ForegroundColor White
|
||||||
|
$resp | ConvertTo-Json -Depth 6 | Write-Host
|
||||||
|
}
|
||||||
|
|
||||||
|
$ok = $true
|
||||||
|
if ([string]::IsNullOrEmpty($resp.action)) {
|
||||||
|
Write-Fail "response.action is empty"
|
||||||
|
$ok = $false
|
||||||
|
}
|
||||||
|
if ($null -eq $resp.confidence) {
|
||||||
|
Write-Fail "response.confidence is missing"
|
||||||
|
$ok = $false
|
||||||
|
}
|
||||||
|
if ($resp.PSObject.Properties["error"] -and -not [string]::IsNullOrEmpty($resp.error)) {
|
||||||
|
Write-Fail "Server returned error: $($resp.error)"
|
||||||
|
$ok = $false
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ok) {
|
||||||
|
$confPct = [math]::Round($resp.confidence * 100, 1)
|
||||||
|
Write-Pass "Decision received -> action='$($resp.action)' conf=$($confPct)% latency=$($resp.inference_ms)ms src=$($resp.vector_source)"
|
||||||
|
|
||||||
|
if ($resp.probabilities) {
|
||||||
|
$top3 = $resp.probabilities.PSObject.Properties |
|
||||||
|
Sort-Object { $_.Value } -Descending |
|
||||||
|
Select-Object -First 3
|
||||||
|
Write-Info "Top-3 action probabilities:"
|
||||||
|
foreach ($p in $top3) {
|
||||||
|
$pct = [math]::Round($p.Value * 100, 1)
|
||||||
|
Write-Info (" " + $p.Name.PadRight(22) + "$pct%")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$script:allPassed = $false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---- Step 4: log line guidance ----------------------------------------------
|
||||||
|
|
||||||
|
Invoke-Step "Step 4 -- Confirm [routes.policy.decide] log line in server console" {
|
||||||
|
Write-Info "After Step 3 you should see in the uvicorn window:"
|
||||||
|
Write-Info " [decide] turn=3 phase=main_1 -> cast_ramp (conf=0.810) | top5=[...] | src=encoder"
|
||||||
|
Write-Info ""
|
||||||
|
Write-Info "If that line is MISSING:"
|
||||||
|
Write-Info " 1. Confirm uvicorn started with --log-level info (the default)"
|
||||||
|
Write-Info " 2. Confirm lab_api.py calls register_policy_routes(app, policy_svc)"
|
||||||
|
Write-Info " 3. Confirm routes/policy.py is included via routes/__init__.py"
|
||||||
|
Write-Info " 4. Logger name is [routes.policy.decide] -- check no filter silences it"
|
||||||
|
Write-Pass "Guidance printed"
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---- Step 5: why Forge ignores the policy server ----------------------------
|
||||||
|
|
||||||
|
Invoke-Step "Step 5 -- Forge IPC status (informational)" {
|
||||||
|
Write-Warn "Forge does NOT call /api/policy/decide on its own."
|
||||||
|
Write-Warn "routes/game.py POST /api/game/start has a TODO: wire Forge engine launch."
|
||||||
|
Write-Warn ""
|
||||||
|
Write-Warn "Until wired, /decide is only hit by:"
|
||||||
|
Write-Warn " - This script"
|
||||||
|
Write-Warn " - overnight-run.py (batch sim path)"
|
||||||
|
Write-Warn " - Manual Invoke-RestMethod / curl calls"
|
||||||
|
Write-Warn ""
|
||||||
|
Write-Warn "Forge launches its built-in AI and plays lands because it has"
|
||||||
|
Write-Warn "no HTTP client pointed at localhost:8080."
|
||||||
|
Write-Warn ""
|
||||||
|
Write-Warn "Next step: wire GameSession inside POST /api/game/start,"
|
||||||
|
Write-Warn "or add a PolicyClient hook in Forge AiController.java."
|
||||||
|
Write-Pass "Status explained"
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---- Summary ----------------------------------------------------------------
|
||||||
|
|
||||||
|
Write-Host ""
|
||||||
|
if ($allPassed) {
|
||||||
|
Write-Host "==========================================" -ForegroundColor Green
|
||||||
|
Write-Host " ALL STEPS PASSED -- policy server live" -ForegroundColor Green
|
||||||
|
Write-Host "==========================================" -ForegroundColor Green
|
||||||
|
} else {
|
||||||
|
Write-Host "==========================================" -ForegroundColor Red
|
||||||
|
Write-Host " ONE OR MORE STEPS FAILED -- see above" -ForegroundColor Red
|
||||||
|
Write-Host "==========================================" -ForegroundColor Red
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
@@ -77,7 +77,7 @@ class CoachService:
|
|||||||
logger.warning("Deck report not found: %s", deck_id)
|
logger.warning("Deck report not found: %s", deck_id)
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
with open(report_path, "r") as f:
|
with open(report_path, "r", encoding="utf-8") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
return DeckReport(**data)
|
return DeckReport(**data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -95,7 +95,7 @@ class CoachService:
|
|||||||
"""Persist a coaching session to disk and SQLite."""
|
"""Persist a coaching session to disk and SQLite."""
|
||||||
COACH_SESSIONS_DIR.mkdir(parents=True, exist_ok=True)
|
COACH_SESSIONS_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
path = COACH_SESSIONS_DIR / f"{session.sessionId}.json"
|
path = COACH_SESSIONS_DIR / f"{session.sessionId}.json"
|
||||||
with open(path, "w") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
f.write(session.model_dump_json(indent=2))
|
f.write(session.model_dump_json(indent=2))
|
||||||
if _DB_AVAILABLE:
|
if _DB_AVAILABLE:
|
||||||
try:
|
try:
|
||||||
@@ -147,7 +147,7 @@ class CoachService:
|
|||||||
if not path.exists():
|
if not path.exists():
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
with open(path, "r") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
return CoachSession(**data)
|
return CoachSession(**data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -168,7 +168,7 @@ class CoachService:
|
|||||||
sessions = []
|
sessions = []
|
||||||
for f in sorted(COACH_SESSIONS_DIR.glob("*.json"), reverse=True):
|
for f in sorted(COACH_SESSIONS_DIR.glob("*.json"), reverse=True):
|
||||||
try:
|
try:
|
||||||
with open(f, "r") as fh:
|
with open(f, "r", encoding="utf-8") as fh:
|
||||||
data = json.load(fh)
|
data = json.load(fh)
|
||||||
if deck_id and data.get("deckId", "") != deck_id:
|
if deck_id and data.get("deckId", "") != deck_id:
|
||||||
continue
|
continue
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ LAB_ROOT = Path(os.environ.get("COMMANDER_LAB_ROOT", Path(__file__).parent.paren
|
|||||||
|
|
||||||
# ── Ollama (OpenAI-compatible API) ────────────────────────────
|
# ── Ollama (OpenAI-compatible API) ────────────────────────────
|
||||||
LLM_URL = os.environ.get("LLM_URL", "http://localhost:11434/v1")
|
LLM_URL = os.environ.get("LLM_URL", "http://localhost:11434/v1")
|
||||||
LLM_MODEL = os.environ.get("LLM_MODEL", "gpt-oss:20b")
|
LLM_MODEL = os.environ.get("LLM_MODEL", "claude-opus-4-20250514")
|
||||||
LLM_TIMEOUT = int(os.environ.get("LLM_TIMEOUT", "360")) # seconds
|
LLM_TIMEOUT = int(os.environ.get("LLM_TIMEOUT", "360")) # seconds
|
||||||
LLM_MAX_RETRIES = 3
|
LLM_MAX_RETRIES = 3
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
with open('ml/eval/forge_evaluator.py', 'rb') as f:
|
||||||
|
data = f.read()
|
||||||
|
|
||||||
|
# Fix 1: absolute JAR path in subprocess call
|
||||||
|
data = data.replace(
|
||||||
|
b'"--forge-jar", self.cfg.forge_jar,',
|
||||||
|
b'"--forge-jar", str(Path(self.cfg.forge_jar).resolve()),'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fix 2: resolve forge_work_dir to absolute path too
|
||||||
|
data = data.replace(
|
||||||
|
b'"--forge-dir", self.cfg.forge_work_dir,',
|
||||||
|
b'"--forge-dir", str(Path(self.cfg.forge_work_dir).resolve()),'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fix 3: change default forge_work_dir
|
||||||
|
data = data.replace(
|
||||||
|
b'forge_work_dir: str = "forge"',
|
||||||
|
b'forge_work_dir: str = "D:/ForgeCommander/forge-repo/forge-gui"'
|
||||||
|
)
|
||||||
|
|
||||||
|
with open('ml/eval/forge_evaluator.py', 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
|
import py_compile
|
||||||
|
py_compile.compile('ml/eval/forge_evaluator.py', doraise=True)
|
||||||
|
print('forge_evaluator.py - Syntax OK')
|
||||||
|
|
||||||
|
# Fix eval_policy.py too
|
||||||
|
with open('scripts/eval_policy.py', 'rb') as f:
|
||||||
|
data = f.read()
|
||||||
|
|
||||||
|
data = data.replace(
|
||||||
|
b'"--forge-dir", default="forge"',
|
||||||
|
b'"--forge-dir", default="D:/ForgeCommander/forge-repo/forge-gui"'
|
||||||
|
)
|
||||||
|
|
||||||
|
with open('scripts/eval_policy.py', 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
|
py_compile.compile('scripts/eval_policy.py', doraise=True)
|
||||||
|
print('eval_policy.py - Syntax OK')
|
||||||
-634
@@ -1,634 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>MTG Commander Battlefield</title>
|
|
||||||
<link rel="stylesheet" href="style.css?v=20260403_0300">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
SETUP SCREEN
|
|
||||||
==================================================== -->
|
|
||||||
<div id="setup-screen">
|
|
||||||
<!-- Populated by ui.js initSetupScreen() -->
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
TABLE SURFACE — 4-Quadrant Battlefield
|
|
||||||
==================================================== -->
|
|
||||||
<div class="table-surface" id="game-battlefield" style="display:none;">
|
|
||||||
|
|
||||||
<!-- TOP BAR: Phase tracker + controls -->
|
|
||||||
<div id="top-bar">
|
|
||||||
<span class="top-bar-logo">Commander</span>
|
|
||||||
|
|
||||||
<div class="turn-counter-badge" id="turn-counter">Turn <span>1</span></div>
|
|
||||||
|
|
||||||
<div class="active-player-indicator" id="active-player-indicator">
|
|
||||||
<span class="active-player-dot" id="active-dot" style="background:#c0392b;"></span>
|
|
||||||
Player 1's Turn
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Phase track -->
|
|
||||||
<div class="phase-track-wrapper">
|
|
||||||
<div class="phase-track" id="phase-track"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Phase/turn action buttons -->
|
|
||||||
<div class="top-bar-actions">
|
|
||||||
<button class="btn-phase" onclick="nextPhase()" title="Advance to next phase (N)">Next Phase</button>
|
|
||||||
<button class="btn-phase btn-next-turn" onclick="nextTurn()" title="End turn, advance to next player (T)">Next Turn</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Utility buttons -->
|
|
||||||
<div class="top-bar-utils">
|
|
||||||
<button class="btn-util" id="btn-undo" onclick="undo()" title="Undo (Ctrl+Z)" disabled>↩</button>
|
|
||||||
<button class="btn-util" id="btn-redo" onclick="redo()" title="Redo (Ctrl+Y)" disabled>↪</button>
|
|
||||||
<button class="btn-util" id="btn-triggers" onclick="toggleTriggerStack()" title="Trigger Stack">❇</button>
|
|
||||||
<button class="btn-util" onclick="openDiceModal()" title="Dice & Coins (G)">🎲</button>
|
|
||||||
<button class="btn-util" onclick="checkStateBasedActions()" title="Check State-Based Actions">SBA</button>
|
|
||||||
<button class="btn-util" onclick="toggleSettingsPanel()" title="Settings">⚙</button>
|
|
||||||
<button class="btn-util" id="btn-search-toggle" onclick="document.getElementById('card-search-sidebar').classList.toggle('collapsed')" title="Card Search (S)">🔍</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Storm counter -->
|
|
||||||
<span id="storm-badge" style="display:none;font-size:10px;color:var(--mtg-gold);background:rgba(200,168,67,0.15);border:1px solid rgba(200,168,67,0.3);border-radius:4px;padding:2px 7px;white-space:nowrap;">Storm: 0</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- DIVIDER LINES -->
|
|
||||||
<div class="divider-h"></div>
|
|
||||||
<div class="divider-v"></div>
|
|
||||||
|
|
||||||
<!-- CORNER ACCENTS -->
|
|
||||||
<div class="corner-accent tl"></div>
|
|
||||||
<div class="corner-accent tr"></div>
|
|
||||||
<div class="corner-accent bl"></div>
|
|
||||||
<div class="corner-accent br"></div>
|
|
||||||
|
|
||||||
<!-- CENTER EMBLEM -->
|
|
||||||
<div class="center-emblem">
|
|
||||||
<svg viewBox="0 0 100 100" fill="none">
|
|
||||||
<circle cx="50" cy="50" r="42" stroke="rgba(255,255,255,0.2)" stroke-width="1.5"/>
|
|
||||||
<circle cx="50" cy="50" r="28" stroke="rgba(255,255,255,0.1)" stroke-width="1"/>
|
|
||||||
<polygon points="50,15 58.5,37 82,37 63,51 69,74 50,60 31,74 37,51 18,37 41.5,37"
|
|
||||||
fill="rgba(255,255,255,0.1)" stroke="rgba(255,255,255,0.2)" stroke-width="0.8"/>
|
|
||||||
</svg>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ================================================
|
|
||||||
4-QUADRANT BATTLEFIELD
|
|
||||||
================================================ -->
|
|
||||||
<div class="battlefield">
|
|
||||||
|
|
||||||
<!-- PLAYER 1: Top-Left (Crimson) -->
|
|
||||||
<div class="player-area p1" id="player-area-1">
|
|
||||||
<span class="player-label" id="player-label-1">Player 1</span>
|
|
||||||
<div class="life-counter" id="life-counter-1">
|
|
||||||
<button onclick="adjustLife(0,-1)" aria-label="Decrease P1 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-0" onclick="showSetLifeDialog(0)">40</span>
|
|
||||||
<button onclick="adjustLife(0,1)" aria-label="Increase P1 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-0" style="display:none;" onclick="adjustPoison(0,1)" title="Poison counters (click +1, right-click -1)" oncontextmenu="event.preventDefault();adjustPoison(0,-1)">0 poison</div>
|
|
||||||
|
|
||||||
<!-- Zone Row: Library, Exile, Graveyard, Hand -->
|
|
||||||
<div class="zone-row" id="zone-row-1">
|
|
||||||
<div class="zone library" id="lib-zone-1" data-player="1" data-zone="library"
|
|
||||||
onclick="openZoneModal(0,'library')" data-tooltip="Library">
|
|
||||||
<span class="card-count" id="lib-count-1">0</span>
|
|
||||||
<span class="zone-icon">📚</span>
|
|
||||||
<span class="zone-name">Library</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone exile" id="exile-zone-1" data-player="1" data-zone="exile"
|
|
||||||
onclick="openZoneModal(0,'exile')" data-tooltip="Exile">
|
|
||||||
<span class="card-count" id="exile-count-1" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">⬡</span>
|
|
||||||
<span class="zone-name">Exile</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone graveyard" id="gv-zone-1" data-player="1" data-zone="graveyard"
|
|
||||||
onclick="openZoneModal(0,'graveyard')" data-tooltip="Graveyard">
|
|
||||||
<span class="card-count" id="gv-count-1" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">☠</span>
|
|
||||||
<span class="zone-name">Grave</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone hand" id="hand-zone-1" data-player="1" data-zone="hand"
|
|
||||||
onclick="openZoneModal(0,'hand')" data-tooltip="Hand">
|
|
||||||
<span class="zone-icon">🃏</span>
|
|
||||||
<span class="zone-name">Hand (<span id="hand-count-1">0</span>)</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Commander Row -->
|
|
||||||
<div class="commander-row" id="cmd-row-1">
|
|
||||||
<div class="zone commander" id="cmd-zone-1" data-player="1" data-zone="command"
|
|
||||||
onclick="openZoneModal(0,'command')" data-tooltip="Command Zone">
|
|
||||||
<span class="zone-icon">👑</span>
|
|
||||||
<span class="zone-name">Commander</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Battle Zone -->
|
|
||||||
<div class="battle-zone" id="battle-zone-1">
|
|
||||||
<div class="battle-zone-inner"></div>
|
|
||||||
<span class="battle-zone-label">Battlefield</span>
|
|
||||||
<!-- Battlefield cards injected here -->
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- PLAYER 2: Top-Right (Sapphire) -->
|
|
||||||
<div class="player-area p2" id="player-area-2">
|
|
||||||
<span class="player-label" id="player-label-2">Player 2</span>
|
|
||||||
<div class="life-counter" id="life-counter-2">
|
|
||||||
<button onclick="adjustLife(1,-1)" aria-label="Decrease P2 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-1" onclick="showSetLifeDialog(1)">40</span>
|
|
||||||
<button onclick="adjustLife(1,1)" aria-label="Increase P2 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-1" style="display:none;" onclick="adjustPoison(1,1)" oncontextmenu="event.preventDefault();adjustPoison(1,-1)">0 poison</div>
|
|
||||||
|
|
||||||
<div class="zone-row" id="zone-row-2">
|
|
||||||
<div class="zone library" id="lib-zone-2" data-player="2" data-zone="library"
|
|
||||||
onclick="openZoneModal(1,'library')" data-tooltip="Library">
|
|
||||||
<span class="card-count" id="lib-count-2">0</span>
|
|
||||||
<span class="zone-icon">📚</span>
|
|
||||||
<span class="zone-name">Library</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone exile" id="exile-zone-2" data-player="2" data-zone="exile"
|
|
||||||
onclick="openZoneModal(1,'exile')" data-tooltip="Exile">
|
|
||||||
<span class="card-count" id="exile-count-2" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">⬡</span>
|
|
||||||
<span class="zone-name">Exile</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone graveyard" id="gv-zone-2" data-player="2" data-zone="graveyard"
|
|
||||||
onclick="openZoneModal(1,'graveyard')" data-tooltip="Graveyard">
|
|
||||||
<span class="card-count" id="gv-count-2" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">☠</span>
|
|
||||||
<span class="zone-name">Grave</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone hand" id="hand-zone-2" data-player="2" data-zone="hand"
|
|
||||||
onclick="openZoneModal(1,'hand')" data-tooltip="Hand">
|
|
||||||
<span class="zone-icon">🃏</span>
|
|
||||||
<span class="zone-name">Hand (<span id="hand-count-2">0</span>)</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="commander-row" id="cmd-row-2">
|
|
||||||
<div class="zone commander" id="cmd-zone-2" data-player="2" data-zone="command"
|
|
||||||
onclick="openZoneModal(1,'command')" data-tooltip="Command Zone">
|
|
||||||
<span class="zone-icon">👑</span>
|
|
||||||
<span class="zone-name">Commander</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="battle-zone" id="battle-zone-2">
|
|
||||||
<div class="battle-zone-inner"></div>
|
|
||||||
<span class="battle-zone-label">Battlefield</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- PLAYER 3: Bottom-Left (Emerald) -->
|
|
||||||
<div class="player-area p3" id="player-area-3">
|
|
||||||
<!-- Battle zone on top for bottom players -->
|
|
||||||
<div class="battle-zone" id="battle-zone-3">
|
|
||||||
<div class="battle-zone-inner"></div>
|
|
||||||
<span class="battle-zone-label">Battlefield</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="commander-row" id="cmd-row-3">
|
|
||||||
<div class="zone commander" id="cmd-zone-3" data-player="3" data-zone="command"
|
|
||||||
onclick="openZoneModal(2,'command')" data-tooltip="Command Zone">
|
|
||||||
<span class="zone-icon">👑</span>
|
|
||||||
<span class="zone-name">Commander</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="zone-row" id="zone-row-3">
|
|
||||||
<div class="zone library" id="lib-zone-3" data-player="3" data-zone="library"
|
|
||||||
onclick="openZoneModal(2,'library')" data-tooltip="Library">
|
|
||||||
<span class="card-count" id="lib-count-3">0</span>
|
|
||||||
<span class="zone-icon">📚</span>
|
|
||||||
<span class="zone-name">Library</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone exile" id="exile-zone-3" data-player="3" data-zone="exile"
|
|
||||||
onclick="openZoneModal(2,'exile')" data-tooltip="Exile">
|
|
||||||
<span class="card-count" id="exile-count-3" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">⬡</span>
|
|
||||||
<span class="zone-name">Exile</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone graveyard" id="gv-zone-3" data-player="3" data-zone="graveyard"
|
|
||||||
onclick="openZoneModal(2,'graveyard')" data-tooltip="Graveyard">
|
|
||||||
<span class="card-count" id="gv-count-3" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">☠</span>
|
|
||||||
<span class="zone-name">Grave</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone hand" id="hand-zone-3" data-player="3" data-zone="hand"
|
|
||||||
onclick="openZoneModal(2,'hand')" data-tooltip="Hand">
|
|
||||||
<span class="zone-icon">🃏</span>
|
|
||||||
<span class="zone-name">Hand (<span id="hand-count-3">0</span>)</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<span class="player-label" id="player-label-3">Player 3</span>
|
|
||||||
<div class="life-counter" id="life-counter-3">
|
|
||||||
<button onclick="adjustLife(2,-1)" aria-label="Decrease P3 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-2" onclick="showSetLifeDialog(2)">40</span>
|
|
||||||
<button onclick="adjustLife(2,1)" aria-label="Increase P3 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-2" style="display:none;" onclick="adjustPoison(2,1)" oncontextmenu="event.preventDefault();adjustPoison(2,-1)">0 poison</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- PLAYER 4: Bottom-Right (Gold) -->
|
|
||||||
<div class="player-area p4" id="player-area-4">
|
|
||||||
<div class="battle-zone" id="battle-zone-4">
|
|
||||||
<div class="battle-zone-inner"></div>
|
|
||||||
<span class="battle-zone-label">Battlefield</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="commander-row" id="cmd-row-4">
|
|
||||||
<div class="zone commander" id="cmd-zone-4" data-player="4" data-zone="command"
|
|
||||||
onclick="openZoneModal(3,'command')" data-tooltip="Command Zone">
|
|
||||||
<span class="zone-icon">👑</span>
|
|
||||||
<span class="zone-name">Commander</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="zone-row" id="zone-row-4">
|
|
||||||
<div class="zone library" id="lib-zone-4" data-player="4" data-zone="library"
|
|
||||||
onclick="openZoneModal(3,'library')" data-tooltip="Library">
|
|
||||||
<span class="card-count" id="lib-count-4">0</span>
|
|
||||||
<span class="zone-icon">📚</span>
|
|
||||||
<span class="zone-name">Library</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone exile" id="exile-zone-4" data-player="4" data-zone="exile"
|
|
||||||
onclick="openZoneModal(3,'exile')" data-tooltip="Exile">
|
|
||||||
<span class="card-count" id="exile-count-4" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">⬡</span>
|
|
||||||
<span class="zone-name">Exile</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone graveyard" id="gv-zone-4" data-player="4" data-zone="graveyard"
|
|
||||||
onclick="openZoneModal(3,'graveyard')" data-tooltip="Graveyard">
|
|
||||||
<span class="card-count" id="gv-count-4" style="display:none;">0</span>
|
|
||||||
<span class="zone-icon">☠</span>
|
|
||||||
<span class="zone-name">Grave</span>
|
|
||||||
</div>
|
|
||||||
<div class="zone hand" id="hand-zone-4" data-player="4" data-zone="hand"
|
|
||||||
onclick="openZoneModal(3,'hand')" data-tooltip="Hand">
|
|
||||||
<span class="zone-icon">🃏</span>
|
|
||||||
<span class="zone-name">Hand (<span id="hand-count-4">0</span>)</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<span class="player-label" id="player-label-4">Player 4</span>
|
|
||||||
<div class="life-counter" id="life-counter-4">
|
|
||||||
<button onclick="adjustLife(3,-1)" aria-label="Decrease P4 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-3" onclick="showSetLifeDialog(3)">40</span>
|
|
||||||
<button onclick="adjustLife(3,1)" aria-label="Increase P4 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-3" style="display:none;" onclick="adjustPoison(3,1)" oncontextmenu="event.preventDefault();adjustPoison(3,-1)">0 poison</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div><!-- /.battlefield -->
|
|
||||||
|
|
||||||
<!-- COMBAT SVG OVERLAY (arrows) -->
|
|
||||||
<svg id="combat-overlay" xmlns="http://www.w3.org/2000/svg" style="position:absolute;inset:44px 0 0 0;pointer-events:none;z-index:30;">
|
|
||||||
<defs>
|
|
||||||
<marker id="arrowhead" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
|
|
||||||
<polygon points="0 0, 10 3.5, 0 7" fill="rgba(211,32,42,0.7)"/>
|
|
||||||
</marker>
|
|
||||||
</defs>
|
|
||||||
<g id="combat-arrows-svg"></g>
|
|
||||||
</svg>
|
|
||||||
|
|
||||||
<!-- MANA POOL DISPLAY (floating, bottom center) -->
|
|
||||||
<div id="mana-pool-display" style="display:none;">
|
|
||||||
<span class="mana-pool-label">MANA:</span>
|
|
||||||
<span class="mana-empty">Empty</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- FLOATING LOG -->
|
|
||||||
<div id="floating-log" class="floating-log"></div>
|
|
||||||
|
|
||||||
<!-- SHORTCUT HINT -->
|
|
||||||
<div class="shortcut-hint"></div>
|
|
||||||
|
|
||||||
<!-- CARD SEARCH SIDEBAR (right side, collapsible) -->
|
|
||||||
<div id="card-search-sidebar" class="collapsed">
|
|
||||||
<div class="sidebar-header">
|
|
||||||
<span class="sidebar-title">Card Search</span>
|
|
||||||
<button class="sidebar-toggle" onclick="document.getElementById('card-search-sidebar').classList.toggle('collapsed')" title="Close search">✕</button>
|
|
||||||
</div>
|
|
||||||
<div class="search-input-wrap">
|
|
||||||
<input class="search-input" id="card-search-input" type="text" placeholder="Search Scryfall..." autocomplete="off"/>
|
|
||||||
</div>
|
|
||||||
<div class="search-status" id="search-status">Type to search cards...</div>
|
|
||||||
<div class="card-results" id="card-results"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Sidebar edge tab -->
|
|
||||||
<div id="sidebar-edge-tab" class="sidebar-edge-tab" title="Toggle card search">‹</div>
|
|
||||||
|
|
||||||
</div><!-- /.table-surface / #game-battlefield -->
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
CONTEXT MENU
|
|
||||||
==================================================== -->
|
|
||||||
<div id="context-menu"></div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
FLOATING CARD PREVIEW
|
|
||||||
==================================================== -->
|
|
||||||
<div id="card-preview">
|
|
||||||
<img src="" alt="Card Preview"/>
|
|
||||||
<div class="card-preview-pt-overlay" id="card-preview-pt-overlay"></div>
|
|
||||||
<div class="card-preview-info">
|
|
||||||
<div class="card-preview-name"></div>
|
|
||||||
<div class="card-preview-type"></div>
|
|
||||||
<div class="card-preview-oracle"></div>
|
|
||||||
<div class="card-preview-pt"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
ZONE MODAL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="zone-modal">
|
|
||||||
<div class="zone-modal-card">
|
|
||||||
<div class="zone-modal-header">
|
|
||||||
<span class="zone-modal-title" id="zone-modal-title">Player Zones</span>
|
|
||||||
<button class="btn-close" onclick="closeZoneModal()">✕</button>
|
|
||||||
</div>
|
|
||||||
<div class="zone-modal-tabs" id="zone-modal-tabs">
|
|
||||||
<!-- Populated by renderZoneModal() -->
|
|
||||||
</div>
|
|
||||||
<div class="zone-modal-body" id="zone-modal-body">
|
|
||||||
<!-- Populated by renderZoneModal() -->
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
DICE MODAL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="dice-modal">
|
|
||||||
<div class="dice-modal-card">
|
|
||||||
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:1rem;">
|
|
||||||
<span style="font-family:var(--font-display);font-size:14px;font-weight:700;color:var(--color-text-muted);text-transform:uppercase;letter-spacing:0.08em;">Dice & Coins</span>
|
|
||||||
<button class="btn-close" onclick="closeDiceModal()">✕</button>
|
|
||||||
</div>
|
|
||||||
<div class="dice-result-display" id="dice-result-display">—</div>
|
|
||||||
<div class="dice-label" id="dice-label">Choose a die</div>
|
|
||||||
<div class="dice-buttons">
|
|
||||||
<button class="btn-dice" onclick="rollDice(4)">d4</button>
|
|
||||||
<button class="btn-dice" onclick="rollDice(6)">d6</button>
|
|
||||||
<button class="btn-dice" onclick="rollDice(8)">d8</button>
|
|
||||||
<button class="btn-dice" onclick="rollDice(10)">d10</button>
|
|
||||||
<button class="btn-dice" onclick="rollDice(12)">d12</button>
|
|
||||||
<button class="btn-dice" onclick="rollDice(20)">d20</button>
|
|
||||||
<button class="btn-dice" onclick="rollDice(100)">d100</button>
|
|
||||||
<button class="btn-dice coin" onclick="flipCoin()">Coin Flip</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
TOKEN MODAL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="token-modal">
|
|
||||||
<div class="token-modal-card">
|
|
||||||
<div class="modal-header">
|
|
||||||
<span class="modal-title">Create Token</span>
|
|
||||||
<button class="btn-close" onclick="closeTokenModal()">✕</button>
|
|
||||||
</div>
|
|
||||||
<div class="modal-body" id="token-modal-body">
|
|
||||||
<div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-bottom:12px;">
|
|
||||||
<div>
|
|
||||||
<label style="font-size:11px;font-weight:600;color:var(--color-text);display:block;margin-bottom:4px;">Token Name</label>
|
|
||||||
<input class="game-input" id="token-name" type="text" placeholder="e.g. 1/1 White Soldier"/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label style="font-size:11px;font-weight:600;color:var(--color-text);display:block;margin-bottom:4px;">Type Line</label>
|
|
||||||
<input class="game-input" id="token-type" type="text" placeholder="Creature — Soldier"/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label style="font-size:11px;font-weight:600;color:var(--color-text);display:block;margin-bottom:4px;">Power/Toughness</label>
|
|
||||||
<input class="game-input" id="token-pt" type="text" placeholder="1/1"/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label style="font-size:11px;font-weight:600;color:var(--color-text);display:block;margin-bottom:4px;">Count</label>
|
|
||||||
<input class="game-input" id="token-count" type="number" value="1" min="1" max="20"/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div style="margin-bottom:10px;">
|
|
||||||
<label style="font-size:11px;font-weight:600;color:var(--color-text);display:block;margin-bottom:4px;">Keywords (comma-separated)</label>
|
|
||||||
<input class="game-input" id="token-keywords" type="text" placeholder="Flying, Vigilance"/>
|
|
||||||
</div>
|
|
||||||
<div style="margin-bottom:12px;">
|
|
||||||
<label style="font-size:11px;font-weight:600;color:var(--color-text);display:block;margin-bottom:4px;">Player</label>
|
|
||||||
<select class="game-input" id="token-player-select" style="height:28px;">
|
|
||||||
<option value="0">Player 1</option>
|
|
||||||
<option value="1">Player 2</option>
|
|
||||||
<option value="2">Player 3</option>
|
|
||||||
<option value="3">Player 4</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
<div style="display:flex;gap:8px;">
|
|
||||||
<button class="btn btn-primary" onclick="createTokenFromModal()">Create Token(s)</button>
|
|
||||||
<button class="btn btn-cancel" onclick="closeTokenModal()">Cancel</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
DECK MODAL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="deck-modal">
|
|
||||||
<div class="deck-modal-card">
|
|
||||||
<div class="modal-header">
|
|
||||||
<span class="modal-title">Deck Manager</span>
|
|
||||||
<button class="btn-close" onclick="closeDeckModal()">✕</button>
|
|
||||||
</div>
|
|
||||||
<div class="modal-body" id="deck-modal-body">
|
|
||||||
<!-- Populated by renderDeckModalContent() -->
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
COMMANDER DAMAGE MATRIX MODAL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="cmd-dmg-matrix-modal">
|
|
||||||
<!-- Populated by renderCmdDmgMatrix() -->
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
TRIGGER STACK PANEL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="trigger-stack-panel">
|
|
||||||
<div class="trigger-stack-card">
|
|
||||||
<div class="trigger-stack-header">
|
|
||||||
<span class="trigger-stack-title">Trigger Stack</span>
|
|
||||||
<button class="btn-close" style="font-size:14px;width:20px;height:20px;" onclick="closeTriggerStack()">✕</button>
|
|
||||||
</div>
|
|
||||||
<div class="trigger-stack-body" id="trigger-stack-body">
|
|
||||||
<div class="trigger-stack-empty">No pending triggers.</div>
|
|
||||||
</div>
|
|
||||||
<div class="trigger-stack-actions">
|
|
||||||
<button class="btn-resolve" onclick="resolveNextTrigger()">Resolve Next</button>
|
|
||||||
<button class="btn-resolve-all" onclick="resolveAllTriggers()">Resolve All</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
SETTINGS PANEL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="settings-panel">
|
|
||||||
<div class="modal-header" style="flex-shrink:0;">
|
|
||||||
<span class="modal-title">Settings</span>
|
|
||||||
<button class="btn-close" onclick="toggleSettingsPanel()">✕</button>
|
|
||||||
</div>
|
|
||||||
<div class="settings-body" id="settings-body" style="flex:1;overflow-y:auto;padding:12px;">
|
|
||||||
<!-- Populated by renderSettingsPanel() -->
|
|
||||||
</div>
|
|
||||||
<div style="padding:10px 14px;border-top:1px solid var(--color-border);display:flex;gap:8px;flex-wrap:wrap;flex-shrink:0;">
|
|
||||||
<button class="btn btn-primary" onclick="openDiceModal()">🎲 Dice</button>
|
|
||||||
<button class="btn btn-gold" onclick="openCmdDmgMatrix()">CMD Damage Matrix</button>
|
|
||||||
<button class="btn btn-cancel" onclick="checkStateBasedActions()">Check SBA</button>
|
|
||||||
<button class="btn btn-cancel" onclick="clearAllManaPools()">Clear All Mana</button>
|
|
||||||
<button class="btn btn-cancel" onclick="autoArrangeBattlefield()">Auto-Arrange</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
PRIORITY BANNER
|
|
||||||
==================================================== -->
|
|
||||||
<div id="priority-banner">
|
|
||||||
<span class="priority-text" id="priority-text">Waiting for priority...</span>
|
|
||||||
<button class="btn-priority-pass" onclick="passPriority()">Pass Priority</button>
|
|
||||||
<button class="btn-close" style="font-size:13px;" onclick="closePriorityWindow()">✕</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
LEGAL ACTIONS BAR
|
|
||||||
==================================================== -->
|
|
||||||
<div id="legal-actions-bar">
|
|
||||||
<!-- Populated by renderLegalActionsBar() -->
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
UNDO HISTORY PANEL
|
|
||||||
==================================================== -->
|
|
||||||
<div id="undo-history-panel">
|
|
||||||
<!-- Populated by renderUndoHistory() -->
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
MANA COLOR PICKER (dynamically appended) -->
|
|
||||||
<!-- Zone card context menu (dynamically appended) -->
|
|
||||||
<!-- SBA alert panel (dynamically appended) -->
|
|
||||||
<!-- Set-life dialog (dynamically appended) -->
|
|
||||||
|
|
||||||
<!-- ====================================================
|
|
||||||
SCRIPTS
|
|
||||||
==================================================== -->
|
|
||||||
<script src="precons.js?v=20260403_0300"></script>
|
|
||||||
<script src="engine.js?v=20260403_0300"></script>
|
|
||||||
<script src="ui.js?v=20260403_0300"></script>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
// ============================================================
|
|
||||||
// STARTUP & BRIDGE FUNCTIONS
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
// Token creation from modal
|
|
||||||
function createTokenFromModal() {
|
|
||||||
if (!gameState) return;
|
|
||||||
const name = document.getElementById('token-name').value.trim() || '1/1 Token';
|
|
||||||
const typeLine = document.getElementById('token-type').value.trim() || 'Creature';
|
|
||||||
const pt = document.getElementById('token-pt').value.trim() || '1/1';
|
|
||||||
const count = parseInt(document.getElementById('token-count').value) || 1;
|
|
||||||
const keywords = document.getElementById('token-keywords').value.trim();
|
|
||||||
const playerIdx = parseInt(document.getElementById('token-player-select').value) || 0;
|
|
||||||
|
|
||||||
const parts = pt.split('/');
|
|
||||||
const power = parts[0] ? parts[0].trim() : '1';
|
|
||||||
const toughness = parts[1] ? parts[1].trim() : '1';
|
|
||||||
|
|
||||||
for (let i = 0; i < count; i++) {
|
|
||||||
addCardToBattlefield(name, '', typeLine, playerIdx, undefined, undefined,
|
|
||||||
keywords ? keywords.replace(/,/g, ' ') : '',
|
|
||||||
{ power, toughness, pt, isToken: true });
|
|
||||||
}
|
|
||||||
addLogEntry('Created ' + count + 'x <strong>' + name + '</strong> token(s) for Player ' + (playerIdx + 1));
|
|
||||||
closeTokenModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pass priority
|
|
||||||
function passPriority() {
|
|
||||||
addLogEntry('Priority passed.');
|
|
||||||
closePriorityWindow();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open precon browser (stub - opens deck modal for current player)
|
|
||||||
function openPreconBrowser() {
|
|
||||||
if (gameState) openDeckModal(gameState.currentPlayerIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open collection modal (stub)
|
|
||||||
function openCollectionModal() { showToast('Collection tracker available in full version.'); }
|
|
||||||
|
|
||||||
// Scry dialog stub
|
|
||||||
function showScryDialog(playerId) {
|
|
||||||
if (!gameState) return;
|
|
||||||
const player = gameState.players[playerId];
|
|
||||||
if (!player || player.zones.library.length === 0) {
|
|
||||||
addLogEntry('!! ' + (player ? player.name : 'Player') + ' has no cards to scry.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const count = Math.min(parseInt(prompt('Scry how many? (1-7)', '1') || '1'), 7, player.zones.library.length);
|
|
||||||
if (isNaN(count) || count < 1) return;
|
|
||||||
const topCards = player.zones.library.slice(0, count);
|
|
||||||
const names = topCards.map(c => c.name || '?').join(', ');
|
|
||||||
addLogEntry('<strong>' + player.name + '</strong> scrys ' + count + ': [' + names + ']');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mill dialog stub
|
|
||||||
function showMillDialog(playerId) {
|
|
||||||
if (!gameState) return;
|
|
||||||
const count = parseInt(prompt('Mill how many cards?', '1') || '1');
|
|
||||||
if (isNaN(count) || count < 1) return;
|
|
||||||
millCards(playerId, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Become monarch
|
|
||||||
function becomeMonarch(playerId) {
|
|
||||||
if (!gameState) return;
|
|
||||||
gameState.monarchPlayerIndex = playerId;
|
|
||||||
const player = gameState.players[playerId];
|
|
||||||
addLogEntry('👑 <strong>' + (player ? player.name : 'Player') + '</strong> becomes the Monarch!');
|
|
||||||
updateMonarchBadges();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empower the life counter right-click on the quadrant labels
|
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
|
||||||
// Wire right-click on life values to set-life dialog
|
|
||||||
for (var i = 0; i < 4; i++) {
|
|
||||||
(function(idx) {
|
|
||||||
var lifeEl = document.getElementById('life-' + idx);
|
|
||||||
if (lifeEl) {
|
|
||||||
lifeEl.addEventListener('contextmenu', function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
showSetLifeDialog(idx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Life counter right-click on quadrant buttons too
|
|
||||||
// (already handled via inline onclick in HTML)
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
+50
-16
@@ -50,6 +50,9 @@ _DEFAULT_ORIGINS = "http://localhost:5173,http://localhost:3000,http://localhost
|
|||||||
_allowed = os.environ.get("ALLOWED_ORIGINS", _DEFAULT_ORIGINS)
|
_allowed = os.environ.get("ALLOWED_ORIGINS", _DEFAULT_ORIGINS)
|
||||||
allowed_origins = [o.strip() for o in _allowed.split(",") if o.strip()]
|
allowed_origins = [o.strip() for o in _allowed.split(",") if o.strip()]
|
||||||
|
|
||||||
|
# Module-level handle so lifespan can call .load() after routes are registered.
|
||||||
|
_POLICY_SVC = None
|
||||||
|
|
||||||
|
|
||||||
# ── Lifespan (replaces deprecated @app.on_event) ────────────────────────────────────────────────────────
|
# ── Lifespan (replaces deprecated @app.on_event) ────────────────────────────────────────────────────────
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
@@ -77,7 +80,6 @@ async def _lifespan(application: FastAPI):
|
|||||||
# RAG Phase 3: background staleness monitor (rebuilds if >14 days old)
|
# RAG Phase 3: background staleness monitor (rebuilds if >14 days old)
|
||||||
async def _rag_staleness_monitor():
|
async def _rag_staleness_monitor():
|
||||||
"""Periodically check if the RAG index needs rebuilding."""
|
"""Periodically check if the RAG index needs rebuilding."""
|
||||||
import time as _time
|
|
||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(6 * 3600) # check every 6 hours
|
await asyncio.sleep(6 * 3600) # check every 6 hours
|
||||||
try:
|
try:
|
||||||
@@ -92,20 +94,17 @@ async def _lifespan(application: FastAPI):
|
|||||||
log.warning("RAG staleness monitor failed: %s", exc)
|
log.warning("RAG staleness monitor failed: %s", exc)
|
||||||
asyncio.ensure_future(_rag_staleness_monitor())
|
asyncio.ensure_future(_rag_staleness_monitor())
|
||||||
|
|
||||||
# Policy routes: register if ML policy model is available
|
# Policy model load — routes were already registered at module scope (before
|
||||||
|
# any app.mount() calls) so /api/policy/* is reachable regardless of the
|
||||||
|
# StaticFiles root mount. Only the heavyweight model load happens here.
|
||||||
try:
|
try:
|
||||||
from ml.serving.policy_server import PolicyInferenceService
|
if _POLICY_SVC is not None:
|
||||||
from routes.policy import register_policy_routes
|
if _POLICY_SVC.load():
|
||||||
_policy_svc = PolicyInferenceService()
|
log.info("Policy model loaded successfully")
|
||||||
if _policy_svc.load():
|
else:
|
||||||
register_policy_routes(application, _policy_svc)
|
log.warning("Policy model not loaded — /api/policy/health will report degraded")
|
||||||
log.info("Policy routes registered (/api/policy/*)")
|
|
||||||
else:
|
|
||||||
log.warning("Policy model not loaded — /api/policy/* routes inactive")
|
|
||||||
except ImportError:
|
|
||||||
log.info("Policy service not available (ml.serving not installed)")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning("Policy route registration failed (non-fatal): %s", e)
|
log.warning("Policy model load failed (non-fatal): %s", e)
|
||||||
|
|
||||||
yield # ← server is running
|
yield # ← server is running
|
||||||
|
|
||||||
@@ -138,6 +137,23 @@ app.include_router(ws_game_router)
|
|||||||
app.include_router(ml_router)
|
app.include_router(ml_router)
|
||||||
app.include_router(game_router) # live game session routes
|
app.include_router(game_router) # live game session routes
|
||||||
|
|
||||||
|
# ── Policy routes — MUST be registered here, before any app.mount() calls ────────────────────
|
||||||
|
# StaticFiles mounted at "/" acts as a sub-application and returns its own 404 for
|
||||||
|
# paths it doesn't serve, rather than falling through to later-registered routes.
|
||||||
|
# Any include_router() called after app.mount("/", ...) will be unreachable.
|
||||||
|
# Registering here (module scope) guarantees the /api/policy/* endpoints exist in
|
||||||
|
# the route list before the root static mount is added below.
|
||||||
|
try:
|
||||||
|
from ml.serving.policy_server import PolicyInferenceService
|
||||||
|
from routes.policy import register_policy_routes
|
||||||
|
_POLICY_SVC = PolicyInferenceService()
|
||||||
|
register_policy_routes(app, _POLICY_SVC)
|
||||||
|
log.info("Policy routes registered (/api/policy/*)")
|
||||||
|
except ImportError:
|
||||||
|
log.info("Policy service not available (ml.serving not installed)")
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Policy route registration failed (non-fatal): %s", e)
|
||||||
|
|
||||||
|
|
||||||
# ── API endpoints ─────────────────────────────────────────────────────────────────────────────
|
# ── API endpoints ─────────────────────────────────────────────────────────────────────────────
|
||||||
# NOTE: all @app.get routes MUST be registered before app.mount() calls.
|
# NOTE: all @app.get routes MUST be registered before app.mount() calls.
|
||||||
@@ -149,11 +165,26 @@ async def health_check():
|
|||||||
"""Health check endpoint for load balancers and Unity client polling."""
|
"""Health check endpoint for load balancers and Unity client polling."""
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|
||||||
# ── Static UI (MUST come after all @app.get routes) ──────────────────────────────────────────
|
# ── Static UI (MUST come after all @app.get / include_router calls) ──────────────────────────
|
||||||
from pathlib import Path as _Path
|
from pathlib import Path as _Path
|
||||||
_legacy_ui_dir = _Path(__file__).parent / "ui"
|
_legacy_ui_dir = _Path(__file__).parent / "ui"
|
||||||
|
_lan_client_dir = _Path(__file__).parent / "lan-client"
|
||||||
|
_precon_decks_dir = _Path(__file__).parent / "precon-decks"
|
||||||
|
_user_decks_dir = _Path(__file__).parent / "user-decks"
|
||||||
_spa_dir = _Path(__file__).parent / "frontend" / "commander-ai-lab-ui" / "dist"
|
_spa_dir = _Path(__file__).parent / "frontend" / "commander-ai-lab-ui" / "dist"
|
||||||
|
|
||||||
|
# Mount LAN client at /lan (served before the root catch-all)
|
||||||
|
if _lan_client_dir.exists():
|
||||||
|
app.mount("/lan", StaticFiles(directory=str(_lan_client_dir), html=True), name="lan-client")
|
||||||
|
|
||||||
|
# Mount precon-decks/ so .dck files are fetchable at /precon-decks/{file}.dck
|
||||||
|
if _precon_decks_dir.exists():
|
||||||
|
app.mount("/precon-decks", StaticFiles(directory=str(_precon_decks_dir)), name="precon-decks")
|
||||||
|
|
||||||
|
# Mount user-decks/ so exported .dck files are fetchable at /user-decks/{file}.dck
|
||||||
|
if _user_decks_dir.exists():
|
||||||
|
app.mount("/user-decks", StaticFiles(directory=str(_user_decks_dir)), name="user-decks")
|
||||||
|
|
||||||
if _legacy_ui_dir.exists():
|
if _legacy_ui_dir.exists():
|
||||||
app.mount("/", StaticFiles(directory=str(_legacy_ui_dir), html=True), name="ui")
|
app.mount("/", StaticFiles(directory=str(_legacy_ui_dir), html=True), name="ui")
|
||||||
elif _spa_dir.exists():
|
elif _spa_dir.exists():
|
||||||
@@ -225,7 +256,9 @@ def _resolve_forge_dir() -> str:
|
|||||||
project_root = Path(__file__).parent
|
project_root = Path(__file__).parent
|
||||||
for candidate in [
|
for candidate in [
|
||||||
project_root / "forge",
|
project_root / "forge",
|
||||||
project_root.parent / "forge-repo",
|
project_root.parent / "Forge-Repo",
|
||||||
|
project_root.parent / "Forge-Repo" / "forge-gui",
|
||||||
|
project_root.parent / "forge-repo", # legacy fallback
|
||||||
project_root.parent / "forge-repo" / "forge-gui",
|
project_root.parent / "forge-repo" / "forge-gui",
|
||||||
project_root.parent / "forge",
|
project_root.parent / "forge",
|
||||||
project_root / "forge-gui-desktop",
|
project_root / "forge-gui-desktop",
|
||||||
@@ -251,7 +284,8 @@ def _resolve_forge_jar() -> str:
|
|||||||
project_root = Path(__file__).parent
|
project_root = Path(__file__).parent
|
||||||
candidates = [
|
candidates = [
|
||||||
project_root / "forge-gui-desktop" / "target",
|
project_root / "forge-gui-desktop" / "target",
|
||||||
project_root.parent / "forge-repo" / "forge-gui-desktop" / "target",
|
project_root.parent / "Forge-Repo" / "forge-gui-desktop" / "target",
|
||||||
|
project_root.parent / "forge-repo" / "forge-gui-desktop" / "target", # legacy fallback
|
||||||
project_root.parent / "forge" / "forge-gui-desktop" / "target",
|
project_root.parent / "forge" / "forge-gui-desktop" / "target",
|
||||||
]
|
]
|
||||||
forge_dir_env = os.environ.get("FORGE_DIR", "") or os.environ.get("FORGE_WORKING_DIR", "")
|
forge_dir_env = os.environ.get("FORGE_DIR", "") or os.environ.get("FORGE_WORKING_DIR", "")
|
||||||
|
|||||||
+52
-20
@@ -10,9 +10,10 @@ const AI_BRIDGE_VERSION = '1.0.0';
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
const AI_BRIDGE_CONFIG = {
|
const AI_BRIDGE_CONFIG = {
|
||||||
// Backend URL — defaults to localhost:8080, configurable via settings
|
// Backend URL — empty string = same origin (recommended when served from backend)
|
||||||
backendUrl: 'http://localhost:8080',
|
// Falls back to localhost:8080 if served from a different origin
|
||||||
wsUrl: 'ws://localhost:8080',
|
backendUrl: '',
|
||||||
|
wsUrl: '',
|
||||||
|
|
||||||
// Timeouts (ms)
|
// Timeouts (ms)
|
||||||
healthCheckTimeout: 3000,
|
healthCheckTimeout: 3000,
|
||||||
@@ -39,6 +40,16 @@ const AI_BRIDGE_CONFIG = {
|
|||||||
class BackendConnector {
|
class BackendConnector {
|
||||||
constructor(config = {}) {
|
constructor(config = {}) {
|
||||||
this.config = { ...AI_BRIDGE_CONFIG, ...config };
|
this.config = { ...AI_BRIDGE_CONFIG, ...config };
|
||||||
|
|
||||||
|
// Derive URLs from current page origin when empty (same-origin mode)
|
||||||
|
if (!this.config.backendUrl) {
|
||||||
|
this.config.backendUrl = window.location.origin; // e.g. http://localhost:8080
|
||||||
|
}
|
||||||
|
if (!this.config.wsUrl) {
|
||||||
|
const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||||
|
this.config.wsUrl = `${proto}//${window.location.host}`;
|
||||||
|
}
|
||||||
|
|
||||||
this._connected = false;
|
this._connected = false;
|
||||||
this._modelLoaded = false;
|
this._modelLoaded = false;
|
||||||
this._ws = null;
|
this._ws = null;
|
||||||
@@ -355,7 +366,11 @@ class GameStateBridge {
|
|||||||
seat_type: p.isAI
|
seat_type: p.isAI
|
||||||
? this._mapAiProfile(gameState.aiDifficulty)
|
? this._mapAiProfile(gameState.aiDifficulty)
|
||||||
: 'Human',
|
: 'Human',
|
||||||
deck_name: p.commanderCardName || '',
|
deck_name: p.deckFileName || (
|
||||||
|
p.commanderCardName
|
||||||
|
? p.commanderCardName.replace(/[^a-zA-Z0-9]/g, '_') + '.dck'
|
||||||
|
: ''
|
||||||
|
),
|
||||||
player_name: p.name,
|
player_name: p.name,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -383,11 +398,13 @@ class GameStateBridge {
|
|||||||
*/
|
*/
|
||||||
_mapAiProfile(difficulty) {
|
_mapAiProfile(difficulty) {
|
||||||
const map = {
|
const map = {
|
||||||
easy: 'AI',
|
easy: 'AI',
|
||||||
normal: 'AI Aggro',
|
normal: 'AI Midrange',
|
||||||
hard: 'AI Control',
|
hard: 'AI Control',
|
||||||
|
combo: 'AI Combo',
|
||||||
|
aggro: 'AI Aggro',
|
||||||
};
|
};
|
||||||
return map[difficulty] || 'AI';
|
return map[difficulty] || 'AI Midrange';
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Game State Serialization ──────────────────────────────
|
// ── Game State Serialization ──────────────────────────────
|
||||||
@@ -440,7 +457,10 @@ class GameStateBridge {
|
|||||||
// Zone contents — card names
|
// Zone contents — card names
|
||||||
hand: p.zones.hand.map(c => c.name || 'Unknown'),
|
hand: p.zones.hand.map(c => c.name || 'Unknown'),
|
||||||
hand_count: p.zones.hand.length,
|
hand_count: p.zones.hand.length,
|
||||||
battlefield: bfCards.map(c => ({
|
// Flat string list for the state encoder
|
||||||
|
battlefield: bfCards.map(c => c.name || 'Unknown'),
|
||||||
|
// Structured data for backend logic that needs detail
|
||||||
|
battlefield_detail: bfCards.map(c => ({
|
||||||
name: c.name || 'Unknown',
|
name: c.name || 'Unknown',
|
||||||
type: c.typeLine || '',
|
type: c.typeLine || '',
|
||||||
power: c.power || '0',
|
power: c.power || '0',
|
||||||
@@ -477,7 +497,7 @@ class GameStateBridge {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
game_id: this._sessionGameId || '',
|
game_id: this._sessionGameId || '',
|
||||||
turn: gameState.turnCounter || 1,
|
turn: gameState.turnNumber || gameState.turnCounter || 1,
|
||||||
phase: phaseMap[phaseId] || 'main_1',
|
phase: phaseMap[phaseId] || 'main_1',
|
||||||
active_player: playerIdx,
|
active_player: playerIdx,
|
||||||
players: players,
|
players: players,
|
||||||
@@ -494,9 +514,9 @@ class GameStateBridge {
|
|||||||
* Get all battlefield cards for a player.
|
* Get all battlefield cards for a player.
|
||||||
*/
|
*/
|
||||||
_getBattlefieldCards(player, gameState) {
|
_getBattlefieldCards(player, gameState) {
|
||||||
if (!gameState || !gameState.battlefield) return [];
|
if (!gameState || !gameState.battlefieldCards) return [];
|
||||||
return (gameState.battlefield || []).filter(
|
return gameState.battlefieldCards.filter(
|
||||||
c => c.controller === player.id || c.owner === player.id
|
c => c.ownerIndex === player.id
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -696,7 +716,7 @@ class MacroActionMapper {
|
|||||||
if (castable.length === 0) return false;
|
if (castable.length === 0) return false;
|
||||||
castable.sort((a, b) => (MacroActionMapper._cmc(b) - MacroActionMapper._cmc(a)));
|
castable.sort((a, b) => (MacroActionMapper._cmc(b) - MacroActionMapper._cmc(a)));
|
||||||
for (const card of castable) {
|
for (const card of castable) {
|
||||||
if (MacroActionMapper._canAfford(playerIdx, card)) {
|
if (MacroActionMapper._canAfford(playerIdx, card, gameState)) {
|
||||||
const idx = hand.indexOf(card);
|
const idx = hand.indexOf(card);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
||||||
@@ -717,7 +737,7 @@ class MacroActionMapper {
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (const card of creatures) {
|
for (const card of creatures) {
|
||||||
if (MacroActionMapper._canAfford(playerIdx, card)) {
|
if (MacroActionMapper._canAfford(playerIdx, card, gameState)) {
|
||||||
const idx = hand.indexOf(card);
|
const idx = hand.indexOf(card);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
||||||
@@ -741,7 +761,7 @@ class MacroActionMapper {
|
|||||||
if (removal.length === 0) return false;
|
if (removal.length === 0) return false;
|
||||||
|
|
||||||
for (const card of removal) {
|
for (const card of removal) {
|
||||||
if (MacroActionMapper._canAfford(playerIdx, card)) {
|
if (MacroActionMapper._canAfford(playerIdx, card, gameState)) {
|
||||||
const idx = hand.indexOf(card);
|
const idx = hand.indexOf(card);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
||||||
@@ -770,7 +790,7 @@ class MacroActionMapper {
|
|||||||
);
|
);
|
||||||
|
|
||||||
for (const card of drawSpells) {
|
for (const card of drawSpells) {
|
||||||
if (MacroActionMapper._canAfford(playerIdx, card)) {
|
if (MacroActionMapper._canAfford(playerIdx, card, gameState)) {
|
||||||
const idx = hand.indexOf(card);
|
const idx = hand.indexOf(card);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
||||||
@@ -815,7 +835,7 @@ class MacroActionMapper {
|
|||||||
);
|
);
|
||||||
|
|
||||||
for (const card of rampSpells) {
|
for (const card of rampSpells) {
|
||||||
if (MacroActionMapper._canAfford(playerIdx, card)) {
|
if (MacroActionMapper._canAfford(playerIdx, card, gameState)) {
|
||||||
const idx = hand.indexOf(card);
|
const idx = hand.indexOf(card);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
if (!card.imageUrl) card.imageUrl = generateAiCardSVG(card);
|
||||||
@@ -873,12 +893,24 @@ class MacroActionMapper {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static _canAfford(playerIdx, card) {
|
static _canAfford(playerIdx, card, gameState) {
|
||||||
if (!gameState) return false;
|
if (!gameState) return false;
|
||||||
|
const cmc = MacroActionMapper._cmc(card);
|
||||||
|
if (cmc === 0) return true; // Free spells always castable
|
||||||
|
|
||||||
|
// Prefer actual mana pool if populated
|
||||||
|
const player = gameState.players[playerIdx];
|
||||||
|
const pool = player?.manaPool;
|
||||||
|
if (pool) {
|
||||||
|
const total = Object.values(pool).reduce((s, v) => s + (v || 0), 0);
|
||||||
|
if (total > 0) return total >= cmc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: count untapped lands
|
||||||
const available = typeof aiCountUntappedLands === 'function'
|
const available = typeof aiCountUntappedLands === 'function'
|
||||||
? aiCountUntappedLands(playerIdx)
|
? aiCountUntappedLands(playerIdx)
|
||||||
: 0;
|
: 0;
|
||||||
return available >= MacroActionMapper._cmc(card);
|
return available >= cmc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static _creatureScore(card) {
|
static _creatureScore(card) {
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,34 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Commander AI Lab — Collection</title>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
||||||
|
<link rel="stylesheet" href="global.css" />
|
||||||
|
<link rel="stylesheet" href="collection.css" />
|
||||||
|
<style>
|
||||||
|
/* No nav bar when embedded in LAN client iframe */
|
||||||
|
:root { --nav-height: 0px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- nav.js removed — collection is embedded via iframe in the LAN client -->
|
||||||
|
<div id="collection-container"></div>
|
||||||
|
<script>
|
||||||
|
window.LAB_API_BASE = window.LAB_API_BASE || '';
|
||||||
|
</script>
|
||||||
|
<script src="collection.js"></script>
|
||||||
|
<script>
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
Collection.init();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<!-- Card Preview Popup (hover tooltip) -->
|
||||||
|
<div id="coll-card-preview" style="display:none; position:fixed; pointer-events:none; z-index:9999;">
|
||||||
|
<img id="coll-card-preview-img" src="" alt="Card preview" style="width:220px; border-radius:8px; box-shadow:0 8px 24px rgba(0,0,0,0.7);" />
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,495 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Commander AI Lab — Deck Builder</title>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
||||||
|
<link rel="stylesheet" href="global.css" />
|
||||||
|
<link rel="stylesheet" href="deckbuilder.css" />
|
||||||
|
<style>
|
||||||
|
/* No nav bar when embedded in LAN client iframe */
|
||||||
|
:root { --nav-height: 0px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- nav.js removed — deck builder is embedded via iframe in the LAN client -->
|
||||||
|
|
||||||
|
<div id="deck-builder-page">
|
||||||
|
|
||||||
|
<!-- TOP BAR -->
|
||||||
|
<header class="deck-topbar">
|
||||||
|
<div class="deck-topbar-commander">
|
||||||
|
<div class="deck-commander-portrait" id="deck-commander-portrait">
|
||||||
|
<span class="deck-commander-portrait-placeholder">⚔</span>
|
||||||
|
<img class="deck-commander-portrait-img" id="deck-commander-img" src="" alt="" style="display:none" />
|
||||||
|
</div>
|
||||||
|
<div class="deck-name-block">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="deck-name-input"
|
||||||
|
id="deck-name-input"
|
||||||
|
placeholder="Untitled Deck"
|
||||||
|
value=""
|
||||||
|
/>
|
||||||
|
<div class="deck-commander-label" id="deck-commander-label">No commander selected</div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-color-chips" id="deck-color-chips"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-topbar-center">
|
||||||
|
<div class="deck-count-display">
|
||||||
|
<span class="deck-count-num" id="deck-total-count">0</span>
|
||||||
|
<span class="deck-count-sep">/</span>
|
||||||
|
<span class="deck-count-target">100</span>
|
||||||
|
<span class="deck-count-label">cards</span>
|
||||||
|
</div>
|
||||||
|
<button class="deck-btn deck-btn-ghost" id="deck-new-btn">+ New Deck</button>
|
||||||
|
<div class="deck-selector-wrap">
|
||||||
|
<select class="deck-selector" id="deck-selector">
|
||||||
|
<option value="">— Select Deck —</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<button class="deck-btn deck-btn-danger" id="deck-delete-btn" title="Delete this deck" style="display:none;">✕ Delete</button>
|
||||||
|
<button class="deck-btn deck-btn-danger-outline" id="deck-clear-all-btn" title="Delete all decks">Clear All</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-topbar-actions">
|
||||||
|
<button class="deck-btn deck-btn-import" id="deck-import-btn" title="Import a decklist">
|
||||||
|
<span>⬆</span> Import
|
||||||
|
</button>
|
||||||
|
<button class="deck-btn deck-btn-ghost" id="deck-export-btn" title="Export as .dck file">
|
||||||
|
<span>⬇</span> Export .dck
|
||||||
|
</button>
|
||||||
|
<button class="deck-btn deck-btn-ghost" id="deck-export-sim-btn" title="Export deck as .dck to Forge sim folder">
|
||||||
|
<span>📤</span> Export to Sim
|
||||||
|
</button>
|
||||||
|
<button class="deck-btn deck-btn-ghost" id="deck-export-game-btn" title="Send deck to LAN game setup" style="border-color:rgba(91,141,217,0.4);color:rgba(91,141,217,0.9);">
|
||||||
|
<span>🎮</span> Use in Game
|
||||||
|
</button>
|
||||||
|
<button class="deck-btn deck-btn-sim" id="deck-sim-btn" title="Run quick simulation">
|
||||||
|
<span class="deck-btn-icon">▶</span> Run Simulation
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<!-- MAIN 3-COLUMN LAYOUT -->
|
||||||
|
<main class="deck-main">
|
||||||
|
|
||||||
|
<!-- LEFT COLUMN: Collection Search -->
|
||||||
|
<aside class="deck-col-left">
|
||||||
|
<div class="deck-panel deck-panel-search">
|
||||||
|
<div class="deck-panel-header">
|
||||||
|
<span class="deck-panel-title">Collection Search</span>
|
||||||
|
<span class="deck-search-count" id="deck-search-count">0 results</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-search-bar-wrap">
|
||||||
|
<input
|
||||||
|
type="search"
|
||||||
|
class="deck-search-bar"
|
||||||
|
id="deck-search-bar"
|
||||||
|
placeholder="Search cards..."
|
||||||
|
autocomplete="off"
|
||||||
|
/>
|
||||||
|
<span class="deck-search-icon">🔍</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-filter-row">
|
||||||
|
<select class="deck-filter-select" id="deck-type-filter">
|
||||||
|
<option value="">All Types</option>
|
||||||
|
<option value="Land">Land</option>
|
||||||
|
<option value="Creature">Creature</option>
|
||||||
|
<option value="Instant">Instant</option>
|
||||||
|
<option value="Sorcery">Sorcery</option>
|
||||||
|
<option value="Artifact">Artifact</option>
|
||||||
|
<option value="Enchantment">Enchantment</option>
|
||||||
|
<option value="Planeswalker">Planeswalker</option>
|
||||||
|
</select>
|
||||||
|
<div class="deck-cmc-filter">
|
||||||
|
<input type="number" class="deck-cmc-input" id="deck-cmc-min" placeholder="0" min="0" max="20" />
|
||||||
|
<span class="deck-cmc-sep">–</span>
|
||||||
|
<input type="number" class="deck-cmc-input" id="deck-cmc-max" placeholder="∞" min="0" max="20" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-color-filter-row" id="deck-color-filter">
|
||||||
|
<button class="deck-color-pip deck-color-W" data-color="W" title="White">W</button>
|
||||||
|
<button class="deck-color-pip deck-color-U" data-color="U" title="Blue">U</button>
|
||||||
|
<button class="deck-color-pip deck-color-B" data-color="B" title="Black">B</button>
|
||||||
|
<button class="deck-color-pip deck-color-R" data-color="R" title="Red">R</button>
|
||||||
|
<button class="deck-color-pip deck-color-G" data-color="G" title="Green">G</button>
|
||||||
|
<button class="deck-color-pip deck-color-C" data-color="C" title="Colorless">C</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Results list -->
|
||||||
|
<div class="deck-search-results" id="deck-search-results">
|
||||||
|
<div class="deck-empty-state">
|
||||||
|
<div class="deck-empty-icon">🃏</div>
|
||||||
|
<div class="deck-empty-text">Search your collection to add cards</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-search-footer">
|
||||||
|
<button class="deck-btn deck-btn-success deck-btn-full" id="deck-add-all-owned-btn" style="display:none">
|
||||||
|
Add All Matching (Owned)
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<!-- CENTER COLUMN: Deck Card List -->
|
||||||
|
<section class="deck-col-center">
|
||||||
|
<div class="deck-panel deck-panel-cards" id="deck-card-list-panel">
|
||||||
|
<div class="deck-panel-header deck-card-list-header">
|
||||||
|
<span class="deck-panel-title">Deck Contents</span>
|
||||||
|
<div class="deck-card-list-actions">
|
||||||
|
<span class="deck-card-list-meta" id="deck-card-list-meta">Select or create a deck</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Card groups by type -->
|
||||||
|
<div class="deck-card-groups" id="deck-card-groups">
|
||||||
|
<div class="deck-empty-state deck-empty-state-lg">
|
||||||
|
<div class="deck-empty-icon">⚔</div>
|
||||||
|
<div class="deck-empty-text">Create or select a deck to start building</div>
|
||||||
|
<button class="deck-btn deck-btn-accent" id="deck-empty-new-btn">+ New Deck</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Quick add by name -->
|
||||||
|
<div class="deck-quickadd-row" id="deck-quickadd-row" style="display:none">
|
||||||
|
<div class="deck-quickadd-wrap">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="deck-quickadd-input"
|
||||||
|
id="deck-quickadd-input"
|
||||||
|
placeholder="Add card by name..."
|
||||||
|
autocomplete="off"
|
||||||
|
/>
|
||||||
|
<div class="deck-quickadd-dropdown" id="deck-quickadd-dropdown" style="display:none"></div>
|
||||||
|
</div>
|
||||||
|
<button class="deck-btn deck-btn-accent-sm" id="deck-quickadd-btn">Add</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Deck stats footer -->
|
||||||
|
<div class="deck-stats-footer" id="deck-stats-footer" style="display:none">
|
||||||
|
<div class="deck-stat-pill">
|
||||||
|
<span class="deck-stat-pill-num" id="deck-footer-total">0</span>
|
||||||
|
<span class="deck-stat-pill-label">total</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-stat-pill">
|
||||||
|
<span class="deck-stat-pill-num deck-stat-owned" id="deck-footer-owned">0</span>
|
||||||
|
<span class="deck-stat-pill-label">owned</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-stat-pill">
|
||||||
|
<span class="deck-stat-pill-num" id="deck-footer-avg-cmc">0.0</span>
|
||||||
|
<span class="deck-stat-pill-label">avg CMC</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- RIGHT COLUMN: EDH Advisor -->
|
||||||
|
<aside class="deck-col-right">
|
||||||
|
|
||||||
|
<!-- Ratio Dashboard -->
|
||||||
|
<div class="deck-panel deck-panel-ratios" id="deck-ratio-panel">
|
||||||
|
<div class="deck-panel-header deck-panel-header-collapsible" data-target="deck-ratio-body">
|
||||||
|
<span class="deck-panel-title">📊 Ratio Dashboard</span>
|
||||||
|
<span class="deck-collapse-arrow">▼</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-panel-body" id="deck-ratio-body">
|
||||||
|
<div class="deck-ratio-bars" id="deck-ratio-bars">
|
||||||
|
<div class="deck-ratio-placeholder">Load a deck to see ratios</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Fix My Ratios -->
|
||||||
|
<div class="deck-panel deck-panel-fix" id="deck-fix-panel">
|
||||||
|
<div class="deck-panel-header deck-panel-header-collapsible" data-target="deck-fix-body">
|
||||||
|
<span class="deck-panel-title">🔧 Fix My Ratios</span>
|
||||||
|
<span class="deck-collapse-arrow">▼</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-panel-body" id="deck-fix-body">
|
||||||
|
<div class="deck-shortfalls" id="deck-shortfalls">
|
||||||
|
<div class="deck-ratio-placeholder">No analysis available</div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-fix-actions" id="deck-fix-actions" style="display:none">
|
||||||
|
<button class="deck-btn deck-btn-ghost deck-btn-full" id="deck-autosuggest-btn">
|
||||||
|
⚡ Auto-suggest from Collection
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="deck-fix-suggestions" id="deck-fix-suggestions" style="display:none"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- EDHREC Recommendations -->
|
||||||
|
<div class="deck-panel deck-panel-edhrec" id="deck-edhrec-panel">
|
||||||
|
<div class="deck-panel-header deck-panel-header-collapsible" data-target="deck-edhrec-body">
|
||||||
|
<span class="deck-panel-title">✨ EDHREC Recs</span>
|
||||||
|
<span class="deck-collapse-arrow">▼</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-panel-body" id="deck-edhrec-body">
|
||||||
|
<div class="deck-edhrec-controls">
|
||||||
|
<label class="deck-toggle-label">
|
||||||
|
<input type="checkbox" class="deck-toggle-check" id="deck-edhrec-owned-only" />
|
||||||
|
<span class="deck-toggle-track"><span class="deck-toggle-thumb"></span></span>
|
||||||
|
Owned Only
|
||||||
|
</label>
|
||||||
|
<button class="deck-btn deck-btn-ghost deck-btn-sm" id="deck-edhrec-refresh-btn">
|
||||||
|
↺ Refresh
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="deck-edhrec-list" id="deck-edhrec-list">
|
||||||
|
<div class="deck-ratio-placeholder">Load a deck to see recommendations</div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-edhrec-bulk" id="deck-edhrec-bulk" style="display:none">
|
||||||
|
<button class="deck-btn deck-btn-success deck-btn-full" id="deck-edhrec-bulk-owned-btn">
|
||||||
|
Add All Owned (respect ratios)
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- AI Research (Perplexity) — hidden until pplx status check -->
|
||||||
|
<div class="deck-panel deck-panel-ai" id="deck-ai-research-panel" style="display:none">
|
||||||
|
<div class="deck-panel-header deck-panel-header-collapsible" data-target="deck-ai-research-body">
|
||||||
|
<span class="deck-panel-title">🧠 AI Research</span>
|
||||||
|
<span class="deck-collapse-arrow">▼</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-panel-body" id="deck-ai-research-body">
|
||||||
|
<div class="deck-ai-controls">
|
||||||
|
<div class="deck-form-field deck-form-field-sm">
|
||||||
|
<label class="deck-form-label-sm">Analysis Goal</label>
|
||||||
|
<input type="text" class="deck-form-input deck-form-input-sm" id="ai-research-goal"
|
||||||
|
placeholder="Identify weaknesses and suggest upgrades" />
|
||||||
|
</div>
|
||||||
|
<div class="deck-ai-row">
|
||||||
|
<div class="deck-form-field deck-form-field-sm deck-form-field-half">
|
||||||
|
<label class="deck-form-label-sm">Budget ($)</label>
|
||||||
|
<input type="number" class="deck-form-input deck-form-input-sm" id="ai-research-budget"
|
||||||
|
placeholder="No limit" min="0" step="10" />
|
||||||
|
</div>
|
||||||
|
<label class="deck-toggle-label deck-toggle-label-sm">
|
||||||
|
<input type="checkbox" class="deck-toggle-check" id="ai-research-use-collection" checked />
|
||||||
|
<span class="deck-toggle-track"><span class="deck-toggle-thumb"></span></span>
|
||||||
|
Use Collection
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div class="deck-form-field deck-form-field-sm">
|
||||||
|
<label class="deck-form-label-sm">Omit Cards (comma-separated)</label>
|
||||||
|
<input type="text" class="deck-form-input deck-form-input-sm" id="ai-research-omit"
|
||||||
|
placeholder="Dockside Extortionist, Rhystic Study" />
|
||||||
|
</div>
|
||||||
|
<button class="deck-btn deck-btn-ai deck-btn-full" id="ai-research-btn" disabled>
|
||||||
|
🔍 Analyze Deck
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="deck-ai-loading" id="ai-research-loading" style="display:none">
|
||||||
|
<div class="deck-ai-spinner"></div>
|
||||||
|
<span>Analyzing with Claude Opus...</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-ai-results" id="ai-research-results" style="display:none"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- AI Deck Generate (Perplexity) — hidden until pplx status check -->
|
||||||
|
<div class="deck-panel deck-panel-ai" id="deck-ai-generate-panel" style="display:none">
|
||||||
|
<div class="deck-panel-header deck-panel-header-collapsible" data-target="deck-ai-generate-body">
|
||||||
|
<span class="deck-panel-title">✨ AI Generate Deck</span>
|
||||||
|
<span class="deck-collapse-arrow">▼</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-panel-body" id="deck-ai-generate-body">
|
||||||
|
<div class="deck-ai-controls">
|
||||||
|
<div class="deck-form-field deck-form-field-sm">
|
||||||
|
<label class="deck-form-label-sm">Commander</label>
|
||||||
|
<div class="deck-ai-commander-wrap">
|
||||||
|
<input type="text" class="deck-form-input deck-form-input-sm" id="ai-gen-commander"
|
||||||
|
placeholder="Search for a commander..." autocomplete="off" />
|
||||||
|
<div class="deck-ai-commander-dropdown" id="ai-gen-commander-dropdown" style="display:none"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-ai-row">
|
||||||
|
<div class="deck-form-field deck-form-field-sm deck-form-field-half">
|
||||||
|
<label class="deck-form-label-sm">Budget ($)</label>
|
||||||
|
<input type="number" class="deck-form-input deck-form-input-sm" id="ai-gen-budget"
|
||||||
|
placeholder="No limit" min="0" step="10" />
|
||||||
|
</div>
|
||||||
|
<label class="deck-toggle-label deck-toggle-label-sm">
|
||||||
|
<input type="checkbox" class="deck-toggle-check" id="ai-gen-use-collection" checked />
|
||||||
|
<span class="deck-toggle-track"><span class="deck-toggle-thumb"></span></span>
|
||||||
|
Use Collection
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div class="deck-form-field deck-form-field-sm">
|
||||||
|
<label class="deck-form-label-sm">Omit Cards (comma-separated)</label>
|
||||||
|
<input type="text" class="deck-form-input deck-form-input-sm" id="ai-gen-omit"
|
||||||
|
placeholder="Rhystic Study, Cyclonic Rift" />
|
||||||
|
</div>
|
||||||
|
<button class="deck-btn deck-btn-ai deck-btn-full" id="ai-gen-btn">
|
||||||
|
✨ Generate Full Deck
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="deck-ai-loading" id="ai-gen-loading" style="display:none">
|
||||||
|
<div class="deck-ai-spinner"></div>
|
||||||
|
<span>Generating deck with Claude Opus...</span>
|
||||||
|
</div>
|
||||||
|
<div class="deck-ai-results" id="ai-gen-results" style="display:none"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</aside>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<!-- CARD PREVIEW TOOLTIP -->
|
||||||
|
<div class="deck-card-preview" id="deck-card-preview" style="display:none">
|
||||||
|
<img class="deck-card-preview-img" id="deck-card-preview-img" src="" alt="" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- TOAST CONTAINER -->
|
||||||
|
<div class="deck-toast-container" id="deck-toast-container"></div>
|
||||||
|
|
||||||
|
<!-- CONFIRM MODAL -->
|
||||||
|
<div class="deck-modal-overlay" id="deck-confirm-modal" style="display:none">
|
||||||
|
<div class="deck-modal">
|
||||||
|
<div class="deck-modal-header">
|
||||||
|
<h3 class="deck-modal-title" id="deck-confirm-title">Confirm Action</h3>
|
||||||
|
<button class="deck-modal-close-btn" id="deck-confirm-close">✕</button>
|
||||||
|
</div>
|
||||||
|
<div class="deck-modal-body">
|
||||||
|
<p class="deck-confirm-message" id="deck-confirm-message"></p>
|
||||||
|
<div class="deck-confirm-changes" id="deck-confirm-changes"></div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-modal-footer">
|
||||||
|
<button class="deck-btn deck-btn-ghost" id="deck-confirm-cancel">Cancel</button>
|
||||||
|
<button class="deck-btn deck-btn-accent" id="deck-confirm-ok">Confirm</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- NEW DECK MODAL -->
|
||||||
|
<div class="deck-modal-overlay" id="deck-new-modal" style="display:none">
|
||||||
|
<div class="deck-modal">
|
||||||
|
<div class="deck-modal-header">
|
||||||
|
<h3 class="deck-modal-title">Create New Deck</h3>
|
||||||
|
<button class="deck-modal-close-btn" id="deck-new-modal-close">✕</button>
|
||||||
|
</div>
|
||||||
|
<div class="deck-modal-body">
|
||||||
|
<div class="deck-form-field">
|
||||||
|
<label class="deck-form-label">Deck Name</label>
|
||||||
|
<input type="text" class="deck-form-input" id="deck-new-name" placeholder="My Commander Deck" />
|
||||||
|
</div>
|
||||||
|
<div class="deck-form-field">
|
||||||
|
<label class="deck-form-label">Commander (optional)</label>
|
||||||
|
<div class="deck-commander-search-wrap">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="deck-form-input"
|
||||||
|
id="deck-new-commander-search"
|
||||||
|
placeholder="Search Scryfall for commander..."
|
||||||
|
autocomplete="off"
|
||||||
|
/>
|
||||||
|
<div class="deck-commander-dropdown" id="deck-new-commander-dropdown" style="display:none"></div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-new-commander-selected" id="deck-new-commander-selected" style="display:none">
|
||||||
|
<span class="deck-selected-commander-name" id="deck-new-commander-name"></span>
|
||||||
|
<button class="deck-clear-commander-btn" id="deck-clear-commander-btn">✕</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-modal-footer">
|
||||||
|
<button class="deck-btn deck-btn-ghost" id="deck-new-cancel">Cancel</button>
|
||||||
|
<button class="deck-btn deck-btn-accent" id="deck-new-create">Create Deck</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- IMPORT MODAL -->
|
||||||
|
<div class="deck-modal-overlay" id="deck-import-modal" style="display:none">
|
||||||
|
<div class="deck-modal deck-modal-wide">
|
||||||
|
<div class="deck-modal-header">
|
||||||
|
<h3 class="deck-modal-title">Import Decklist</h3>
|
||||||
|
<button class="deck-modal-close-btn" id="deck-import-modal-close">✕</button>
|
||||||
|
</div>
|
||||||
|
<div class="deck-modal-body">
|
||||||
|
<div class="deck-import-tabs">
|
||||||
|
<button class="deck-import-tab active" data-tab="paste">Paste Text</button>
|
||||||
|
<button class="deck-import-tab" data-tab="file">Upload File</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="deck-import-paste-panel" class="deck-import-panel">
|
||||||
|
<textarea id="deck-import-textarea" rows="14" placeholder="Paste your decklist here...
|
||||||
|
|
||||||
|
Supported formats:
|
||||||
|
[Commander]
|
||||||
|
1 Korvold, Fae-Cursed King
|
||||||
|
|
||||||
|
[Deck]
|
||||||
|
1 Sol Ring
|
||||||
|
1 Cultivate
|
||||||
|
37 Forest
|
||||||
|
|
||||||
|
Or plain list:
|
||||||
|
Sol Ring
|
||||||
|
Cultivate
|
||||||
|
Forest"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="deck-import-file-panel" class="deck-import-panel" style="display:none">
|
||||||
|
<div class="deck-import-dropzone" id="deck-import-dropzone">
|
||||||
|
<div class="deck-import-dropzone-label">Drop a .dck or .txt file here, or click to browse</div>
|
||||||
|
<input type="file" id="deck-import-file" accept=".dck,.txt,.dec,.mwDeck" style="display:none" />
|
||||||
|
</div>
|
||||||
|
<div class="deck-import-file-info" id="deck-import-file-name"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-import-options">
|
||||||
|
<label class="deck-import-option-label">
|
||||||
|
<input type="radio" name="import-mode" value="new" checked /> Create new deck
|
||||||
|
</label>
|
||||||
|
<label class="deck-import-option-label">
|
||||||
|
<input type="radio" name="import-mode" value="add" /> Add to current deck
|
||||||
|
</label>
|
||||||
|
<label class="deck-import-option-label">
|
||||||
|
<input type="radio" name="import-mode" value="replace" /> Replace current deck cards
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="deck-import-new-name-row" class="deck-import-name-row">
|
||||||
|
<label>Deck name (optional):</label>
|
||||||
|
<input type="text" id="deck-import-name" placeholder="Auto-detected from commander" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="deck-import-progress-wrap" id="deck-import-progress" style="display:none">
|
||||||
|
<div class="deck-import-progress-label">Importing cards (Scryfall lookup)...</div>
|
||||||
|
<div class="deck-import-progress-track">
|
||||||
|
<div class="deck-import-progress-fill" id="deck-import-progress-bar"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="deck-import-result" style="display:none"></div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-modal-footer">
|
||||||
|
<button class="deck-btn deck-btn-ghost" id="deck-import-cancel">Cancel</button>
|
||||||
|
<button class="deck-btn deck-btn-accent" id="deck-import-go">Import</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div><!-- /#deck-builder-page -->
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Set API base URL — when served from a different host, override this value
|
||||||
|
window.LAB_API_BASE = window.LAB_API_BASE || '';
|
||||||
|
</script>
|
||||||
|
<script src="deckbuilder.js"></script>
|
||||||
|
<!-- Card Preview Popup (hover tooltip) -->
|
||||||
|
<div id="deck-card-preview" style="display:none; position:fixed; pointer-events:none; z-index:60;">
|
||||||
|
<img id="deck-card-preview-img" src="" alt="Card preview" style="width:220px; border-radius:8px; box-shadow:0 8px 24px rgba(0,0,0,0.7);" />
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,245 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Commander AI Lab — Auto Deck Generator V3</title>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
||||||
|
<link rel="stylesheet" href="global.css" />
|
||||||
|
<link rel="stylesheet" href="deckgenerator.css" />
|
||||||
|
<style>
|
||||||
|
/* No nav bar when embedded in LAN client iframe */
|
||||||
|
:root { --nav-height: 0px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- nav.js removed — deck generator is embedded via iframe in the LAN client -->
|
||||||
|
|
||||||
|
<div id="deckgen-page">
|
||||||
|
|
||||||
|
<!-- MAIN LAYOUT -->
|
||||||
|
<main class="dg-main">
|
||||||
|
|
||||||
|
<!-- LEFT: Configuration -->
|
||||||
|
<section class="dg-col-config">
|
||||||
|
|
||||||
|
<!-- Commander Search -->
|
||||||
|
<div class="dg-panel dg-panel-commander">
|
||||||
|
<div class="dg-panel-header">
|
||||||
|
<span class="dg-panel-title">Commander</span>
|
||||||
|
</div>
|
||||||
|
<div class="dg-panel-body">
|
||||||
|
<div class="dg-commander-search-wrap">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="dg-input"
|
||||||
|
id="dg-commander-input"
|
||||||
|
placeholder="Search for a legendary creature..."
|
||||||
|
autocomplete="off"
|
||||||
|
/>
|
||||||
|
<div class="dg-commander-dropdown" id="dg-commander-dropdown"></div>
|
||||||
|
</div>
|
||||||
|
<div class="dg-commander-selected" id="dg-commander-selected" style="display:none">
|
||||||
|
<div class="dg-commander-card">
|
||||||
|
<button class="dg-commander-clear" id="dg-commander-clear" title="Clear commander">×</button>
|
||||||
|
<img class="dg-commander-img" id="dg-commander-img" src="" alt="" />
|
||||||
|
<div class="dg-commander-info">
|
||||||
|
<div class="dg-commander-name" id="dg-commander-name"></div>
|
||||||
|
<div class="dg-commander-type" id="dg-commander-type"></div>
|
||||||
|
<div class="dg-commander-colors" id="dg-commander-colors"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Strategy & Bracket -->
|
||||||
|
<div class="dg-panel">
|
||||||
|
<div class="dg-panel-header">
|
||||||
|
<span class="dg-panel-title">Strategy & Power</span>
|
||||||
|
</div>
|
||||||
|
<div class="dg-panel-body">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="dg-input"
|
||||||
|
id="dg-strategy"
|
||||||
|
placeholder="e.g. zombie tokens, voltron, spellslinger..."
|
||||||
|
title="Describe your deck strategy — the AI uses this to pick thematically aligned cards"
|
||||||
|
/>
|
||||||
|
<div class="dg-bracket-row">
|
||||||
|
<label class="dg-label">
|
||||||
|
Target Bracket
|
||||||
|
<span class="tip-icon" data-tip="B1 = precon-level casual. B2 = upgraded with synergies. B3 = high power with strong combos. B4 = cEDH with maximum optimization, fast mana, and instant-speed interaction.">?</span>
|
||||||
|
</label>
|
||||||
|
<div class="dg-bracket-btns" id="dg-bracket-btns">
|
||||||
|
<button class="dg-bracket-btn" data-bracket="1" title="Casual — precon level">B1</button>
|
||||||
|
<button class="dg-bracket-btn" data-bracket="2" title="Upgraded Casual">B2</button>
|
||||||
|
<button class="dg-bracket-btn dg-bracket-active" data-bracket="3" title="High Power">B3</button>
|
||||||
|
<button class="dg-bracket-btn" data-bracket="4" title="cEDH — maximum power">B4</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Deck Name -->
|
||||||
|
<div class="dg-panel">
|
||||||
|
<div class="dg-panel-header">
|
||||||
|
<span class="dg-panel-title">Deck Name</span>
|
||||||
|
</div>
|
||||||
|
<div class="dg-panel-body">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="dg-input"
|
||||||
|
id="dg-deck-name"
|
||||||
|
placeholder="Auto-generated from commander name"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Budget -->
|
||||||
|
<div class="dg-panel">
|
||||||
|
<div class="dg-panel-header">
|
||||||
|
<span class="dg-panel-title">Budget</span>
|
||||||
|
</div>
|
||||||
|
<div class="dg-panel-body">
|
||||||
|
<div class="dg-budget-row">
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
class="dg-input dg-input-narrow"
|
||||||
|
id="dg-budget"
|
||||||
|
placeholder="No limit"
|
||||||
|
min="0"
|
||||||
|
step="25"
|
||||||
|
/>
|
||||||
|
<select class="dg-select" id="dg-budget-mode" title="Total Deck: caps the combined price of all 100 cards. Per Card Max: caps each individual card price.">
|
||||||
|
<option value="total">Total Deck $</option>
|
||||||
|
<option value="per_card">Per Card Max $</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Options -->
|
||||||
|
<div class="dg-panel">
|
||||||
|
<div class="dg-panel-header">
|
||||||
|
<span class="dg-panel-title">Options</span>
|
||||||
|
</div>
|
||||||
|
<div class="dg-panel-body">
|
||||||
|
<label class="dg-toggle-row">
|
||||||
|
<span class="dg-toggle-label">
|
||||||
|
Use my collection
|
||||||
|
<span class="tip-icon" data-tip="Prioritizes cards you already own before pulling from the full card pool. Saves money and ensures the deck is ready to play. Requires an imported collection.">?</span>
|
||||||
|
</span>
|
||||||
|
<input type="checkbox" class="dg-toggle-check" id="dg-use-collection" checked />
|
||||||
|
<span class="dg-toggle-track"><span class="dg-toggle-thumb"></span></span>
|
||||||
|
</label>
|
||||||
|
<label class="dg-toggle-row">
|
||||||
|
<span class="dg-toggle-label">
|
||||||
|
Smart Substitution
|
||||||
|
<span class="tip-icon" data-tip="When a suggested card isn't in your collection, automatically finds the closest functional replacement you do own. Keeps the deck playable without buying extra cards.">?</span>
|
||||||
|
</span>
|
||||||
|
<input type="checkbox" class="dg-toggle-check" id="dg-substitution" checked />
|
||||||
|
<span class="dg-toggle-track"><span class="dg-toggle-thumb"></span></span>
|
||||||
|
</label>
|
||||||
|
<div class="dg-model-row">
|
||||||
|
<label class="dg-label">
|
||||||
|
AI Model
|
||||||
|
<span class="tip-icon" data-tip="Deck generation is powered by Claude Opus 4, Anthropic's most capable model.">?</span>
|
||||||
|
</label>
|
||||||
|
<input type="hidden" id="dg-model" value="claude-opus-4-20250514" />
|
||||||
|
<span style="font-size:0.8rem; color:var(--text-muted, #888);">claude-opus-4-20250514</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Omit Cards (chips input) -->
|
||||||
|
<div class="dg-panel">
|
||||||
|
<div class="dg-panel-header">
|
||||||
|
<span class="dg-panel-title">Omit Cards</span>
|
||||||
|
</div>
|
||||||
|
<div class="dg-panel-body">
|
||||||
|
<div class="dg-omit-wrap" id="dg-omit-wrap">
|
||||||
|
<div class="dg-omit-chips" id="dg-omit-chips"></div>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="dg-input dg-omit-input"
|
||||||
|
id="dg-omit-input"
|
||||||
|
placeholder="Type a card name and press Enter…"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<small class="dg-hint">Cards listed here will be excluded from the generated deck.</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Action Buttons -->
|
||||||
|
<div class="dg-actions">
|
||||||
|
<button class="dg-btn dg-btn-preview" id="dg-preview-btn" disabled>
|
||||||
|
<span class="dg-btn-icon">⚡</span> Generate Deck
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Conversation Log -->
|
||||||
|
<div id="dg-conversation-log" class="dg-conversation-log">
|
||||||
|
<div class="dg-log-entry dg-log-info"><span class="dg-log-ts">[Ready]</span> Waiting for deck generation...</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- RIGHT: Preview Results -->
|
||||||
|
<section class="dg-col-results">
|
||||||
|
<!-- Empty State -->
|
||||||
|
<div class="dg-empty-state" id="dg-empty-state">
|
||||||
|
<div class="dg-empty-icon">⚡</div>
|
||||||
|
<div class="dg-empty-title">V3 AI Deck Generator</div>
|
||||||
|
<div class="dg-empty-text">Select a commander, set your bracket and strategy, then click "Generate Deck" to build a complete 100-card deck using Claude Opus with structured output.</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Loading State -->
|
||||||
|
<div class="dg-loading" id="dg-loading" style="display:none">
|
||||||
|
<div class="dg-spinner"></div>
|
||||||
|
<div class="dg-loading-text" id="dg-loading-text">Generating deck via Claude Opus 4...</div>
|
||||||
|
</div>
|
||||||
|
<div class="dg-progress-wrap" id="dg-progress-wrap" style="display:none">
|
||||||
|
<div class="dg-progress-bar">
|
||||||
|
<div class="dg-progress-fill" id="dg-progress-fill"></div>
|
||||||
|
</div>
|
||||||
|
<div class="dg-progress-meta">
|
||||||
|
<span class="dg-progress-phase" id="dg-progress-phase"></span>
|
||||||
|
<span class="dg-progress-elapsed" id="dg-progress-elapsed">0s elapsed</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Results -->
|
||||||
|
<div class="dg-results" id="dg-results" style="display:none">
|
||||||
|
<div class="dg-results-header">
|
||||||
|
<div id="dg-strategy-bar"></div>
|
||||||
|
<div id="dg-stats-bar" class="dg-stats-bar"></div>
|
||||||
|
<div id="dg-sub-summary" style="display:none"></div>
|
||||||
|
<div class="dg-result-actions">
|
||||||
|
<button class="dg-btn dg-btn-secondary" id="dg-regenerate-btn">↻ Regenerate</button>
|
||||||
|
<button class="dg-btn dg-btn-primary" id="dg-save-btn">💾 Save to Deck Builder</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-csv">CSV</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-dck">Forge .dck</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-moxfield">Moxfield</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-shopping">🛒 Shopping</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="dg-card-groups" class="dg-card-groups"></div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<!-- TOAST CONTAINER -->
|
||||||
|
<div class="dg-toast-container" id="dg-toast-container"></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
window.LAB_API_BASE = window.LAB_API_BASE || '';
|
||||||
|
</script>
|
||||||
|
<script src="deckgenerator.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,795 @@
|
|||||||
|
/**
|
||||||
|
* Commander AI Lab — Auto Deck Generator V3 UI
|
||||||
|
* ═══════════════════════════════════════════════
|
||||||
|
*
|
||||||
|
* V3 Endpoints:
|
||||||
|
* GET /api/deck/v3/status — check V3 generator status
|
||||||
|
* GET /api/deck-generator/commander-search — autocomplete commanders
|
||||||
|
* POST /api/deck/v3/generate — generate deck (local AI structured output)
|
||||||
|
* POST /api/deck/v3/commit — generate + save to Deck Builder
|
||||||
|
* POST /api/deck/v3/export/csv — export as CSV
|
||||||
|
* POST /api/deck/v3/export/dck — export as Forge .dck
|
||||||
|
* POST /api/deck/v3/export/moxfield — export in Moxfield paste format
|
||||||
|
* POST /api/deck/v3/export/shopping — shopping list of missing cards
|
||||||
|
*/
|
||||||
|
|
||||||
|
const DeckGenerator = (() => {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const API_BASE = window.LAB_API_BASE || window.location.origin;
|
||||||
|
const DEBOUNCE_MS = 350;
|
||||||
|
|
||||||
|
const COLOR_MAP = {
|
||||||
|
W: { label: 'W', cls: 'dg-pip-W' },
|
||||||
|
U: { label: 'U', cls: 'dg-pip-U' },
|
||||||
|
B: { label: 'B', cls: 'dg-pip-B' },
|
||||||
|
R: { label: 'R', cls: 'dg-pip-R' },
|
||||||
|
G: { label: 'G', cls: 'dg-pip-G' },
|
||||||
|
C: { label: 'C', cls: 'dg-pip-C' },
|
||||||
|
};
|
||||||
|
|
||||||
|
const TYPE_ORDER = ['Creature', 'Instant', 'Sorcery', 'Artifact', 'Enchantment', 'Planeswalker', 'Land', 'Battle', 'Other'];
|
||||||
|
const TYPE_ICONS = {
|
||||||
|
'Creature': '🐉', 'Instant': '⚡', 'Sorcery': '🌀', 'Artifact': '🔧',
|
||||||
|
'Enchantment': '✨', 'Planeswalker': '🌟', 'Land': '🏔', 'Battle': '⚔',
|
||||||
|
'Other': '🃏',
|
||||||
|
};
|
||||||
|
|
||||||
|
const STATUS_BADGES = {
|
||||||
|
owned: { label: 'Owned', cls: 'dg-badge-owned' },
|
||||||
|
substituted: { label: 'Substituted', cls: 'dg-badge-substituted' },
|
||||||
|
missing: { label: 'Missing', cls: 'dg-badge-missing' },
|
||||||
|
};
|
||||||
|
|
||||||
|
// ── State ──────────────────────────────────────────────
|
||||||
|
let state = {
|
||||||
|
commander: null,
|
||||||
|
targetBracket: 3,
|
||||||
|
previewResult: null,
|
||||||
|
lastRequestBody: null,
|
||||||
|
isLoading: false,
|
||||||
|
v3Ready: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
let searchTimeout = null;
|
||||||
|
|
||||||
|
// ── Progress bar state ──────────────────────────────
|
||||||
|
const PROGRESS_PHASES = [
|
||||||
|
{ at: 0, pct: 5, msg: 'Connecting to AI model...' },
|
||||||
|
{ at: 3, pct: 10, msg: 'Resolving commander on Scryfall...' },
|
||||||
|
{ at: 6, pct: 18, msg: 'Building collection summary...' },
|
||||||
|
{ at: 10, pct: 25, msg: 'Sending prompt to LLM...' },
|
||||||
|
{ at: 20, pct: 40, msg: 'AI is building the 99...' },
|
||||||
|
{ at: 35, pct: 55, msg: 'Selecting cards and filling slots...' },
|
||||||
|
{ at: 50, pct: 65, msg: 'Running smart substitution...' },
|
||||||
|
{ at: 70, pct: 78, msg: 'Cross-referencing with collection...' },
|
||||||
|
{ at: 90, pct: 88, msg: 'Almost there — finalizing deck...' },
|
||||||
|
{ at: 110, pct: 93, msg: 'Still working — large model can take a moment...' },
|
||||||
|
];
|
||||||
|
let _progressInterval = null;
|
||||||
|
let _progressStart = 0;
|
||||||
|
|
||||||
|
function startProgress() {
|
||||||
|
_progressStart = Date.now();
|
||||||
|
const wrap = $('dg-progress-wrap');
|
||||||
|
const fill = $('dg-progress-fill');
|
||||||
|
const phase = $('dg-progress-phase');
|
||||||
|
const elapsed = $('dg-progress-elapsed');
|
||||||
|
if (!wrap) return;
|
||||||
|
wrap.style.display = 'block';
|
||||||
|
fill.style.width = '0%';
|
||||||
|
phase.textContent = '';
|
||||||
|
elapsed.textContent = '0s elapsed';
|
||||||
|
let lastPhaseIdx = -1;
|
||||||
|
_progressInterval = setInterval(() => {
|
||||||
|
const secs = Math.floor((Date.now() - _progressStart) / 1000);
|
||||||
|
elapsed.textContent = secs + 's elapsed';
|
||||||
|
for (let i = PROGRESS_PHASES.length - 1; i >= 0; i--) {
|
||||||
|
if (secs >= PROGRESS_PHASES[i].at) {
|
||||||
|
fill.style.width = PROGRESS_PHASES[i].pct + '%';
|
||||||
|
if (i !== lastPhaseIdx) {
|
||||||
|
phase.textContent = PROGRESS_PHASES[i].msg;
|
||||||
|
logMessage(PROGRESS_PHASES[i].msg, 'info');
|
||||||
|
lastPhaseIdx = i;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopProgress(success) {
|
||||||
|
if (_progressInterval) {
|
||||||
|
clearInterval(_progressInterval);
|
||||||
|
_progressInterval = null;
|
||||||
|
}
|
||||||
|
const fill = $('dg-progress-fill');
|
||||||
|
const phase = $('dg-progress-phase');
|
||||||
|
const elapsed = $('dg-progress-elapsed');
|
||||||
|
if (fill) fill.style.width = success ? '100%' : '0%';
|
||||||
|
if (phase) phase.textContent = success ? 'Done!' : 'Failed';
|
||||||
|
const secs = Math.floor((Date.now() - _progressStart) / 1000);
|
||||||
|
if (elapsed) elapsed.textContent = secs + 's total';
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Omit Cards (chips) ─────────────────────────────
|
||||||
|
let omitCards = [];
|
||||||
|
|
||||||
|
function addOmitCard(name) {
|
||||||
|
const trimmed = name.trim();
|
||||||
|
if (!trimmed) return;
|
||||||
|
if (omitCards.some(c => c.toLowerCase() === trimmed.toLowerCase())) return;
|
||||||
|
omitCards.push(trimmed);
|
||||||
|
renderOmitChips();
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeOmitCard(idx) {
|
||||||
|
omitCards.splice(idx, 1);
|
||||||
|
renderOmitChips();
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderOmitChips() {
|
||||||
|
const container = $('dg-omit-chips');
|
||||||
|
if (!container) return;
|
||||||
|
container.innerHTML = omitCards.map((name, i) =>
|
||||||
|
'<span class="dg-chip">' +
|
||||||
|
escHtml(name) +
|
||||||
|
'<span class="dg-chip-x" data-idx="' + i + '">×</span>' +
|
||||||
|
'</span>'
|
||||||
|
).join('');
|
||||||
|
container.querySelectorAll('.dg-chip-x').forEach(el => {
|
||||||
|
el.addEventListener('click', () => removeOmitCard(parseInt(el.dataset.idx, 10)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const $ = (id) => document.getElementById(id);
|
||||||
|
|
||||||
|
// ── Init ────────────────────────────────────────────────
|
||||||
|
async function init() {
|
||||||
|
await checkV3Status();
|
||||||
|
bindEvents();
|
||||||
|
checkQueryParams();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkV3Status() {
|
||||||
|
try {
|
||||||
|
const resp = await fetch(API_BASE + '/api/deck/v3/status');
|
||||||
|
const data = await resp.json();
|
||||||
|
state.v3Ready = data.initialized;
|
||||||
|
if (!state.v3Ready) {
|
||||||
|
// Warn but do not block — local/Ollama mode may still work
|
||||||
|
console.warn('V3 status: not initialized. Error:', data.error || 'unknown');
|
||||||
|
toast('V3 generator not ready — check that the Anthropic API key is configured', 'warning');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('V3 status check failed:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function bindEvents() {
|
||||||
|
// Commander search
|
||||||
|
const input = $('dg-commander-input');
|
||||||
|
input.addEventListener('input', () => {
|
||||||
|
clearTimeout(searchTimeout);
|
||||||
|
const q = input.value.trim();
|
||||||
|
if (q.length < 2) { hideDropdown(); return; }
|
||||||
|
searchTimeout = setTimeout(() => searchCommander(q), DEBOUNCE_MS);
|
||||||
|
});
|
||||||
|
|
||||||
|
input.addEventListener('focus', () => {
|
||||||
|
const q = input.value.trim();
|
||||||
|
if (q.length >= 2) searchCommander(q);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('click', (e) => {
|
||||||
|
const wrap = document.querySelector('.dg-commander-search-wrap');
|
||||||
|
if (wrap && !wrap.contains(e.target)) hideDropdown();
|
||||||
|
});
|
||||||
|
|
||||||
|
$('dg-commander-clear').addEventListener('click', clearCommander);
|
||||||
|
|
||||||
|
// Bracket buttons
|
||||||
|
document.querySelectorAll('.dg-bracket-btn').forEach(btn => {
|
||||||
|
btn.addEventListener('click', () => {
|
||||||
|
document.querySelectorAll('.dg-bracket-btn').forEach(b => b.classList.remove('dg-bracket-active'));
|
||||||
|
btn.classList.add('dg-bracket-active');
|
||||||
|
state.targetBracket = parseInt(btn.dataset.bracket, 10);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Generate & save
|
||||||
|
$('dg-preview-btn').addEventListener('click', generateDeck);
|
||||||
|
$('dg-save-btn').addEventListener('click', commitDeck);
|
||||||
|
$('dg-regenerate-btn').addEventListener('click', generateDeck);
|
||||||
|
|
||||||
|
// Export buttons
|
||||||
|
$('dg-export-csv').addEventListener('click', () => exportDeck('csv'));
|
||||||
|
$('dg-export-dck').addEventListener('click', () => exportDeck('dck'));
|
||||||
|
$('dg-export-moxfield').addEventListener('click', () => exportMoxfield());
|
||||||
|
$('dg-export-shopping').addEventListener('click', () => exportShopping());
|
||||||
|
|
||||||
|
// Omit cards chips input
|
||||||
|
const omitInput = $('dg-omit-input');
|
||||||
|
if (omitInput) {
|
||||||
|
omitInput.addEventListener('keydown', (e) => {
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
e.preventDefault();
|
||||||
|
addOmitCard(omitInput.value);
|
||||||
|
omitInput.value = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const omitWrap = $('dg-omit-wrap');
|
||||||
|
if (omitWrap) {
|
||||||
|
omitWrap.addEventListener('click', () => omitInput && omitInput.focus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkQueryParams() {
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
const name = params.get('commander');
|
||||||
|
if (name) {
|
||||||
|
$('dg-commander-input').value = name;
|
||||||
|
searchCommander(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Build Request Body ──────────────────────────────────
|
||||||
|
function buildRequestBody() {
|
||||||
|
const budget = parseFloat($('dg-budget').value) || null;
|
||||||
|
return {
|
||||||
|
commander_name: state.commander.name,
|
||||||
|
strategy: $('dg-strategy').value.trim(),
|
||||||
|
target_bracket: state.targetBracket,
|
||||||
|
budget_usd: budget,
|
||||||
|
budget_mode: $('dg-budget-mode').value,
|
||||||
|
omit_cards: omitCards.slice(),
|
||||||
|
use_collection: $('dg-use-collection').checked,
|
||||||
|
run_substitution: $('dg-substitution').checked,
|
||||||
|
model: $('dg-model').value,
|
||||||
|
deck_name: $('dg-deck-name').value.trim() || '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Commander Search ────────────────────────────────────
|
||||||
|
function positionDropdown() {
|
||||||
|
const input = $('dg-commander-input');
|
||||||
|
const dropdown = $('dg-commander-dropdown');
|
||||||
|
const rect = input.getBoundingClientRect();
|
||||||
|
dropdown.style.top = (rect.bottom + 4) + 'px';
|
||||||
|
dropdown.style.left = rect.left + 'px';
|
||||||
|
dropdown.style.width = rect.width + 'px';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function searchCommander(q) {
|
||||||
|
const dropdown = $('dg-commander-dropdown');
|
||||||
|
dropdown.innerHTML = '<div class="dg-dropdown-loading">Searching...</div>';
|
||||||
|
dropdown.classList.add('open');
|
||||||
|
positionDropdown();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await fetch(API_BASE + '/api/deck-generator/commander-search?q=' + encodeURIComponent(q));
|
||||||
|
const data = await resp.json();
|
||||||
|
renderDropdown(data.results || []);
|
||||||
|
} catch (e) {
|
||||||
|
dropdown.innerHTML = '<div class="dg-dropdown-empty">Search failed</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderDropdown(results) {
|
||||||
|
const dropdown = $('dg-commander-dropdown');
|
||||||
|
|
||||||
|
if (results.length === 0) {
|
||||||
|
dropdown.innerHTML = '<div class="dg-dropdown-empty">No legendary creatures found</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dropdown.innerHTML = results.map((r, idx) => {
|
||||||
|
const colors = (r.color_identity || [])
|
||||||
|
.map(c => COLOR_MAP[c] ? '<span class="dg-color-pip ' + COLOR_MAP[c].cls + '">' + COLOR_MAP[c].label + '</span>' : '')
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
const badge = r.in_collection
|
||||||
|
? '<span class="dg-dropdown-item-badge dg-badge-owned">Owned</span>'
|
||||||
|
: '<span class="dg-dropdown-item-badge dg-badge-scryfall">Scryfall</span>';
|
||||||
|
|
||||||
|
const imgSrc = r.image_url ? r.image_url.replace('version=normal', 'version=small') : '';
|
||||||
|
const imgTag = imgSrc
|
||||||
|
? '<img class="dg-dropdown-item-img" src="' + imgSrc + '" alt="" loading="lazy" />'
|
||||||
|
: '<div class="dg-dropdown-item-img" style="background:var(--lab-surface)"></div>';
|
||||||
|
|
||||||
|
return '<div class="dg-dropdown-item" data-idx="' + idx + '">'
|
||||||
|
+ imgTag
|
||||||
|
+ '<div class="dg-dropdown-item-info">'
|
||||||
|
+ ' <div class="dg-dropdown-item-name">' + escHtml(r.name) + '</div>'
|
||||||
|
+ ' <div class="dg-dropdown-item-type">' + escHtml(r.type_line || '') + '</div>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<div style="display:flex;gap:3px;align-items:center">' + colors + '</div>'
|
||||||
|
+ badge
|
||||||
|
+ '</div>';
|
||||||
|
}).join('');
|
||||||
|
|
||||||
|
dropdown.querySelectorAll('.dg-dropdown-item').forEach(el => {
|
||||||
|
el.addEventListener('click', () => {
|
||||||
|
selectCommander(results[parseInt(el.dataset.idx, 10)]);
|
||||||
|
hideDropdown();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectCommander(cmdr) {
|
||||||
|
state.commander = cmdr;
|
||||||
|
$('dg-commander-input').style.display = 'none';
|
||||||
|
$('dg-commander-selected').style.display = 'block';
|
||||||
|
$('dg-commander-name').textContent = cmdr.name;
|
||||||
|
$('dg-commander-type').textContent = cmdr.type_line || '';
|
||||||
|
|
||||||
|
$('dg-commander-colors').innerHTML = (cmdr.color_identity || [])
|
||||||
|
.map(c => COLOR_MAP[c] ? '<span class="dg-color-pip ' + COLOR_MAP[c].cls + '">' + COLOR_MAP[c].label + '</span>' : '')
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
const img = $('dg-commander-img');
|
||||||
|
if (cmdr.image_url) {
|
||||||
|
var imgUrl = cmdr.image_url;
|
||||||
|
if (imgUrl.indexOf('version=small') !== -1) {
|
||||||
|
imgUrl = imgUrl.replace('version=small', 'version=normal');
|
||||||
|
} else if (imgUrl.indexOf('version=') === -1 && imgUrl.indexOf('?') !== -1) {
|
||||||
|
imgUrl += '&version=normal';
|
||||||
|
} else if (imgUrl.indexOf('version=') === -1) {
|
||||||
|
imgUrl += '?version=normal';
|
||||||
|
}
|
||||||
|
img.src = imgUrl;
|
||||||
|
img.style.display = 'block';
|
||||||
|
} else {
|
||||||
|
img.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
$('dg-preview-btn').disabled = false;
|
||||||
|
|
||||||
|
// Auto-fill deck name if empty
|
||||||
|
const deckNameInput = $('dg-deck-name');
|
||||||
|
if (deckNameInput && !deckNameInput.value.trim()) {
|
||||||
|
deckNameInput.value = 'Auto - ' + cmdr.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearCommander() {
|
||||||
|
state.commander = null;
|
||||||
|
$('dg-commander-input').style.display = '';
|
||||||
|
$('dg-commander-input').value = '';
|
||||||
|
$('dg-commander-selected').style.display = 'none';
|
||||||
|
$('dg-preview-btn').disabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hideDropdown() {
|
||||||
|
$('dg-commander-dropdown').classList.remove('open');
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Conversation Log Helper --
|
||||||
|
function logMessage(msg, type = 'info') {
|
||||||
|
const log = $('dg-conversation-log');
|
||||||
|
if (!log) return;
|
||||||
|
const entry = document.createElement('div');
|
||||||
|
entry.className = 'dg-log-entry dg-log-' + type;
|
||||||
|
const ts = new Date().toLocaleTimeString();
|
||||||
|
entry.innerHTML = '<span class="dg-log-ts">[' + ts + ']</span> ' + msg;
|
||||||
|
log.appendChild(entry);
|
||||||
|
log.scrollTop = log.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearLog() {
|
||||||
|
const log = $('dg-conversation-log');
|
||||||
|
if (!log) return;
|
||||||
|
log.innerHTML = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Generate Deck ───────────────────────────────────────
|
||||||
|
async function generateDeck() {
|
||||||
|
clearLog();
|
||||||
|
logMessage('Connecting to Claude AI...', 'info');
|
||||||
|
if (!state.commander) { toast('Select a commander first', 'error'); return; }
|
||||||
|
// Non-blocking warning only — local Ollama mode does not require v3Ready
|
||||||
|
if (!state.v3Ready) {
|
||||||
|
logMessage('Warning: V3 status check failed. Attempting generation anyway via local AI...', 'warning');
|
||||||
|
}
|
||||||
|
logMessage('Building request body with commander: <b>' + state.commander.name + '</b>', 'info');
|
||||||
|
|
||||||
|
setLoading(true, 'Generating deck via Local AI...');
|
||||||
|
startProgress();
|
||||||
|
|
||||||
|
const body = buildRequestBody();
|
||||||
|
logMessage('Sending request to AI model... this may take a moment.', 'info');
|
||||||
|
state.lastRequestBody = body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await fetch(API_BASE + '/api/deck/v3/generate', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!resp.ok) {
|
||||||
|
const err = await resp.json().catch(() => ({}));
|
||||||
|
throw new Error(err.error || err.detail || 'Generation failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
state.previewResult = await resp.json();
|
||||||
|
logMessage('AI response received! Processing deck data...', 'success');
|
||||||
|
|
||||||
|
const stats = state.previewResult.stats || {};
|
||||||
|
const cardCount = (stats.total_cards || 0);
|
||||||
|
logMessage('Deck generated: <b>' + cardCount + ' cards</b> | $' + (stats.total_price_usd || 0).toFixed(2) + ' estimated', 'success');
|
||||||
|
stopProgress(true);
|
||||||
|
renderResults(state.previewResult);
|
||||||
|
|
||||||
|
toast('Deck generated: ' + (stats.total_cards || 0) + ' cards | $' + (stats.total_price_usd || 0), 'success');
|
||||||
|
} catch (e) {
|
||||||
|
stopProgress(false);
|
||||||
|
toast(e.message || 'Generation failed', 'error');
|
||||||
|
logMessage('Error: ' + (e.message || 'Unknown error'), 'error');
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLoading(loading, text) {
|
||||||
|
state.isLoading = loading;
|
||||||
|
$('dg-empty-state').style.display = 'none';
|
||||||
|
$('dg-loading').style.display = loading ? 'flex' : 'none';
|
||||||
|
$('dg-results').style.display = loading ? 'none' : 'block';
|
||||||
|
$('dg-preview-btn').disabled = loading || !state.commander;
|
||||||
|
if (text) $('dg-loading-text').textContent = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Render Results ──────────────────────────────────────
|
||||||
|
function renderResults(result) {
|
||||||
|
$('dg-loading').style.display = 'none';
|
||||||
|
$('dg-results').style.display = 'block';
|
||||||
|
$('dg-preview-btn').disabled = false;
|
||||||
|
|
||||||
|
renderStrategyBar(result);
|
||||||
|
renderStatsBar(result);
|
||||||
|
renderSubstitutionSummary(result);
|
||||||
|
renderCardGroups(result);
|
||||||
|
renderUseInGameButton(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderUseInGameButton(result) {
|
||||||
|
// Only show when running inside the LAN client iframe
|
||||||
|
if (window.parent === window) return;
|
||||||
|
|
||||||
|
var existing = document.getElementById('dg-export-game-btn');
|
||||||
|
if (existing) existing.remove();
|
||||||
|
|
||||||
|
var btn = document.createElement('button');
|
||||||
|
btn.id = 'dg-export-game-btn';
|
||||||
|
btn.className = 'dg-btn dg-btn-preview';
|
||||||
|
btn.style.cssText = 'margin:12px auto;display:block;border-color:rgba(91,141,217,0.4);color:rgba(91,141,217,0.9);';
|
||||||
|
btn.innerHTML = '<span class="dg-btn-icon">🎮</span> Use in Game';
|
||||||
|
btn.addEventListener('click', function() {
|
||||||
|
var cards = (result.cards || []).map(function(c) {
|
||||||
|
return { name: c.name, quantity: c.count || 1, is_commander: false };
|
||||||
|
});
|
||||||
|
if (result.commander) {
|
||||||
|
cards.unshift({ name: result.commander.name, quantity: 1, is_commander: true });
|
||||||
|
}
|
||||||
|
window.parent.postMessage({
|
||||||
|
type: 'DECK_BUILDER_EXPORT',
|
||||||
|
deck: cards,
|
||||||
|
deckName: result.commander ? result.commander.name + ' Deck' : 'Generated Deck',
|
||||||
|
commander: result.commander ? result.commander.name : '',
|
||||||
|
}, '*');
|
||||||
|
toast('Deck sent to game setup!', 'success');
|
||||||
|
});
|
||||||
|
|
||||||
|
$('dg-results').appendChild(btn);
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderStrategyBar(result) {
|
||||||
|
const bar = $('dg-strategy-bar');
|
||||||
|
if (!bar) return;
|
||||||
|
const bracket = result.bracket || {};
|
||||||
|
const archetype = result.archetype || '';
|
||||||
|
const strategy = result.strategy_summary || '';
|
||||||
|
|
||||||
|
bar.innerHTML = '<div class="dg-strategy-info">'
|
||||||
|
+ '<span class="dg-bracket-badge dg-bracket-' + (bracket.level || 0) + '">B' + (bracket.level || '?') + '</span>'
|
||||||
|
+ '<span class="dg-archetype-badge">' + escHtml(archetype) + '</span>'
|
||||||
|
+ '<span class="dg-strategy-text">' + escHtml(strategy) + '</span>'
|
||||||
|
+ '</div>'
|
||||||
|
+ (bracket.reasoning ? '<div class="dg-bracket-reasoning">' + escHtml(bracket.reasoning) + '</div>' : '')
|
||||||
|
+ (bracket.game_changers && bracket.game_changers.length
|
||||||
|
? '<div class="dg-game-changers">Game Changers: ' + bracket.game_changers.map(c => '<span class="dg-gc-chip">' + escHtml(c) + '</span>').join('') + '</div>'
|
||||||
|
: '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderStatsBar(result) {
|
||||||
|
const stats = result.stats || {};
|
||||||
|
const container = $('dg-stats-bar');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
const chips = [
|
||||||
|
chipHtml('Total', stats.total_cards || 0, ''),
|
||||||
|
chipHtml('Owned', (stats.by_status || {}).owned || 0, 'dg-stat-owned'),
|
||||||
|
chipHtml('Subbed', (stats.by_status || {}).substituted || 0, 'dg-stat-substituted'),
|
||||||
|
chipHtml('Missing', (stats.by_status || {}).missing || 0, 'dg-stat-missing'),
|
||||||
|
chipHtml('Lands', stats.land_count || 0, ''),
|
||||||
|
chipHtml('$' + (stats.total_price_usd || 0).toFixed(0), 'Total', 'dg-stat-price'),
|
||||||
|
];
|
||||||
|
|
||||||
|
container.innerHTML = chips.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function chipHtml(label, num, cls) {
|
||||||
|
return '<div class="dg-stat-chip ' + cls + '">'
|
||||||
|
+ '<span class="dg-stat-num">' + num + '</span>'
|
||||||
|
+ '<span class="dg-stat-label">' + label + '</span>'
|
||||||
|
+ '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderSubstitutionSummary(result) {
|
||||||
|
const el = $('dg-sub-summary');
|
||||||
|
const sub = result.substitution_stats;
|
||||||
|
if (!sub) { el.style.display = 'none'; return; }
|
||||||
|
|
||||||
|
el.style.display = 'block';
|
||||||
|
el.innerHTML = '<div class="dg-sub-bar">'
|
||||||
|
+ '<span class="dg-sub-stat dg-sub-owned">' + sub.owned + ' owned</span>'
|
||||||
|
+ '<span class="dg-sub-stat dg-sub-substituted">' + sub.substituted + ' substituted</span>'
|
||||||
|
+ '<span class="dg-sub-stat dg-sub-missing">' + sub.missing + ' still missing</span>'
|
||||||
|
+ '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderCardGroups(result) {
|
||||||
|
const container = $('dg-card-groups');
|
||||||
|
if (!container) return;
|
||||||
|
const cards = result.cards || [];
|
||||||
|
const commander = result.commander;
|
||||||
|
|
||||||
|
const groups = {};
|
||||||
|
for (const card of cards) {
|
||||||
|
const cat = card.category || 'Other';
|
||||||
|
if (!groups[cat]) groups[cat] = [];
|
||||||
|
groups[cat].push(card);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortedTypes = TYPE_ORDER.filter(t => groups[t] && groups[t].length > 0);
|
||||||
|
for (const t of Object.keys(groups)) {
|
||||||
|
if (!sortedTypes.includes(t)) sortedTypes.push(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
let html = '';
|
||||||
|
|
||||||
|
if (commander) {
|
||||||
|
html += '<div class="dg-card-group">'
|
||||||
|
+ '<div class="dg-group-header">'
|
||||||
|
+ '<span class="dg-group-title">⚔ Commander</span>'
|
||||||
|
+ '<span class="dg-group-count">1</span>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<div class="dg-group-body">'
|
||||||
|
+ '<div class="dg-card-row dg-card-row-commander">'
|
||||||
|
+ '<span class="dg-card-row-name" style="font-weight:600;color:#fff">' + escHtml(commander.name) + '</span>'
|
||||||
|
+ '<span class="dg-card-row-source dg-src-collection">Commander</span>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '</div></div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const type of sortedTypes) {
|
||||||
|
const groupCards = groups[type];
|
||||||
|
if (!groupCards || groupCards.length === 0) continue;
|
||||||
|
|
||||||
|
groupCards.sort((a, b) => (a.name || '').localeCompare(b.name || ''));
|
||||||
|
|
||||||
|
const icon = TYPE_ICONS[type] || '🃏';
|
||||||
|
html += '<div class="dg-card-group">'
|
||||||
|
+ '<div class="dg-group-header" data-type="' + type + '">'
|
||||||
|
+ '<span class="dg-group-title">' + icon + ' ' + escHtml(type) + '</span>'
|
||||||
|
+ '<span class="dg-group-count">' + groupCards.reduce(function(s,c){ return s + (c.count || 1); }, 0) + '</span>'
|
||||||
|
+ '</div>'
|
||||||
|
+ '<div class="dg-group-body" data-group="' + type + '">';
|
||||||
|
|
||||||
|
for (const card of groupCards) {
|
||||||
|
html += renderCardRow(card);
|
||||||
|
}
|
||||||
|
|
||||||
|
html += '</div></div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
container.innerHTML = html;
|
||||||
|
|
||||||
|
container.querySelectorAll('.dg-group-header').forEach(header => {
|
||||||
|
header.addEventListener('click', () => {
|
||||||
|
const type = header.dataset.type;
|
||||||
|
if (!type) return;
|
||||||
|
const body = container.querySelector('.dg-group-body[data-group="' + type + '"]');
|
||||||
|
if (body) body.classList.toggle('collapsed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderCardRow(card) {
|
||||||
|
const statusInfo = STATUS_BADGES[card.status] || STATUS_BADGES.missing;
|
||||||
|
const statusBadge = '<span class="dg-card-status ' + statusInfo.cls + '">' + statusInfo.label + '</span>';
|
||||||
|
|
||||||
|
const roleTags = (card.role_tags || []).slice(0, 3)
|
||||||
|
.map(r => '<span class="dg-role-chip">' + escHtml(r) + '</span>')
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
let subHtml = '';
|
||||||
|
if (card.status === 'substituted' && card.selected_substitute) {
|
||||||
|
subHtml = '<div class="dg-card-sub-line">'
|
||||||
|
+ '<span class="dg-sub-arrow">→</span> '
|
||||||
|
+ '<span class="dg-sub-name">' + escHtml(card.selected_substitute) + '</span>'
|
||||||
|
+ '</div>';
|
||||||
|
} else if (card.status === 'missing' && card.alternatives && card.alternatives.length > 0) {
|
||||||
|
subHtml = '<div class="dg-card-sub-line dg-sub-suggestions">'
|
||||||
|
+ '<span class="dg-sub-label">Alternatives:</span> '
|
||||||
|
+ card.alternatives.slice(0, 3).map(a =>
|
||||||
|
'<span class="dg-alt-chip" title="' + escAttr(a.reason || '') + '">'
|
||||||
|
+ escHtml(a.name) + ' (' + (a.similarity_score * 100).toFixed(0) + '%)'
|
||||||
|
+ '</span>'
|
||||||
|
).join('')
|
||||||
|
+ '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
const price = card.estimated_price_usd
|
||||||
|
? '<span class="dg-card-price">$' + card.estimated_price_usd.toFixed(2) + '</span>'
|
||||||
|
: '';
|
||||||
|
|
||||||
|
return '<div class="dg-card-row dg-card-status-' + card.status + '">'
|
||||||
|
+ '<div class="dg-card-row-main">'
|
||||||
|
+ '<span class="dg-card-row-name">' + ((card.count || 1) > 1 ? '<span class="dg-card-count">' + card.count + 'x</span> ' : '') + escHtml(card.name) + '</span>'
|
||||||
|
+ '<span class="dg-card-row-roles">' + roleTags + '</span>'
|
||||||
|
+ statusBadge
|
||||||
|
+ price
|
||||||
|
+ '</div>'
|
||||||
|
+ (card.reason ? '<div class="dg-card-reason">' + escHtml(card.reason) + '</div>' : '')
|
||||||
|
+ subHtml
|
||||||
|
+ '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Commit Deck ─────────────────────────────────────────
|
||||||
|
async function commitDeck() {
|
||||||
|
if (!state.commander) { toast('No commander selected', 'error'); return; }
|
||||||
|
|
||||||
|
$('dg-save-btn').disabled = true;
|
||||||
|
$('dg-save-btn').textContent = 'Saving...';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const body = state.lastRequestBody || buildRequestBody();
|
||||||
|
const resp = await fetch(API_BASE + '/api/deck/v3/commit', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!resp.ok) {
|
||||||
|
const err = await resp.json().catch(() => ({}));
|
||||||
|
throw new Error(err.error || err.detail || 'Save failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await resp.json();
|
||||||
|
toast('Deck saved: ' + (result.deck_name || 'Unknown'), 'success');
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = 'deckbuilder.html?deck_id=' + result.deck_id;
|
||||||
|
}, 1200);
|
||||||
|
} catch (e) {
|
||||||
|
toast(e.message || 'Save failed', 'error');
|
||||||
|
} finally {
|
||||||
|
$('dg-save-btn').disabled = false;
|
||||||
|
$('dg-save-btn').innerHTML = '<span class="dg-btn-icon">💾</span> Save to Deck Builder';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Exports ─────────────────────────────────────────────
|
||||||
|
async function exportDeck(format) {
|
||||||
|
if (!state.commander) { toast('Generate a deck first', 'error'); return; }
|
||||||
|
|
||||||
|
const body = state.lastRequestBody || buildRequestBody();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await fetch(API_BASE + '/api/deck/v3/export/' + format, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!resp.ok) throw new Error('Export failed');
|
||||||
|
|
||||||
|
const blob = await resp.blob();
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = (state.commander.name || 'deck').replace(/\s+/g, '_') + '.' + (format === 'csv' ? 'csv' : 'dck');
|
||||||
|
a.click();
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
|
||||||
|
toast('Exported ' + format.toUpperCase(), 'success');
|
||||||
|
} catch (e) {
|
||||||
|
toast(e.message || 'Export failed', 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function exportMoxfield() {
|
||||||
|
if (!state.commander) { toast('Generate a deck first', 'error'); return; }
|
||||||
|
|
||||||
|
const body = state.lastRequestBody || buildRequestBody();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await fetch(API_BASE + '/api/deck/v3/export/moxfield', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await resp.json();
|
||||||
|
if (data.content) {
|
||||||
|
await navigator.clipboard.writeText(data.content);
|
||||||
|
toast('Moxfield deck copied to clipboard', 'success');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
toast('Moxfield export failed', 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function exportShopping() {
|
||||||
|
if (!state.commander) { toast('Generate a deck first', 'error'); return; }
|
||||||
|
|
||||||
|
const body = state.lastRequestBody || buildRequestBody();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resp = await fetch(API_BASE + '/api/deck/v3/export/shopping', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await resp.json();
|
||||||
|
const list = data.shopping_list || [];
|
||||||
|
|
||||||
|
if (list.length === 0) {
|
||||||
|
toast('All cards are owned — nothing to buy', 'success');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lines = ['Shopping List — ' + (data.commander || 'Deck'), ''];
|
||||||
|
for (const item of list) {
|
||||||
|
lines.push(item.count + 'x ' + item.name + ' ($' + (item.estimated_price_usd || 0).toFixed(2) + ') — ' + item.category);
|
||||||
|
}
|
||||||
|
lines.push('', 'Total: ' + list.length + ' cards, ~$' + (data.estimated_cost_usd || 0).toFixed(2));
|
||||||
|
|
||||||
|
await navigator.clipboard.writeText(lines.join('\n'));
|
||||||
|
toast('Shopping list copied (' + list.length + ' cards, ~$' + (data.estimated_cost_usd || 0).toFixed(0) + ')', 'success');
|
||||||
|
} catch (e) {
|
||||||
|
toast('Shopping list failed', 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Toast ───────────────────────────────────────────────
|
||||||
|
function toast(msg, type) {
|
||||||
|
type = type || 'info';
|
||||||
|
const container = $('dg-toast-container');
|
||||||
|
const el = document.createElement('div');
|
||||||
|
el.className = 'dg-toast dg-toast-' + type;
|
||||||
|
el.textContent = msg;
|
||||||
|
container.appendChild(el);
|
||||||
|
setTimeout(() => el.remove(), 4000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Helpers ─────────────────────────────────────────────
|
||||||
|
function escHtml(str) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.textContent = str;
|
||||||
|
return div.innerHTML;
|
||||||
|
}
|
||||||
|
|
||||||
|
function escAttr(str) {
|
||||||
|
return str.replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
||||||
|
}
|
||||||
|
|
||||||
|
return { init };
|
||||||
|
})();
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
DeckGenerator.init();
|
||||||
|
});
|
||||||
+232
-109
@@ -2088,7 +2088,7 @@ function autoResolveTriggerEffect(trigger) {
|
|||||||
|
|
||||||
// Helper: find the card on battlefield for counter placement
|
// Helper: find the card on battlefield for counter placement
|
||||||
function findCardOnBattlefield(cardName, ownerIdx) {
|
function findCardOnBattlefield(cardName, ownerIdx) {
|
||||||
var bf = (gameState.players[ownerIdx] || {}).battlefield || [];
|
var bf = gameState.battlefieldCards.filter(function(c) { return c.ownerIndex === ownerIdx; });
|
||||||
for (var i = 0; i < bf.length; i++) {
|
for (var i = 0; i < bf.length; i++) {
|
||||||
if (bf[i].name === cardName) return bf[i];
|
if (bf[i].name === cardName) return bf[i];
|
||||||
}
|
}
|
||||||
@@ -2233,7 +2233,7 @@ function autoResolveTriggerEffect(trigger) {
|
|||||||
var opps4 = getOpponents(oi);
|
var opps4 = getOpponents(oi);
|
||||||
var placed = false;
|
var placed = false;
|
||||||
for (var cmi = 0; cmi < opps4.length && !placed; cmi++) {
|
for (var cmi = 0; cmi < opps4.length && !placed; cmi++) {
|
||||||
var cmBf = (gameState.players[opps4[cmi]] || {}).battlefield || [];
|
var cmBf = gameState.battlefieldCards.filter(function(c) { return c.ownerIndex === opps4[cmi]; });
|
||||||
for (var cmj = 0; cmj < cmBf.length && !placed; cmj++) {
|
for (var cmj = 0; cmj < cmBf.length && !placed; cmj++) {
|
||||||
if (cmBf[cmj].type_line && cmBf[cmj].type_line.toLowerCase().indexOf('creature') !== -1) {
|
if (cmBf[cmj].type_line && cmBf[cmj].type_line.toLowerCase().indexOf('creature') !== -1) {
|
||||||
cmBf[cmj].counters = cmBf[cmj].counters || {};
|
cmBf[cmj].counters = cmBf[cmj].counters || {};
|
||||||
@@ -2250,7 +2250,7 @@ function autoResolveTriggerEffect(trigger) {
|
|||||||
|
|
||||||
case 'counter_all': {
|
case 'counter_all': {
|
||||||
// +1/+1 counter on each creature you control
|
// +1/+1 counter on each creature you control
|
||||||
var caBf = (gameState.players[oi] || {}).battlefield || [];
|
var caBf = gameState.battlefieldCards.filter(function(c) { return c.ownerIndex === oi; });
|
||||||
var caCount = 0;
|
var caCount = 0;
|
||||||
for (var cai = 0; cai < caBf.length; cai++) {
|
for (var cai = 0; cai < caBf.length; cai++) {
|
||||||
if (caBf[cai].type_line && caBf[cai].type_line.toLowerCase().indexOf('creature') !== -1) {
|
if (caBf[cai].type_line && caBf[cai].type_line.toLowerCase().indexOf('creature') !== -1) {
|
||||||
@@ -2362,7 +2362,7 @@ function autoResolveTriggerEffect(trigger) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case 'untap_all': {
|
case 'untap_all': {
|
||||||
var uaBf = (gameState.players[oi] || {}).battlefield || [];
|
var uaBf = gameState.battlefieldCards.filter(function(c) { return c.ownerIndex === oi; });
|
||||||
var uaCount = 0;
|
var uaCount = 0;
|
||||||
for (var uai = 0; uai < uaBf.length; uai++) {
|
for (var uai = 0; uai < uaBf.length; uai++) {
|
||||||
if (uaBf[uai].tapped) {
|
if (uaBf[uai].tapped) {
|
||||||
@@ -3247,19 +3247,15 @@ function addCardToBattlefield(name, imageUrl, typeLine, ownerIndex, x, y, oracle
|
|||||||
} else {
|
} else {
|
||||||
// Place cards by type in designated battlefield zones
|
// Place cards by type in designated battlefield zones
|
||||||
var zone = getCardTypeZone(typeLine || '');
|
var zone = getCardTypeZone(typeLine || '');
|
||||||
var cardW = 110, cardH = 150;
|
var cardW = 63, cardH = 84;
|
||||||
// Zone regions (percentage of section area)
|
// Zone regions (percentage of section area)
|
||||||
// Creatures: top center (30%-70% x, 5%-45% y)
|
// Offset from edges to avoid overlapping the overlaid zone-row/commander-row
|
||||||
// Artifacts/Enchantments: top right (70%-95% x, 5%-45% y)
|
|
||||||
// Lands: bottom left (5%-35% x, 55%-90% y)
|
|
||||||
// Instants/Sorceries: bottom center (35%-65% x, 55%-90% y)
|
|
||||||
// Other: center (30%-70% x, 25%-75% y)
|
|
||||||
var zones = {
|
var zones = {
|
||||||
creature: { x1: 0.25, x2: 0.65, y1: 0.05, y2: 0.42 },
|
creature: { x1: 0.25, x2: 0.70, y1: 0.20, y2: 0.55 },
|
||||||
artifact: { x1: 0.68, x2: 0.95, y1: 0.05, y2: 0.42 },
|
artifact: { x1: 0.70, x2: 0.95, y1: 0.20, y2: 0.55 },
|
||||||
land: { x1: 0.03, x2: 0.32, y1: 0.55, y2: 0.90 },
|
land: { x1: 0.03, x2: 0.35, y1: 0.55, y2: 0.85 },
|
||||||
spell: { x1: 0.35, x2: 0.65, y1: 0.55, y2: 0.90 },
|
spell: { x1: 0.35, x2: 0.65, y1: 0.55, y2: 0.85 },
|
||||||
other: { x1: 0.25, x2: 0.70, y1: 0.25, y2: 0.75 }
|
other: { x1: 0.25, x2: 0.70, y1: 0.30, y2: 0.70 }
|
||||||
};
|
};
|
||||||
var z = zones[zone] || zones.other;
|
var z = zones[zone] || zones.other;
|
||||||
var areaW = rect.width;
|
var areaW = rect.width;
|
||||||
@@ -5290,8 +5286,7 @@ function setLife(playerId, newLife) {
|
|||||||
eliminatePlayer(playerId, 'life total reached 0');
|
eliminatePlayer(playerId, 'life total reached 0');
|
||||||
}
|
}
|
||||||
checkStateBasedActions();
|
checkStateBasedActions();
|
||||||
// Sync enhanced player panels
|
if (typeof renderCmdDmgInline === 'function') renderCmdDmgInline();
|
||||||
if (typeof renderEnhancedPanels === 'function') renderEnhancedPanels();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -5326,8 +5321,7 @@ function adjustCommanderDamage(targetPlayerId, sourcePlayerId, amount) {
|
|||||||
panelContainer.replaceWith(newPanel);
|
panelContainer.replaceWith(newPanel);
|
||||||
}
|
}
|
||||||
checkStateBasedActions();
|
checkStateBasedActions();
|
||||||
// Sync enhanced player panels
|
if (typeof renderCmdDmgInline === 'function') renderCmdDmgInline();
|
||||||
if (typeof renderEnhancedPanels === 'function') renderEnhancedPanels();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -5360,8 +5354,6 @@ function adjustPoison(playerId, amount) {
|
|||||||
panelContainer.replaceWith(newPanel);
|
panelContainer.replaceWith(newPanel);
|
||||||
}
|
}
|
||||||
if (gameState.rulesEngine && gameState.rulesEngine.sbaChecks) checkStateBasedActions();
|
if (gameState.rulesEngine && gameState.rulesEngine.sbaChecks) checkStateBasedActions();
|
||||||
// Sync enhanced player panels
|
|
||||||
if (typeof renderEnhancedPanels === 'function') renderEnhancedPanels();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -8245,6 +8237,7 @@ function drawCard(playerId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const card = player.zones.library.shift(); // Take from top (index 0)
|
const card = player.zones.library.shift(); // Take from top (index 0)
|
||||||
|
if (typeof _enrichCardFromCache === 'function') _enrichCardFromCache(card);
|
||||||
player.zones.hand.push(card);
|
player.zones.hand.push(card);
|
||||||
addLogEntry('<strong>' + player.name + '</strong> drew a card (' + player.zones.hand.length + ' in hand)');
|
addLogEntry('<strong>' + player.name + '</strong> drew a card (' + player.zones.hand.length + ' in hand)');
|
||||||
updatePlayerZones();
|
updatePlayerZones();
|
||||||
@@ -10877,16 +10870,20 @@ function parseDeckList(text) {
|
|||||||
let line = raw.trim();
|
let line = raw.trim();
|
||||||
if (!line) continue;
|
if (!line) continue;
|
||||||
|
|
||||||
// Section headers
|
// Section headers — supports "// Commander", "Commander:", "[Commander]", "[Main]", "[metadata]"
|
||||||
if (/^\/\/\s*/i.test(line) || /^(COMMANDER|COMPANION|SIDEBOARD|MAYBEBOARD|MAINBOARD|DECK|LAND)[:\s]*$/i.test(line)) {
|
if (/^\/\/\s*/i.test(line) || /^\[?(COMMANDER|COMPANION|SIDEBOARD|MAYBEBOARD|MAINBOARD|MAIN|DECK|LAND|METADATA)\]?[:\s]*$/i.test(line)) {
|
||||||
const cleaned = line.replace(/^\/\/\s*/, '').replace(/:$/, '').trim().toLowerCase();
|
const cleaned = line.replace(/^\/\/\s*/, '').replace(/[\[\]:]/g, '').trim().toLowerCase();
|
||||||
if (/commander/i.test(cleaned)) currentSection = 'commander';
|
if (/commander/i.test(cleaned)) currentSection = 'commander';
|
||||||
else if (/companion/i.test(cleaned)) currentSection = 'companion';
|
else if (/companion/i.test(cleaned)) currentSection = 'companion';
|
||||||
else if (/sideboard/i.test(cleaned)) currentSection = 'sideboard';
|
else if (/sideboard/i.test(cleaned)) currentSection = 'sideboard';
|
||||||
|
else if (/metadata/i.test(cleaned)) currentSection = 'metadata';
|
||||||
else currentSection = 'mainboard';
|
else currentSection = 'mainboard';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip metadata lines like "Name=Elven Empire"
|
||||||
|
if (currentSection === 'metadata') continue;
|
||||||
|
|
||||||
// Skip comment lines
|
// Skip comment lines
|
||||||
if (/^#/.test(line) || /^\*/.test(line)) continue;
|
if (/^#/.test(line) || /^\*/.test(line)) continue;
|
||||||
|
|
||||||
@@ -12556,32 +12553,6 @@ function renderDeckSelectionGrid() {
|
|||||||
'<div style="color:var(--color-text-secondary);font-size:var(--text-sm);">AI Deck (auto-generated)</div>';
|
'<div style="color:var(--color-text-secondary);font-size:var(--text-sm);">AI Deck (auto-generated)</div>';
|
||||||
} else {
|
} else {
|
||||||
var idx = i;
|
var idx = i;
|
||||||
// Build saved decks options
|
|
||||||
var savedDecks = (typeof getDecks === 'function') ? getDecks() : [];
|
|
||||||
var savedDecksHtml = '';
|
|
||||||
if (savedDecks.length === 0) {
|
|
||||||
savedDecksHtml = '<div style="color:var(--color-text-faint);font-size:var(--text-sm);padding:var(--space-3);">No saved decks. Build one in Deck Builder first.</div>';
|
|
||||||
} else {
|
|
||||||
savedDecksHtml = '<div class="setup-custom-deck-list" id="setup-custom-deck-list-' + idx + '">';
|
|
||||||
for (var di = 0; di < savedDecks.length; di++) {
|
|
||||||
var sd = savedDecks[di];
|
|
||||||
var cardCount = 0;
|
|
||||||
if (sd.cards) sd.cards.forEach(function(c) { cardCount += (c.quantity || 1); });
|
|
||||||
if (sd.commander) cardCount++;
|
|
||||||
var deckColors = '';
|
|
||||||
if (sd.commander && sd.commander.colorIdentity && sd.commander.colorIdentity.length) {
|
|
||||||
var manaSymbols = { W: '\u2600', U: '\uD83D\uDCA7', B: '\uD83D\uDC80', R: '\uD83D\uDD25', G: '\uD83C\uDF33' };
|
|
||||||
sd.commander.colorIdentity.forEach(function(c) { deckColors += (manaSymbols[c] || c) + ' '; });
|
|
||||||
}
|
|
||||||
savedDecksHtml += '<button class="setup-custom-deck-btn" id="setup-custom-deck-btn-' + idx + '-' + di + '"' +
|
|
||||||
' onclick="selectSetupCustomDeck(' + idx + ',\'' + sd.id.replace(/'/g, "\\'") + '\')">' +
|
|
||||||
'<span class="setup-custom-deck-name">' + (sd.name || 'Untitled').replace(/</g, '<') + '</span>' +
|
|
||||||
'<span class="setup-custom-deck-meta">' + deckColors + cardCount + ' cards</span>' +
|
|
||||||
'</button>';
|
|
||||||
}
|
|
||||||
savedDecksHtml += '</div>';
|
|
||||||
}
|
|
||||||
|
|
||||||
panel.innerHTML =
|
panel.innerHTML =
|
||||||
'<div class="deck-select-panel-header">' +
|
'<div class="deck-select-panel-header">' +
|
||||||
'<div class="player-color-dot" style="background:' + color + '"></div>' +
|
'<div class="player-color-dot" style="background:' + color + '"></div>' +
|
||||||
@@ -12591,7 +12562,6 @@ function renderDeckSelectionGrid() {
|
|||||||
'<div class="deck-select-tabs">' +
|
'<div class="deck-select-tabs">' +
|
||||||
'<button class="deck-select-tab active" id="setup-deck-tab-precon-' + idx + '" onclick="switchSetupDeckTab(' + idx + ',\'precon\')">Precon</button>' +
|
'<button class="deck-select-tab active" id="setup-deck-tab-precon-' + idx + '" onclick="switchSetupDeckTab(' + idx + ',\'precon\')">Precon</button>' +
|
||||||
'<button class="deck-select-tab" id="setup-deck-tab-mydecks-' + idx + '" onclick="switchSetupDeckTab(' + idx + ',\'mydecks\')">My Decks</button>' +
|
'<button class="deck-select-tab" id="setup-deck-tab-mydecks-' + idx + '" onclick="switchSetupDeckTab(' + idx + ',\'mydecks\')">My Decks</button>' +
|
||||||
'<button class="deck-select-tab" id="setup-deck-tab-import-' + idx + '" onclick="switchSetupDeckTab(' + idx + ',\'import\')">Import</button>' +
|
|
||||||
'</div>' +
|
'</div>' +
|
||||||
'<div class="deck-select-tab-content active" id="setup-deck-content-precon-' + idx + '">' +
|
'<div class="deck-select-tab-content active" id="setup-deck-content-precon-' + idx + '">' +
|
||||||
'<select class="deck-select-precon-select" id="setup-deck-precon-select-' + idx + '" onchange="selectSetupPrecon(' + idx + ')">' +
|
'<select class="deck-select-precon-select" id="setup-deck-precon-select-' + idx + '" onchange="selectSetupPrecon(' + idx + ')">' +
|
||||||
@@ -12599,20 +12569,95 @@ function renderDeckSelectionGrid() {
|
|||||||
'</select>' +
|
'</select>' +
|
||||||
'</div>' +
|
'</div>' +
|
||||||
'<div class="deck-select-tab-content" id="setup-deck-content-mydecks-' + idx + '">' +
|
'<div class="deck-select-tab-content" id="setup-deck-content-mydecks-' + idx + '">' +
|
||||||
savedDecksHtml +
|
'<div class="setup-custom-deck-list" id="setup-custom-deck-list-' + idx + '">' +
|
||||||
'</div>' +
|
'<div style="color:var(--color-text-faint);font-size:var(--text-sm);padding:var(--space-3);">Loading user decks...</div>' +
|
||||||
'<div class="deck-select-tab-content" id="setup-deck-content-import-' + idx + '">' +
|
'</div>' +
|
||||||
'<textarea class="deck-select-import-text" id="setup-deck-import-text-' + idx + '" placeholder="Paste deck list here... // Commander 1 Atraxa, Praetors\' Voice // Deck 1 Sol Ring ..."></textarea>' +
|
|
||||||
'<button class="deck-select-import-btn" onclick="importSetupDeck(' + idx + ')">\uD83D\uDCE5 Validate & Use This List</button>' +
|
|
||||||
'</div>';
|
'</div>';
|
||||||
}
|
}
|
||||||
|
|
||||||
grid.appendChild(panel);
|
grid.appendChild(panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch user-created decks from the API and populate My Decks tabs
|
||||||
|
_loadUserDecksForSetup();
|
||||||
|
}
|
||||||
|
|
||||||
|
function _loadUserDecksForSetup() {
|
||||||
|
var apiBase = (typeof LAB_API_BASE !== 'undefined' && LAB_API_BASE) ? LAB_API_BASE : '';
|
||||||
|
fetch(apiBase + '/api/lab/decks')
|
||||||
|
.then(function(res) { return res.json(); })
|
||||||
|
.then(function(data) {
|
||||||
|
var decks = (data.decks || []).filter(function(d) { return d.source === 'deckbuilder'; });
|
||||||
|
for (var i = 0; i < setupPlayerCount; i++) {
|
||||||
|
if (setupAiPlayers.has(i)) continue;
|
||||||
|
var listEl = document.getElementById('setup-custom-deck-list-' + i);
|
||||||
|
if (!listEl) continue;
|
||||||
|
|
||||||
|
if (decks.length === 0) {
|
||||||
|
listEl.innerHTML = '<div style="color:var(--color-text-faint);font-size:var(--text-sm);padding:var(--space-3);">No saved decks. Build one in Deck Builder first.</div>';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var html = '';
|
||||||
|
for (var di = 0; di < decks.length; di++) {
|
||||||
|
var d = decks[di];
|
||||||
|
var cardCount = d.totalCards || d.card_count || d.total_cards || 0;
|
||||||
|
var cmdr = d.commander || '';
|
||||||
|
html += '<button class="setup-custom-deck-btn" id="setup-custom-deck-btn-' + i + '-' + di + '"' +
|
||||||
|
' onclick="selectSetupUserDeck(' + i + ',' + d.deck_id + ',\'' + (d.name || 'Untitled').replace(/'/g, "\\'") + '\')">' +
|
||||||
|
'<span class="setup-custom-deck-name">' + (d.name || 'Untitled').replace(/</g, '<') + '</span>' +
|
||||||
|
'<span class="setup-custom-deck-meta">' + (cmdr ? cmdr + ' \u2014 ' : '') + cardCount + ' cards</span>' +
|
||||||
|
'</button>';
|
||||||
|
}
|
||||||
|
listEl.innerHTML = html;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(function(e) {
|
||||||
|
console.warn('[Setup] Could not load user decks:', e);
|
||||||
|
for (var i = 0; i < setupPlayerCount; i++) {
|
||||||
|
if (setupAiPlayers.has(i)) continue;
|
||||||
|
var listEl = document.getElementById('setup-custom-deck-list-' + i);
|
||||||
|
if (listEl) listEl.innerHTML = '<div style="color:var(--color-text-faint);font-size:var(--text-sm);padding:var(--space-3);">Could not load decks. Is the server running?</div>';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectSetupUserDeck(playerIdx, deckId, deckName) {
|
||||||
|
var statusEl = document.getElementById('setup-deck-status-' + playerIdx);
|
||||||
|
|
||||||
|
// Highlight selected button
|
||||||
|
var listEl = document.getElementById('setup-custom-deck-list-' + playerIdx);
|
||||||
|
if (listEl) {
|
||||||
|
listEl.querySelectorAll('.setup-custom-deck-btn').forEach(function(b) { b.classList.remove('selected'); });
|
||||||
|
}
|
||||||
|
// Find clicked button and highlight it
|
||||||
|
var allBtns = listEl ? listEl.querySelectorAll('.setup-custom-deck-btn') : [];
|
||||||
|
allBtns.forEach(function(b) {
|
||||||
|
if (b.textContent.indexOf(deckName) !== -1) b.classList.add('selected');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Export the deck to a .dck file, then store it as a precon-style choice
|
||||||
|
var apiBase = (typeof LAB_API_BASE !== 'undefined' && LAB_API_BASE) ? LAB_API_BASE : '';
|
||||||
|
if (statusEl) { statusEl.textContent = 'Exporting...'; statusEl.className = 'deck-select-status'; }
|
||||||
|
|
||||||
|
fetch(apiBase + '/api/decks/' + deckId + '/export-to-sim', { method: 'POST' })
|
||||||
|
.then(function(res) { return res.json(); })
|
||||||
|
.then(function(result) {
|
||||||
|
if (result.success) {
|
||||||
|
setupDeckChoices[playerIdx] = { type: 'user-deck', deckName: result.deckName, fileName: result.deckName + '.dck' };
|
||||||
|
if (statusEl) { statusEl.textContent = deckName + ' (' + (result.totalCards || '?') + ' cards)'; statusEl.className = 'deck-select-status loaded'; }
|
||||||
|
} else {
|
||||||
|
if (statusEl) { statusEl.textContent = 'Export failed'; statusEl.className = 'deck-select-status'; }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(function(e) {
|
||||||
|
console.error('[Setup] Export failed:', e);
|
||||||
|
if (statusEl) { statusEl.textContent = 'Export error'; statusEl.className = 'deck-select-status'; }
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function switchSetupDeckTab(playerIdx, tab) {
|
function switchSetupDeckTab(playerIdx, tab) {
|
||||||
var tabs = ['precon', 'mydecks', 'import'];
|
var tabs = ['precon', 'mydecks'];
|
||||||
tabs.forEach(function(t) {
|
tabs.forEach(function(t) {
|
||||||
var tabBtn = document.getElementById('setup-deck-tab-' + t + '-' + playerIdx);
|
var tabBtn = document.getElementById('setup-deck-tab-' + t + '-' + playerIdx);
|
||||||
var content = document.getElementById('setup-deck-content-' + t + '-' + playerIdx);
|
var content = document.getElementById('setup-deck-content-' + t + '-' + playerIdx);
|
||||||
@@ -12641,51 +12686,6 @@ function selectSetupPrecon(playerIdx) {
|
|||||||
if (statusEl) { statusEl.textContent = 'Precon: ' + preconName; statusEl.className = 'deck-select-status loaded'; }
|
if (statusEl) { statusEl.textContent = 'Precon: ' + preconName; statusEl.className = 'deck-select-status loaded'; }
|
||||||
}
|
}
|
||||||
|
|
||||||
function importSetupDeck(playerIdx) {
|
|
||||||
var textarea = document.getElementById('setup-deck-import-text-' + playerIdx);
|
|
||||||
var statusEl = document.getElementById('setup-deck-status-' + playerIdx);
|
|
||||||
if (!textarea) return;
|
|
||||||
var text = textarea.value.trim();
|
|
||||||
if (!text) {
|
|
||||||
if (statusEl) { statusEl.textContent = 'Paste a deck list first.'; statusEl.className = 'deck-select-status'; }
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var parsed = parseDeckList(text);
|
|
||||||
if (!parsed.length) {
|
|
||||||
if (statusEl) { statusEl.textContent = 'Could not parse cards from list.'; statusEl.className = 'deck-select-status'; }
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var total = parsed.reduce(function(sum, e) { return sum + e.qty; }, 0);
|
|
||||||
setupDeckChoices[playerIdx] = { type: 'import', text: text };
|
|
||||||
if (statusEl) { statusEl.textContent = 'Deck ready (' + total + ' cards)'; statusEl.className = 'deck-select-status loaded'; }
|
|
||||||
}
|
|
||||||
|
|
||||||
function selectSetupCustomDeck(playerIdx, deckId) {
|
|
||||||
var decks = (typeof getDecks === 'function') ? getDecks() : [];
|
|
||||||
var deck = decks.find(function(d) { return d.id === deckId; });
|
|
||||||
if (!deck) return;
|
|
||||||
var statusEl = document.getElementById('setup-deck-status-' + playerIdx);
|
|
||||||
|
|
||||||
// Highlight selected button, unhighlight others
|
|
||||||
var listEl = document.getElementById('setup-custom-deck-list-' + playerIdx);
|
|
||||||
if (listEl) {
|
|
||||||
listEl.querySelectorAll('.setup-custom-deck-btn').forEach(function(b) { b.classList.remove('selected'); });
|
|
||||||
}
|
|
||||||
var btns = document.querySelectorAll('[id^="setup-custom-deck-btn-' + playerIdx + '-"]');
|
|
||||||
btns.forEach(function(b) { b.classList.remove('selected'); });
|
|
||||||
// Find the right button by matching deckId in onclick
|
|
||||||
btns.forEach(function(b) {
|
|
||||||
if (b.getAttribute('onclick') && b.getAttribute('onclick').indexOf(deckId) !== -1) {
|
|
||||||
b.classList.add('selected');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
setupDeckChoices[playerIdx] = { type: 'custom', deckId: deckId };
|
|
||||||
var cardCount = 0;
|
|
||||||
if (deck.cards) deck.cards.forEach(function(c) { cardCount += (c.quantity || 1); });
|
|
||||||
if (deck.commander) cardCount++;
|
|
||||||
if (statusEl) { statusEl.textContent = deck.name + ' (' + cardCount + ' cards)'; statusEl.className = 'deck-select-status loaded'; }
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadCustomDeckForPlayer(playerIdx, deckId) {
|
async function loadCustomDeckForPlayer(playerIdx, deckId) {
|
||||||
var decks = (typeof getDecks === 'function') ? getDecks() : [];
|
var decks = (typeof getDecks === 'function') ? getDecks() : [];
|
||||||
@@ -12719,7 +12719,31 @@ async function loadPreconForPlayer(playerIdx, preconId) {
|
|||||||
var player = gameState.players[playerIdx];
|
var player = gameState.players[playerIdx];
|
||||||
if (!player) return;
|
if (!player) return;
|
||||||
|
|
||||||
var parsedCards = parseDeckList(precon.decklist);
|
// Try to get decklist text: embedded first, then fetch .dck from server
|
||||||
|
var decklistText = precon.decklist || '';
|
||||||
|
if (!decklistText && precon.fileName) {
|
||||||
|
// 1. Try the API endpoint (returns JSON with decklist field)
|
||||||
|
try {
|
||||||
|
var apiResp = await fetch('/api/lab/precons/deck?fileName=' + encodeURIComponent(precon.fileName));
|
||||||
|
if (apiResp.ok) {
|
||||||
|
var deckData = await apiResp.json();
|
||||||
|
if (deckData && deckData.decklist) decklistText = deckData.decklist;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[loadPreconForPlayer] API deck fetch failed:', e);
|
||||||
|
}
|
||||||
|
// 2. Fallback: fetch .dck file directly from static mount
|
||||||
|
if (!decklistText) {
|
||||||
|
try {
|
||||||
|
var staticResp = await fetch('/precon-decks/' + encodeURIComponent(precon.fileName));
|
||||||
|
if (staticResp.ok) decklistText = await staticResp.text();
|
||||||
|
} catch (e2) {
|
||||||
|
console.warn('[loadPreconForPlayer] Static .dck fetch failed:', precon.fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsedCards = parseDeckList(decklistText);
|
||||||
if (!parsedCards.length) return;
|
if (!parsedCards.length) return;
|
||||||
|
|
||||||
var allCardNames = [];
|
var allCardNames = [];
|
||||||
@@ -12755,6 +12779,9 @@ async function loadPreconForPlayer(playerIdx, preconId) {
|
|||||||
if (commandCards.length > 0) {
|
if (commandCards.length > 0) {
|
||||||
player.commanderCardName = commandCards[0].name || '';
|
player.commanderCardName = commandCards[0].name || '';
|
||||||
}
|
}
|
||||||
|
if (precon.fileName) {
|
||||||
|
player.deckFileName = precon.fileName;
|
||||||
|
}
|
||||||
|
|
||||||
// Shuffle library
|
// Shuffle library
|
||||||
for (var si = libraryCards.length - 1; si > 0; si--) {
|
for (var si = libraryCards.length - 1; si > 0; si--) {
|
||||||
@@ -12773,6 +12800,28 @@ async function loadPreconForPlayer(playerIdx, preconId) {
|
|||||||
addLogEntry('\uD83D\uDCCB <strong>' + player.name + '</strong> loaded precon "' + precon.name + '" (' + (libraryCards.length + commandCards.length) + ' cards)');
|
addLogEntry('\uD83D\uDCCB <strong>' + player.name + '</strong> loaded precon "' + precon.name + '" (' + (libraryCards.length + commandCards.length) + ' cards)');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function loadUserDeckForPlayer(playerIdx, deckName) {
|
||||||
|
var player = gameState.players[playerIdx];
|
||||||
|
if (!player || !deckName) return;
|
||||||
|
|
||||||
|
// Fetch the .dck file content from user-decks/ via static mount or API
|
||||||
|
var apiBase = (typeof LAB_API_BASE !== 'undefined' && LAB_API_BASE) ? LAB_API_BASE : '';
|
||||||
|
var deckText = '';
|
||||||
|
try {
|
||||||
|
var resp = await fetch(apiBase + '/user-decks/' + encodeURIComponent(deckName) + '.dck');
|
||||||
|
if (resp.ok) deckText = await resp.text();
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[loadUserDeckForPlayer] Could not fetch .dck file:', e);
|
||||||
|
}
|
||||||
|
if (!deckText) {
|
||||||
|
addLogEntry('\u26A0 Could not load user deck "' + deckName + '"');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await importDeckForPlayer(playerIdx, deckText);
|
||||||
|
player.deckFileName = deckName + '.dck';
|
||||||
|
}
|
||||||
|
|
||||||
async function importDeckForPlayer(playerIdx, deckText) {
|
async function importDeckForPlayer(playerIdx, deckText) {
|
||||||
var player = gameState.players[playerIdx];
|
var player = gameState.players[playerIdx];
|
||||||
if (!player || !deckText.trim()) return;
|
if (!player || !deckText.trim()) return;
|
||||||
@@ -12872,8 +12921,8 @@ async function startGameWithDecks() {
|
|||||||
await loadPreconForPlayer(hd.idx, hd.choice.preconId);
|
await loadPreconForPlayer(hd.idx, hd.choice.preconId);
|
||||||
} else if (hd.choice.type === 'custom') {
|
} else if (hd.choice.type === 'custom') {
|
||||||
await loadCustomDeckForPlayer(hd.idx, hd.choice.deckId);
|
await loadCustomDeckForPlayer(hd.idx, hd.choice.deckId);
|
||||||
} else if (hd.choice.type === 'import') {
|
} else if (hd.choice.type === 'user-deck') {
|
||||||
await importDeckForPlayer(hd.idx, hd.choice.text);
|
await loadUserDeckForPlayer(hd.idx, hd.choice.deckName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -12996,6 +13045,8 @@ function startGame() {
|
|||||||
renderHandArea();
|
renderHandArea();
|
||||||
renderPhaseTracker();
|
renderPhaseTracker();
|
||||||
updateTurnDisplay();
|
updateTurnDisplay();
|
||||||
|
if (typeof renderCmdDmgInline === 'function') renderCmdDmgInline();
|
||||||
|
if (typeof refreshLeftPanelTriggers === 'function') refreshLeftPanelTriggers();
|
||||||
|
|
||||||
// Set default search focus
|
// Set default search focus
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -16201,6 +16252,72 @@ function aiPickMultiTargetAttacks(playerIdx, creatures, difficulty) {
|
|||||||
return assignments;
|
return assignments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- AI Helper: Declare attacks for an AI player (used by ai-bridge) ----
|
||||||
|
function aiDeclareAttacks(playerIdx) {
|
||||||
|
if (!gameState) return;
|
||||||
|
var player = gameState.players[playerIdx];
|
||||||
|
if (!player || player.eliminated) return;
|
||||||
|
var difficulty = gameState.aiDifficulty || 'easy';
|
||||||
|
|
||||||
|
var creatures = aiGetUntappedCreatures(playerIdx).filter(function(c) {
|
||||||
|
return !hasSummoningSickness(c);
|
||||||
|
});
|
||||||
|
if (creatures.length === 0) return;
|
||||||
|
|
||||||
|
var attackerCards = [];
|
||||||
|
|
||||||
|
if (difficulty === 'easy') {
|
||||||
|
attackerCards = creatures.filter(function() { return Math.random() > 0.5; });
|
||||||
|
if (attackerCards.length === 0 && creatures.length > 0) attackerCards = [creatures[0]];
|
||||||
|
} else {
|
||||||
|
var primaryTarget = aiPickAttackTarget(playerIdx);
|
||||||
|
if (primaryTarget === -1) return;
|
||||||
|
|
||||||
|
creatures.forEach(function(c) {
|
||||||
|
var eval_ = aiEvaluateAttack(c, primaryTarget);
|
||||||
|
if (difficulty === 'normal') {
|
||||||
|
if (eval_.shouldAttack || eval_.risk === 'trade') attackerCards.push(c);
|
||||||
|
} else {
|
||||||
|
if (eval_.shouldAttack) attackerCards.push(c);
|
||||||
|
else if (creatures.length >= 4 && eval_.risk === 'bad-trade') attackerCards.push(c);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (difficulty === 'hard' && attackerCards.length > 2) {
|
||||||
|
var sortedByToughness = attackerCards.slice().sort(function(a, b) {
|
||||||
|
return getCreatureToughness(b) - getCreatureToughness(a);
|
||||||
|
});
|
||||||
|
var holdBack = sortedByToughness.find(function(c) {
|
||||||
|
return !hasKeyword(c, 'vigilance') && getCreatureToughness(c) >= 3;
|
||||||
|
});
|
||||||
|
if (holdBack) {
|
||||||
|
attackerCards = attackerCards.filter(function(c) { return c.id !== holdBack.id; });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attackerCards.length === 0) return;
|
||||||
|
|
||||||
|
var assignments = aiPickMultiTargetAttacks(playerIdx, attackerCards, difficulty);
|
||||||
|
if (assignments.length === 0) return;
|
||||||
|
|
||||||
|
gameState.combat.attackers = assignments.map(function(a) {
|
||||||
|
return { cardId: a.card.id, targetPlayerId: a.targetIdx };
|
||||||
|
});
|
||||||
|
|
||||||
|
attackerCards.forEach(function(c) {
|
||||||
|
if (!hasKeyword(c, 'vigilance')) {
|
||||||
|
c.tapped = true;
|
||||||
|
var el = document.getElementById('bc-' + c.id);
|
||||||
|
if (el) el.classList.add('tapped');
|
||||||
|
}
|
||||||
|
var el2 = document.getElementById('bc-' + c.id);
|
||||||
|
if (el2) el2.classList.add('attacking');
|
||||||
|
});
|
||||||
|
|
||||||
|
fireBattlefieldTriggers('attack');
|
||||||
|
}
|
||||||
|
|
||||||
// ---- AI Helper: Check if AI has a counterspell and should use it ----
|
// ---- AI Helper: Check if AI has a counterspell and should use it ----
|
||||||
function aiCheckForCounterspell(playerIdx) {
|
function aiCheckForCounterspell(playerIdx) {
|
||||||
if (!gameState) return null;
|
if (!gameState) return null;
|
||||||
@@ -16569,7 +16686,7 @@ function _aiRunPostCombat(playerIdx, player, difficulty, heldCards) {
|
|||||||
var postDelay = 600;
|
var postDelay = 600;
|
||||||
|
|
||||||
// MAIN PHASE 2 (same logic as inline version)
|
// MAIN PHASE 2 (same logic as inline version)
|
||||||
if (difficulty !== 'easy') {
|
{
|
||||||
postActions.push({ delay: postDelay, fn: function() {
|
postActions.push({ delay: postDelay, fn: function() {
|
||||||
var bluffReserve = 0;
|
var bluffReserve = 0;
|
||||||
var bluffCardName = '';
|
var bluffCardName = '';
|
||||||
@@ -17181,12 +17298,12 @@ function _runLocalAiTurn() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
// MAIN PHASE 2: Post-combat spell casting (normal & hard)
|
// MAIN PHASE 2: Post-combat spell casting
|
||||||
// Hard: play held-back removal + instants + any remaining affordable spells
|
// Hard: play held-back removal + instants + any remaining affordable spells
|
||||||
// Normal: try to play any remaining affordable spells
|
// Normal/Easy: try to play any remaining affordable spells
|
||||||
// BLUFFING: On Normal/Hard, hold back mana if we have instant-speed answers
|
// BLUFFING: On Normal/Hard, hold back mana if we have instant-speed answers
|
||||||
// ========================================
|
// ========================================
|
||||||
if (difficulty !== 'easy') {
|
{
|
||||||
actions.push({ delay: delay, fn: function() {
|
actions.push({ delay: delay, fn: function() {
|
||||||
if (_aiBlockerPauseState) return; // Skip — human blocker flow handles this
|
if (_aiBlockerPauseState) return; // Skip — human blocker flow handles this
|
||||||
var bluffReserve = 0;
|
var bluffReserve = 0;
|
||||||
@@ -25610,3 +25727,9 @@ function _safeRender(fn, ...args) {
|
|||||||
try { window[fn](...args); } catch(e) {}
|
try { window[fn](...args); } catch(e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expose functions needed by ai-bridge.js
|
||||||
|
window.aiTryCastCommander = aiTryCastCommander;
|
||||||
|
window.aiDeclareAttacks = aiDeclareAttacks;
|
||||||
|
window.playCardFromHand = playCardFromHand;
|
||||||
|
window.aiCountUntappedLands = aiCountUntappedLands;
|
||||||
@@ -0,0 +1,955 @@
|
|||||||
|
/* ═══════════════════════════════════════════════════════════
|
||||||
|
Commander AI Lab — Global Design System
|
||||||
|
Dark & Polished · Premium Dashboard Aesthetic
|
||||||
|
═══════════════════════════════════════════════════════════ */
|
||||||
|
|
||||||
|
/* ── Reset ──────────────────────────────────────────────── */
|
||||||
|
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
|
|
||||||
|
/* ── Design Tokens ──────────────────────────────────────── */
|
||||||
|
:root {
|
||||||
|
/* Surfaces — layered dark with subtle warmth */
|
||||||
|
--bg: #0c0e14;
|
||||||
|
--surface-0: #12141c;
|
||||||
|
--surface-1: #171a24;
|
||||||
|
--surface-2: #1c1f2b;
|
||||||
|
--surface-3: #222533;
|
||||||
|
--surface-hover: #252839;
|
||||||
|
|
||||||
|
/* Borders */
|
||||||
|
--border: #2a2d3a;
|
||||||
|
--border-subtle: #1e2130;
|
||||||
|
--border-focus: #4a6fa5;
|
||||||
|
|
||||||
|
/* Text */
|
||||||
|
--text: #e1e4eb;
|
||||||
|
--text-secondary:#8b90a0;
|
||||||
|
--text-tertiary: #5c6070;
|
||||||
|
--text-inverse: #0c0e14;
|
||||||
|
|
||||||
|
/* Accent — cool blue with subtle depth */
|
||||||
|
--accent: #5b9ef0;
|
||||||
|
--accent-hover: #7ab4ff;
|
||||||
|
--accent-dim: rgba(91, 158, 240, 0.12);
|
||||||
|
--accent-glow: rgba(91, 158, 240, 0.25);
|
||||||
|
|
||||||
|
/* Semantic */
|
||||||
|
--success: #4ade80;
|
||||||
|
--success-dim: rgba(74, 222, 128, 0.12);
|
||||||
|
--warning: #fbbf24;
|
||||||
|
--warning-dim: rgba(251, 191, 36, 0.12);
|
||||||
|
--error: #f87171;
|
||||||
|
--error-dim: rgba(248, 113, 113, 0.12);
|
||||||
|
--purple: #a78bfa;
|
||||||
|
--purple-dim: rgba(167, 139, 250, 0.12);
|
||||||
|
--gold: #fcd34d;
|
||||||
|
|
||||||
|
/* Radius */
|
||||||
|
--radius-sm: 6px;
|
||||||
|
--radius-md: 10px;
|
||||||
|
--radius-lg: 14px;
|
||||||
|
--radius-xl: 20px;
|
||||||
|
|
||||||
|
/* Shadows */
|
||||||
|
--shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
|
||||||
|
--shadow-md: 0 4px 16px rgba(0,0,0,0.35);
|
||||||
|
--shadow-lg: 0 12px 40px rgba(0,0,0,0.45);
|
||||||
|
--shadow-glow: 0 0 20px var(--accent-dim);
|
||||||
|
|
||||||
|
/* Transitions */
|
||||||
|
--ease: cubic-bezier(0.16, 1, 0.3, 1);
|
||||||
|
--duration: 180ms;
|
||||||
|
|
||||||
|
/* Typography */
|
||||||
|
--font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
--font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
|
||||||
|
|
||||||
|
/* Spacing scale */
|
||||||
|
--sp-1: 4px; --sp-2: 8px; --sp-3: 12px;
|
||||||
|
--sp-4: 16px; --sp-5: 20px; --sp-6: 24px;
|
||||||
|
--sp-8: 32px; --sp-10: 40px; --sp-12: 48px;
|
||||||
|
--sp-16: 64px;
|
||||||
|
|
||||||
|
/* Layout */
|
||||||
|
--content-max: 1280px;
|
||||||
|
--sidebar-width: 280px;
|
||||||
|
--nav-height: 56px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Base ───────────────────────────────────────────────── */
|
||||||
|
html {
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
text-rendering: optimizeLegibility;
|
||||||
|
scroll-behavior: smooth;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
min-height: 100vh;
|
||||||
|
font-family: var(--font);
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.6;
|
||||||
|
color: var(--text);
|
||||||
|
background: var(--bg);
|
||||||
|
}
|
||||||
|
|
||||||
|
h1, h2, h3, h4, h5, h6 {
|
||||||
|
color: #fff;
|
||||||
|
font-weight: 600;
|
||||||
|
line-height: 1.25;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 { font-size: 24px; }
|
||||||
|
h2 { font-size: 18px; }
|
||||||
|
h3 { font-size: 16px; }
|
||||||
|
|
||||||
|
a { color: var(--accent); text-decoration: none; transition: color var(--duration) var(--ease); }
|
||||||
|
a:hover { color: var(--accent-hover); }
|
||||||
|
|
||||||
|
img, svg { display: block; max-width: 100%; }
|
||||||
|
button { cursor: pointer; font: inherit; }
|
||||||
|
input, select, textarea { font: inherit; color: inherit; }
|
||||||
|
table { border-collapse: collapse; width: 100%; }
|
||||||
|
|
||||||
|
::selection {
|
||||||
|
background: var(--accent-dim);
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
:focus-visible {
|
||||||
|
outline: 2px solid var(--accent);
|
||||||
|
outline-offset: 2px;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Scrollbar ──────────────────────────────────────────── */
|
||||||
|
::-webkit-scrollbar { width: 8px; height: 8px; }
|
||||||
|
::-webkit-scrollbar-track { background: transparent; }
|
||||||
|
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
|
||||||
|
::-webkit-scrollbar-thumb:hover { background: var(--text-tertiary); }
|
||||||
|
|
||||||
|
/* ── Navigation ─────────────────────────────────────────── */
|
||||||
|
.nav {
|
||||||
|
position: sticky;
|
||||||
|
top: 0;
|
||||||
|
z-index: 100;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
height: var(--nav-height);
|
||||||
|
padding: 0 var(--sp-6);
|
||||||
|
background: rgba(12, 14, 20, 0.85);
|
||||||
|
backdrop-filter: blur(16px) saturate(1.5);
|
||||||
|
-webkit-backdrop-filter: blur(16px) saturate(1.5);
|
||||||
|
border-bottom: 1px solid var(--border-subtle);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-brand {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--sp-3);
|
||||||
|
font-size: 15px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #fff;
|
||||||
|
letter-spacing: -0.01em;
|
||||||
|
text-decoration: none;
|
||||||
|
white-space: nowrap;
|
||||||
|
margin-right: var(--sp-8);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-brand svg {
|
||||||
|
width: 28px;
|
||||||
|
height: 28px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-links {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--sp-1);
|
||||||
|
overflow-x: auto;
|
||||||
|
scrollbar-width: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-links::-webkit-scrollbar { display: none; }
|
||||||
|
|
||||||
|
.nav-links a {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--sp-2);
|
||||||
|
padding: 6px 12px;
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
white-space: nowrap;
|
||||||
|
transition: all var(--duration) var(--ease);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-links a:hover {
|
||||||
|
color: var(--text);
|
||||||
|
background: var(--surface-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-links a.active {
|
||||||
|
color: #fff;
|
||||||
|
background: var(--accent-dim);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-links a .nav-icon {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-links a.active .nav-icon,
|
||||||
|
.nav-links a:hover .nav-icon {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Page Layout ────────────────────────────────────────── */
|
||||||
|
.page {
|
||||||
|
max-width: var(--content-max);
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: var(--sp-8) var(--sp-6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header {
|
||||||
|
margin-bottom: var(--sp-8);
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header h1 {
|
||||||
|
font-size: 28px;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: -0.02em;
|
||||||
|
margin-bottom: var(--sp-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header p {
|
||||||
|
color: var(--text-secondary);
|
||||||
|
font-size: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Cards / Panels ─────────────────────────────────────── */
|
||||||
|
.panel {
|
||||||
|
background: var(--surface-1);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius-lg);
|
||||||
|
padding: var(--sp-6);
|
||||||
|
margin-bottom: var(--sp-6);
|
||||||
|
transition: border-color var(--duration) var(--ease);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel:hover {
|
||||||
|
border-color: var(--border-focus);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
margin-bottom: var(--sp-5);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-header h2 {
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-header .badge {
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
padding: 3px 10px;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.05em;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Stat Cards ─────────────────────────────────────────── */
|
||||||
|
.stat-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||||
|
gap: var(--sp-4);
|
||||||
|
margin-bottom: var(--sp-6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-card {
|
||||||
|
background: var(--surface-1);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius-md);
|
||||||
|
padding: var(--sp-5);
|
||||||
|
transition: all var(--duration) var(--ease);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-card:hover {
|
||||||
|
border-color: var(--border-focus);
|
||||||
|
transform: translateY(-1px);
|
||||||
|
box-shadow: var(--shadow-md);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-label {
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.06em;
|
||||||
|
margin-bottom: var(--sp-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-value {
|
||||||
|
font-size: 28px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #fff;
|
||||||
|
line-height: 1.1;
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-sub {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-tertiary);
|
||||||
|
margin-top: var(--sp-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Buttons ────────────────────────────────────────────── */
|
||||||
|
.btn {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: var(--sp-2);
|
||||||
|
padding: 8px 18px;
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all var(--duration) var(--ease);
|
||||||
|
white-space: nowrap;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
background: var(--accent);
|
||||||
|
color: var(--text-inverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:hover {
|
||||||
|
background: var(--accent-hover);
|
||||||
|
box-shadow: 0 4px 14px var(--accent-glow);
|
||||||
|
transform: translateY(-1px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:active {
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:disabled {
|
||||||
|
opacity: 0.4;
|
||||||
|
cursor: not-allowed;
|
||||||
|
transform: none;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary {
|
||||||
|
background: var(--surface-3);
|
||||||
|
color: var(--text);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary:hover {
|
||||||
|
background: var(--surface-hover);
|
||||||
|
border-color: var(--text-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-ghost {
|
||||||
|
background: transparent;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
padding: 6px 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-ghost:hover {
|
||||||
|
color: var(--text);
|
||||||
|
background: var(--surface-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-success { background: var(--success); color: var(--text-inverse); }
|
||||||
|
.btn-success:hover { box-shadow: 0 4px 14px var(--success-dim); }
|
||||||
|
|
||||||
|
.btn-purple { background: var(--purple); color: var(--text-inverse); }
|
||||||
|
.btn-purple:hover { box-shadow: 0 4px 14px var(--purple-dim); }
|
||||||
|
|
||||||
|
.btn-danger { background: var(--error); color: #fff; }
|
||||||
|
.btn-danger:hover { box-shadow: 0 4px 14px var(--error-dim); }
|
||||||
|
|
||||||
|
.btn-sm { padding: 5px 12px; font-size: 12px; }
|
||||||
|
.btn-lg { padding: 12px 24px; font-size: 15px; }
|
||||||
|
.btn-icon-only { padding: 8px; width: 36px; height: 36px; }
|
||||||
|
|
||||||
|
/* ── Forms ──────────────────────────────────────────────── */
|
||||||
|
.form-group {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: var(--sp-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-label {
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.05em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-input,
|
||||||
|
.form-select,
|
||||||
|
.form-textarea {
|
||||||
|
width: 100%;
|
||||||
|
padding: 9px 14px;
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--text);
|
||||||
|
background: var(--surface-0);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
transition: all var(--duration) var(--ease);
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-input:focus,
|
||||||
|
.form-select:focus,
|
||||||
|
.form-textarea:focus {
|
||||||
|
border-color: var(--accent);
|
||||||
|
box-shadow: 0 0 0 3px var(--accent-dim);
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-input::placeholder,
|
||||||
|
.form-textarea::placeholder {
|
||||||
|
color: var(--text-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-select {
|
||||||
|
cursor: pointer;
|
||||||
|
appearance: none;
|
||||||
|
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235c6070' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
background-position: right 12px center;
|
||||||
|
padding-right: 36px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-textarea {
|
||||||
|
resize: vertical;
|
||||||
|
min-height: 100px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-checkbox {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--sp-2);
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-checkbox input[type="checkbox"] {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
accent-color: var(--accent);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
|
||||||
|
gap: var(--sp-4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Progress Bar ───────────────────────────────────────── */
|
||||||
|
.progress-container {
|
||||||
|
margin-top: var(--sp-4);
|
||||||
|
padding: var(--sp-4);
|
||||||
|
background: var(--surface-0);
|
||||||
|
border-radius: var(--radius-md);
|
||||||
|
border: 1px solid var(--border-subtle);
|
||||||
|
}
|
||||||
|
|
||||||
|
.progress-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--sp-3);
|
||||||
|
margin-bottom: var(--sp-3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.progress-label {
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
|
.progress-track {
|
||||||
|
height: 6px;
|
||||||
|
background: var(--surface-3);
|
||||||
|
border-radius: 3px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.progress-fill {
|
||||||
|
height: 100%;
|
||||||
|
background: linear-gradient(90deg, var(--accent), var(--accent-hover));
|
||||||
|
border-radius: 3px;
|
||||||
|
transition: width 0.3s var(--ease);
|
||||||
|
}
|
||||||
|
|
||||||
|
.progress-detail {
|
||||||
|
margin-top: var(--sp-2);
|
||||||
|
font-size: 12px;
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
color: var(--text-tertiary);
|
||||||
|
line-height: 1.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Phase Badge ────────────────────────────────────────── */
|
||||||
|
.badge-status {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
padding: 3px 10px;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.05em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge-status::before {
|
||||||
|
content: '';
|
||||||
|
width: 6px;
|
||||||
|
height: 6px;
|
||||||
|
border-radius: 50%;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge-idle { background: var(--surface-3); color: var(--text-secondary); }
|
||||||
|
.badge-idle::before { background: var(--text-tertiary); }
|
||||||
|
|
||||||
|
.badge-running { background: var(--accent-dim); color: var(--accent); }
|
||||||
|
.badge-running::before { background: var(--accent); animation: pulse-dot 1.5s ease-in-out infinite; }
|
||||||
|
|
||||||
|
.badge-success { background: var(--success-dim); color: var(--success); }
|
||||||
|
.badge-success::before { background: var(--success); }
|
||||||
|
|
||||||
|
.badge-error { background: var(--error-dim); color: var(--error); }
|
||||||
|
.badge-error::before { background: var(--error); }
|
||||||
|
|
||||||
|
.badge-warning { background: var(--warning-dim); color: var(--warning); }
|
||||||
|
.badge-warning::before { background: var(--warning); }
|
||||||
|
|
||||||
|
@keyframes pulse-dot {
|
||||||
|
0%, 100% { opacity: 1; }
|
||||||
|
50% { opacity: 0.4; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Empty State ────────────────────────────────────────── */
|
||||||
|
.empty-state {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: var(--sp-12) var(--sp-6);
|
||||||
|
text-align: center;
|
||||||
|
color: var(--text-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-state svg {
|
||||||
|
width: 48px;
|
||||||
|
height: 48px;
|
||||||
|
margin-bottom: var(--sp-4);
|
||||||
|
opacity: 0.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-state p {
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: var(--sp-3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Tables ─────────────────────────────────────────────── */
|
||||||
|
.data-table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table thead th {
|
||||||
|
text-align: left;
|
||||||
|
padding: var(--sp-3) var(--sp-4);
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.06em;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
background: var(--surface-0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table tbody td {
|
||||||
|
padding: var(--sp-3) var(--sp-4);
|
||||||
|
border-bottom: 1px solid var(--border-subtle);
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table tbody tr:hover {
|
||||||
|
background: var(--surface-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.data-table tbody tr:last-child td {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Tags ───────────────────────────────────────────────── */
|
||||||
|
.tag {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
padding: 2px 8px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tag-blue { background: var(--accent-dim); color: var(--accent); }
|
||||||
|
.tag-green { background: var(--success-dim); color: var(--success); }
|
||||||
|
.tag-yellow { background: var(--warning-dim); color: var(--warning); }
|
||||||
|
.tag-red { background: var(--error-dim); color: var(--error); }
|
||||||
|
.tag-purple { background: var(--purple-dim); color: var(--purple); }
|
||||||
|
|
||||||
|
/* ── Toast / Notification ───────────────────────────────── */
|
||||||
|
.toast {
|
||||||
|
position: fixed;
|
||||||
|
bottom: var(--sp-6);
|
||||||
|
right: var(--sp-6);
|
||||||
|
padding: var(--sp-3) var(--sp-5);
|
||||||
|
background: var(--surface-3);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius-md);
|
||||||
|
box-shadow: var(--shadow-lg);
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--text);
|
||||||
|
z-index: 1000;
|
||||||
|
animation: toast-in 0.3s var(--ease);
|
||||||
|
}
|
||||||
|
|
||||||
|
.toast-success { border-left: 3px solid var(--success); }
|
||||||
|
.toast-error { border-left: 3px solid var(--error); }
|
||||||
|
.toast-warning { border-left: 3px solid var(--warning); }
|
||||||
|
|
||||||
|
@keyframes toast-in {
|
||||||
|
from { opacity: 0; transform: translateY(16px); }
|
||||||
|
to { opacity: 1; transform: translateY(0); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Loading Spinner ────────────────────────────────────── */
|
||||||
|
.spinner {
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
border: 2px solid var(--border);
|
||||||
|
border-top-color: var(--accent);
|
||||||
|
border-radius: 50%;
|
||||||
|
animation: spin 0.6s linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes spin {
|
||||||
|
to { transform: rotate(360deg); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Divider ────────────────────────────────────────────── */
|
||||||
|
.divider {
|
||||||
|
height: 1px;
|
||||||
|
background: var(--border);
|
||||||
|
margin: var(--sp-5) 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Utility Classes ────────────────────────────────────── */
|
||||||
|
.hidden { display: none !important; }
|
||||||
|
.text-accent { color: var(--accent); }
|
||||||
|
.text-success { color: var(--success); }
|
||||||
|
.text-warning { color: var(--warning); }
|
||||||
|
.text-error { color: var(--error); }
|
||||||
|
.text-muted { color: var(--text-secondary); }
|
||||||
|
.text-faint { color: var(--text-tertiary); }
|
||||||
|
.text-mono { font-family: var(--font-mono); }
|
||||||
|
.text-sm { font-size: 12px; }
|
||||||
|
.text-xs { font-size: 11px; }
|
||||||
|
.font-bold { font-weight: 700; }
|
||||||
|
.text-center { text-align: center; }
|
||||||
|
.text-right { text-align: right; }
|
||||||
|
.mt-2 { margin-top: var(--sp-2); }
|
||||||
|
.mt-4 { margin-top: var(--sp-4); }
|
||||||
|
.mt-6 { margin-top: var(--sp-6); }
|
||||||
|
.mb-2 { margin-bottom: var(--sp-2); }
|
||||||
|
.mb-4 { margin-bottom: var(--sp-4); }
|
||||||
|
.mb-6 { margin-bottom: var(--sp-6); }
|
||||||
|
.gap-2 { gap: var(--sp-2); }
|
||||||
|
.gap-4 { gap: var(--sp-4); }
|
||||||
|
.flex { display: flex; }
|
||||||
|
.flex-col { flex-direction: column; }
|
||||||
|
.items-center { align-items: center; }
|
||||||
|
.justify-between { justify-content: space-between; }
|
||||||
|
.flex-1 { flex: 1; }
|
||||||
|
.grid { display: grid; }
|
||||||
|
.sr-only {
|
||||||
|
position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
|
||||||
|
overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border-width: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Responsive ─────────────────────────────────────────── */
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
.page {
|
||||||
|
padding: var(--sp-5) var(--sp-4);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav {
|
||||||
|
padding: 0 var(--sp-4);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-brand {
|
||||||
|
margin-right: var(--sp-4);
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-grid {
|
||||||
|
grid-template-columns: repeat(2, 1fr);
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-row {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 480px) {
|
||||||
|
.stat-grid {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ═══════════════════════════════════════════════════════════
|
||||||
|
PREMIUM UPGRADE LAYER
|
||||||
|
═══════════════════════════════════════════════════════════ */
|
||||||
|
|
||||||
|
/* ── Ambient background glow (body) ─────────────────────── */
|
||||||
|
body {
|
||||||
|
background-image:
|
||||||
|
radial-gradient(ellipse 80% 50% at 10% -10%, rgba(91,158,240,0.07) 0%, transparent 60%),
|
||||||
|
radial-gradient(ellipse 60% 40% at 90% 110%, rgba(167,139,250,0.05) 0%, transparent 60%);
|
||||||
|
background-attachment: fixed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Page title: subtle gradient text ───────────────────── */
|
||||||
|
.page-header h1 {
|
||||||
|
background: linear-gradient(135deg, #fff 60%, rgba(91,158,240,0.85) 100%);
|
||||||
|
-webkit-background-clip: text;
|
||||||
|
-webkit-text-fill-color: transparent;
|
||||||
|
background-clip: text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Nav: stronger glass + accent underline ─────────────── */
|
||||||
|
.nav {
|
||||||
|
background: rgba(9,11,16,0.80);
|
||||||
|
backdrop-filter: blur(20px) saturate(1.8);
|
||||||
|
-webkit-backdrop-filter: blur(20px) saturate(1.8);
|
||||||
|
box-shadow: 0 1px 0 rgba(91,158,240,0.08), 0 4px 24px rgba(0,0,0,0.3);
|
||||||
|
border-bottom-color: rgba(255,255,255,0.06);
|
||||||
|
}
|
||||||
|
.nav-brand svg { filter: drop-shadow(0 0 8px rgba(91,158,240,0.4)); }
|
||||||
|
.nav-links a.active { box-shadow: inset 0 0 0 1px rgba(91,158,240,0.2); }
|
||||||
|
|
||||||
|
/* ── Panels: gradient border on hover ───────────────────── */
|
||||||
|
.panel {
|
||||||
|
position: relative;
|
||||||
|
transition: border-color var(--duration) var(--ease),
|
||||||
|
box-shadow var(--duration) var(--ease);
|
||||||
|
}
|
||||||
|
.panel::before {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
inset: 0;
|
||||||
|
border-radius: var(--radius-lg);
|
||||||
|
padding: 1px;
|
||||||
|
background: linear-gradient(135deg, rgba(91,158,240,0.18), transparent 50%, rgba(167,139,250,0.1));
|
||||||
|
-webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
|
||||||
|
-webkit-mask-composite: xor;
|
||||||
|
mask-composite: exclude;
|
||||||
|
pointer-events: none;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity var(--duration) var(--ease);
|
||||||
|
}
|
||||||
|
.panel:hover::before { opacity: 1; }
|
||||||
|
.panel:hover {
|
||||||
|
box-shadow: 0 8px 32px rgba(0,0,0,0.35), 0 0 0 1px rgba(91,158,240,0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Stat cards: gradient surface + shimmer top edge ─────── */
|
||||||
|
.stat-card {
|
||||||
|
position: relative;
|
||||||
|
background: linear-gradient(145deg, var(--surface-2), var(--surface-1));
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.stat-card::after {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
top: 0; left: 0; right: 0;
|
||||||
|
height: 1px;
|
||||||
|
background: linear-gradient(90deg, transparent, rgba(255,255,255,0.12), transparent);
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
.stat-card:hover {
|
||||||
|
box-shadow: var(--shadow-md), 0 0 20px rgba(91,158,240,0.08);
|
||||||
|
transform: translateY(-2px);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Buttons: gradient fills + inner highlight ───────────── */
|
||||||
|
.btn { position: relative; overflow: hidden; }
|
||||||
|
.btn::after {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
top: 0; left: 0; right: 0;
|
||||||
|
height: 50%;
|
||||||
|
background: linear-gradient(to bottom, rgba(255,255,255,0.07), transparent);
|
||||||
|
border-radius: inherit;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
.btn-primary {
|
||||||
|
background: linear-gradient(135deg, var(--accent), #4a8de0);
|
||||||
|
box-shadow: 0 2px 8px rgba(91,158,240,0.25);
|
||||||
|
}
|
||||||
|
.btn-primary:hover {
|
||||||
|
background: linear-gradient(135deg, var(--accent-hover), var(--accent));
|
||||||
|
box-shadow: 0 4px 20px rgba(91,158,240,0.4);
|
||||||
|
}
|
||||||
|
.btn-success {
|
||||||
|
background: linear-gradient(135deg, var(--success), #38c46c);
|
||||||
|
box-shadow: 0 2px 8px rgba(74,222,128,0.2);
|
||||||
|
}
|
||||||
|
.btn-success:hover { box-shadow: 0 4px 20px rgba(74,222,128,0.4); transform: translateY(-1px); }
|
||||||
|
.btn-purple {
|
||||||
|
background: linear-gradient(135deg, var(--purple), #9171f0);
|
||||||
|
box-shadow: 0 2px 8px rgba(167,139,250,0.2);
|
||||||
|
}
|
||||||
|
.btn-purple:hover { box-shadow: 0 4px 20px rgba(167,139,250,0.4); transform: translateY(-1px); }
|
||||||
|
|
||||||
|
/* ── Tags: subtle colored border ────────────────────────── */
|
||||||
|
.tag-blue { border: 1px solid rgba(91,158,240,0.25); }
|
||||||
|
.tag-green { border: 1px solid rgba(74,222,128,0.25); }
|
||||||
|
.tag-yellow { border: 1px solid rgba(251,191,36,0.25); }
|
||||||
|
.tag-red { border: 1px solid rgba(248,113,113,0.25); }
|
||||||
|
.tag-purple { border: 1px solid rgba(167,139,250,0.25); }
|
||||||
|
|
||||||
|
/* ── Progress fill: glow ─────────────────────────────────── */
|
||||||
|
.progress-fill { box-shadow: 0 0 8px rgba(91,158,240,0.5); }
|
||||||
|
|
||||||
|
/* ── Badge running: glow dot ─────────────────────────────── */
|
||||||
|
.badge-running { box-shadow: 0 0 12px rgba(91,158,240,0.15); }
|
||||||
|
.badge-running::before { box-shadow: 0 0 6px var(--accent); }
|
||||||
|
|
||||||
|
/* ── Toast: glassmorphism + colored glow ─────────────────── */
|
||||||
|
.toast { backdrop-filter: blur(12px); }
|
||||||
|
.toast-success { box-shadow: var(--shadow-lg), 0 0 20px rgba(74,222,128,0.1); }
|
||||||
|
.toast-error { box-shadow: var(--shadow-lg), 0 0 20px rgba(248,113,113,0.1); }
|
||||||
|
|
||||||
|
/* ── Divider: fade-out gradient ──────────────────────────── */
|
||||||
|
.divider {
|
||||||
|
background: linear-gradient(90deg, transparent, var(--border), transparent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Inputs: inset shadow for depth ─────────────────────── */
|
||||||
|
.form-input, .form-select, .form-textarea {
|
||||||
|
box-shadow: inset 0 1px 3px rgba(0,0,0,0.25);
|
||||||
|
}
|
||||||
|
.form-input:focus, .form-select:focus, .form-textarea:focus {
|
||||||
|
box-shadow: 0 0 0 3px var(--accent-dim), inset 0 1px 3px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ══════════════════════════════════════════════════════════════
|
||||||
|
Tooltip System (.tip-icon + data-tip)
|
||||||
|
Shared across all pages — do not duplicate in page-specific CSS
|
||||||
|
══════════════════════════════════════════════════════════════ */
|
||||||
|
|
||||||
|
.tip-icon {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 15px;
|
||||||
|
height: 15px;
|
||||||
|
border-radius: 50%;
|
||||||
|
background: var(--surface-3);
|
||||||
|
color: var(--text-secondary);
|
||||||
|
font-size: 10px;
|
||||||
|
font-weight: 700;
|
||||||
|
font-style: normal;
|
||||||
|
cursor: default;
|
||||||
|
position: relative;
|
||||||
|
vertical-align: middle;
|
||||||
|
margin-left: 4px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
transition: background 0.15s, color 0.15s;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tip-icon:hover {
|
||||||
|
background: var(--accent-dim);
|
||||||
|
color: var(--accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tip-icon::after {
|
||||||
|
content: attr(data-tip);
|
||||||
|
position: absolute;
|
||||||
|
bottom: calc(100% + 8px);
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
background: var(--surface-0, #1a1a2e);
|
||||||
|
color: var(--text, #e0e0e0);
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: var(--radius-md, 6px);
|
||||||
|
padding: 8px 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 400;
|
||||||
|
line-height: 1.5;
|
||||||
|
white-space: normal;
|
||||||
|
width: 240px;
|
||||||
|
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
|
||||||
|
pointer-events: none;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.15s ease;
|
||||||
|
z-index: 1000;
|
||||||
|
text-align: left;
|
||||||
|
text-transform: none;
|
||||||
|
letter-spacing: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tip-icon::before {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
bottom: calc(100% + 3px);
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
border: 5px solid transparent;
|
||||||
|
border-top-color: var(--border, #333);
|
||||||
|
pointer-events: none;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.15s ease;
|
||||||
|
z-index: 1001;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tip-icon:hover::after,
|
||||||
|
.tip-icon:hover::before {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tip-icon.tip-below::after {
|
||||||
|
bottom: auto;
|
||||||
|
top: calc(100% + 8px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tip-icon.tip-below::before {
|
||||||
|
bottom: auto;
|
||||||
|
top: calc(100% + 3px);
|
||||||
|
border-top-color: transparent;
|
||||||
|
border-bottom-color: var(--border, #333);
|
||||||
|
}
|
||||||
+334
-33
@@ -51,6 +51,8 @@
|
|||||||
<button class="btn-util" onclick="checkStateBasedActions()" title="Check State-Based Actions">SBA</button>
|
<button class="btn-util" onclick="checkStateBasedActions()" title="Check State-Based Actions">SBA</button>
|
||||||
<button class="btn-util" onclick="toggleSettingsPanel()" title="Settings">⚙</button>
|
<button class="btn-util" onclick="toggleSettingsPanel()" title="Settings">⚙</button>
|
||||||
<button class="btn-util" id="btn-search-toggle" onclick="document.getElementById('card-search-sidebar').classList.toggle('collapsed')" title="Card Search (S)">🔍</button>
|
<button class="btn-util" id="btn-search-toggle" onclick="document.getElementById('card-search-sidebar').classList.toggle('collapsed')" title="Card Search (S)">🔍</button>
|
||||||
|
<button class="btn-util" id="btn-deckbuilder-toggle" onclick="toggleDeckBuilderPanel()" title="Deck Builder">🃏</button>
|
||||||
|
<button class="btn-util btn-util-exit" onclick="location.reload()" title="Return to Start Screen">✕</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Storm counter -->
|
<!-- Storm counter -->
|
||||||
@@ -67,15 +69,6 @@
|
|||||||
<div class="corner-accent bl"></div>
|
<div class="corner-accent bl"></div>
|
||||||
<div class="corner-accent br"></div>
|
<div class="corner-accent br"></div>
|
||||||
|
|
||||||
<!-- CENTER EMBLEM -->
|
|
||||||
<div class="center-emblem">
|
|
||||||
<svg viewBox="0 0 100 100" fill="none">
|
|
||||||
<circle cx="50" cy="50" r="42" stroke="rgba(255,255,255,0.2)" stroke-width="1.5"/>
|
|
||||||
<circle cx="50" cy="50" r="28" stroke="rgba(255,255,255,0.1)" stroke-width="1"/>
|
|
||||||
<polygon points="50,15 58.5,37 82,37 63,51 69,74 50,60 31,74 37,51 18,37 41.5,37"
|
|
||||||
fill="rgba(255,255,255,0.1)" stroke="rgba(255,255,255,0.2)" stroke-width="0.8"/>
|
|
||||||
</svg>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- ================================================
|
<!-- ================================================
|
||||||
4-QUADRANT BATTLEFIELD
|
4-QUADRANT BATTLEFIELD
|
||||||
@@ -85,12 +78,6 @@
|
|||||||
<!-- PLAYER 1: Top-Left (Crimson) -->
|
<!-- PLAYER 1: Top-Left (Crimson) -->
|
||||||
<div class="player-area p1" id="player-area-1">
|
<div class="player-area p1" id="player-area-1">
|
||||||
<span class="player-label" id="player-label-1">Player 1</span>
|
<span class="player-label" id="player-label-1">Player 1</span>
|
||||||
<div class="life-counter" id="life-counter-1">
|
|
||||||
<button onclick="adjustLife(0,-1)" aria-label="Decrease P1 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-0" onclick="showSetLifeDialog(0)">40</span>
|
|
||||||
<button onclick="adjustLife(0,1)" aria-label="Increase P1 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-0" style="display:none;" onclick="adjustPoison(0,1)" title="Poison counters (click +1, right-click -1)" oncontextmenu="event.preventDefault();adjustPoison(0,-1)">0 poison</div>
|
|
||||||
|
|
||||||
<!-- Zone Row: Library, Exile, Graveyard, Hand -->
|
<!-- Zone Row: Library, Exile, Graveyard, Hand -->
|
||||||
<div class="zone-row" id="zone-row-1">
|
<div class="zone-row" id="zone-row-1">
|
||||||
@@ -139,12 +126,6 @@
|
|||||||
<!-- PLAYER 2: Top-Right (Sapphire) -->
|
<!-- PLAYER 2: Top-Right (Sapphire) -->
|
||||||
<div class="player-area p2" id="player-area-2">
|
<div class="player-area p2" id="player-area-2">
|
||||||
<span class="player-label" id="player-label-2">Player 2</span>
|
<span class="player-label" id="player-label-2">Player 2</span>
|
||||||
<div class="life-counter" id="life-counter-2">
|
|
||||||
<button onclick="adjustLife(1,-1)" aria-label="Decrease P2 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-1" onclick="showSetLifeDialog(1)">40</span>
|
|
||||||
<button onclick="adjustLife(1,1)" aria-label="Increase P2 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-1" style="display:none;" onclick="adjustPoison(1,1)" oncontextmenu="event.preventDefault();adjustPoison(1,-1)">0 poison</div>
|
|
||||||
|
|
||||||
<div class="zone-row" id="zone-row-2">
|
<div class="zone-row" id="zone-row-2">
|
||||||
<div class="zone library" id="lib-zone-2" data-player="2" data-zone="library"
|
<div class="zone library" id="lib-zone-2" data-player="2" data-zone="library"
|
||||||
@@ -229,12 +210,6 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<span class="player-label" id="player-label-3">Player 3</span>
|
<span class="player-label" id="player-label-3">Player 3</span>
|
||||||
<div class="life-counter" id="life-counter-3">
|
|
||||||
<button onclick="adjustLife(2,-1)" aria-label="Decrease P3 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-2" onclick="showSetLifeDialog(2)">40</span>
|
|
||||||
<button onclick="adjustLife(2,1)" aria-label="Increase P3 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-2" style="display:none;" onclick="adjustPoison(2,1)" oncontextmenu="event.preventDefault();adjustPoison(2,-1)">0 poison</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- PLAYER 4: Bottom-Right (Gold) -->
|
<!-- PLAYER 4: Bottom-Right (Gold) -->
|
||||||
@@ -279,16 +254,89 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<span class="player-label" id="player-label-4">Player 4</span>
|
<span class="player-label" id="player-label-4">Player 4</span>
|
||||||
<div class="life-counter" id="life-counter-4">
|
|
||||||
<button onclick="adjustLife(3,-1)" aria-label="Decrease P4 life">−</button>
|
|
||||||
<span class="life-counter-value" id="life-3" onclick="showSetLifeDialog(3)">40</span>
|
|
||||||
<button onclick="adjustLife(3,1)" aria-label="Increase P4 life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="poison-badge" id="poison-3" style="display:none;" onclick="adjustPoison(3,1)" oncontextmenu="event.preventDefault();adjustPoison(3,-1)">0 poison</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div><!-- /.battlefield -->
|
</div><!-- /.battlefield -->
|
||||||
|
|
||||||
|
<!-- LIFE PANELS OVERLAY (top-most layer) -->
|
||||||
|
<div id="life-panels-overlay">
|
||||||
|
<div class="life-panel lp-p1" id="life-panel-1">
|
||||||
|
<div class="life-panel-box life-box">
|
||||||
|
<span class="life-panel-label">Life</span>
|
||||||
|
<div class="life-counter" id="life-counter-1">
|
||||||
|
<button onclick="adjustLife(0,-1)" aria-label="Decrease P1 life">−</button>
|
||||||
|
<span class="life-counter-value" id="life-0" onclick="showSetLifeDialog(0)">40</span>
|
||||||
|
<button onclick="adjustLife(0,1)" aria-label="Increase P1 life">+</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box cmd-dmg-box" id="cmd-dmg-inline-0" onclick="openCmdDmgMatrix()" title="Open Commander Damage matrix">
|
||||||
|
<span class="life-panel-label">Cmd Dmg</span>
|
||||||
|
<div class="cmd-dmg-inline-row">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box counters-box">
|
||||||
|
<span class="life-panel-label">Counters</span>
|
||||||
|
<div class="poison-badge" id="poison-0" onclick="adjustPoison(0,1)" title="Poison counters (click +1, right-click -1)" oncontextmenu="event.preventDefault();adjustPoison(0,-1)">0 poison</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="life-panel lp-p2" id="life-panel-2">
|
||||||
|
<div class="life-panel-box life-box">
|
||||||
|
<span class="life-panel-label">Life</span>
|
||||||
|
<div class="life-counter" id="life-counter-2">
|
||||||
|
<button onclick="adjustLife(1,-1)" aria-label="Decrease P2 life">−</button>
|
||||||
|
<span class="life-counter-value" id="life-1" onclick="showSetLifeDialog(1)">40</span>
|
||||||
|
<button onclick="adjustLife(1,1)" aria-label="Increase P2 life">+</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box cmd-dmg-box" id="cmd-dmg-inline-1" onclick="openCmdDmgMatrix()" title="Open Commander Damage matrix">
|
||||||
|
<span class="life-panel-label">Cmd Dmg</span>
|
||||||
|
<div class="cmd-dmg-inline-row">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box counters-box">
|
||||||
|
<span class="life-panel-label">Counters</span>
|
||||||
|
<div class="poison-badge" id="poison-1" onclick="adjustPoison(1,1)" oncontextmenu="event.preventDefault();adjustPoison(1,-1)">0 poison</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="life-panel lp-p3" id="life-panel-3">
|
||||||
|
<div class="life-panel-box life-box">
|
||||||
|
<span class="life-panel-label">Life</span>
|
||||||
|
<div class="life-counter" id="life-counter-3">
|
||||||
|
<button onclick="adjustLife(2,-1)" aria-label="Decrease P3 life">−</button>
|
||||||
|
<span class="life-counter-value" id="life-2" onclick="showSetLifeDialog(2)">40</span>
|
||||||
|
<button onclick="adjustLife(2,1)" aria-label="Increase P3 life">+</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box cmd-dmg-box" id="cmd-dmg-inline-2" onclick="openCmdDmgMatrix()" title="Open Commander Damage matrix">
|
||||||
|
<span class="life-panel-label">Cmd Dmg</span>
|
||||||
|
<div class="cmd-dmg-inline-row">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box counters-box">
|
||||||
|
<span class="life-panel-label">Counters</span>
|
||||||
|
<div class="poison-badge" id="poison-2" onclick="adjustPoison(2,1)" oncontextmenu="event.preventDefault();adjustPoison(2,-1)">0 poison</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="life-panel lp-p4" id="life-panel-4">
|
||||||
|
<div class="life-panel-box life-box">
|
||||||
|
<span class="life-panel-label">Life</span>
|
||||||
|
<div class="life-counter" id="life-counter-4">
|
||||||
|
<button onclick="adjustLife(3,-1)" aria-label="Decrease P4 life">−</button>
|
||||||
|
<span class="life-counter-value" id="life-3" onclick="showSetLifeDialog(3)">40</span>
|
||||||
|
<button onclick="adjustLife(3,1)" aria-label="Increase P4 life">+</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box cmd-dmg-box" id="cmd-dmg-inline-3" onclick="openCmdDmgMatrix()" title="Open Commander Damage matrix">
|
||||||
|
<span class="life-panel-label">Cmd Dmg</span>
|
||||||
|
<div class="cmd-dmg-inline-row">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="life-panel-box counters-box">
|
||||||
|
<span class="life-panel-label">Counters</span>
|
||||||
|
<div class="poison-badge" id="poison-3" onclick="adjustPoison(3,1)" oncontextmenu="event.preventDefault();adjustPoison(3,-1)">0 poison</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- COMBAT SVG OVERLAY (arrows) -->
|
<!-- COMBAT SVG OVERLAY (arrows) -->
|
||||||
<svg id="combat-overlay" xmlns="http://www.w3.org/2000/svg" style="position:absolute;inset:44px 0 0 0;pointer-events:none;z-index:30;">
|
<svg id="combat-overlay" xmlns="http://www.w3.org/2000/svg" style="position:absolute;inset:44px 0 0 0;pointer-events:none;z-index:30;">
|
||||||
<defs>
|
<defs>
|
||||||
@@ -321,6 +369,24 @@
|
|||||||
<!-- Sidebar edge tab -->
|
<!-- Sidebar edge tab -->
|
||||||
<div id="sidebar-edge-tab" class="sidebar-edge-tab" title="Toggle card search">‹</div>
|
<div id="sidebar-edge-tab" class="sidebar-edge-tab" title="Toggle card search">‹</div>
|
||||||
|
|
||||||
|
<!-- LEFT ACTION PANEL (triggers + quick actions) -->
|
||||||
|
<div id="left-action-panel" class="collapsed">
|
||||||
|
<div class="left-panel-header">
|
||||||
|
<span class="left-panel-title">Triggers <span id="left-panel-trigger-count" class="left-panel-count">0</span></span>
|
||||||
|
<button class="sidebar-toggle" onclick="toggleLeftPanel()" title="Close panel">✕</button>
|
||||||
|
</div>
|
||||||
|
<div class="left-panel-actions">
|
||||||
|
<button class="left-panel-btn" onclick="openTokenModal((typeof gameState !== 'undefined' && gameState) ? (gameState.currentPlayerIndex || 0) : 0)" title="Create token">✨ Create Token</button>
|
||||||
|
<button class="left-panel-btn" onclick="autoArrangeBattlefield()" title="Auto-arrange battlefield">⇄ Auto-Arrange</button>
|
||||||
|
</div>
|
||||||
|
<div class="left-panel-body" id="left-panel-triggers">
|
||||||
|
<div class="left-panel-empty">No pending triggers.</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Left panel edge tab -->
|
||||||
|
<div id="left-panel-edge-tab" class="left-panel-edge-tab" onclick="toggleLeftPanel()" title="Toggle quick actions">›</div>
|
||||||
|
|
||||||
</div><!-- /.table-surface / #game-battlefield -->
|
</div><!-- /.table-surface / #game-battlefield -->
|
||||||
|
|
||||||
<!-- ====================================================
|
<!-- ====================================================
|
||||||
@@ -524,10 +590,18 @@
|
|||||||
|
|
||||||
<!-- ====================================================
|
<!-- ====================================================
|
||||||
SCRIPTS
|
SCRIPTS
|
||||||
|
Load order matters:
|
||||||
|
1. precons.js — static PRECON_DATA fallback
|
||||||
|
2. engine.js — game state & core logic
|
||||||
|
3. ai-bridge.js — backend connector & DeckSourceClient
|
||||||
|
4. precon-loader.js — startGameWithDecks() + loadPreconDeckFromDck()
|
||||||
|
5. player-panels.js — player panel rendering
|
||||||
|
6. ui.js — setup screen, deck picker, UI wiring
|
||||||
==================================================== -->
|
==================================================== -->
|
||||||
<script src="precons.js?v=20260403_0300"></script>
|
<script src="precons.js?v=20260403_0300"></script>
|
||||||
<script src="engine.js?v=20260403_0300"></script>
|
<script src="engine.js?v=20260403_0300"></script>
|
||||||
<script src="ai-bridge.js?v=20260403_0300"></script>
|
<script src="ai-bridge.js?v=20260403_0300"></script>
|
||||||
|
<script src="precon-loader.js?v=20260403_0300"></script>
|
||||||
<script src="player-panels.js?v=20260403_0300"></script>
|
<script src="player-panels.js?v=20260403_0300"></script>
|
||||||
<script src="ui.js?v=20260403_0300"></script>
|
<script src="ui.js?v=20260403_0300"></script>
|
||||||
|
|
||||||
@@ -634,10 +708,237 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
indicator.className = 'backend-status ' +
|
indicator.className = 'backend-status ' +
|
||||||
(status.connected ? (status.modelLoaded ? 'connected-ml' : 'connected') : 'disconnected');
|
(status.connected ? (status.modelLoaded ? 'connected-ml' : 'connected') : 'disconnected');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wire up WebSocket push events from backend
|
||||||
|
AIBridge.connector.on('ai:decision', function(msg) {
|
||||||
|
if (!gameState || !msg.action) return;
|
||||||
|
var playerIdx = gameState.currentPlayerIndex;
|
||||||
|
var player = gameState.players[playerIdx];
|
||||||
|
if (player && player.isAI) {
|
||||||
|
AIBridge.MacroActionMapper.execute(msg.action, playerIdx, gameState, msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
AIBridge.connector.on('state:snapshot', function(msg) {
|
||||||
|
if (typeof AI_BRIDGE_CONFIG !== 'undefined' && AI_BRIDGE_CONFIG.verbose) {
|
||||||
|
console.log('[WS] State snapshot received from backend');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<!-- ====================================================
|
||||||
|
DECK BUILDER PANEL (iframe)
|
||||||
|
==================================================== -->
|
||||||
|
<div id="panel-deckbuilder" class="fullscreen-panel hidden">
|
||||||
|
<div class="fullscreen-panel-header">
|
||||||
|
<span>Deck Builder</span>
|
||||||
|
<button class="btn-util" onclick="toggleDeckBuilderPanel()" title="Close">×</button>
|
||||||
|
</div>
|
||||||
|
<iframe src="deckbuilder.html"
|
||||||
|
style="width:100%;height:calc(100vh - 36px);border:none;"
|
||||||
|
id="deckbuilder-iframe">
|
||||||
|
</iframe>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="panel-collection" class="fullscreen-panel hidden">
|
||||||
|
<div class="fullscreen-panel-header">
|
||||||
|
<span>Collection</span>
|
||||||
|
<button class="btn-util" onclick="toggleCollectionPanel()" title="Close">×</button>
|
||||||
|
</div>
|
||||||
|
<iframe src="collection.html"
|
||||||
|
style="width:100%;height:calc(100vh - 36px);border:none;"
|
||||||
|
id="collection-iframe">
|
||||||
|
</iframe>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="panel-deckgenerator" class="fullscreen-panel hidden">
|
||||||
|
<div class="fullscreen-panel-header">
|
||||||
|
<span>Auto Deck Generator</span>
|
||||||
|
<button class="btn-util" onclick="toggleDeckGenPanel()" title="Close">×</button>
|
||||||
|
</div>
|
||||||
|
<iframe src="deckgenerator.html"
|
||||||
|
style="width:100%;height:calc(100vh - 36px);border:none;"
|
||||||
|
id="deckgenerator-iframe">
|
||||||
|
</iframe>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function toggleDeckBuilderPanel() {
|
||||||
|
var panel = document.getElementById('panel-deckbuilder');
|
||||||
|
if (panel) panel.classList.toggle('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleDeckGenPanel() {
|
||||||
|
var panel = document.getElementById('panel-deckgenerator');
|
||||||
|
if (panel) panel.classList.toggle('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleCollectionPanel() {
|
||||||
|
var panel = document.getElementById('panel-collection');
|
||||||
|
if (panel) panel.classList.toggle('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleLeftPanel() {
|
||||||
|
var panel = document.getElementById('left-action-panel');
|
||||||
|
if (panel) {
|
||||||
|
panel.classList.toggle('collapsed');
|
||||||
|
if (!panel.classList.contains('collapsed') && typeof refreshLeftPanelTriggers === 'function') {
|
||||||
|
refreshLeftPanelTriggers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the triggers list inside the left action panel.
|
||||||
|
// Mirrors #trigger-stack-body rendering but stays embedded in the panel.
|
||||||
|
function refreshLeftPanelTriggers() {
|
||||||
|
var body = document.getElementById('left-panel-triggers');
|
||||||
|
var countEl = document.getElementById('left-panel-trigger-count');
|
||||||
|
if (!body) return;
|
||||||
|
if (typeof gameState === 'undefined' || !gameState) {
|
||||||
|
if (countEl) countEl.textContent = '0';
|
||||||
|
body.innerHTML = '<div class="left-panel-empty">No pending triggers.</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var pending = (gameState.triggerStack || []).filter(function(t){ return !t.resolved; });
|
||||||
|
if (countEl) countEl.textContent = String(pending.length);
|
||||||
|
if (pending.length === 0) {
|
||||||
|
body.innerHTML = '<div class="left-panel-empty">No pending triggers.</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var icons = (typeof TRIGGER_TYPE_ICONS !== 'undefined') ? TRIGGER_TYPE_ICONS : {};
|
||||||
|
var labels = (typeof TRIGGER_TYPE_LABELS !== 'undefined') ? TRIGGER_TYPE_LABELS : {};
|
||||||
|
var html = '';
|
||||||
|
pending.forEach(function(t) {
|
||||||
|
var icon = icons[t.type] || '\u2747';
|
||||||
|
var label = labels[t.type] || t.type || '';
|
||||||
|
var owner = (gameState.players && gameState.players[t.ownerIndex]) || {};
|
||||||
|
var color = owner.color || '#888';
|
||||||
|
var name = (t.cardName || 'Unknown').replace(/</g,'<');
|
||||||
|
var snip = (t.oracleSnippet || '').replace(/</g,'<');
|
||||||
|
html +=
|
||||||
|
'<div class="lpt-item">' +
|
||||||
|
'<div class="lpt-icon">' + icon + '</div>' +
|
||||||
|
'<div class="lpt-body">' +
|
||||||
|
'<div class="lpt-title">' + name + '</div>' +
|
||||||
|
'<div class="lpt-type">' + label + '</div>' +
|
||||||
|
'<div class="lpt-text">' + snip + '</div>' +
|
||||||
|
'</div>' +
|
||||||
|
'<div class="lpt-dot" style="background:' + color + '"></div>' +
|
||||||
|
'<button class="lpt-dismiss" title="Dismiss" onclick="if(typeof dismissTrigger===\'function\')dismissTrigger(' + t.id + ');refreshLeftPanelTriggers();">✕</button>' +
|
||||||
|
'</div>';
|
||||||
|
});
|
||||||
|
body.innerHTML = html;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate each seat's Cmd Dmg score-area box with per-opponent badges.
|
||||||
|
// Skips the seat's own player number from the display.
|
||||||
|
function renderCmdDmgInline() {
|
||||||
|
if (typeof gameState === 'undefined' || !gameState || !gameState.players) return;
|
||||||
|
var players = gameState.players;
|
||||||
|
for (var i = 0; i < players.length; i++) {
|
||||||
|
var box = document.getElementById('cmd-dmg-inline-' + i);
|
||||||
|
if (!box) continue;
|
||||||
|
var row = box.querySelector('.cmd-dmg-inline-row');
|
||||||
|
if (!row) continue;
|
||||||
|
var me = players[i];
|
||||||
|
if (!me) continue;
|
||||||
|
var html = '';
|
||||||
|
for (var j = 0; j < players.length; j++) {
|
||||||
|
if (j === i) continue; // leave own player number out
|
||||||
|
var op = players[j];
|
||||||
|
if (!op) continue;
|
||||||
|
var dmg = (me.commanderDamage && me.commanderDamage[op.id]) || 0;
|
||||||
|
var danger = dmg >= 21 ? ' danger' : dmg >= 15 ? ' crit' : dmg >= 8 ? ' warn' : '';
|
||||||
|
html +=
|
||||||
|
'<span class="cdi-badge' + danger + '" title="Commander damage from ' + (op.name||'P'+(j+1)) + '">' +
|
||||||
|
'<span class="cdi-dot" style="background:' + (op.color||'#888') + '"></span>' +
|
||||||
|
'P' + (j + 1) + ' <b>' + dmg + '</b>' +
|
||||||
|
'</span>';
|
||||||
|
}
|
||||||
|
row.innerHTML = html || '—';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hook into the trigger-stack badge updater so the left panel stays in sync.
|
||||||
|
(function() {
|
||||||
|
if (typeof window === 'undefined') return;
|
||||||
|
var tryHook = function() {
|
||||||
|
if (typeof window.updateTriggerBadge === 'function' && !window.updateTriggerBadge._leftPanelHooked) {
|
||||||
|
var orig = window.updateTriggerBadge;
|
||||||
|
window.updateTriggerBadge = function() {
|
||||||
|
var r = orig.apply(this, arguments);
|
||||||
|
try { refreshLeftPanelTriggers(); } catch(e) {}
|
||||||
|
return r;
|
||||||
|
};
|
||||||
|
window.updateTriggerBadge._leftPanelHooked = true;
|
||||||
|
}
|
||||||
|
// Also wrap adjustCommanderDamage so badges refresh after matrix clicks.
|
||||||
|
if (typeof window.adjustCommanderDamage === 'function' && !window.adjustCommanderDamage._cdmInlineHooked) {
|
||||||
|
var origAdj = window.adjustCommanderDamage;
|
||||||
|
window.adjustCommanderDamage = function() {
|
||||||
|
var r = origAdj.apply(this, arguments);
|
||||||
|
try { renderCmdDmgInline(); } catch(e) {}
|
||||||
|
return r;
|
||||||
|
};
|
||||||
|
window.adjustCommanderDamage._cdmInlineHooked = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (document.readyState === 'loading') {
|
||||||
|
document.addEventListener('DOMContentLoaded', tryHook);
|
||||||
|
} else {
|
||||||
|
tryHook();
|
||||||
|
}
|
||||||
|
// Also attempt the hook again a tick later (engine.js loads after this script in some orderings)
|
||||||
|
setTimeout(tryHook, 0);
|
||||||
|
setTimeout(tryHook, 500);
|
||||||
|
})();
|
||||||
|
|
||||||
|
// Listen for deck exports from the deck builder iframe
|
||||||
|
window.addEventListener('message', function(e) {
|
||||||
|
if (!e.data || e.data.type !== 'DECK_BUILDER_EXPORT') return;
|
||||||
|
|
||||||
|
var deckData = e.data;
|
||||||
|
// Build a decklist text string compatible with importDeckForPlayer()
|
||||||
|
var lines = [];
|
||||||
|
var commanders = deckData.deck.filter(function(c) { return c.is_commander; });
|
||||||
|
var nonCmd = deckData.deck.filter(function(c) { return !c.is_commander; });
|
||||||
|
|
||||||
|
if (commanders.length > 0) {
|
||||||
|
lines.push('[Commander]');
|
||||||
|
commanders.forEach(function(c) { lines.push('1 ' + c.name); });
|
||||||
|
lines.push('');
|
||||||
|
}
|
||||||
|
lines.push('[Deck]');
|
||||||
|
nonCmd.forEach(function(c) { lines.push((c.quantity || 1) + ' ' + c.name); });
|
||||||
|
|
||||||
|
// Store on window for the setup screen to pick up
|
||||||
|
window._pendingDeckImport = {
|
||||||
|
text: lines.join('\n'),
|
||||||
|
deckName: deckData.deckName || 'Imported Deck',
|
||||||
|
commander: deckData.commander || '',
|
||||||
|
};
|
||||||
|
|
||||||
|
// If a game is in progress, import into current player's slot
|
||||||
|
if (typeof gameState !== 'undefined' && gameState && typeof importDeckForPlayer === 'function') {
|
||||||
|
var playerIdx = gameState.currentPlayerIndex || 0;
|
||||||
|
importDeckForPlayer(playerIdx, window._pendingDeckImport.text).then(function() {
|
||||||
|
if (typeof renderHandArea === 'function') renderHandArea();
|
||||||
|
if (typeof renderPlayerPanels === 'function') renderPlayerPanels();
|
||||||
|
if (typeof addLogEntry === 'function') {
|
||||||
|
addLogEntry('Imported deck "' + window._pendingDeckImport.deckName + '" from Deck Builder');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (typeof addLogEntry === 'function') {
|
||||||
|
addLogEntry('Deck "' + window._pendingDeckImport.deckName + '" ready — start a game to use it.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the deck builder panel
|
||||||
|
toggleDeckBuilderPanel();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -228,6 +228,11 @@ function removeEmblem(playerId, idx) {
|
|||||||
renderEnhancedPanels();
|
renderEnhancedPanels();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// index: 0 1 2 3 → area: 1 2 4 3
|
||||||
|
// players[2] → bottom-RIGHT (area-4), players[3] → bottom-LEFT (area-3)
|
||||||
|
// Keeps turn order P1→P2→P3→P4 visually clockwise.
|
||||||
|
var PLAYER_INDEX_TO_QUADRANT = [1, 2, 4, 3];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Re-render all enhanced player panels in their quadrant positions.
|
* Re-render all enhanced player panels in their quadrant positions.
|
||||||
*/
|
*/
|
||||||
@@ -236,7 +241,7 @@ function renderEnhancedPanels() {
|
|||||||
|
|
||||||
for (var i = 0; i < gameState.players.length; i++) {
|
for (var i = 0; i < gameState.players.length; i++) {
|
||||||
var player = gameState.players[i];
|
var player = gameState.players[i];
|
||||||
var quadrant = i + 1;
|
var quadrant = PLAYER_INDEX_TO_QUADRANT[i] || (i + 1);
|
||||||
var areaEl = document.getElementById('player-area-' + quadrant);
|
var areaEl = document.getElementById('player-area-' + quadrant);
|
||||||
if (!areaEl) continue;
|
if (!areaEl) continue;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,426 @@
|
|||||||
|
/**
|
||||||
|
* precon-loader.js
|
||||||
|
* Handles loading precon .dck files from the backend into the lan-client simulator.
|
||||||
|
* Depends on: engine.js (gameState, initGame, addLogEntry, renderPlayerPanels)
|
||||||
|
* ai-bridge.js (AIBridge.deckSource.fetchPreconDeck)
|
||||||
|
* ui.js (setupDeckChoices, setupPlayerCount, setupStartingLife)
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Scryfall collection endpoint — accepts up to 75 card identifiers per call
|
||||||
|
const SCRYFALL_COLLECTION_URL = 'https://api.scryfall.com/cards/collection';
|
||||||
|
|
||||||
|
// ────────────────────────────────────────────────────────────
|
||||||
|
// Scryfall image cache: cardName -> { imageUrl, typeLine, ... }
|
||||||
|
// Persisted to localStorage so repeat games don't re-hit Scryfall.
|
||||||
|
// ────────────────────────────────────────────────────────────
|
||||||
|
const _SCRYFALL_CACHE_KEY = 'commanderLab.scryfallCache.v1';
|
||||||
|
const _SCRYFALL_CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000; // 30 days
|
||||||
|
const _SCRYFALL_CACHE_MAX_ENTRIES = 20000;
|
||||||
|
|
||||||
|
let _scryfallCache = {};
|
||||||
|
(function _loadScryfallCacheFromLocalStorage() {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(_SCRYFALL_CACHE_KEY);
|
||||||
|
if (!raw) return;
|
||||||
|
const parsed = JSON.parse(raw);
|
||||||
|
if (!parsed || typeof parsed !== 'object') return;
|
||||||
|
const now = Date.now();
|
||||||
|
const stamp = parsed.__stamp || 0;
|
||||||
|
if (now - stamp > _SCRYFALL_CACHE_TTL_MS) {
|
||||||
|
console.log('[scryfall-cache] expired (> 30 days), clearing');
|
||||||
|
localStorage.removeItem(_SCRYFALL_CACHE_KEY);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
delete parsed.__stamp;
|
||||||
|
_scryfallCache = parsed;
|
||||||
|
const n = Object.keys(_scryfallCache).length;
|
||||||
|
console.log('[scryfall-cache] loaded ' + n + ' entries from localStorage');
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[scryfall-cache] load failed:', e);
|
||||||
|
try { localStorage.removeItem(_SCRYFALL_CACHE_KEY); } catch (_) {}
|
||||||
|
_scryfallCache = {};
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
let _scryfallCacheSaveTimer = null;
|
||||||
|
function _persistScryfallCache() {
|
||||||
|
// Debounced — coalesce rapid writes during batch enrichment
|
||||||
|
if (_scryfallCacheSaveTimer) return;
|
||||||
|
_scryfallCacheSaveTimer = setTimeout(function () {
|
||||||
|
_scryfallCacheSaveTimer = null;
|
||||||
|
try {
|
||||||
|
// Trim to MAX_ENTRIES if over (drop entries with no imageUrl first)
|
||||||
|
const keys = Object.keys(_scryfallCache);
|
||||||
|
if (keys.length > _SCRYFALL_CACHE_MAX_ENTRIES) {
|
||||||
|
const trimmed = {};
|
||||||
|
const sorted = keys.sort((a, b) =>
|
||||||
|
(_scryfallCache[b].imageUrl ? 1 : 0) - (_scryfallCache[a].imageUrl ? 1 : 0)
|
||||||
|
).slice(0, _SCRYFALL_CACHE_MAX_ENTRIES);
|
||||||
|
for (const k of sorted) trimmed[k] = _scryfallCache[k];
|
||||||
|
_scryfallCache = trimmed;
|
||||||
|
}
|
||||||
|
const payload = Object.assign({ __stamp: Date.now() }, _scryfallCache);
|
||||||
|
localStorage.setItem(_SCRYFALL_CACHE_KEY, JSON.stringify(payload));
|
||||||
|
} catch (e) {
|
||||||
|
// Quota exceeded — clear and skip. Cache will rebuild next session.
|
||||||
|
console.warn('[scryfall-cache] persist failed (quota?):', e);
|
||||||
|
try { localStorage.removeItem(_SCRYFALL_CACHE_KEY); } catch (_) {}
|
||||||
|
}
|
||||||
|
}, 750);
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearScryfallCache() {
|
||||||
|
_scryfallCache = {};
|
||||||
|
try { localStorage.removeItem(_SCRYFALL_CACHE_KEY); } catch (_) {}
|
||||||
|
console.log('[scryfall-cache] cleared');
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// startGameWithDecks
|
||||||
|
// Called by ui.js startGame() when players have selected precon decks.
|
||||||
|
// ============================================================
|
||||||
|
async function startGameWithDecks() {
|
||||||
|
const playerNames = [];
|
||||||
|
for (let i = 0; i < (setupPlayerCount || 4); i++) {
|
||||||
|
const inp = document.getElementById('player-name-' + i);
|
||||||
|
playerNames.push(inp && inp.value.trim() ? inp.value.trim() : 'Player ' + (i + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
initGame(setupPlayerCount || 4, playerNames, setupStartingLife || 40);
|
||||||
|
|
||||||
|
const choices = typeof setupDeckChoices !== 'undefined' ? setupDeckChoices : {};
|
||||||
|
const loadPromises = Object.entries(choices).map(async ([idxStr, choice]) => {
|
||||||
|
const playerIdx = parseInt(idxStr, 10);
|
||||||
|
if (choice.type !== 'precon' || !choice.preconId) return;
|
||||||
|
try {
|
||||||
|
const fileName = choice.preconId.endsWith('.dck') ? choice.preconId : choice.preconId + '.dck';
|
||||||
|
const data = await AIBridge.deckSource.fetchPreconDeck(fileName);
|
||||||
|
if (!data || !data.decklist) {
|
||||||
|
addLogEntry('\u26A0 Could not load deck for Player ' + (playerIdx + 1) + ' (no decklist returned)');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
loadPreconDeckFromDck(playerIdx, data.decklist, choice.preconId);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[precon-loader] Failed to load deck for player', playerIdx, e);
|
||||||
|
addLogEntry('\u26A0 Deck load failed for Player ' + (playerIdx + 1) + ': ' + e.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(loadPromises);
|
||||||
|
|
||||||
|
if (typeof renderPlayerPanels === 'function') renderPlayerPanels();
|
||||||
|
if (typeof updateActivePlayerHighlight === 'function') updateActivePlayerHighlight();
|
||||||
|
|
||||||
|
// Block on Scryfall enrichment BEFORE the game becomes interactive. This is
|
||||||
|
// critical: the opening 7 cards are dealt via player.zones.hand.push(lib.shift())
|
||||||
|
// directly (bypassing drawCard()), so they don't get _enrichCardFromCache'd at
|
||||||
|
// draw time. Without awaiting here, the AI's first turn fires with empty
|
||||||
|
// typeLine/cmc/oracleText/isRamp/isRemoval flags and can only play lands.
|
||||||
|
addLogEntry('\u23F3 Loading card data from Scryfall...');
|
||||||
|
try {
|
||||||
|
await enrichAllZonesWithScryfall();
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[precon-loader] Enrichment failed, continuing anyway:', e);
|
||||||
|
}
|
||||||
|
addLogEntry('\u2705 Card data loaded. Game ready.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// loadPreconDeckFromDck
|
||||||
|
// Parses a Forge-format .dck file string and populates a player's zones.
|
||||||
|
// ============================================================
|
||||||
|
function loadPreconDeckFromDck(playerIdx, dckContent, deckLabel) {
|
||||||
|
if (!gameState) return;
|
||||||
|
const player = gameState.players[playerIdx];
|
||||||
|
if (!player) return;
|
||||||
|
|
||||||
|
let commander = '';
|
||||||
|
let section = '';
|
||||||
|
const mainCards = [];
|
||||||
|
|
||||||
|
for (const rawLine of dckContent.split('\n')) {
|
||||||
|
const line = rawLine.trim();
|
||||||
|
if (!line || line.startsWith('//')) continue;
|
||||||
|
|
||||||
|
if (line.startsWith('[') && line.endsWith(']')) {
|
||||||
|
section = line.slice(1, -1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (line.includes('=') && !line.match(/^\d/)) continue;
|
||||||
|
|
||||||
|
const m = line.match(/^(\d+)\s+(.+?)(?:\|.*)?$/);
|
||||||
|
if (!m) continue;
|
||||||
|
|
||||||
|
const qty = parseInt(m[1], 10);
|
||||||
|
const name = m[2].trim();
|
||||||
|
|
||||||
|
if (section === 'Commander') {
|
||||||
|
commander = name;
|
||||||
|
} else if (section === 'Main') {
|
||||||
|
for (let q = 0; q < qty; q++) mainCards.push(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
player.zones.command = [];
|
||||||
|
player.zones.library = [];
|
||||||
|
player.zones.hand = [];
|
||||||
|
player.zones.graveyard = [];
|
||||||
|
player.zones.exile = [];
|
||||||
|
|
||||||
|
if (commander) {
|
||||||
|
player.zones.command.push({
|
||||||
|
id: 'cmd-' + playerIdx + '-' + Date.now(),
|
||||||
|
name: commander,
|
||||||
|
imageUrl: '',
|
||||||
|
typeLine: 'Legendary Creature',
|
||||||
|
oracleText: '',
|
||||||
|
manaCost: '',
|
||||||
|
});
|
||||||
|
player.commanderCardName = commander;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const cardName of mainCards) {
|
||||||
|
player.zones.library.push({
|
||||||
|
id: 'lib-' + playerIdx + '-' + cardName + '-' + Math.random().toString(36).slice(2, 6),
|
||||||
|
name: cardName,
|
||||||
|
imageUrl: '',
|
||||||
|
typeLine: '',
|
||||||
|
oracleText: '',
|
||||||
|
manaCost: '',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fisher-Yates shuffle
|
||||||
|
const lib = player.zones.library;
|
||||||
|
for (let i = lib.length - 1; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[lib[i], lib[j]] = [lib[j], lib[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw opening hand
|
||||||
|
for (let d = 0; d < 7 && lib.length > 0; d++) {
|
||||||
|
player.zones.hand.push(lib.shift());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof updateZoneCountsForPlayer === 'function') updateZoneCountsForPlayer(playerIdx);
|
||||||
|
if (typeof renderCommanderZone === 'function') renderCommanderZone(playerIdx);
|
||||||
|
if (typeof renderHandZone === 'function') renderHandZone(playerIdx);
|
||||||
|
|
||||||
|
const label = deckLabel ? deckLabel.replace('.dck', '') : 'Unknown Deck';
|
||||||
|
addLogEntry(
|
||||||
|
'\uD83C\uDCCF <strong>' + player.name + '</strong> loaded: <em>' + label + '</em>' +
|
||||||
|
(commander ? ' \u2014 Commander: <strong>' + commander + '</strong>' : '') +
|
||||||
|
' (' + lib.length + ' in library, 7 in hand)'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// SCRYFALL IMAGE ENRICHMENT
|
||||||
|
// After zones are populated with card names, batch-fetch card
|
||||||
|
// data from Scryfall and backfill imageUrl on every card object.
|
||||||
|
// Uses the /cards/collection endpoint (75 cards per request).
|
||||||
|
// Results are cached in _scryfallCache so re-renders are instant.
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
async function enrichAllZonesWithScryfall() {
|
||||||
|
if (!gameState) return;
|
||||||
|
|
||||||
|
// Gather all unique card names across all players and all zones
|
||||||
|
const nameSet = new Set();
|
||||||
|
let totalCards = 0;
|
||||||
|
let cacheHits = 0;
|
||||||
|
for (const player of gameState.players) {
|
||||||
|
for (const zone of Object.values(player.zones)) {
|
||||||
|
for (const card of zone) {
|
||||||
|
if (!card.name) continue;
|
||||||
|
totalCards++;
|
||||||
|
if (_scryfallCache[card.name]) {
|
||||||
|
cacheHits++;
|
||||||
|
} else {
|
||||||
|
nameSet.add(card.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nameSet.size === 0) {
|
||||||
|
// Everything was in cache — just apply and bail, no network
|
||||||
|
addLogEntry('\u2705 Card images loaded from cache (' + cacheHits + ' cards).');
|
||||||
|
_applyScryfallDataToAllCards();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const names = Array.from(nameSet);
|
||||||
|
const hitRatio = totalCards > 0 ? Math.round((cacheHits / totalCards) * 100) : 0;
|
||||||
|
addLogEntry('\uD83C\uDF0D Fetching ' + names.length + ' new cards from Scryfall (' + hitRatio + '% cache hit rate)...');
|
||||||
|
|
||||||
|
// Split into batches of 75 (Scryfall max)
|
||||||
|
const BATCH = 75;
|
||||||
|
for (let i = 0; i < names.length; i += BATCH) {
|
||||||
|
const batch = names.slice(i, i + BATCH);
|
||||||
|
try {
|
||||||
|
await _fetchScryfallBatch(batch);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[precon-loader] Scryfall batch failed', e);
|
||||||
|
}
|
||||||
|
// Polite delay between batches to respect Scryfall rate limit (50-100ms)
|
||||||
|
if (i + BATCH < names.length) await _sleep(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply cached data to every card object in every zone
|
||||||
|
_applyScryfallDataToAllCards();
|
||||||
|
|
||||||
|
addLogEntry('\u2705 Card images loaded.');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _fetchScryfallBatch(names) {
|
||||||
|
const identifiers = names.map(name => ({ name }));
|
||||||
|
const resp = await fetch(SCRYFALL_COLLECTION_URL, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ identifiers }),
|
||||||
|
});
|
||||||
|
if (!resp.ok) throw new Error('Scryfall HTTP ' + resp.status);
|
||||||
|
const json = await resp.json();
|
||||||
|
|
||||||
|
for (const card of (json.data || [])) {
|
||||||
|
const name = card.name;
|
||||||
|
// Pick the best image: normal > large > small, handle double-faced
|
||||||
|
let imageUrl = '';
|
||||||
|
if (card.image_uris) {
|
||||||
|
imageUrl = card.image_uris.normal || card.image_uris.large || card.image_uris.small || '';
|
||||||
|
} else if (card.card_faces && card.card_faces[0] && card.card_faces[0].image_uris) {
|
||||||
|
imageUrl = card.card_faces[0].image_uris.normal || card.card_faces[0].image_uris.large || '';
|
||||||
|
}
|
||||||
|
_scryfallCache[name] = {
|
||||||
|
imageUrl,
|
||||||
|
typeLine: card.type_line || '',
|
||||||
|
oracleText: card.oracle_text || (card.card_faces && card.card_faces[0] ? card.card_faces[0].oracle_text : '') || '',
|
||||||
|
manaCost: card.mana_cost || (card.card_faces && card.card_faces[0] ? card.card_faces[0].mana_cost : '') || '',
|
||||||
|
cmc: card.cmc || 0,
|
||||||
|
power: card.power || '',
|
||||||
|
toughness: card.toughness || '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark not-found cards so we don't re-fetch them on next enrichment
|
||||||
|
for (const name of names) {
|
||||||
|
if (!_scryfallCache[name]) _scryfallCache[name] = { imageUrl: '', typeLine: '', oracleText: '', manaCost: '', cmc: 0, power: '', toughness: '' };
|
||||||
|
}
|
||||||
|
|
||||||
|
_persistScryfallCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
function _applyScryfallDataToAllCards() {
|
||||||
|
if (!gameState) return;
|
||||||
|
for (const player of gameState.players) {
|
||||||
|
for (const zone of Object.values(player.zones)) {
|
||||||
|
for (const card of zone) {
|
||||||
|
const data = _scryfallCache[card.name];
|
||||||
|
if (data) {
|
||||||
|
card.imageUrl = data.imageUrl || card.imageUrl;
|
||||||
|
card.typeLine = data.typeLine || card.typeLine;
|
||||||
|
card.oracleText = data.oracleText || card.oracleText;
|
||||||
|
card.manaCost = data.manaCost || card.manaCost;
|
||||||
|
card.cmc = data.cmc || card.cmc || 0;
|
||||||
|
if (data.power) card.power = data.power;
|
||||||
|
if (data.toughness) card.toughness = data.toughness;
|
||||||
|
|
||||||
|
// Tag roles from oracle text so aiCategorizeHand works
|
||||||
|
const ot = (card.oracleText || '').toLowerCase();
|
||||||
|
if (!card.isRamp) card.isRamp = /search your library for a.*land|add \{|adds? one mana/i.test(ot);
|
||||||
|
if (!card.isRemoval) card.isRemoval = /destroy target|exile target|counter target spell/i.test(ot);
|
||||||
|
if (!card.isBoardWipe) card.isBoardWipe = /destroy all|exile all|all creatures get -/i.test(ot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Also enrich battlefield cards owned by this player
|
||||||
|
if (gameState.battlefieldCards) {
|
||||||
|
for (const card of gameState.battlefieldCards) {
|
||||||
|
if (card.ownerIndex !== player.id) continue;
|
||||||
|
const data = _scryfallCache[card.name];
|
||||||
|
if (data) {
|
||||||
|
if (!card.imageUrl) card.imageUrl = data.imageUrl || '';
|
||||||
|
card.typeLine = data.typeLine || card.typeLine;
|
||||||
|
card.oracleText = data.oracleText || card.oracleText;
|
||||||
|
card.manaCost = data.manaCost || card.manaCost;
|
||||||
|
card.cmc = data.cmc || card.cmc || 0;
|
||||||
|
|
||||||
|
const ot = (card.oracleText || '').toLowerCase();
|
||||||
|
if (!card.isRamp) card.isRamp = /search your library for a.*land|add \{|adds? one mana/i.test(ot);
|
||||||
|
if (!card.isRemoval) card.isRemoval = /destroy target|exile target|counter target spell/i.test(ot);
|
||||||
|
if (!card.isBoardWipe) card.isBoardWipe = /destroy all|exile all|all creatures get -/i.test(ot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trigger UI refresh so images appear without a full reload
|
||||||
|
if (typeof renderPlayerPanels === 'function') renderPlayerPanels();
|
||||||
|
if (typeof renderCommanderZone === 'function') {
|
||||||
|
for (let i = 0; i < gameState.players.length; i++) renderCommanderZone(i);
|
||||||
|
}
|
||||||
|
if (typeof renderHandZone === 'function') {
|
||||||
|
for (let i = 0; i < gameState.players.length; i++) renderHandZone(i);
|
||||||
|
}
|
||||||
|
if (typeof updateZoneCounts === 'function') updateZoneCounts();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronously enrich a single card from the Scryfall cache.
|
||||||
|
* Called at draw time so cards have typeLine/cmc/oracle data
|
||||||
|
* before the AI's first turn, even if async enrichment hasn't completed.
|
||||||
|
*/
|
||||||
|
function _enrichCardFromCache(card) {
|
||||||
|
if (!card || !card.name) return;
|
||||||
|
const data = _scryfallCache[card.name];
|
||||||
|
if (!data) return;
|
||||||
|
card.imageUrl = data.imageUrl || card.imageUrl || '';
|
||||||
|
card.typeLine = data.typeLine || card.typeLine || '';
|
||||||
|
card.oracleText = data.oracleText || card.oracleText || '';
|
||||||
|
card.manaCost = data.manaCost || card.manaCost || '';
|
||||||
|
card.cmc = data.cmc || card.cmc || 0;
|
||||||
|
if (data.power) card.power = data.power;
|
||||||
|
if (data.toughness) card.toughness = data.toughness;
|
||||||
|
|
||||||
|
// Tag oracle roles
|
||||||
|
const ot = (card.oracleText || '').toLowerCase();
|
||||||
|
if (!card.isRamp) card.isRamp = /search your library for a.*land|add \{|adds? one mana/i.test(ot);
|
||||||
|
if (!card.isRemoval) card.isRemoval = /destroy target|exile target|counter target spell/i.test(ot);
|
||||||
|
if (!card.isBoardWipe) card.isBoardWipe = /destroy all|exile all|all creatures get -/i.test(ot);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enrich a single card object on-demand (useful for cards drawn or played after initial load).
|
||||||
|
* Returns a Promise that resolves when the card's imageUrl is set.
|
||||||
|
*/
|
||||||
|
async function enrichCardWithScryfall(card) {
|
||||||
|
if (!card || !card.name) return;
|
||||||
|
if (card.imageUrl) return; // already has image
|
||||||
|
|
||||||
|
const cached = _scryfallCache[card.name];
|
||||||
|
if (cached) {
|
||||||
|
card.imageUrl = cached.imageUrl || '';
|
||||||
|
card.typeLine = cached.typeLine || card.typeLine;
|
||||||
|
card.oracleText = cached.oracleText || card.oracleText;
|
||||||
|
card.manaCost = cached.manaCost || card.manaCost;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _fetchScryfallBatch([card.name]);
|
||||||
|
const data = _scryfallCache[card.name];
|
||||||
|
if (data) {
|
||||||
|
card.imageUrl = data.imageUrl || '';
|
||||||
|
card.typeLine = data.typeLine || card.typeLine;
|
||||||
|
card.oracleText = data.oracleText || card.oracleText;
|
||||||
|
card.manaCost = data.manaCost || card.manaCost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
+697
-173
File diff suppressed because it is too large.
Load diff
+264
-74
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* MTG Commander Simulator — ui.js
|
* MTG Commander Simulator — ui.js
|
||||||
* All UI rendering functions adapted for the 4-quadrant battlefield layout.
|
* All UI rendering functions adapted for the 4-quadrant battlefield layout.
|
||||||
* This file overrides/extends DOM-touching functions from engine.js.
|
* This file overrides/extends DOM-touching functions from engine.js.
|
||||||
* ============================================================
|
* ============================================================
|
||||||
@@ -17,6 +17,12 @@ let undoHistoryVisible = false;
|
|||||||
let aiTurnActive = false;
|
let aiTurnActive = false;
|
||||||
let floatingPreviewTimeout = null;
|
let floatingPreviewTimeout = null;
|
||||||
|
|
||||||
|
// Hand-to-battlefield drag state
|
||||||
|
let isDraggingFromHand = false;
|
||||||
|
let handDragPlayerIdx = null;
|
||||||
|
let handDragCardIdx = null;
|
||||||
|
let handDragGhost = null;
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// LOG SYSTEM (override engine stub)
|
// LOG SYSTEM (override engine stub)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -83,7 +89,7 @@ function setLife(playerId, newLife) {
|
|||||||
updateLifeCounterDisplay(playerId);
|
updateLifeCounterDisplay(playerId);
|
||||||
const diff = newLife - oldLife;
|
const diff = newLife - oldLife;
|
||||||
const sign = diff > 0 ? '+' : '';
|
const sign = diff > 0 ? '+' : '';
|
||||||
addLogEntry('<strong>' + player.name + '</strong> life: ' + oldLife + ' → <strong>' + newLife + '</strong> (' + sign + diff + ')');
|
addLogEntry('<strong>' + player.name + '</strong> life: ' + oldLife + ' → <strong>' + newLife + '</strong> (' + sign + diff + ')');
|
||||||
if (diff > 0) fireBattlefieldTriggers('life_gain');
|
if (diff > 0) fireBattlefieldTriggers('life_gain');
|
||||||
else if (diff < 0) fireBattlefieldTriggers('life_loss');
|
else if (diff < 0) fireBattlefieldTriggers('life_loss');
|
||||||
if (newLife <= 0 && !player.eliminated) eliminatePlayer(playerId, 'life total reached 0');
|
if (newLife <= 0 && !player.eliminated) eliminatePlayer(playerId, 'life total reached 0');
|
||||||
@@ -117,7 +123,7 @@ function updateActivePlayerHighlight() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// RENDER PLAYER PANELS (adapted for 4-quadrant — updates zones/labels)
|
// RENDER PLAYER PANELS (adapted for 4-quadrant — updates zones/labels)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
function renderPlayerPanels() {
|
function renderPlayerPanels() {
|
||||||
if (!gameState) return;
|
if (!gameState) return;
|
||||||
@@ -144,23 +150,13 @@ function updateZoneCountsForPlayer(playerIdx) {
|
|||||||
const libCount = document.getElementById('lib-count-' + pNum);
|
const libCount = document.getElementById('lib-count-' + pNum);
|
||||||
if (libCount) libCount.textContent = player.zones.library.length;
|
if (libCount) libCount.textContent = player.zones.library.length;
|
||||||
|
|
||||||
// Update exile count
|
|
||||||
const exCount = document.getElementById('exile-count-' + pNum);
|
|
||||||
if (exCount) {
|
|
||||||
exCount.textContent = player.zones.exile.length;
|
|
||||||
exCount.style.display = player.zones.exile.length > 0 ? 'block' : 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update graveyard count
|
|
||||||
const gvCount = document.getElementById('gv-count-' + pNum);
|
|
||||||
if (gvCount) {
|
|
||||||
gvCount.textContent = player.zones.graveyard.length;
|
|
||||||
gvCount.style.display = player.zones.graveyard.length > 0 ? 'block' : 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update hand count
|
// Update hand count
|
||||||
const handCount = document.getElementById('hand-count-' + pNum);
|
const handCount = document.getElementById('hand-count-' + pNum);
|
||||||
if (handCount) handCount.textContent = player.zones.hand.length;
|
if (handCount) handCount.textContent = player.zones.hand.length;
|
||||||
|
|
||||||
|
// Exile + Graveyard: render last card image face-up
|
||||||
|
renderLastCardInZone(playerIdx, 'exile');
|
||||||
|
renderLastCardInZone(playerIdx, 'graveyard');
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePlayerZones() {
|
function updatePlayerZones() {
|
||||||
@@ -211,6 +207,50 @@ function renderCommanderZone(playerIdx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// EXILE / GRAVEYARD — Show last card image face-up
|
||||||
|
// ============================================================
|
||||||
|
function renderLastCardInZone(playerIdx, zoneName) {
|
||||||
|
if (!gameState) return;
|
||||||
|
const player = gameState.players[playerIdx];
|
||||||
|
if (!player) return;
|
||||||
|
const pNum = playerIdx + 1;
|
||||||
|
|
||||||
|
const zoneIdPrefix = (zoneName === 'exile') ? 'exile-zone-' : 'gv-zone-';
|
||||||
|
const zoneEl = document.getElementById(zoneIdPrefix + pNum);
|
||||||
|
if (!zoneEl) return;
|
||||||
|
|
||||||
|
const cards = player.zones[zoneName] || [];
|
||||||
|
const lastCard = cards[cards.length - 1]; // most recently added card
|
||||||
|
|
||||||
|
if (lastCard && lastCard.imageUrl) {
|
||||||
|
const safeImg = (lastCard.imageUrl || '').replace(/'/g, "\\'");
|
||||||
|
const safeName = (lastCard.name || '').replace(/'/g, "\\'");
|
||||||
|
const safeType = (lastCard.typeLine || '').replace(/'/g, "\\'");
|
||||||
|
zoneEl.innerHTML =
|
||||||
|
'<img src="' + safeImg + '" alt="' + (lastCard.name || zoneName) + '" ' +
|
||||||
|
'style="width:100%;height:100%;object-fit:cover;border-radius:6px;" ' +
|
||||||
|
'onmouseenter="showFloatingCardPreview(\'' + safeImg + '\', event, {name:\'' + safeName + '\', typeLine:\'' + safeType + '\'})" ' +
|
||||||
|
'onmousemove="positionFloatingPreview(event)" ' +
|
||||||
|
'onmouseleave="hideFloatingCardPreview()" ' +
|
||||||
|
'draggable="false" />';
|
||||||
|
// Always show count badge on top
|
||||||
|
if (cards.length > 0) {
|
||||||
|
zoneEl.innerHTML += '<span class="card-count" id="' + (zoneName === 'exile' ? 'exile' : 'gv') + '-count-' + pNum + '" ' +
|
||||||
|
'style="display:block;">' + cards.length + '</span>';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback: icon + name + count (original look)
|
||||||
|
const icon = (zoneName === 'exile') ? '⬡' : '☠';
|
||||||
|
const label = (zoneName === 'exile') ? 'Exile' : 'Grave';
|
||||||
|
const countId = (zoneName === 'exile') ? 'exile-count-' : 'gv-count-';
|
||||||
|
zoneEl.innerHTML =
|
||||||
|
'<span class="card-count" id="' + countId + pNum + '" style="' + (cards.length > 0 ? 'display:block;' : 'display:none;') + '">' + cards.length + '</span>' +
|
||||||
|
'<span class="zone-icon">' + icon + '</span>' +
|
||||||
|
'<span class="zone-name">' + label + '</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// HAND ZONE RENDERING (thumbnails in zone.hand)
|
// HAND ZONE RENDERING (thumbnails in zone.hand)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -223,6 +263,7 @@ function renderHandZone(playerIdx) {
|
|||||||
if (!handEl) return;
|
if (!handEl) return;
|
||||||
|
|
||||||
const hand = player.zones.hand || [];
|
const hand = player.zones.hand || [];
|
||||||
|
const hideFaces = !!player.isAI; // AI hands render face-down
|
||||||
|
|
||||||
// Update count badge
|
// Update count badge
|
||||||
const countEl = document.getElementById('hand-count-' + pNum);
|
const countEl = document.getElementById('hand-count-' + pNum);
|
||||||
@@ -240,13 +281,23 @@ function renderHandZone(playerIdx) {
|
|||||||
const maxShow = Math.min(hand.length, 7); // limit thumbnails
|
const maxShow = Math.min(hand.length, 7); // limit thumbnails
|
||||||
for (let i = 0; i < maxShow; i++) {
|
for (let i = 0; i < maxShow; i++) {
|
||||||
const card = hand[i];
|
const card = hand[i];
|
||||||
|
if (hideFaces) {
|
||||||
|
// AI hand — show a face-down card back (no name, no image, no preview)
|
||||||
|
thumbsHtml +=
|
||||||
|
'<div class="hand-thumb face-down" ' +
|
||||||
|
'data-player="' + playerIdx + '" data-hand-index="' + i + '" ' +
|
||||||
|
'title="Opponent hand (hidden)"></div>';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const imgSrc = card.imageUrl || '';
|
const imgSrc = card.imageUrl || '';
|
||||||
const escapedName = (card.name || '').replace(/'/g, "\\'");
|
const escapedName = (card.name || '').replace(/'/g, "\\'");
|
||||||
const escapedType = (card.typeLine || '').replace(/'/g, "\\'");
|
const escapedType = (card.typeLine || '').replace(/'/g, "\\'");
|
||||||
if (imgSrc) {
|
if (imgSrc) {
|
||||||
thumbsHtml +=
|
thumbsHtml +=
|
||||||
'<div class="hand-thumb" ' +
|
'<div class="hand-thumb" ' +
|
||||||
|
'data-player="' + playerIdx + '" data-hand-index="' + i + '" ' +
|
||||||
'onclick="openZoneModal(' + playerIdx + ')" ' +
|
'onclick="openZoneModal(' + playerIdx + ')" ' +
|
||||||
|
'onmousedown="startHandDrag(event,' + playerIdx + ',' + i + ',\'' + imgSrc.replace(/'/g, "\\'") + '\')" ' +
|
||||||
'onmouseenter="showFloatingCardPreview(\'' + imgSrc.replace(/'/g, "\\'") + '\',event,{name:\'' + escapedName + '\',typeLine:\'' + escapedType + '\'})" ' +
|
'onmouseenter="showFloatingCardPreview(\'' + imgSrc.replace(/'/g, "\\'") + '\',event,{name:\'' + escapedName + '\',typeLine:\'' + escapedType + '\'})" ' +
|
||||||
'onmousemove="positionFloatingPreview(event)" ' +
|
'onmousemove="positionFloatingPreview(event)" ' +
|
||||||
'onmouseleave="hideFloatingCardPreview()">' +
|
'onmouseleave="hideFloatingCardPreview()">' +
|
||||||
@@ -259,7 +310,11 @@ function renderHandZone(playerIdx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (hand.length > maxShow) {
|
if (hand.length > maxShow) {
|
||||||
thumbsHtml += '<div class="hand-thumb" onclick="openZoneModal(' + playerIdx + ')" style="background:rgba(255,255,255,0.05);display:flex;align-items:center;justify-content:center;font-size:9px;color:rgba(255,255,255,0.4);">+' + (hand.length - maxShow) + '</div>';
|
if (hideFaces) {
|
||||||
|
thumbsHtml += '<div class="hand-thumb face-down overflow" title="Opponent hand (hidden)">+' + (hand.length - maxShow) + '</div>';
|
||||||
|
} else {
|
||||||
|
thumbsHtml += '<div class="hand-thumb" onclick="openZoneModal(' + playerIdx + ')" style="background:rgba(255,255,255,0.05);display:flex;align-items:center;justify-content:center;font-size:9px;color:rgba(255,255,255,0.4);">+' + (hand.length - maxShow) + '</div>';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handEl.innerHTML = thumbsHtml;
|
handEl.innerHTML = thumbsHtml;
|
||||||
@@ -290,7 +345,6 @@ function renderBattlefieldCard(cardData) {
|
|||||||
card.style.top = (cardData.y || 10) + 'px';
|
card.style.top = (cardData.y || 10) + 'px';
|
||||||
|
|
||||||
const playerColor = (gameState.players[cardData.ownerIndex] || {}).color || 'transparent';
|
const playerColor = (gameState.players[cardData.ownerIndex] || {}).color || 'transparent';
|
||||||
card.style.borderColor = playerColor;
|
|
||||||
|
|
||||||
if (cardData.tapped) card.classList.add('tapped');
|
if (cardData.tapped) card.classList.add('tapped');
|
||||||
if (cardData.faceDown) card.classList.add('face-down');
|
if (cardData.faceDown) card.classList.add('face-down');
|
||||||
@@ -324,7 +378,6 @@ function renderBattlefieldCard(cardData) {
|
|||||||
card.innerHTML =
|
card.innerHTML =
|
||||||
protBadge +
|
protBadge +
|
||||||
'<img src="' + imgUrl + '" alt="' + altName + '" draggable="false" loading="lazy"/>' +
|
'<img src="' + imgUrl + '" alt="' + altName + '" draggable="false" loading="lazy"/>' +
|
||||||
'<div class="card-owner-bar" style="background:' + playerColor + '"></div>' +
|
|
||||||
kwBadgeHtml;
|
kwBadgeHtml;
|
||||||
|
|
||||||
// Counter badges
|
// Counter badges
|
||||||
@@ -358,7 +411,7 @@ function renderBattlefieldCard(cardData) {
|
|||||||
card.appendChild(dmgBadge);
|
card.appendChild(dmgBadge);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event listeners — use both for compatibility (Comet: inline onclick already on container)
|
// Event listeners — use both for compatibility (Comet: inline onclick already on container)
|
||||||
card.addEventListener('mousedown', e => {
|
card.addEventListener('mousedown', e => {
|
||||||
if (e.button !== 0) return;
|
if (e.button !== 0) return;
|
||||||
if (cardData.attachedTo) return;
|
if (cardData.attachedTo) return;
|
||||||
@@ -523,6 +576,63 @@ document.addEventListener('mouseup', e => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// HAND-TO-BATTLEFIELD DRAG & DROP
|
||||||
|
// ============================================================
|
||||||
|
function startHandDrag(e, playerIdx, handIndex, imgSrc) {
|
||||||
|
// Only left mouse button
|
||||||
|
if (e.button !== 0) return;
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
|
isDraggingFromHand = true;
|
||||||
|
handDragPlayerIdx = playerIdx;
|
||||||
|
handDragCardIdx = handIndex;
|
||||||
|
hideFloatingCardPreview();
|
||||||
|
|
||||||
|
// Create ghost element
|
||||||
|
handDragGhost = document.createElement('div');
|
||||||
|
handDragGhost.className = 'hand-drag-ghost';
|
||||||
|
handDragGhost.innerHTML = '<img src="' + imgSrc + '" draggable="false"/>';
|
||||||
|
handDragGhost.style.left = e.clientX + 'px';
|
||||||
|
handDragGhost.style.top = e.clientY + 'px';
|
||||||
|
document.body.appendChild(handDragGhost);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('mousemove', function(e) {
|
||||||
|
if (!isDraggingFromHand || !handDragGhost) return;
|
||||||
|
handDragGhost.style.left = e.clientX + 'px';
|
||||||
|
handDragGhost.style.top = e.clientY + 'px';
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('mouseup', function(e) {
|
||||||
|
if (!isDraggingFromHand) return;
|
||||||
|
|
||||||
|
// Clean up ghost
|
||||||
|
if (handDragGhost) {
|
||||||
|
handDragGhost.remove();
|
||||||
|
handDragGhost = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if dropped over a battle-zone
|
||||||
|
const pNum = handDragPlayerIdx + 1;
|
||||||
|
const battleZone = document.getElementById('battle-zone-' + pNum);
|
||||||
|
if (battleZone) {
|
||||||
|
const rect = battleZone.getBoundingClientRect();
|
||||||
|
if (e.clientX >= rect.left && e.clientX <= rect.right &&
|
||||||
|
e.clientY >= rect.top && e.clientY <= rect.bottom) {
|
||||||
|
// Play the card
|
||||||
|
if (typeof playCardFromHand === 'function') {
|
||||||
|
playCardFromHand(handDragPlayerIdx, handDragCardIdx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isDraggingFromHand = false;
|
||||||
|
handDragPlayerIdx = null;
|
||||||
|
handDragCardIdx = null;
|
||||||
|
});
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// PHASE TRACKER
|
// PHASE TRACKER
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -570,7 +680,7 @@ function handlePhaseClick(phaseIdx) {
|
|||||||
// Just provide info about the phase, don't jump to it
|
// Just provide info about the phase, don't jump to it
|
||||||
if (!gameState) return;
|
if (!gameState) return;
|
||||||
const phase = PHASES[phaseIdx];
|
const phase = PHASES[phaseIdx];
|
||||||
if (phase) addLogEntry('Phase info: <strong>' + phase.label + '</strong> — ' + phase.tip);
|
if (phase) addLogEntry('Phase info: <strong>' + phase.label + '</strong> — ' + phase.tip);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTurnDisplay() {
|
function updateTurnDisplay() {
|
||||||
@@ -595,29 +705,7 @@ function updateStormCounter() {
|
|||||||
// MANA POOL DISPLAY
|
// MANA POOL DISPLAY
|
||||||
// ============================================================
|
// ============================================================
|
||||||
function renderManaPool(playerIdx) {
|
function renderManaPool(playerIdx) {
|
||||||
if (!gameState) return;
|
// mana pool display removed (Phase 1)
|
||||||
const pool = getManaPool(playerIdx !== undefined ? playerIdx : gameState.currentPlayerIndex);
|
|
||||||
const display = document.getElementById('mana-pool-display');
|
|
||||||
if (!display) return;
|
|
||||||
|
|
||||||
const colors = ['W', 'U', 'B', 'R', 'G', 'C'];
|
|
||||||
let html = '<span class="mana-pool-label">MANA:</span>';
|
|
||||||
let hasAny = false;
|
|
||||||
|
|
||||||
colors.forEach(c => {
|
|
||||||
if (pool[c] > 0) {
|
|
||||||
hasAny = true;
|
|
||||||
html += '<span class="mana-pip mana-' + c + '" title="' + getManaColorName(c) + ': ' + pool[c] + '">' + pool[c] + '</span>';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!hasAny) {
|
|
||||||
html += '<span class="mana-empty">Empty</span>';
|
|
||||||
}
|
|
||||||
|
|
||||||
html += '<button class="mana-pool-clear-btn" onclick="clearManaPool(' + gameState.currentPlayerIndex + ')" title="Clear mana pool">x</button>';
|
|
||||||
display.innerHTML = html;
|
|
||||||
display.style.display = gameState.inGame ? 'flex' : 'none';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -768,6 +856,20 @@ function openZoneModal(playerId, zone) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function closeZoneModal() {
|
function closeZoneModal() {
|
||||||
|
// Auto-shuffle the library when closing, and ONLY the library
|
||||||
|
if (zoneModalPlayerId !== null && gameState) {
|
||||||
|
const player = gameState.players[zoneModalPlayerId];
|
||||||
|
if (player && player.zones.library.length > 1) {
|
||||||
|
const lib = player.zones.library;
|
||||||
|
for (let i = lib.length - 1; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[lib[i], lib[j]] = [lib[j], lib[i]];
|
||||||
|
}
|
||||||
|
addLogEntry('🔀 <strong>' + player.name + "'s</strong> library shuffled.");
|
||||||
|
updateZoneCountsForPlayer(zoneModalPlayerId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const modal = document.getElementById('zone-modal');
|
const modal = document.getElementById('zone-modal');
|
||||||
if (modal) modal.classList.remove('visible');
|
if (modal) modal.classList.remove('visible');
|
||||||
zoneModalPlayerId = null;
|
zoneModalPlayerId = null;
|
||||||
@@ -784,19 +886,28 @@ function renderZoneModal() {
|
|||||||
const player = gameState.players[zoneModalPlayerId];
|
const player = gameState.players[zoneModalPlayerId];
|
||||||
if (!player) return;
|
if (!player) return;
|
||||||
|
|
||||||
|
// AI players' hands are private — don't allow the Hand tab to be active
|
||||||
|
if (player.isAI && activeZoneTab === 'hand') {
|
||||||
|
activeZoneTab = 'graveyard';
|
||||||
|
}
|
||||||
|
|
||||||
const titleEl = document.getElementById('zone-modal-title');
|
const titleEl = document.getElementById('zone-modal-title');
|
||||||
if (titleEl) titleEl.textContent = player.name + "'s Zones";
|
if (titleEl) titleEl.textContent = player.name + "'s Zones";
|
||||||
|
|
||||||
// Render tabs
|
// Render tabs
|
||||||
const tabsEl = document.getElementById('zone-modal-tabs');
|
const tabsEl = document.getElementById('zone-modal-tabs');
|
||||||
if (tabsEl) {
|
if (tabsEl) {
|
||||||
const zones = [
|
let zones = [
|
||||||
{ key: 'graveyard', icon: '☠', label: 'Graveyard' },
|
{ key: 'graveyard', icon: '☠', label: 'Graveyard' },
|
||||||
{ key: 'exile', icon: '⬡', label: 'Exile' },
|
{ key: 'exile', icon: '⬡', label: 'Exile' },
|
||||||
{ key: 'command', icon: '👑', label: 'Command' },
|
{ key: 'command', icon: '👑', label: 'Command' },
|
||||||
{ key: 'hand', icon: '✋', label: 'Hand' },
|
{ key: 'hand', icon: '✋', label: 'Hand' },
|
||||||
{ key: 'library', icon: '📚', label: 'Library' },
|
{ key: 'library', icon: '📚', label: 'Library' },
|
||||||
];
|
];
|
||||||
|
// Strip the Hand tab when inspecting an AI opponent (hand is private)
|
||||||
|
if (player.isAI) {
|
||||||
|
zones = zones.filter(z => z.key !== 'hand');
|
||||||
|
}
|
||||||
|
|
||||||
tabsEl.innerHTML = zones.map(z => {
|
tabsEl.innerHTML = zones.map(z => {
|
||||||
const count = (player.zones[z.key] || []).length;
|
const count = (player.zones[z.key] || []).length;
|
||||||
@@ -955,7 +1066,7 @@ function handleZmCtxAction(playerId, zone, cardIndex, action) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// HAND AREA — full hand display (used in hand-area section, NOT the zone thumbnails)
|
// HAND AREA — full hand display (used in hand-area section, NOT the zone thumbnails)
|
||||||
// For the new layout, hand is rendered as thumbnails in the zone.hand element
|
// For the new layout, hand is rendered as thumbnails in the zone.hand element
|
||||||
// ============================================================
|
// ============================================================
|
||||||
function renderHandArea() {
|
function renderHandArea() {
|
||||||
@@ -1014,6 +1125,8 @@ function initSetupScreen() {
|
|||||||
'<button class="ai-diff-btn active" onclick="window._setAiDiff(this,\'easy\')" data-diff="easy" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:rgba(91,141,217,0.2);color:rgba(255,255,255,0.7);cursor:pointer;">Easy</button>' +
|
'<button class="ai-diff-btn active" onclick="window._setAiDiff(this,\'easy\')" data-diff="easy" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:rgba(91,141,217,0.2);color:rgba(255,255,255,0.7);cursor:pointer;">Easy</button>' +
|
||||||
'<button class="ai-diff-btn" onclick="window._setAiDiff(this,\'normal\')" data-diff="normal" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:transparent;color:rgba(255,255,255,0.5);cursor:pointer;">Normal</button>' +
|
'<button class="ai-diff-btn" onclick="window._setAiDiff(this,\'normal\')" data-diff="normal" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:transparent;color:rgba(255,255,255,0.5);cursor:pointer;">Normal</button>' +
|
||||||
'<button class="ai-diff-btn" onclick="window._setAiDiff(this,\'hard\')" data-diff="hard" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:transparent;color:rgba(255,255,255,0.5);cursor:pointer;">Hard</button>' +
|
'<button class="ai-diff-btn" onclick="window._setAiDiff(this,\'hard\')" data-diff="hard" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:transparent;color:rgba(255,255,255,0.5);cursor:pointer;">Hard</button>' +
|
||||||
|
'<button class="ai-diff-btn" onclick="window._setAiDiff(this,\'aggro\')" data-diff="aggro" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:transparent;color:rgba(255,255,255,0.5);cursor:pointer;">Aggro</button>' +
|
||||||
|
'<button class="ai-diff-btn" onclick="window._setAiDiff(this,\'combo\')" data-diff="combo" style="font-size:11px;padding:3px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.1);background:transparent;color:rgba(255,255,255,0.5);cursor:pointer;">Combo</button>' +
|
||||||
'</div>' +
|
'</div>' +
|
||||||
'</div>' +
|
'</div>' +
|
||||||
|
|
||||||
@@ -1034,6 +1147,12 @@ function initSetupScreen() {
|
|||||||
'<div id="deck-assignment-grid" class="deck-assignment-grid"></div>' +
|
'<div id="deck-assignment-grid" class="deck-assignment-grid"></div>' +
|
||||||
'</div>' +
|
'</div>' +
|
||||||
|
|
||||||
|
'<div class="setup-section" style="display:flex;gap:8px;justify-content:center;flex-wrap:wrap;">' +
|
||||||
|
'<button class="setup-tool-btn" onclick="toggleDeckBuilderPanel()" style="display:flex;align-items:center;gap:6px;padding:8px 16px;border-radius:6px;border:1px solid rgba(255,255,255,0.12);background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.7);cursor:pointer;font-size:12px;">🃏 Deck Builder</button>' +
|
||||||
|
'<button class="setup-tool-btn" onclick="toggleDeckGenPanel()" style="display:flex;align-items:center;gap:6px;padding:8px 16px;border-radius:6px;border:1px solid rgba(255,255,255,0.12);background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.7);cursor:pointer;font-size:12px;">⚡ Auto Gen</button>' +
|
||||||
|
'<button class="setup-tool-btn" onclick="toggleCollectionPanel()" style="display:flex;align-items:center;gap:6px;padding:8px 16px;border-radius:6px;border:1px solid rgba(255,255,255,0.12);background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.7);cursor:pointer;font-size:12px;">📚 Collection</button>' +
|
||||||
|
'</div>' +
|
||||||
|
|
||||||
'<button class="btn-start" onclick="startGame()">Begin Battle</button>' +
|
'<button class="btn-start" onclick="startGame()">Begin Battle</button>' +
|
||||||
'</div>';
|
'</div>';
|
||||||
|
|
||||||
@@ -1123,7 +1242,23 @@ function initSetupScreen() {
|
|||||||
// Sync AI config to engine globals
|
// Sync AI config to engine globals
|
||||||
setupAiPlayers = _localAiSet;
|
setupAiPlayers = _localAiSet;
|
||||||
setupAiDifficulty = _localAiDiff;
|
setupAiDifficulty = _localAiDiff;
|
||||||
initGame(selectedPlayerCount, playerNames, startingLife);
|
setupPlayerCount = selectedPlayerCount;
|
||||||
|
setupStartingLife = startingLife;
|
||||||
|
|
||||||
|
// Sync player names to engine globals
|
||||||
|
for (let i = 0; i < playerNames.length; i++) {
|
||||||
|
var nameInput = document.getElementById('player-name-' + i);
|
||||||
|
if (nameInput) nameInput.value = playerNames[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// If any deck choices were made, use startGameWithDecks (async deck loading)
|
||||||
|
var hasDecks = typeof setupDeckChoices !== 'undefined' &&
|
||||||
|
Object.keys(setupDeckChoices).length > 0;
|
||||||
|
if (hasDecks && typeof startGameWithDecks === 'function') {
|
||||||
|
startGameWithDecks();
|
||||||
|
} else {
|
||||||
|
initGame(selectedPlayerCount, playerNames, startingLife);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Trigger display update for player count
|
// Trigger display update for player count
|
||||||
@@ -1308,18 +1443,83 @@ function switchDeckTab(playerIdx, tab) {
|
|||||||
case 'mydecks':
|
case 'mydecks':
|
||||||
content.innerHTML =
|
content.innerHTML =
|
||||||
'<div class="deck-mydecks-content">' +
|
'<div class="deck-mydecks-content">' +
|
||||||
'<div class="deck-tab-empty">' +
|
'<div class="deck-mydecks-list" id="mydecks-list-' + playerIdx + '">' +
|
||||||
'<div style="margin-bottom:8px;">Import a decklist:</div>' +
|
'<div style="color:var(--color-text-faint,#888);font-size:12px;padding:12px;">Loading user decks...</div>' +
|
||||||
'<textarea class="deck-import-textarea" id="deck-import-' + playerIdx + '" ' +
|
|
||||||
'placeholder="Paste decklist here...\n1 Sol Ring\n1 Command Tower\n..."></textarea>' +
|
|
||||||
'<button class="btn btn-sm" style="margin-top:6px;" ' +
|
|
||||||
'onclick="importDeckFromTextarea(' + playerIdx + ')">Import Decklist</button>' +
|
|
||||||
'</div>' +
|
'</div>' +
|
||||||
'</div>';
|
'</div>';
|
||||||
|
_loadMyDecksTab(playerIdx);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _loadMyDecksTab(playerIdx) {
|
||||||
|
var listEl = document.getElementById('mydecks-list-' + playerIdx);
|
||||||
|
if (!listEl) return;
|
||||||
|
|
||||||
|
var apiBase = (typeof LAB_API_BASE !== 'undefined' && LAB_API_BASE) ? LAB_API_BASE : '';
|
||||||
|
fetch(apiBase + '/api/lab/decks')
|
||||||
|
.then(function(res) { return res.json(); })
|
||||||
|
.then(function(data) {
|
||||||
|
var decks = (data.decks || []).filter(function(d) { return d.source === 'deckbuilder'; });
|
||||||
|
if (decks.length === 0) {
|
||||||
|
listEl.innerHTML = '<div style="color:var(--color-text-faint,#888);font-size:12px;padding:12px;">No saved decks. Use the Deck Builder or Auto Gen to create one.</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var html = '';
|
||||||
|
decks.forEach(function(d) {
|
||||||
|
var name = d.name || 'Untitled';
|
||||||
|
var cmdr = d.commander || d.commander_name || '';
|
||||||
|
var count = d.totalCards || d.card_count || d.total_cards || 0;
|
||||||
|
html += '<button class="deck-precon-item" onclick="selectUserDeck(' + playerIdx + ',' + d.deck_id + ',\'' + name.replace(/'/g, "\\'") + '\')">' +
|
||||||
|
'<span class="deck-precon-name">' + name.replace(/</g, '<') + '</span>' +
|
||||||
|
'<span class="deck-precon-cmdr">' + (cmdr ? cmdr.replace(/</g, '<') + ' \u2014 ' : '') + count + ' cards</span>' +
|
||||||
|
'</button>';
|
||||||
|
});
|
||||||
|
listEl.innerHTML = html;
|
||||||
|
})
|
||||||
|
.catch(function(e) {
|
||||||
|
console.warn('[_loadMyDecksTab] Error:', e);
|
||||||
|
listEl.innerHTML = '<div style="color:var(--color-text-faint,#888);font-size:12px;padding:12px;">Could not load decks. Is the server running?</div>';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectUserDeck(playerIdx, deckId, deckName) {
|
||||||
|
var statusEl = document.getElementById('deck-status-' + playerIdx);
|
||||||
|
if (statusEl) { statusEl.textContent = 'Exporting...'; statusEl.className = 'deck-select-status'; }
|
||||||
|
|
||||||
|
// Highlight selected button
|
||||||
|
var listEl = document.getElementById('mydecks-list-' + playerIdx);
|
||||||
|
if (listEl) {
|
||||||
|
listEl.querySelectorAll('.deck-precon-item').forEach(function(b) { b.classList.remove('selected'); });
|
||||||
|
// Find the clicked button
|
||||||
|
listEl.querySelectorAll('.deck-precon-item').forEach(function(b) {
|
||||||
|
if (b.querySelector('.deck-precon-name') && b.querySelector('.deck-precon-name').textContent === deckName) {
|
||||||
|
b.classList.add('selected');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export to .dck then store the choice
|
||||||
|
var apiBase = (typeof LAB_API_BASE !== 'undefined' && LAB_API_BASE) ? LAB_API_BASE : '';
|
||||||
|
fetch(apiBase + '/api/decks/' + deckId + '/export-to-sim', { method: 'POST' })
|
||||||
|
.then(function(res) { return res.json(); })
|
||||||
|
.then(function(result) {
|
||||||
|
if (result.success) {
|
||||||
|
if (typeof setupDeckChoices !== 'undefined') {
|
||||||
|
setupDeckChoices[playerIdx] = { type: 'user-deck', deckName: result.deckName, fileName: result.deckName + '.dck' };
|
||||||
|
}
|
||||||
|
if (statusEl) { statusEl.textContent = deckName + ' (' + (result.totalCards || '?') + ' cards)'; statusEl.className = 'deck-select-status loaded'; }
|
||||||
|
} else {
|
||||||
|
if (statusEl) { statusEl.textContent = 'Export failed'; statusEl.className = 'deck-select-status'; }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(function(e) {
|
||||||
|
console.error('[selectUserDeck] Export failed:', e);
|
||||||
|
if (statusEl) { statusEl.textContent = 'Export error'; statusEl.className = 'deck-select-status'; }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function filterPrecons(playerIdx, query) {
|
function filterPrecons(playerIdx, query) {
|
||||||
var list = document.getElementById('precon-list-' + playerIdx);
|
var list = document.getElementById('precon-list-' + playerIdx);
|
||||||
if (!list) return;
|
if (!list) return;
|
||||||
@@ -1461,15 +1661,9 @@ function initGame(playerCount, playerNames, startingLife) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign precon decks if selected
|
// NOTE: Precon deck loading is handled by the "Begin Battle" button
|
||||||
for (let i = 0; i < playerCount; i++) {
|
// which calls startGameWithDecks() when setupDeckChoices has entries.
|
||||||
const sel = document.getElementById('deck-sel-' + i);
|
// The old deck-sel-{i} dropdown path is removed.
|
||||||
if (sel && sel.value) {
|
|
||||||
const preconList2 = (typeof PRECON_DATA !== 'undefined') ? PRECON_DATA : (typeof PRECONS !== 'undefined') ? PRECONS : [];
|
|
||||||
const precon = preconList2.find(p => p.id === sel.value);
|
|
||||||
if (precon) loadPreconDeck(i, precon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark AI players
|
// Mark AI players
|
||||||
setupAiPlayers.forEach(function(idx) {
|
setupAiPlayers.forEach(function(idx) {
|
||||||
@@ -1863,10 +2057,7 @@ function showToast(message) {
|
|||||||
// MANA POOL DISPLAY BUTTON
|
// MANA POOL DISPLAY BUTTON
|
||||||
// ============================================================
|
// ============================================================
|
||||||
function showManaPool() {
|
function showManaPool() {
|
||||||
const display = document.getElementById('mana-pool-display');
|
// mana pool display removed (Phase 1)
|
||||||
if (display) {
|
|
||||||
display.style.display = display.style.display === 'none' ? 'flex' : 'none';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -1993,8 +2184,7 @@ function renderSettingsPanel() {
|
|||||||
'<div class="settings-row"><span>Commander Tax</span>' + renderToggle('commanderTaxEnabled', re.commanderTaxEnabled) + '</div>' +
|
'<div class="settings-row"><span>Commander Tax</span>' + renderToggle('commanderTaxEnabled', re.commanderTaxEnabled) + '</div>' +
|
||||||
'<div class="settings-row"><span>Auto Resolve Triggers</span>' + renderToggle('autoResolveTriggers', re.autoResolveTriggers) + '</div>' +
|
'<div class="settings-row"><span>Auto Resolve Triggers</span>' + renderToggle('autoResolveTriggers', re.autoResolveTriggers) + '</div>' +
|
||||||
'<div class="settings-row"><span>Enforce Hand Size</span>' + renderToggle('enforceHandSize', re.enforceHandSize) + '</div>' +
|
'<div class="settings-row"><span>Enforce Hand Size</span>' + renderToggle('enforceHandSize', re.enforceHandSize) + '</div>' +
|
||||||
'<div class="settings-row"><span>Auto Eliminate on SBA</span>' + renderToggle('autoEliminateOnSBA', re.autoEliminateOnSBA) + '</div>' +
|
'<div class="settings-row"><span>Auto Eliminate on SBA</span>' + renderToggle('autoEliminateOnSBA', re.autoEliminateOnSBA) + '</div>';
|
||||||
'<div class="settings-row"><span>Mana Auto-Clear</span>' + renderToggle('manaAutoClear', gameState.manaAutoClear) + '</div>';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderToggle(key, value) {
|
function renderToggle(key, value) {
|
||||||
@@ -2014,7 +2204,7 @@ function toggleSetting(key) {
|
|||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// PLACEHOLDER STUBS for engine-called UI functions
|
// PLACEHOLDER STUBS for engine-called UI functions
|
||||||
// (engine.js calls these — we provide no-op or simple implementations)
|
// (engine.js calls these — we provide no-op or simple implementations)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
function renderPlayerPanelsNoOp() { renderPlayerPanels(); }
|
function renderPlayerPanelsNoOp() { renderPlayerPanels(); }
|
||||||
@@ -2246,7 +2436,7 @@ function openPreconBrowser() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// DOM UTILITY — updateCardCounterBadge
|
// DOM UTILITY — updateCardCounterBadge
|
||||||
// ============================================================
|
// ============================================================
|
||||||
function updateCardCounterBadge(cardId) {
|
function updateCardCounterBadge(cardId) {
|
||||||
if (!gameState) return;
|
if (!gameState) return;
|
||||||
@@ -2280,7 +2470,7 @@ function updateCardCounterBadge(cardId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// DOM — toggleTap visual update
|
// DOM — toggleTap visual update
|
||||||
// ============================================================
|
// ============================================================
|
||||||
function _refreshCardEl(cardId) {
|
function _refreshCardEl(cardId) {
|
||||||
if (!gameState) return;
|
if (!gameState) return;
|
||||||
@@ -2293,7 +2483,7 @@ function _refreshCardEl(cardId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// DOMContentLoaded — wire up everything
|
// DOMContentLoaded — wire up everything
|
||||||
// ============================================================
|
// ============================================================
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
initSetupScreen();
|
initSetupScreen();
|
||||||
|
|||||||
+204
-83
@@ -4,10 +4,15 @@ Commander AI Lab — Action Labeler
|
|||||||
Maps raw Forge action records (from DecisionSnapshot JSONL)
|
Maps raw Forge action records (from DecisionSnapshot JSONL)
|
||||||
to one of 8 macro-action labels for supervised policy learning.
|
to one of 8 macro-action labels for supervised policy learning.
|
||||||
|
|
||||||
Classification logic:
|
Classification logic (updated):
|
||||||
1. Check action.type (cast, attack, land, cast_commander, pass)
|
1. Check action.type for unambiguous types (attack, land, pass, hold,
|
||||||
2. For "cast" actions, classify by card oracle text / card type keywords
|
cast_commander)
|
||||||
3. For ambiguous cases, use the raw log line and card name heuristics
|
2. For "cast" actions:
|
||||||
|
a. If action.card is present, match directly against name databases
|
||||||
|
b. If action.card is absent (Forge omits it), scan the active
|
||||||
|
player's hand against name databases to infer the cast type.
|
||||||
|
Priority: removal > ramp > draw > commander > creature (default)
|
||||||
|
3. Fallback: PASS
|
||||||
|
|
||||||
Macro-actions (from ml.config.scope):
|
Macro-actions (from ml.config.scope):
|
||||||
0: CAST_CREATURE — Play a creature
|
0: CAST_CREATURE — Play a creature
|
||||||
@@ -41,11 +46,6 @@ logger = logging.getLogger("ml.labeler")
|
|||||||
# Card Type Keyword Databases
|
# Card Type Keyword Databases
|
||||||
# ══════════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════════
|
||||||
|
|
||||||
# Known removal spells (partial list — expanded from common Commander staples)
|
|
||||||
REMOVAL_KEYWORDS = {
|
|
||||||
"destroy", "exile", "sacrifice", "counter", "bounce", "return.*to.*hand",
|
|
||||||
"-\\d+/-\\d+", "damage to", "fight", "deathtouch",
|
|
||||||
}
|
|
||||||
REMOVAL_CARD_NAMES = {
|
REMOVAL_CARD_NAMES = {
|
||||||
"swords to plowshares", "path to exile", "beast within", "chaos warp",
|
"swords to plowshares", "path to exile", "beast within", "chaos warp",
|
||||||
"counterspell", "swan song", "negate", "arcane denial", "doomblade",
|
"counterspell", "swan song", "negate", "arcane denial", "doomblade",
|
||||||
@@ -62,45 +62,52 @@ REMOVAL_CARD_NAMES = {
|
|||||||
"decree of pain", "in garruk's wake", "plague wind",
|
"decree of pain", "in garruk's wake", "plague wind",
|
||||||
"fire covenant", "imprisoned in the moon", "darksteel mutation",
|
"fire covenant", "imprisoned in the moon", "darksteel mutation",
|
||||||
"kenrith's transformation", "song of the dryads",
|
"kenrith's transformation", "song of the dryads",
|
||||||
|
"abrade", "disenchant", "naturalize", "wear", "tear",
|
||||||
|
"shattering spree", "shatter", "smash to smithereens",
|
||||||
|
"lightning bolt", "lightning helix", "condemn", "oust",
|
||||||
|
"pongify", "reality shift", "turn to frog", "polymorph",
|
||||||
|
"boros charm", "utter end", "anguished unmaking", "mortify",
|
||||||
|
"putrefy", "maelstrom pulse", "void rend", "render silent",
|
||||||
|
"overwhelming denial", "test of talents", "dispel", "spell pierce",
|
||||||
|
"flusterstorm", "mental misstep", "pact of negation",
|
||||||
|
"opposition agent", "hullbreacher", "narset parter of veils",
|
||||||
|
"grafdigger's cage", "rest in peace", "leyline of the void",
|
||||||
|
"tormod's crypt", "relic of progenitus",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Known draw spells
|
|
||||||
DRAW_KEYWORDS = {
|
|
||||||
"draw", "scry", "look at the top", "reveal.*library",
|
|
||||||
"search your library.*hand", "impulse", "brainstorm",
|
|
||||||
}
|
|
||||||
DRAW_CARD_NAMES = {
|
DRAW_CARD_NAMES = {
|
||||||
"rhystic study", "mystic remora", "phyrexian arena", "sylvan library",
|
"rhystic study", "mystic remora", "phyrexian arena", "sylvan library",
|
||||||
"harmonize", "blue sun's zenith", "pull from tomorrow",
|
"harmonize", "blue sun's zenith", "pull from tomorrow",
|
||||||
"windfall", "wheel of fortune", "timetwister", "brainstorm",
|
"windfall", "wheel of fortune", "timetwister", "brainstorm",
|
||||||
"ponder", "preordain", "opt", "consider", "treasure cruise",
|
"ponder", "preordain", "opt", "consider", "treasure cruise",
|
||||||
"dig through time", "fact or fiction", "nights whisper",
|
"dig through time", "fact or fiction", "night's whisper",
|
||||||
"nights whisper", "sign in blood", "read the bones",
|
"sign in blood", "read the bones", "painful truths",
|
||||||
"painful truths", "ambition's cost", "promise of power",
|
"ambition's cost", "promise of power", "necropotence",
|
||||||
"necropotence", "dark confidant", "toski bearer of secrets",
|
"dark confidant", "toski bearer of secrets", "beast whisperer",
|
||||||
"beast whisperer", "guardian project", "the great henge",
|
"guardian project", "the great henge", "skullclamp",
|
||||||
"skullclamp", "esper sentinel", "archivist of oghma",
|
"esper sentinel", "archivist of oghma", "mind's eye",
|
||||||
"mind's eye", "consecrated sphinx", "ancient craving",
|
"consecrated sphinx", "ancient craving", "faithless looting",
|
||||||
"faithless looting", "cathartic reunion", "thrill of possibility",
|
"cathartic reunion", "thrill of possibility", "jeska's will",
|
||||||
"jeska's will", "valakut awakening", "stinging study",
|
"valakut awakening", "stinging study", "notion thief",
|
||||||
"night's whisper", "notion thief", "tymna the weaver",
|
"tymna the weaver", "shamanic revelation", "rishkar's expertise",
|
||||||
"shamanic revelation", "rishkar's expertise", "return of the wildspeaker",
|
"return of the wildspeaker", "garruk's uprising", "elemental bond",
|
||||||
"garruk's uprising", "elemental bond", "kindred discovery",
|
"kindred discovery", "coastal piracy", "reconnaissance mission",
|
||||||
"coastal piracy", "reconnaissance mission", "bident of thassa",
|
"bident of thassa", "distant melody", "slate of ancestry",
|
||||||
|
"wheel of fate", "reforge the soul", "magus of the wheel",
|
||||||
|
"river's rebuke", "prosperity", "intellectual offering",
|
||||||
|
"peer into the abyss", "mind spring", "stroke of genius",
|
||||||
|
"jace's ingenuity", "tidings", "concentrate", "divination",
|
||||||
|
"inspiration", "counsel of the soratami", "accumulated knowledge",
|
||||||
|
"predict", "pressure point", "quicken", "serum visions",
|
||||||
|
"gitaxian probe", "mental note", "thought scour",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Known ramp cards
|
|
||||||
RAMP_KEYWORDS = {
|
|
||||||
"add {", "add one mana", "mana of any color",
|
|
||||||
"land.*onto the battlefield", "search.*basic land.*battlefield",
|
|
||||||
}
|
|
||||||
RAMP_CARD_NAMES = {
|
RAMP_CARD_NAMES = {
|
||||||
"sol ring", "mana crypt", "mana vault", "arcane signet",
|
"sol ring", "mana crypt", "mana vault", "arcane signet",
|
||||||
"commander's sphere", "chromatic lantern", "fellwar stone",
|
"commander's sphere", "chromatic lantern", "fellwar stone",
|
||||||
"mind stone", "thought vessel", "thran dynamo", "worn powerstone",
|
"mind stone", "thought vessel", "thran dynamo", "worn powerstone",
|
||||||
"gilded lotus", "hedron archive", "everflowing chalice",
|
"gilded lotus", "hedron archive", "everflowing chalice",
|
||||||
"coalition relic", "prismatic lens", "signets", # catch-all partial
|
"coalition relic", "prismatic lens",
|
||||||
"talisman", # catch-all for talisman cycle
|
|
||||||
"cultivate", "kodama's reach", "rampant growth", "farseek",
|
"cultivate", "kodama's reach", "rampant growth", "farseek",
|
||||||
"nature's lore", "three visits", "skyshroud claim",
|
"nature's lore", "three visits", "skyshroud claim",
|
||||||
"explosive vegetation", "migration path", "circuitous route",
|
"explosive vegetation", "migration path", "circuitous route",
|
||||||
@@ -112,13 +119,144 @@ RAMP_CARD_NAMES = {
|
|||||||
"birds of paradise", "llanowar elves", "elvish mystic",
|
"birds of paradise", "llanowar elves", "elvish mystic",
|
||||||
"fyndhorn elves", "avacyn's pilgrim", "elves of deep shadow",
|
"fyndhorn elves", "avacyn's pilgrim", "elves of deep shadow",
|
||||||
"bloom tender", "priest of titania", "selvala heart of the wilds",
|
"bloom tender", "priest of titania", "selvala heart of the wilds",
|
||||||
|
"boreal druid", "green sun's zenith", "eternal witness",
|
||||||
|
"reclamation sage", "mystic snake", "fierce empath",
|
||||||
|
"signal the clans", "crop rotation", "harrow", "kodama's reach",
|
||||||
|
"traverse the ulvenwald", "search for tomorrow", "primal growth",
|
||||||
|
"land grant", "untamed wilds", "rangers path", "rangers' path",
|
||||||
|
"nissa's pilgrimage", "forest grove", "grove of the burnwillows",
|
||||||
|
"cabal stronghold", "urborg tomb of yawgmoth", "urborg, tomb of yawgmoth",
|
||||||
|
"bojuka bog", "ghost quarter", "field of ruin", "strip mine",
|
||||||
|
"wasteland", "tectonic edge", "dust bowl",
|
||||||
|
"worn powerstone", "basalt monolith", "grim monolith",
|
||||||
|
"mana geode", "darksteel ingot", "manalith", "opaline unicorn",
|
||||||
|
"vessel of endless rest", "corrupted grafstone", "dreamstone hedron",
|
||||||
|
"sisay's ring", "ur-golem's eye", "blinkmoth urn",
|
||||||
|
"astral cornucopia", "druids' repository", "black market",
|
||||||
|
"cryptolith rite", "earthcraft", "citanul hierophants",
|
||||||
|
"zendikar resurgent", "mirari's wake", "mana reflection",
|
||||||
|
"vorinclex voice of hunger", "vorinclex, voice of hunger",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Known creature-type indicators
|
# Partial-match prefixes (for cycles like Signets, Talismans)
|
||||||
CREATURE_KEYWORDS = {
|
RAMP_PARTIAL_PREFIXES = {
|
||||||
"creature", "token", "enters the battlefield",
|
"talisman", "signet", "locket", "banner", "obelisk",
|
||||||
|
"keyrune", "cluestone", "monolith", "medallion",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DRAW_PARTIAL_PREFIXES = {
|
||||||
|
"scroll", "tome", "journal",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ══════════════════════════════════════════════════════════
|
||||||
|
# Core classification helpers
|
||||||
|
# ══════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
def _classify_card_name(name: str) -> Optional[MacroAction]:
|
||||||
|
"""
|
||||||
|
Classify a single card name into a MacroAction, or None if unknown.
|
||||||
|
|
||||||
|
Checks exact matches first, then partial prefix matches.
|
||||||
|
Returns None for unrecognised cards (caller handles the default).
|
||||||
|
"""
|
||||||
|
n = name.lower().strip()
|
||||||
|
if not n:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if n in REMOVAL_CARD_NAMES:
|
||||||
|
return MacroAction.CAST_REMOVAL
|
||||||
|
if n in DRAW_CARD_NAMES:
|
||||||
|
return MacroAction.CAST_DRAW
|
||||||
|
if n in RAMP_CARD_NAMES:
|
||||||
|
return MacroAction.CAST_RAMP
|
||||||
|
|
||||||
|
# Partial prefix match for cycles (Talismans, Signets, etc.)
|
||||||
|
for prefix in RAMP_PARTIAL_PREFIXES:
|
||||||
|
if n.startswith(prefix):
|
||||||
|
return MacroAction.CAST_RAMP
|
||||||
|
for prefix in DRAW_PARTIAL_PREFIXES:
|
||||||
|
if n.startswith(prefix):
|
||||||
|
return MacroAction.CAST_DRAW
|
||||||
|
|
||||||
|
# Substring match (catches "Doom Blade" inside longer names etc.)
|
||||||
|
for known in REMOVAL_CARD_NAMES:
|
||||||
|
if known in n or n in known:
|
||||||
|
return MacroAction.CAST_REMOVAL
|
||||||
|
for known in DRAW_CARD_NAMES:
|
||||||
|
if known in n or n in known:
|
||||||
|
return MacroAction.CAST_DRAW
|
||||||
|
for known in RAMP_CARD_NAMES:
|
||||||
|
if known in n or n in known:
|
||||||
|
return MacroAction.CAST_RAMP
|
||||||
|
|
||||||
|
return None # Unknown → caller defaults to CAST_CREATURE
|
||||||
|
|
||||||
|
|
||||||
|
def _classify_from_hand(
|
||||||
|
players: list,
|
||||||
|
active_seat: int,
|
||||||
|
commander_names: Optional[set] = None,
|
||||||
|
) -> MacroAction:
|
||||||
|
"""
|
||||||
|
Infer the cast action type by scanning the active player's hand.
|
||||||
|
|
||||||
|
When action.card is absent (Forge omits it), the hand snapshot
|
||||||
|
is the best available signal. We scan all hand cards and return
|
||||||
|
the highest-priority match:
|
||||||
|
|
||||||
|
removal > ramp > draw > commander > creature (default)
|
||||||
|
|
||||||
|
Priority rationale: Forge's AI plays the highest-impact available
|
||||||
|
action, so if any removal/ramp/draw spell is in hand during a cast
|
||||||
|
decision, it's the most likely card played.
|
||||||
|
"""
|
||||||
|
if active_seat >= len(players):
|
||||||
|
return MacroAction.CAST_CREATURE
|
||||||
|
|
||||||
|
hand: List[str] = players[active_seat].get("hand", [])
|
||||||
|
command_zone: List[str] = players[active_seat].get("command_zone", [])
|
||||||
|
|
||||||
|
# Build commander name set from command_zone if not provided
|
||||||
|
if commander_names is None:
|
||||||
|
commander_names = {c.lower().strip() for c in command_zone if c}
|
||||||
|
|
||||||
|
found_removal = False
|
||||||
|
found_ramp = False
|
||||||
|
found_draw = False
|
||||||
|
found_commander = False
|
||||||
|
|
||||||
|
for card in hand:
|
||||||
|
card_lower = card.lower().strip()
|
||||||
|
|
||||||
|
# Commander check: hand card matches a command zone card
|
||||||
|
if card_lower in commander_names:
|
||||||
|
found_commander = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
result = _classify_card_name(card_lower)
|
||||||
|
if result == MacroAction.CAST_REMOVAL:
|
||||||
|
found_removal = True
|
||||||
|
elif result == MacroAction.CAST_RAMP:
|
||||||
|
found_ramp = True
|
||||||
|
elif result == MacroAction.CAST_DRAW:
|
||||||
|
found_draw = True
|
||||||
|
|
||||||
|
# Return highest-priority found
|
||||||
|
if found_removal:
|
||||||
|
return MacroAction.CAST_REMOVAL
|
||||||
|
if found_ramp:
|
||||||
|
return MacroAction.CAST_RAMP
|
||||||
|
if found_draw:
|
||||||
|
return MacroAction.CAST_DRAW
|
||||||
|
if found_commander:
|
||||||
|
return MacroAction.CAST_COMMANDER
|
||||||
|
return MacroAction.CAST_CREATURE
|
||||||
|
|
||||||
|
|
||||||
|
# ══════════════════════════════════════════════════════════
|
||||||
|
# Main label_action entry point
|
||||||
|
# ══════════════════════════════════════════════════════════
|
||||||
|
|
||||||
def label_action(decision: dict) -> int:
|
def label_action(decision: dict) -> int:
|
||||||
"""
|
"""
|
||||||
@@ -133,74 +271,52 @@ def label_action(decision: dict) -> int:
|
|||||||
action = decision.get("action", {})
|
action = decision.get("action", {})
|
||||||
action_type = action.get("type", "").lower()
|
action_type = action.get("type", "").lower()
|
||||||
card_name = (action.get("card", "") or "").lower().strip()
|
card_name = (action.get("card", "") or "").lower().strip()
|
||||||
raw_line = (action.get("raw", "") or "").lower()
|
|
||||||
|
|
||||||
# ── Direct type matching ────────────────────────────
|
players = decision.get("players", [])
|
||||||
|
active_seat = decision.get("active_seat", 0)
|
||||||
|
|
||||||
|
# ── Unambiguous direct-type matches ──────────────────────
|
||||||
|
|
||||||
# Commander cast is unambiguous
|
|
||||||
if action_type == "cast_commander":
|
if action_type == "cast_commander":
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_COMMANDER]
|
return ACTION_TO_IDX[MacroAction.CAST_COMMANDER]
|
||||||
|
|
||||||
# Attack is unambiguous
|
|
||||||
if action_type == "attack":
|
if action_type == "attack":
|
||||||
return ACTION_TO_IDX[MacroAction.ATTACK_OPPONENT]
|
return ACTION_TO_IDX[MacroAction.ATTACK_OPPONENT]
|
||||||
|
|
||||||
# Land play → classify as CAST_RAMP (developing mana is ramp)
|
# Land play → developing mana base = ramp
|
||||||
if action_type == "land":
|
if action_type == "land":
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_RAMP]
|
return ACTION_TO_IDX[MacroAction.CAST_RAMP]
|
||||||
|
|
||||||
# Pass / hold mana
|
# Pass / hold mana — check available mana to distinguish
|
||||||
if action_type in ("pass", "hold"):
|
if action_type in ("pass", "hold"):
|
||||||
# Check if player had mana open (from state)
|
|
||||||
active_seat = decision.get("active_seat", 0)
|
|
||||||
players = decision.get("players", [])
|
|
||||||
if active_seat < len(players):
|
if active_seat < len(players):
|
||||||
mana = players[active_seat].get("mana", 0)
|
mana = players[active_seat].get("mana", 0)
|
||||||
if mana >= 2:
|
if mana >= 2:
|
||||||
return ACTION_TO_IDX[MacroAction.HOLD_MANA]
|
return ACTION_TO_IDX[MacroAction.HOLD_MANA]
|
||||||
return ACTION_TO_IDX[MacroAction.PASS]
|
return ACTION_TO_IDX[MacroAction.PASS]
|
||||||
|
|
||||||
# ── Cast actions: classify by card identity ─────────
|
# ── Cast actions ──────────────────────────────────────
|
||||||
|
|
||||||
if action_type == "cast":
|
if action_type == "cast":
|
||||||
# 1. Check known card name databases
|
# Path A: action.card is present — classify it directly
|
||||||
if card_name in REMOVAL_CARD_NAMES or _partial_match(card_name, REMOVAL_CARD_NAMES):
|
if card_name:
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_REMOVAL]
|
result = _classify_card_name(card_name)
|
||||||
|
if result is not None:
|
||||||
|
return ACTION_TO_IDX[result]
|
||||||
|
# Unrecognised named card → default creature
|
||||||
|
return ACTION_TO_IDX[MacroAction.CAST_CREATURE]
|
||||||
|
|
||||||
if card_name in DRAW_CARD_NAMES or _partial_match(card_name, DRAW_CARD_NAMES):
|
# Path B: action.card absent (Forge omits it) — infer from hand
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_DRAW]
|
inferred = _classify_from_hand(players, active_seat)
|
||||||
|
return ACTION_TO_IDX[inferred]
|
||||||
|
|
||||||
if card_name in RAMP_CARD_NAMES or _partial_match(card_name, RAMP_CARD_NAMES):
|
# ── Fallback ──────────────────────────────────────────
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_RAMP]
|
|
||||||
|
|
||||||
# 2. Check raw log line for keyword patterns
|
|
||||||
for keyword in REMOVAL_KEYWORDS:
|
|
||||||
if re.search(keyword, raw_line):
|
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_REMOVAL]
|
|
||||||
|
|
||||||
for keyword in DRAW_KEYWORDS:
|
|
||||||
if re.search(keyword, raw_line):
|
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_DRAW]
|
|
||||||
|
|
||||||
for keyword in RAMP_KEYWORDS:
|
|
||||||
if re.search(keyword, raw_line):
|
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_RAMP]
|
|
||||||
|
|
||||||
# 3. Default: if it's a spell cast and not recognized, assume creature
|
|
||||||
# (most casts in Commander are creatures or creature-adjacent)
|
|
||||||
return ACTION_TO_IDX[MacroAction.CAST_CREATURE]
|
|
||||||
|
|
||||||
# ── Fallback ────────────────────────────────────────
|
|
||||||
return ACTION_TO_IDX[MacroAction.PASS]
|
return ACTION_TO_IDX[MacroAction.PASS]
|
||||||
|
|
||||||
|
|
||||||
def _partial_match(name: str, name_set: set) -> bool:
|
# ══════════════════════════════════════════════════════════
|
||||||
"""Check if card name partially matches any name in the set."""
|
# File-level helpers
|
||||||
for known in name_set:
|
# ══════════════════════════════════════════════════════════
|
||||||
if known in name or name in known:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def label_decisions_file(jsonl_path: str) -> Tuple[np.ndarray, List[dict]]:
|
def label_decisions_file(jsonl_path: str) -> Tuple[np.ndarray, List[dict]]:
|
||||||
"""
|
"""
|
||||||
@@ -214,7 +330,7 @@ def label_decisions_file(jsonl_path: str) -> Tuple[np.ndarray, List[dict]]:
|
|||||||
labels = []
|
labels = []
|
||||||
raw = []
|
raw = []
|
||||||
|
|
||||||
with open(jsonl_path, "r") as f:
|
with open(jsonl_path, "r", encoding="utf-8") as f:
|
||||||
for i, line in enumerate(f):
|
for i, line in enumerate(f):
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line:
|
if not line:
|
||||||
@@ -232,7 +348,11 @@ def label_decisions_file(jsonl_path: str) -> Tuple[np.ndarray, List[dict]]:
|
|||||||
|
|
||||||
|
|
||||||
def print_label_distribution(labels: np.ndarray) -> Dict[str, int]:
|
def print_label_distribution(labels: np.ndarray) -> Dict[str, int]:
|
||||||
"""Print and return the distribution of action labels."""
|
"""Print and return the distribution of action labels.
|
||||||
|
|
||||||
|
Iterates MacroAction enum in index order so the display always
|
||||||
|
matches ACTION_TO_IDX regardless of dict insertion order.
|
||||||
|
"""
|
||||||
dist = {}
|
dist = {}
|
||||||
for action in MacroAction:
|
for action in MacroAction:
|
||||||
idx = ACTION_TO_IDX[action]
|
idx = ACTION_TO_IDX[action]
|
||||||
@@ -242,10 +362,11 @@ def print_label_distribution(labels: np.ndarray) -> Dict[str, int]:
|
|||||||
total = len(labels)
|
total = len(labels)
|
||||||
logger.info(f"\n{'Action':<25} {'Count':>8} {'Pct':>8}")
|
logger.info(f"\n{'Action':<25} {'Count':>8} {'Pct':>8}")
|
||||||
logger.info("─" * 43)
|
logger.info("─" * 43)
|
||||||
|
# Sort descending by count for readability
|
||||||
for action_name, count in sorted(dist.items(), key=lambda x: -x[1]):
|
for action_name, count in sorted(dist.items(), key=lambda x: -x[1]):
|
||||||
pct = (count / total * 100) if total > 0 else 0
|
pct = (count / total * 100) if total > 0 else 0
|
||||||
logger.info(f" {action_name:<23} {count:>8} {pct:>7.1f}%")
|
logger.info(f" {action_name:<23} {count:>8} {pct:>7.1f}%")
|
||||||
logger.info(f"{'─' * 43}")
|
logger.info("─" * 43)
|
||||||
logger.info(f" {'TOTAL':<23} {total:>8}")
|
logger.info(f" {'TOTAL':<23} {total:>8}")
|
||||||
|
|
||||||
return dist
|
return dist
|
||||||
+9
-1
@@ -292,7 +292,15 @@ class DistillationDefaults:
|
|||||||
ppo_episodes_per_iter: int = 64
|
ppo_episodes_per_iter: int = 64
|
||||||
ppo_lr: float = 3e-4
|
ppo_lr: float = 3e-4
|
||||||
ppo_clip_epsilon: float = 0.2
|
ppo_clip_epsilon: float = 0.2
|
||||||
ppo_entropy_coeff: float = 0.01
|
# Increased from 0.01 to 0.04 to prevent premature convergence on the
|
||||||
|
# "pass"/"land" macro-actions. The policy-collapse bug (see
|
||||||
|
# commander-ai-lab-land-only-bug-analysis.md) is exacerbated by a low
|
||||||
|
# entropy bonus: PPO settled on argmax very early in training, so the
|
||||||
|
# model never learned to cast creatures / removal / draw spells even
|
||||||
|
# when they were affordable. A higher entropy bonus keeps the action
|
||||||
|
# distribution flat enough during self-play for those alternatives to
|
||||||
|
# accumulate reward signal.
|
||||||
|
ppo_entropy_coeff: float = 0.04
|
||||||
ppo_eval_episodes: int = 100
|
ppo_eval_episodes: int = 100
|
||||||
|
|
||||||
# Dataset mixing — Forge-only by default (Phase 5 follow-up decision)
|
# Dataset mixing — Forge-only by default (Phase 5 follow-up decision)
|
||||||
|
|||||||
+33
-18
@@ -6,8 +6,9 @@ Reads ML decision JSONL files, encodes states, labels actions,
|
|||||||
and produces a training-ready dataset saved as NPZ.
|
and produces a training-ready dataset saved as NPZ.
|
||||||
|
|
||||||
Phase 2: Source Awareness (Issue #66)
|
Phase 2: Source Awareness (Issue #66)
|
||||||
- Scans both Forge sim files (ml-decisions-sim-*.jsonl) and
|
- Scans Forge sim files (ml-decisions-sim-*.jsonl) → source "forge"
|
||||||
PPO self-play files (ml-decisions-ppo-*.jsonl)
|
- Scans PPO self-play (ml-decisions-ppo-*.jsonl) → source "ppo"
|
||||||
|
- Scans DeepSeek files (ml-decisions-ds-*.jsonl) → source "ds"
|
||||||
- Configurable source_weights for sampling ratios
|
- Configurable source_weights for sampling ratios
|
||||||
- min_reward_threshold filter for PPO data quality
|
- min_reward_threshold filter for PPO data quality
|
||||||
- Each sample tagged with source for downstream analysis
|
- Each sample tagged with source for downstream analysis
|
||||||
@@ -21,7 +22,7 @@ Output NPZ structure:
|
|||||||
game_ids: object (N,) — game ID for each sample
|
game_ids: object (N,) — game ID for each sample
|
||||||
outcomes: object (N,) — game outcome per sample
|
outcomes: object (N,) — game outcome per sample
|
||||||
playstyles: object (N,) — deck archetype per sample
|
playstyles: object (N,) — deck archetype per sample
|
||||||
sources: object (N,) — data source ("forge" or "ppo")
|
sources: object (N,) — data source ("forge", "ppo", or "ds")
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
@@ -62,11 +63,11 @@ class DatasetConfig:
|
|||||||
embeddings_dir: Optional[str] = None
|
embeddings_dir: Optional[str] = None
|
||||||
max_samples: Optional[int] = None
|
max_samples: Optional[int] = None
|
||||||
|
|
||||||
# Source weights control sampling ratios between Forge and PPO data.
|
# Source weights control sampling ratios between Forge, PPO, and DS data.
|
||||||
# Default: Forge-only (ppo=0.0). Use MIXED_MODE_PRESETS from
|
# ds=4.0 oversamples the 8 DeepSeek files from ~5.8% → ~20% of effective
|
||||||
# ml.config.scope for blended configurations (e.g. 90/10, 80/20).
|
# training tokens, matching their higher signal quality relative to sim data.
|
||||||
source_weights: Dict[str, float] = field(
|
source_weights: Dict[str, float] = field(
|
||||||
default_factory=lambda: {"forge": 1.0, "ppo": 0.0}
|
default_factory=lambda: {"forge": 1.0, "ppo": 0.0, "ds": 4.0}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Minimum reward threshold for PPO data — only include decisions
|
# Minimum reward threshold for PPO data — only include decisions
|
||||||
@@ -81,12 +82,13 @@ class DatasetConfig:
|
|||||||
|
|
||||||
def _discover_jsonl_files(results_dir: str) -> Dict[str, List[Path]]:
|
def _discover_jsonl_files(results_dir: str) -> Dict[str, List[Path]]:
|
||||||
"""
|
"""
|
||||||
Scan results directory for both Forge sim and PPO decision files.
|
Scan results directory for Forge sim, PPO, and DeepSeek decision files.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict mapping source name to list of JSONL file paths.
|
Dict mapping source name to list of JSONL file paths.
|
||||||
Keys: "forge" for ml-decisions-sim-*.jsonl
|
Keys: "forge" for ml-decisions-sim-*.jsonl
|
||||||
"ppo" for ml-decisions-ppo-*.jsonl
|
"ppo" for ml-decisions-ppo-*.jsonl
|
||||||
|
"ds" for ml-decisions-ds-*.jsonl
|
||||||
"""
|
"""
|
||||||
results_path = Path(results_dir)
|
results_path = Path(results_dir)
|
||||||
sources = {}
|
sources = {}
|
||||||
@@ -101,10 +103,16 @@ def _discover_jsonl_files(results_dir: str) -> Dict[str, List[Path]]:
|
|||||||
if ppo_files:
|
if ppo_files:
|
||||||
sources["ppo"] = ppo_files
|
sources["ppo"] = ppo_files
|
||||||
|
|
||||||
# Also pick up legacy files that don't have sim/ppo prefix
|
# DeepSeek high-quality decisions (upweighted by default)
|
||||||
|
ds_files = sorted(results_path.glob("ml-decisions-ds-*.jsonl"))
|
||||||
|
if ds_files:
|
||||||
|
sources["ds"] = ds_files
|
||||||
|
|
||||||
|
# Legacy files that don't match any known prefix — treat as forge
|
||||||
|
known = set(forge_files + ppo_files + ds_files)
|
||||||
legacy_files = sorted(
|
legacy_files = sorted(
|
||||||
f for f in results_path.glob("ml-decisions-*.jsonl")
|
f for f in results_path.glob("ml-decisions-*.jsonl")
|
||||||
if f not in set(forge_files + ppo_files)
|
if f not in known
|
||||||
)
|
)
|
||||||
if legacy_files:
|
if legacy_files:
|
||||||
sources.setdefault("forge", []).extend(legacy_files)
|
sources.setdefault("forge", []).extend(legacy_files)
|
||||||
@@ -123,7 +131,7 @@ def _passes_reward_filter(
|
|||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Filter PPO decisions by reward threshold.
|
Filter PPO decisions by reward threshold.
|
||||||
Forge data always passes. PPO data must meet the threshold.
|
Forge and DS data always passes. PPO data must meet the threshold.
|
||||||
"""
|
"""
|
||||||
if source != "ppo":
|
if source != "ppo":
|
||||||
return True
|
return True
|
||||||
@@ -155,7 +163,7 @@ def _apply_source_weights(
|
|||||||
if weight <= 0:
|
if weight <= 0:
|
||||||
logger.info(" Skipping %d %s samples (weight=0)", len(samples), source)
|
logger.info(" Skipping %d %s samples (weight=0)", len(samples), source)
|
||||||
continue
|
continue
|
||||||
if weight >= 1.0 and weight == 1.0:
|
if weight == 1.0:
|
||||||
combined.extend(samples)
|
combined.extend(samples)
|
||||||
elif weight < 1.0:
|
elif weight < 1.0:
|
||||||
# Subsample
|
# Subsample
|
||||||
@@ -193,8 +201,9 @@ def build_dataset(
|
|||||||
Build complete training dataset from all ML decision files.
|
Build complete training dataset from all ML decision files.
|
||||||
|
|
||||||
Phase 2 additions:
|
Phase 2 additions:
|
||||||
- Scans both Forge and PPO JSONL files
|
- Scans Forge, PPO, and DeepSeek (ds) JSONL files
|
||||||
- Applies source_weights for configurable mixing
|
- Applies source_weights for configurable mixing
|
||||||
|
(default: ds=4.0 to oversample high-quality DeepSeek decisions)
|
||||||
- Filters PPO data by min_reward_threshold
|
- Filters PPO data by min_reward_threshold
|
||||||
- Tags each sample with its source
|
- Tags each sample with its source
|
||||||
|
|
||||||
@@ -202,14 +211,15 @@ def build_dataset(
|
|||||||
results_dir: Path to results/ directory containing JSONL files
|
results_dir: Path to results/ directory containing JSONL files
|
||||||
embeddings_dir: Path to embeddings/ directory (default: auto-detect)
|
embeddings_dir: Path to embeddings/ directory (default: auto-detect)
|
||||||
max_samples: Cap total samples (None = unlimited)
|
max_samples: Cap total samples (None = unlimited)
|
||||||
source_weights: Dict of source → sampling weight (default: forge=1.0, ppo=0.5)
|
source_weights: Dict of source → sampling weight
|
||||||
|
(default: forge=1.0, ppo=0.0, ds=4.0)
|
||||||
min_reward_threshold: Min episode_return for PPO data (default: 0.0)
|
min_reward_threshold: Min episode_return for PPO data (default: 0.0)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict with keys: states, labels, game_ids, outcomes, playstyles, sources
|
Dict with keys: states, labels, game_ids, outcomes, playstyles, sources
|
||||||
"""
|
"""
|
||||||
if source_weights is None:
|
if source_weights is None:
|
||||||
source_weights = {"forge": 1.0, "ppo": 0.0}
|
source_weights = {"forge": 1.0, "ppo": 0.0, "ds": 4.0}
|
||||||
|
|
||||||
# Load card embeddings
|
# Load card embeddings
|
||||||
card_index = CardEmbeddingIndex(embeddings_dir)
|
card_index = CardEmbeddingIndex(embeddings_dir)
|
||||||
@@ -225,7 +235,7 @@ def build_dataset(
|
|||||||
|
|
||||||
encoder = StateEncoder(card_index)
|
encoder = StateEncoder(card_index)
|
||||||
|
|
||||||
# --- Phase 2 Task 1: Discover files from both sources ---
|
# --- Phase 2 Task 1: Discover files from all sources ---
|
||||||
source_files = _discover_jsonl_files(results_dir)
|
source_files = _discover_jsonl_files(results_dir)
|
||||||
if not source_files:
|
if not source_files:
|
||||||
logger.error("No ML decision files found in %s", results_dir)
|
logger.error("No ML decision files found in %s", results_dir)
|
||||||
@@ -256,7 +266,7 @@ def build_dataset(
|
|||||||
logger.info("Processing %s [%s] ...", jsonl_file.name, source)
|
logger.info("Processing %s [%s] ...", jsonl_file.name, source)
|
||||||
file_count = 0
|
file_count = 0
|
||||||
|
|
||||||
with open(jsonl_file, "r") as f:
|
with open(jsonl_file, "r", encoding="utf-8") as f:
|
||||||
for line_num, line in enumerate(f):
|
for line_num, line in enumerate(f):
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line:
|
if not line:
|
||||||
@@ -503,7 +513,11 @@ def main():
|
|||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--ppo-weight", type=float, default=0.0,
|
"--ppo-weight", type=float, default=0.0,
|
||||||
help="Sampling weight for PPO self-play data (default: 0.0, Forge-only)",
|
help="Sampling weight for PPO self-play data (default: 0.0, disabled)",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--ds-weight", type=float, default=4.0,
|
||||||
|
help="Sampling weight for DeepSeek decision data (default: 4.0, ~20%% effective share)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--min-reward", type=float, default=0.0,
|
"--min-reward", type=float, default=0.0,
|
||||||
@@ -515,6 +529,7 @@ def main():
|
|||||||
source_weights = {
|
source_weights = {
|
||||||
"forge": args.forge_weight,
|
"forge": args.forge_weight,
|
||||||
"ppo": args.ppo_weight,
|
"ppo": args.ppo_weight,
|
||||||
|
"ds": args.ds_weight,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Build dataset
|
# Build dataset
|
||||||
|
|||||||
@@ -523,7 +523,7 @@ class QualityGate:
|
|||||||
report_path = os.path.join(
|
report_path = os.path.join(
|
||||||
self.config.reports_dir, f"quality_{timestamp}.json"
|
self.config.reports_dir, f"quality_{timestamp}.json"
|
||||||
)
|
)
|
||||||
with open(report_path, "w") as f:
|
with open(report_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(metrics, f, indent=2)
|
json.dump(metrics, f, indent=2)
|
||||||
logger.info("Quality report saved: %s", report_path)
|
logger.info("Quality report saved: %s", report_path)
|
||||||
|
|
||||||
@@ -582,6 +582,6 @@ class QualityGate:
|
|||||||
path = os.path.join(self.config.reports_dir, "verdict_history.json")
|
path = os.path.join(self.config.reports_dir, "verdict_history.json")
|
||||||
|
|
||||||
history = [v.to_dict() for v in self.verdicts]
|
history = [v.to_dict() for v in self.verdicts]
|
||||||
with open(path, "w") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
json.dump(history, f, indent=2)
|
json.dump(history, f, indent=2)
|
||||||
logger.info("Verdict history saved: %s (%d entries)", path, len(history))
|
logger.info("Verdict history saved: %s (%d entries)", path, len(history))
|
||||||
@@ -141,8 +141,17 @@ class CardEmbeddingIndex:
|
|||||||
logger.error("Auto-download failed: %s", e)
|
logger.error("Auto-download failed: %s", e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_embedding(self, card_name: str) -> np.ndarray:
|
def get_embedding(self, card_name) -> np.ndarray:
|
||||||
"""Get embedding vector for a card name. Returns zero vector if not found."""
|
"""Get embedding vector for a card name. Returns zero vector if not found.
|
||||||
|
|
||||||
|
Accepts either a plain string or a dict with a 'name' key (defensive
|
||||||
|
against the LAN client sending structured battlefield objects).
|
||||||
|
"""
|
||||||
|
# Handle dict objects from the LAN client's battlefield_detail
|
||||||
|
if isinstance(card_name, dict):
|
||||||
|
card_name = card_name.get('name', '')
|
||||||
|
if not card_name or not isinstance(card_name, str):
|
||||||
|
return self._zero_vec
|
||||||
if not self._loaded:
|
if not self._loaded:
|
||||||
return self._zero_vec
|
return self._zero_vec
|
||||||
idx = self._name_to_idx.get(card_name.lower())
|
idx = self._name_to_idx.get(card_name.lower())
|
||||||
@@ -155,8 +164,11 @@ class CardEmbeddingIndex:
|
|||||||
return self._embeddings[idx]
|
return self._embeddings[idx]
|
||||||
return self._zero_vec
|
return self._zero_vec
|
||||||
|
|
||||||
def mean_pool_zone(self, card_names: List[str]) -> np.ndarray:
|
def mean_pool_zone(self, card_names: list) -> np.ndarray:
|
||||||
"""Mean-pool embeddings for a list of card names (a game zone)."""
|
"""Mean-pool embeddings for a list of card names (a game zone).
|
||||||
|
|
||||||
|
Each entry may be a plain string or a dict with a 'name' key.
|
||||||
|
"""
|
||||||
if not card_names:
|
if not card_names:
|
||||||
return self._zero_vec
|
return self._zero_vec
|
||||||
vectors = [self.get_embedding(name) for name in card_names]
|
vectors = [self.get_embedding(name) for name in card_names]
|
||||||
@@ -167,6 +179,24 @@ class CardEmbeddingIndex:
|
|||||||
return self._loaded
|
return self._loaded
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_cmdr_dmg(raw) -> int:
|
||||||
|
"""Collapse cmdr_dmg to a scalar integer regardless of source format.
|
||||||
|
|
||||||
|
Forge sends cmdr_dmg as a dict keyed by opponent seat string, e.g.
|
||||||
|
{"0": 14, "2": 7}. _build_encoder_snapshot already sums this before
|
||||||
|
passing to predict(), but the live Forge IPC path (and legacy JSONL)
|
||||||
|
may send the raw dict. Summing all values is correct for Commander:
|
||||||
|
the total commander damage received from all opponents is what matters
|
||||||
|
for the life-loss threshold (21).
|
||||||
|
"""
|
||||||
|
if isinstance(raw, dict):
|
||||||
|
return sum(int(v) for v in raw.values())
|
||||||
|
try:
|
||||||
|
return int(raw)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
class StateEncoder:
|
class StateEncoder:
|
||||||
"""
|
"""
|
||||||
Encodes a decision snapshot JSON dict into a fixed-size numpy vector.
|
Encodes a decision snapshot JSON dict into a fixed-size numpy vector.
|
||||||
@@ -241,6 +271,10 @@ class StateEncoder:
|
|||||||
Index 7 priority for total_power:
|
Index 7 priority for total_power:
|
||||||
1. p['total_power'] — real Forge value from Step 4 / _build_encoder_snapshot()
|
1. p['total_power'] — real Forge value from Step 4 / _build_encoder_snapshot()
|
||||||
2. p['creatures'] * 3 — legacy heuristic for old JSONL snapshots
|
2. p['creatures'] * 3 — legacy heuristic for old JSONL snapshots
|
||||||
|
|
||||||
|
cmdr_dmg is resolved via _resolve_cmdr_dmg() which handles both the
|
||||||
|
live Forge format (dict keyed by opponent seat) and the pre-collapsed
|
||||||
|
int from _build_encoder_snapshot / JSONL training snapshots.
|
||||||
"""
|
"""
|
||||||
features = []
|
features = []
|
||||||
|
|
||||||
@@ -250,7 +284,8 @@ class StateEncoder:
|
|||||||
features.append(life / 40.0)
|
features.append(life / 40.0)
|
||||||
|
|
||||||
# idx 1: commander damage taken
|
# idx 1: commander damage taken
|
||||||
cmdr_dmg = p.get("cmdr_dmg", 0)
|
# _resolve_cmdr_dmg handles dict (live Forge IPC) and int (JSONL/pre-collapsed)
|
||||||
|
cmdr_dmg = _resolve_cmdr_dmg(p.get("cmdr_dmg", 0))
|
||||||
features.append(cmdr_dmg / 21.0)
|
features.append(cmdr_dmg / 21.0)
|
||||||
|
|
||||||
# idx 2: mana available
|
# idx 2: mana available
|
||||||
@@ -380,7 +415,7 @@ def encode_decisions_file(
|
|||||||
states = []
|
states = []
|
||||||
raw = []
|
raw = []
|
||||||
|
|
||||||
with open(jsonl_path, "r") as f:
|
with open(jsonl_path, "r", encoding="utf-8") as f:
|
||||||
for i, line in enumerate(f):
|
for i, line in enumerate(f):
|
||||||
if max_samples and i >= max_samples:
|
if max_samples and i >= max_samples:
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ class EloHistory:
|
|||||||
def _load(self):
|
def _load(self):
|
||||||
if os.path.exists(self.path):
|
if os.path.exists(self.path):
|
||||||
try:
|
try:
|
||||||
with open(self.path, "r") as f:
|
with open(self.path, "r", encoding="utf-8") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
self.entries = data.get("entries", [])
|
self.entries = data.get("entries", [])
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -138,7 +138,7 @@ class EloHistory:
|
|||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
os.makedirs(os.path.dirname(self.path) or ".", exist_ok=True)
|
os.makedirs(os.path.dirname(self.path) or ".", exist_ok=True)
|
||||||
with open(self.path, "w") as f:
|
with open(self.path, "w", encoding="utf-8") as f:
|
||||||
json.dump({"entries": self.entries}, f, indent=2)
|
json.dump({"entries": self.entries}, f, indent=2)
|
||||||
|
|
||||||
def append(self, generation: int, ratings: Dict[str, float]):
|
def append(self, generation: int, ratings: Dict[str, float]):
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ class ForgeEvaluator:
|
|||||||
if not Path(batch_path).exists():
|
if not Path(batch_path).exists():
|
||||||
logger.warning("[Eval] Batch JSON not found at %s — falling back to synthetic", batch_path)
|
logger.warning("[Eval] Batch JSON not found at %s — falling back to synthetic", batch_path)
|
||||||
return self._run_synthetic_eval(service)
|
return self._run_synthetic_eval(service)
|
||||||
with open(batch_path) as f:
|
with open(batch_path, encoding="utf-8") as f:
|
||||||
batch = json.load(f)
|
batch = json.load(f)
|
||||||
games_list = batch.get("games", [])
|
games_list = batch.get("games", [])
|
||||||
if not games_list:
|
if not games_list:
|
||||||
@@ -543,6 +543,6 @@ class ForgeEvaluator:
|
|||||||
"games": [asdict(r) for r in self._records],
|
"games": [asdict(r) for r in self._records],
|
||||||
}
|
}
|
||||||
os.makedirs(self.cfg.results_dir, exist_ok=True)
|
os.makedirs(self.cfg.results_dir, exist_ok=True)
|
||||||
with open(self._result_path, "w") as f:
|
with open(self._result_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(payload, f, indent=2)
|
json.dump(payload, f, indent=2)
|
||||||
logger.info("[Eval %s] Results written to %s", self.cfg.run_id, self._result_path)
|
logger.info("[Eval %s] Results written to %s", self.cfg.run_id, self._result_path)
|
||||||
@@ -0,0 +1,548 @@
|
|||||||
|
"""
|
||||||
|
Commander AI Lab — Forge Evaluator (Issue #153)
|
||||||
|
═════════════════════════════════════════════════
|
||||||
|
Reusable eval harness that:
|
||||||
|
1. Starts the policy inference server pointing at a given checkpoint
|
||||||
|
2. Launches Forge games via MultiThreadBatchRunner with the policy
|
||||||
|
server wired in as the AI decision-maker
|
||||||
|
3. Collects per-game metrics and writes a JSON results file
|
||||||
|
|
||||||
|
Metrics collected per game:
|
||||||
|
- won : bool
|
||||||
|
- turns : int
|
||||||
|
- life_final : int (winning seat final life)
|
||||||
|
- life_delta : int (winner life - loser life at game end)
|
||||||
|
- cmd_damage : int (commander damage dealt by policy seat)
|
||||||
|
- illegal_acts : int (actions rejected by Forge — should be 0)
|
||||||
|
- entropy_mean : float (mean policy entropy across decision steps)
|
||||||
|
- inference_ms : float (mean per-step inference latency)
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
from ml.eval.forge_evaluator import ForgeEvaluator, EvalConfig
|
||||||
|
cfg = EvalConfig(
|
||||||
|
checkpoint_path="ml/models/checkpoints/forge-trained/final.pt",
|
||||||
|
forge_jar="forge/forge-gui-desktop-2.0.12-SNAPSHOT-jar-with-dependencies.jar",
|
||||||
|
deck_files=["decks/cedh_test_a.dck", "decks/cedh_test_b.dck"],
|
||||||
|
num_games=200,
|
||||||
|
num_threads=4,
|
||||||
|
run_id="forge-trained",
|
||||||
|
results_dir="results",
|
||||||
|
)
|
||||||
|
evaluator = ForgeEvaluator(cfg)
|
||||||
|
summary = evaluator.run()
|
||||||
|
print(summary)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
import subprocess
|
||||||
|
from dataclasses import dataclass, field, asdict
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
project_root = str(Path(__file__).parent.parent.parent)
|
||||||
|
if project_root not in sys.path:
|
||||||
|
sys.path.insert(0, project_root)
|
||||||
|
|
||||||
|
logger = logging.getLogger("ml.eval.forge_evaluator")
|
||||||
|
|
||||||
|
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
# Configuration
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class EvalConfig:
|
||||||
|
"""All parameters needed for a single evaluation run."""
|
||||||
|
|
||||||
|
# Model
|
||||||
|
checkpoint_path: str = "ml/models/checkpoints/best_policy.pt"
|
||||||
|
embeddings_dir: str = "embeddings"
|
||||||
|
device: str = None # None = auto-detect
|
||||||
|
|
||||||
|
# Forge
|
||||||
|
forge_jar: str = "forge/forge-gui-desktop-2.0.12-SNAPSHOT-jar-with-dependencies.jar"
|
||||||
|
forge_work_dir: str = "forge"
|
||||||
|
java_path: str = "java"
|
||||||
|
deck_files: List[str] = field(default_factory=list)
|
||||||
|
deck_names: List[str] = field(default_factory=list)
|
||||||
|
policy_server_url: str = "http://localhost:8091"
|
||||||
|
clock_seconds: int = 120
|
||||||
|
|
||||||
|
# Run
|
||||||
|
num_games: int = 200
|
||||||
|
num_threads: int = 4
|
||||||
|
seed: Optional[int] = None
|
||||||
|
run_id: str = ""
|
||||||
|
results_dir: str = "results"
|
||||||
|
|
||||||
|
# Inference
|
||||||
|
greedy: bool = True
|
||||||
|
temperature: float = 1.0
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if not self.run_id:
|
||||||
|
self.run_id = str(uuid.uuid4())[:8]
|
||||||
|
|
||||||
|
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
# Per-game result
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class GameRecord:
|
||||||
|
game_index: int
|
||||||
|
won: bool
|
||||||
|
turns: int
|
||||||
|
life_final_policy: int # life total of policy seat at game end
|
||||||
|
life_final_opponent: int
|
||||||
|
life_delta: int # policy life - opponent life
|
||||||
|
commander_damage_dealt: int
|
||||||
|
illegal_actions: int
|
||||||
|
entropy_mean: float # mean per-step policy entropy
|
||||||
|
inference_ms_mean: float # mean per-step inference latency
|
||||||
|
decision_steps: int # number of decision points in this game
|
||||||
|
error: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
# Eval summary
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class EvalSummary:
|
||||||
|
run_id: str
|
||||||
|
checkpoint_path: str
|
||||||
|
num_games: int
|
||||||
|
wins: int
|
||||||
|
losses: int
|
||||||
|
win_rate: float
|
||||||
|
win_rate_ci95_low: float
|
||||||
|
win_rate_ci95_high: float
|
||||||
|
avg_turns: float
|
||||||
|
avg_life_delta: float
|
||||||
|
avg_commander_damage: float
|
||||||
|
avg_illegal_actions: float
|
||||||
|
avg_entropy: float
|
||||||
|
avg_inference_ms: float
|
||||||
|
total_decision_steps: int
|
||||||
|
elapsed_wall_s: float
|
||||||
|
games_per_sec: float
|
||||||
|
error_count: int
|
||||||
|
result_file: str
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return (
|
||||||
|
f"EvalSummary run={self.run_id}\n"
|
||||||
|
f" checkpoint : {self.checkpoint_path}\n"
|
||||||
|
f" games : {self.num_games} ({self.wins}W / {self.losses}L)\n"
|
||||||
|
f" win rate : {self.win_rate:.1%} "
|
||||||
|
f"[{self.win_rate_ci95_low:.1%}, {self.win_rate_ci95_high:.1%}] 95% CI\n"
|
||||||
|
f" avg turns : {self.avg_turns:.1f}\n"
|
||||||
|
f" avg life Δ : {self.avg_life_delta:+.1f}\n"
|
||||||
|
f" avg cmd dmg: {self.avg_commander_damage:.1f}\n"
|
||||||
|
f" illegal : {self.avg_illegal_actions:.2f}/game\n"
|
||||||
|
f" entropy : {self.avg_entropy:.4f}\n"
|
||||||
|
f" latency : {self.avg_inference_ms:.1f} ms/step\n"
|
||||||
|
f" throughput : {self.games_per_sec:.2f} games/sec\n"
|
||||||
|
f" result file: {self.result_file}\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
# Main evaluator
|
||||||
|
# ─────────────────────────────────────────────
|
||||||
|
|
||||||
|
class ForgeEvaluator:
|
||||||
|
"""
|
||||||
|
Orchestrates a full eval run:
|
||||||
|
1. Spin up PolicyInferenceService on cfg.policy_server_url port
|
||||||
|
2. Run cfg.num_games Forge games via MultiThreadBatchRunner
|
||||||
|
3. Parse Forge JSONL output for per-game metrics
|
||||||
|
4. Write results to {results_dir}/eval-{run_id}.json
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, cfg: EvalConfig):
|
||||||
|
self.cfg = cfg
|
||||||
|
self._records: List[GameRecord] = []
|
||||||
|
self._result_path = os.path.join(
|
||||||
|
cfg.results_dir, f"eval-{cfg.run_id}.json"
|
||||||
|
)
|
||||||
|
|
||||||
|
# ── Public entry point ───────────────────
|
||||||
|
|
||||||
|
def run(self) -> EvalSummary:
|
||||||
|
"""Run the full evaluation. Returns an EvalSummary."""
|
||||||
|
t0 = time.time()
|
||||||
|
os.makedirs(self.cfg.results_dir, exist_ok=True)
|
||||||
|
|
||||||
|
logger.info("[Eval %s] Starting %d-game eval run", self.cfg.run_id, self.cfg.num_games)
|
||||||
|
logger.info("[Eval %s] Checkpoint: %s", self.cfg.run_id, self.cfg.checkpoint_path)
|
||||||
|
|
||||||
|
# Load policy service
|
||||||
|
service = self._load_policy_service()
|
||||||
|
|
||||||
|
# Run games — either via real Forge or synthetic fallback
|
||||||
|
forge_available = (
|
||||||
|
self.cfg.forge_jar
|
||||||
|
and Path(self.cfg.forge_jar).exists()
|
||||||
|
and len(self.cfg.deck_files) >= 3
|
||||||
|
)
|
||||||
|
|
||||||
|
if forge_available:
|
||||||
|
self._records = self._run_forge_games(service)
|
||||||
|
else:
|
||||||
|
logger.warning(
|
||||||
|
"[Eval %s] Forge JAR or decks not found — running synthetic eval.",
|
||||||
|
self.cfg.run_id
|
||||||
|
)
|
||||||
|
self._records = self._run_synthetic_eval(service)
|
||||||
|
|
||||||
|
elapsed = time.time() - t0
|
||||||
|
summary = self._build_summary(elapsed)
|
||||||
|
self._write_results(summary)
|
||||||
|
logger.info("[Eval %s] Done. %s", self.cfg.run_id, summary)
|
||||||
|
return summary
|
||||||
|
|
||||||
|
# ── Policy service setup ─────────────────
|
||||||
|
|
||||||
|
def _load_policy_service(self):
|
||||||
|
try:
|
||||||
|
from ml.serving.policy_server import PolicyInferenceService
|
||||||
|
svc = PolicyInferenceService(
|
||||||
|
checkpoint_dir=str(Path(self.cfg.checkpoint_path).parent),
|
||||||
|
embeddings_dir=self.cfg.embeddings_dir,
|
||||||
|
device=self.cfg.device,
|
||||||
|
)
|
||||||
|
svc.load()
|
||||||
|
ok = svc.reload(self.cfg.checkpoint_path)
|
||||||
|
if not ok:
|
||||||
|
logger.warning("[Eval] Policy service load failed — entropy/latency metrics unavailable")
|
||||||
|
return svc
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("[Eval] Could not load PolicyInferenceService: %s", e)
|
||||||
|
return None
|
||||||
|
|
||||||
|
# ── Real Forge game runner ───────────────
|
||||||
|
|
||||||
|
def _run_forge_games(self, service) -> List[GameRecord]:
|
||||||
|
"""Launch LabCli Java subprocess with --ml-log, then parse JSONL output."""
|
||||||
|
logger.info("[Eval %s] Launching Forge CLI subprocess for %d games", self.cfg.run_id, self.cfg.num_games)
|
||||||
|
deck_args = []
|
||||||
|
for i, df in enumerate(self.cfg.deck_files[:4]):
|
||||||
|
deck_args.extend([f"--deck{i+1}", Path(df).stem])
|
||||||
|
# Resolve Java 17
|
||||||
|
java_exe = self.cfg.java_path
|
||||||
|
for c in [r"C:\Program Files\Eclipse Adoptium\jdk-17.0.18.8-hotspot\bin\java.exe",
|
||||||
|
r"C:\Program Files\Java\jdk-17\bin\java.exe"]:
|
||||||
|
if Path(c).exists():
|
||||||
|
java_exe = c
|
||||||
|
break
|
||||||
|
lab_jar = os.path.join(project_root, "target", "commander-ai-lab-1.0.0-SNAPSHOT.jar")
|
||||||
|
if not Path(lab_jar).exists():
|
||||||
|
logger.error("[Eval] Lab JAR not found at %s -- run mvn package -DskipTests first", lab_jar)
|
||||||
|
return self._run_synthetic_eval(service)
|
||||||
|
cmd = [
|
||||||
|
java_exe, "-jar", lab_jar,
|
||||||
|
"--forge-jar", self.cfg.forge_jar,
|
||||||
|
"--forge-dir", self.cfg.forge_work_dir,
|
||||||
|
*deck_args,
|
||||||
|
"--games", str(self.cfg.num_games),
|
||||||
|
"--threads", str(self.cfg.num_threads),
|
||||||
|
"--clock", str(self.cfg.clock_seconds),
|
||||||
|
"--ml-log",
|
||||||
|
"--output", os.path.join(self.cfg.results_dir, f"eval-{self.cfg.run_id}-batch.json"),
|
||||||
|
]
|
||||||
|
if self.cfg.seed is not None:
|
||||||
|
cmd.extend(["--seed", str(self.cfg.seed)])
|
||||||
|
logger.info("[Eval %s] CMD: %s", self.cfg.run_id, " ".join(cmd))
|
||||||
|
try:
|
||||||
|
result = subprocess.run(cmd, capture_output=True, text=True,
|
||||||
|
timeout=self.cfg.clock_seconds * self.cfg.num_games + 300)
|
||||||
|
if result.returncode != 0:
|
||||||
|
logger.warning("[Eval %s] Forge CLI exit %d: %s", self.cfg.run_id, result.returncode, result.stderr[:500])
|
||||||
|
except (subprocess.TimeoutExpired, FileNotFoundError) as e:
|
||||||
|
logger.error("[Eval %s] Forge CLI failed: %s -- falling back to synthetic", self.cfg.run_id, e)
|
||||||
|
return self._run_synthetic_eval(service)
|
||||||
|
return self._parse_batch_json(service)
|
||||||
|
|
||||||
|
def _parse_batch_json(self, service) -> List[GameRecord]:
|
||||||
|
"""Parse the batch JSON output from LabCli Java subprocess."""
|
||||||
|
batch_path = os.path.join(
|
||||||
|
self.cfg.results_dir, f"eval-{self.cfg.run_id}-batch.json"
|
||||||
|
)
|
||||||
|
if not Path(batch_path).exists():
|
||||||
|
logger.warning("[Eval] Batch JSON not found at %s — falling back to synthetic", batch_path)
|
||||||
|
return self._run_synthetic_eval(service)
|
||||||
|
with open(batch_path, encoding="utf-8") as f:
|
||||||
|
batch = json.load(f)
|
||||||
|
games_list = batch.get("games", [])
|
||||||
|
if not games_list:
|
||||||
|
logger.warning("[Eval] No games in batch JSON — falling back to synthetic")
|
||||||
|
return self._run_synthetic_eval(service)
|
||||||
|
records = []
|
||||||
|
for idx, g in enumerate(games_list):
|
||||||
|
won = g.get("winningSeat", -1) == 0
|
||||||
|
turns = g.get("totalTurns", 0)
|
||||||
|
pr = g.get("playerResults", [])
|
||||||
|
lp = pr[0].get("finalLife", 40) if len(pr) > 0 else 40
|
||||||
|
lo = pr[1].get("finalLife", 40) if len(pr) > 1 else 40
|
||||||
|
records.append(GameRecord(
|
||||||
|
game_index=idx,
|
||||||
|
won=won,
|
||||||
|
turns=turns,
|
||||||
|
life_final_policy=lp,
|
||||||
|
life_final_opponent=lo,
|
||||||
|
life_delta=lp - lo,
|
||||||
|
commander_damage_dealt=0,
|
||||||
|
illegal_actions=0,
|
||||||
|
entropy_mean=0.0,
|
||||||
|
inference_ms_mean=0.0,
|
||||||
|
decision_steps=turns,
|
||||||
|
))
|
||||||
|
if len(records) >= self.cfg.num_games:
|
||||||
|
break
|
||||||
|
logger.info("[Eval %s] Parsed %d games from batch JSON", self.cfg.run_id, len(records))
|
||||||
|
return records
|
||||||
|
|
||||||
|
def _run_forge_via_subprocess(self, service) -> List[GameRecord]:
|
||||||
|
"""Parse JSONL files produced by a separately-launched Forge batch."""
|
||||||
|
records = []
|
||||||
|
results_path = Path(self.cfg.results_dir)
|
||||||
|
pattern = f"ml-decisions-forge-eval-{self.cfg.run_id}*.jsonl"
|
||||||
|
|
||||||
|
all_files = sorted(results_path.glob(pattern))
|
||||||
|
if not all_files:
|
||||||
|
logger.warning("[Eval] No JSONL files found matching %s — falling back to synthetic", pattern)
|
||||||
|
return self._run_synthetic_eval(service)
|
||||||
|
|
||||||
|
from ml.training.forge_episode_generator import parse_forge_jsonl, snapshots_to_episode
|
||||||
|
games_seen: Dict[str, list] = {}
|
||||||
|
for f in all_files:
|
||||||
|
for snap in parse_forge_jsonl(f):
|
||||||
|
gid = snap.get("game_id", "g0")
|
||||||
|
games_seen.setdefault(gid, []).append(snap)
|
||||||
|
|
||||||
|
for idx, (gid, snaps) in enumerate(games_seen.items()):
|
||||||
|
records.append(self._snaps_to_record(idx, gid, snaps, service))
|
||||||
|
if len(records) >= self.cfg.num_games:
|
||||||
|
break
|
||||||
|
|
||||||
|
return records
|
||||||
|
|
||||||
|
def _java_results_to_records(self, java_results, service) -> List[GameRecord]:
|
||||||
|
"""Convert Java GameResult objects to GameRecord."""
|
||||||
|
records = []
|
||||||
|
for jr in java_results:
|
||||||
|
try:
|
||||||
|
won = getattr(jr, "winningSeat", 0) == 0 # seat 0 = policy
|
||||||
|
records.append(GameRecord(
|
||||||
|
game_index=getattr(jr, "gameIndex", len(records)),
|
||||||
|
won=won,
|
||||||
|
turns=getattr(jr, "turns", 0),
|
||||||
|
life_final_policy=getattr(jr, "lifeAtEnd", [40])[0] if hasattr(jr, "lifeAtEnd") else 40,
|
||||||
|
life_final_opponent=getattr(jr, "lifeAtEnd", [40, 40])[1] if hasattr(jr, "lifeAtEnd") else 40,
|
||||||
|
life_delta=0,
|
||||||
|
commander_damage_dealt=0,
|
||||||
|
illegal_actions=getattr(jr, "illegalActions", 0),
|
||||||
|
entropy_mean=0.0,
|
||||||
|
inference_ms_mean=0.0,
|
||||||
|
decision_steps=0,
|
||||||
|
))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning("[Eval] Skipping malformed GameResult: %s", e)
|
||||||
|
return records
|
||||||
|
|
||||||
|
# ── Synthetic fallback ───────────────────
|
||||||
|
|
||||||
|
def _run_synthetic_eval(self, service) -> List[GameRecord]:
|
||||||
|
"""Run synthetic games using self_play engine (no Forge required).
|
||||||
|
|
||||||
|
Used when Forge JAR is missing or deck files aren't configured.
|
||||||
|
Metrics like illegal_actions and commander_damage are 0 in this mode.
|
||||||
|
"""
|
||||||
|
import warnings
|
||||||
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||||||
|
|
||||||
|
records = []
|
||||||
|
try:
|
||||||
|
from ml.training.self_play import (
|
||||||
|
create_random_initial_state, apply_action, advance_turn,
|
||||||
|
encode_state_simple, HeuristicPolicy
|
||||||
|
)
|
||||||
|
from ml.config.scope import NUM_ACTIONS, IDX_TO_ACTION
|
||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
except ImportError as e:
|
||||||
|
logger.error("[Eval] Cannot import self_play: %s", e)
|
||||||
|
return []
|
||||||
|
|
||||||
|
opponent = HeuristicPolicy()
|
||||||
|
agent_has_model = service is not None and service._loaded
|
||||||
|
|
||||||
|
for game_idx in range(self.cfg.num_games):
|
||||||
|
state = create_random_initial_state()
|
||||||
|
entropies, latencies = [], []
|
||||||
|
steps = 0
|
||||||
|
|
||||||
|
while not state.game_over and steps < 60:
|
||||||
|
sv = encode_state_simple(state, 0, "midrange")
|
||||||
|
t0 = time.time()
|
||||||
|
|
||||||
|
if agent_has_model:
|
||||||
|
snap = state.to_snapshot()
|
||||||
|
result = service.predict(
|
||||||
|
snap, playstyle="midrange",
|
||||||
|
greedy=self.cfg.greedy,
|
||||||
|
temperature=self.cfg.temperature,
|
||||||
|
)
|
||||||
|
action_idx = result.get("action_index", 7)
|
||||||
|
probs_map = result.get("probabilities", {})
|
||||||
|
probs = np.array(list(probs_map.values())) if probs_map else np.ones(NUM_ACTIONS) / NUM_ACTIONS
|
||||||
|
else:
|
||||||
|
action_idx = np.random.randint(NUM_ACTIONS)
|
||||||
|
probs = np.ones(NUM_ACTIONS) / NUM_ACTIONS
|
||||||
|
|
||||||
|
latencies.append((time.time() - t0) * 1000)
|
||||||
|
p = np.clip(probs, 1e-9, 1.0)
|
||||||
|
p /= p.sum()
|
||||||
|
entropies.append(float(-np.sum(p * np.log(p))))
|
||||||
|
|
||||||
|
state = apply_action(state, action_idx, 0)
|
||||||
|
if not state.game_over:
|
||||||
|
opp_sv = encode_state_simple(state, 1, "midrange")
|
||||||
|
opp_a, _ = opponent.select_action(opp_sv)
|
||||||
|
state = apply_action(state, opp_a, 1)
|
||||||
|
if not state.game_over:
|
||||||
|
state = advance_turn(state)
|
||||||
|
steps += 1
|
||||||
|
|
||||||
|
won = state.winner == 0
|
||||||
|
lp = state.players[0].life_total
|
||||||
|
lo = state.players[1].life_total
|
||||||
|
records.append(GameRecord(
|
||||||
|
game_index=game_idx,
|
||||||
|
won=won,
|
||||||
|
turns=state.turn,
|
||||||
|
life_final_policy=lp,
|
||||||
|
life_final_opponent=lo,
|
||||||
|
life_delta=lp - lo,
|
||||||
|
commander_damage_dealt=0,
|
||||||
|
illegal_actions=0,
|
||||||
|
entropy_mean=float(np.mean(entropies)) if entropies else 0.0,
|
||||||
|
inference_ms_mean=float(np.mean(latencies)) if latencies else 0.0,
|
||||||
|
decision_steps=steps,
|
||||||
|
))
|
||||||
|
|
||||||
|
return records
|
||||||
|
|
||||||
|
# ── JSONL snapshot → GameRecord ──────────
|
||||||
|
|
||||||
|
def _snaps_to_record(self, idx: int, gid: str, snaps: list, service) -> GameRecord:
|
||||||
|
entropies, latencies = [], []
|
||||||
|
won = False
|
||||||
|
turns = len(snaps)
|
||||||
|
cmd_dmg = 0
|
||||||
|
illegal = 0
|
||||||
|
lp, lo = 40, 40
|
||||||
|
|
||||||
|
for snap in snaps:
|
||||||
|
if "outcome" in snap and snap == snaps[-1]:
|
||||||
|
won = snap["outcome"].get("winner_seat", -1) == 0
|
||||||
|
lp = snap["outcome"].get("life_remaining", 40)
|
||||||
|
lo = snap["outcome"].get("opp_life_remaining", 0)
|
||||||
|
cmd_dmg = snap["outcome"].get("commander_damage_dealt", 0)
|
||||||
|
|
||||||
|
if service and service._loaded and "state_vector" in snap:
|
||||||
|
t0 = time.time()
|
||||||
|
result = service.predict(snap, playstyle="midrange", greedy=self.cfg.greedy)
|
||||||
|
latencies.append((time.time() - t0) * 1000)
|
||||||
|
probs_map = result.get("probabilities", {})
|
||||||
|
if probs_map:
|
||||||
|
p = np.clip(list(probs_map.values()), 1e-9, 1.0)
|
||||||
|
p /= p.sum()
|
||||||
|
entropies.append(float(-np.sum(p * np.log(p))))
|
||||||
|
|
||||||
|
return GameRecord(
|
||||||
|
game_index=idx,
|
||||||
|
won=won,
|
||||||
|
turns=turns,
|
||||||
|
life_final_policy=lp,
|
||||||
|
life_final_opponent=lo,
|
||||||
|
life_delta=lp - lo,
|
||||||
|
commander_damage_dealt=cmd_dmg,
|
||||||
|
illegal_actions=illegal,
|
||||||
|
entropy_mean=float(np.mean(entropies)) if entropies else 0.0,
|
||||||
|
inference_ms_mean=float(np.mean(latencies)) if latencies else 0.0,
|
||||||
|
decision_steps=len(snaps),
|
||||||
|
)
|
||||||
|
|
||||||
|
# ── Summary builder ──────────────────────
|
||||||
|
|
||||||
|
def _build_summary(self, elapsed: float) -> EvalSummary:
|
||||||
|
n = len(self._records)
|
||||||
|
if n == 0:
|
||||||
|
return EvalSummary(
|
||||||
|
run_id=self.cfg.run_id,
|
||||||
|
checkpoint_path=self.cfg.checkpoint_path,
|
||||||
|
num_games=0, wins=0, losses=0,
|
||||||
|
win_rate=0.0, win_rate_ci95_low=0.0, win_rate_ci95_high=0.0,
|
||||||
|
avg_turns=0.0, avg_life_delta=0.0, avg_commander_damage=0.0,
|
||||||
|
avg_illegal_actions=0.0, avg_entropy=0.0, avg_inference_ms=0.0,
|
||||||
|
total_decision_steps=0, elapsed_wall_s=elapsed, games_per_sec=0.0,
|
||||||
|
error_count=0, result_file=self._result_path,
|
||||||
|
)
|
||||||
|
|
||||||
|
wins = sum(1 for r in self._records if r.won)
|
||||||
|
losses = n - wins
|
||||||
|
wr = wins / n
|
||||||
|
|
||||||
|
# Wilson score 95% CI
|
||||||
|
z = 1.96
|
||||||
|
denom = 1 + z**2 / n
|
||||||
|
centre = (wr + z**2 / (2 * n)) / denom
|
||||||
|
margin = (z * (wr * (1 - wr) / n + z**2 / (4 * n**2)) ** 0.5) / denom
|
||||||
|
ci_lo = max(0.0, centre - margin)
|
||||||
|
ci_hi = min(1.0, centre + margin)
|
||||||
|
|
||||||
|
errors = sum(1 for r in self._records if r.error)
|
||||||
|
|
||||||
|
return EvalSummary(
|
||||||
|
run_id=self.cfg.run_id,
|
||||||
|
checkpoint_path=self.cfg.checkpoint_path,
|
||||||
|
num_games=n,
|
||||||
|
wins=wins,
|
||||||
|
losses=losses,
|
||||||
|
win_rate=wr,
|
||||||
|
win_rate_ci95_low=ci_lo,
|
||||||
|
win_rate_ci95_high=ci_hi,
|
||||||
|
avg_turns=float(np.mean([r.turns for r in self._records])),
|
||||||
|
avg_life_delta=float(np.mean([r.life_delta for r in self._records])),
|
||||||
|
avg_commander_damage=float(np.mean([r.commander_damage_dealt for r in self._records])),
|
||||||
|
avg_illegal_actions=float(np.mean([r.illegal_actions for r in self._records])),
|
||||||
|
avg_entropy=float(np.mean([r.entropy_mean for r in self._records])),
|
||||||
|
avg_inference_ms=float(np.mean([r.inference_ms_mean for r in self._records])),
|
||||||
|
total_decision_steps=sum(r.decision_steps for r in self._records),
|
||||||
|
elapsed_wall_s=elapsed,
|
||||||
|
games_per_sec=n / max(elapsed, 0.001),
|
||||||
|
error_count=errors,
|
||||||
|
result_file=self._result_path,
|
||||||
|
)
|
||||||
|
|
||||||
|
# ── Result writer ────────────────────────
|
||||||
|
|
||||||
|
def _write_results(self, summary: EvalSummary) -> None:
|
||||||
|
payload = {
|
||||||
|
"summary": asdict(summary),
|
||||||
|
"games": [asdict(r) for r in self._records],
|
||||||
|
}
|
||||||
|
os.makedirs(self.cfg.results_dir, exist_ok=True)
|
||||||
|
with open(self._result_path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(payload, f, indent=2)
|
||||||
|
logger.info("[Eval %s] Results written to %s", self.cfg.run_id, self._result_path)
|
||||||
@@ -417,7 +417,7 @@ def main():
|
|||||||
|
|
||||||
# Save results
|
# Save results
|
||||||
output_path = args.output or os.path.join(ckpt_dir, "tournament_results.json")
|
output_path = args.output or os.path.join(ckpt_dir, "tournament_results.json")
|
||||||
with open(output_path, "w") as f:
|
with open(output_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(result.to_dict(), f, indent=2)
|
json.dump(result.to_dict(), f, indent=2)
|
||||||
logger.info("Results saved: %s", output_path)
|
logger.info("Results saved: %s", output_path)
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ def load_precon_cards(precon_dir: str) -> dict:
|
|||||||
cards = {"commander": None, "creatures": [], "spells": [], "lands": [], "artifacts": [], "all": []}
|
cards = {"commander": None, "creatures": [], "spells": [], "lands": [], "artifacts": [], "all": []}
|
||||||
|
|
||||||
section = "Main"
|
section = "Main"
|
||||||
with open(dck_file, "r") as f:
|
with open(dck_file, "r", encoding="utf-8") as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line or line.startswith("//") or line.startswith("Name="):
|
if not line or line.startswith("//") or line.startswith("Name="):
|
||||||
@@ -380,7 +380,7 @@ def main():
|
|||||||
os.makedirs(os.path.dirname(args.output) or ".", exist_ok=True)
|
os.makedirs(os.path.dirname(args.output) or ".", exist_ok=True)
|
||||||
total_decisions = 0
|
total_decisions = 0
|
||||||
|
|
||||||
with open(args.output, "w") as f:
|
with open(args.output, "w", encoding="utf-8") as f:
|
||||||
for game_idx in range(args.games):
|
for game_idx in range(args.games):
|
||||||
# Pick two random decks
|
# Pick two random decks
|
||||||
d1_name, d2_name = random.sample(deck_names, 2)
|
d1_name, d2_name = random.sample(deck_names, 2)
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ def cmd_status(args):
|
|||||||
for f in jsonl_files:
|
for f in jsonl_files:
|
||||||
lines = 0
|
lines = 0
|
||||||
try:
|
try:
|
||||||
with open(f) as fh:
|
with open(f, encoding="utf-8") as fh:
|
||||||
lines = sum(1 for _ in fh)
|
lines = sum(1 for _ in fh)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -130,7 +130,7 @@ def cmd_inspect(args):
|
|||||||
|
|
||||||
print(f"\n Inspecting: {filepath.name}\n")
|
print(f"\n Inspecting: {filepath.name}\n")
|
||||||
count = 0
|
count = 0
|
||||||
with open(filepath) as f:
|
with open(filepath, encoding="utf-8") as f:
|
||||||
for i, line in enumerate(f):
|
for i, line in enumerate(f):
|
||||||
if i >= args.limit:
|
if i >= args.limit:
|
||||||
break
|
break
|
||||||
@@ -150,7 +150,7 @@ def cmd_inspect(args):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f" [{i:>4}] ERROR: {e}")
|
print(f" [{i:>4}] ERROR: {e}")
|
||||||
|
|
||||||
with open(filepath) as f:
|
with open(filepath, encoding="utf-8") as f:
|
||||||
total = sum(1 for _ in f)
|
total = sum(1 for _ in f)
|
||||||
print(f"\n Showing {count} of {total:,} total decisions.\n")
|
print(f"\n Showing {count} of {total:,} total decisions.\n")
|
||||||
|
|
||||||
|
|||||||
+48
-13
@@ -52,14 +52,44 @@ from ml.config.scope import (
|
|||||||
)
|
)
|
||||||
from ml.encoder.state_encoder import CardEmbeddingIndex, StateEncoder
|
from ml.encoder.state_encoder import CardEmbeddingIndex, StateEncoder
|
||||||
|
|
||||||
logger = logging.getLogger("ml.serving")
|
# Unique logger name so log lines are attributable to this module specifically
|
||||||
|
logger = logging.getLogger("ml.serving.policy_server")
|
||||||
|
|
||||||
|
|
||||||
|
def _unwrap_logits(raw):
|
||||||
|
"""
|
||||||
|
Normalise whatever self.model() returns to a plain logits tensor.
|
||||||
|
|
||||||
|
PolicyNetwork.forward() returns a raw tensor, but the model may be
|
||||||
|
wrapped (DataParallel, torch.compile) or accidentally swapped for
|
||||||
|
PolicyValueNetwork whose forward_with_value() returns (logits, value).
|
||||||
|
A dict return would cause 'dict / float' TypeError on the temperature
|
||||||
|
division -- guard against all three cases here.
|
||||||
|
"""
|
||||||
|
if isinstance(raw, (tuple, list)):
|
||||||
|
# (logits, value) from PolicyValueNetwork or similar
|
||||||
|
return raw[0]
|
||||||
|
if isinstance(raw, dict):
|
||||||
|
# e.g. {"logits": tensor, "value": tensor}
|
||||||
|
for key in ("logits", "policy", "action_logits"):
|
||||||
|
if key in raw:
|
||||||
|
return raw[key]
|
||||||
|
# Fallback: grab the first tensor value in the dict
|
||||||
|
for v in raw.values():
|
||||||
|
if isinstance(v, torch.Tensor):
|
||||||
|
return v
|
||||||
|
raise TypeError(
|
||||||
|
f"model() returned a dict with no recognisable logits key: {list(raw.keys())}"
|
||||||
|
)
|
||||||
|
# Plain tensor -- the normal case
|
||||||
|
return raw
|
||||||
|
|
||||||
|
|
||||||
class PolicyInferenceService:
|
class PolicyInferenceService:
|
||||||
"""
|
"""
|
||||||
Loads a trained PolicyNetwork and provides action predictions.
|
Loads a trained PolicyNetwork and provides action predictions.
|
||||||
|
|
||||||
Thread-safe — uses torch.no_grad() for inference.
|
Thread-safe -- uses torch.no_grad() for inference.
|
||||||
Designed to be embedded in lab_api.py or run standalone.
|
Designed to be embedded in lab_api.py or run standalone.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -100,7 +130,7 @@ class PolicyInferenceService:
|
|||||||
"""
|
"""
|
||||||
if not TORCH_AVAILABLE:
|
if not TORCH_AVAILABLE:
|
||||||
self._load_error = "PyTorch not installed"
|
self._load_error = "PyTorch not installed"
|
||||||
logger.error("PyTorch not installed — cannot serve policy")
|
logger.error("PyTorch not installed -- cannot serve policy")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 1. Load card embeddings
|
# 1. Load card embeddings
|
||||||
@@ -146,7 +176,7 @@ class PolicyInferenceService:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._load_error = str(e)
|
self._load_error = str(e)
|
||||||
logger.error("Failed to load checkpoint: %s", e)
|
logger.error("Failed to load checkpoint: %s", e, exc_info=True)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def reload(self, checkpoint_path: str = None) -> bool:
|
def reload(self, checkpoint_path: str = None) -> bool:
|
||||||
@@ -178,7 +208,7 @@ class PolicyInferenceService:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._load_error = str(e)
|
self._load_error = str(e)
|
||||||
logger.error("Hot-reload failed: %s", e)
|
logger.error("Hot-reload failed: %s", e, exc_info=True)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def predict(
|
def predict(
|
||||||
@@ -224,7 +254,10 @@ class PolicyInferenceService:
|
|||||||
|
|
||||||
# Run inference
|
# Run inference
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
logits = self.model(state_tensor)
|
raw_output = self.model(state_tensor)
|
||||||
|
# Guard: unwrap tuple/dict returns (DataParallel, torch.compile,
|
||||||
|
# or accidental PolicyValueNetwork swap) before any arithmetic.
|
||||||
|
logits = _unwrap_logits(raw_output)
|
||||||
|
|
||||||
if greedy:
|
if greedy:
|
||||||
action_idx = logits.argmax(dim=-1).item()
|
action_idx = logits.argmax(dim=-1).item()
|
||||||
@@ -254,7 +287,7 @@ class PolicyInferenceService:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
elapsed_ms = (time.time() - t_start) * 1000
|
elapsed_ms = (time.time() - t_start) * 1000
|
||||||
logger.error("Inference error: %s", e)
|
logger.error("Inference error: %s", e, exc_info=True)
|
||||||
return {
|
return {
|
||||||
"error": str(e),
|
"error": str(e),
|
||||||
"inference_ms": round(elapsed_ms, 2),
|
"inference_ms": round(elapsed_ms, 2),
|
||||||
@@ -286,7 +319,9 @@ class PolicyInferenceService:
|
|||||||
|
|
||||||
# Batch inference
|
# Batch inference
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
logits = self.model(state_tensor)
|
raw_output = self.model(state_tensor)
|
||||||
|
# Same guard as predict() -- unwrap before softmax/argmax.
|
||||||
|
logits = _unwrap_logits(raw_output)
|
||||||
probs = torch.softmax(logits, dim=-1).cpu().numpy()
|
probs = torch.softmax(logits, dim=-1).cpu().numpy()
|
||||||
|
|
||||||
if greedy:
|
if greedy:
|
||||||
@@ -312,7 +347,7 @@ class PolicyInferenceService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Batch inference error: %s", e)
|
logger.error("Batch inference error: %s", e, exc_info=True)
|
||||||
results = [{"error": str(e)}] * len(snapshots)
|
results = [{"error": str(e)}] * len(snapshots)
|
||||||
|
|
||||||
return results
|
return results
|
||||||
@@ -331,9 +366,9 @@ class PolicyInferenceService:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# ══════════════════════════════════════════════════════════
|
# ==========================================================
|
||||||
# Standalone FastAPI Server
|
# Standalone FastAPI Server
|
||||||
# ══════════════════════════════════════════════════════════
|
# ==========================================================
|
||||||
|
|
||||||
def create_standalone_app():
|
def create_standalone_app():
|
||||||
"""Create a standalone FastAPI app for the policy server."""
|
"""Create a standalone FastAPI app for the policy server."""
|
||||||
@@ -341,7 +376,7 @@ def create_standalone_app():
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Commander AI Lab — Policy Server",
|
title="Commander AI Lab -- Policy Server",
|
||||||
description="Serves macro-action predictions from a trained policy network",
|
description="Serves macro-action predictions from a trained policy network",
|
||||||
version="1.0.0",
|
version="1.0.0",
|
||||||
)
|
)
|
||||||
@@ -424,7 +459,7 @@ def main():
|
|||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Commander AI Lab — Policy Inference Server"
|
description="Commander AI Lab -- Policy Inference Server"
|
||||||
)
|
)
|
||||||
parser.add_argument("--port", type=int, default=8090)
|
parser.add_argument("--port", type=int, default=8090)
|
||||||
parser.add_argument("--host", default="127.0.0.1")
|
parser.add_argument("--host", default="127.0.0.1")
|
||||||
|
|||||||
@@ -253,7 +253,7 @@ class DecisionExporter:
|
|||||||
filename = f"ml-decisions-ppo-{self._batch_id}.jsonl"
|
filename = f"ml-decisions-ppo-{self._batch_id}.jsonl"
|
||||||
filepath = os.path.join(self.config.output_dir, filename)
|
filepath = os.path.join(self.config.output_dir, filename)
|
||||||
|
|
||||||
with open(filepath, "w") as f:
|
with open(filepath, "w", encoding="utf-8") as f:
|
||||||
for record in self._records:
|
for record in self._records:
|
||||||
f.write(json.dumps(record) + "\n")
|
f.write(json.dumps(record) + "\n")
|
||||||
|
|
||||||
|
|||||||
@@ -759,7 +759,7 @@ class DistillationLoop:
|
|||||||
"""Save generation history to JSON."""
|
"""Save generation history to JSON."""
|
||||||
path = os.path.join(self.config.history_dir, "generations.json")
|
path = os.path.join(self.config.history_dir, "generations.json")
|
||||||
history = [g.to_dict() for g in self.generations]
|
history = [g.to_dict() for g in self.generations]
|
||||||
with open(path, "w") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
json.dump(history, f, indent=2)
|
json.dump(history, f, indent=2)
|
||||||
logger.info(" Generation history saved: %s", path)
|
logger.info(" Generation history saved: %s", path)
|
||||||
|
|
||||||
@@ -793,7 +793,7 @@ class DistillationLoop:
|
|||||||
def _save_summary(self, summary: Dict):
|
def _save_summary(self, summary: Dict):
|
||||||
"""Save final summary to JSON."""
|
"""Save final summary to JSON."""
|
||||||
path = os.path.join(self.config.history_dir, "summary.json")
|
path = os.path.join(self.config.history_dir, "summary.json")
|
||||||
with open(path, "w") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
json.dump(summary, f, indent=2, default=str)
|
json.dump(summary, f, indent=2, default=str)
|
||||||
logger.info(" Summary saved: %s", path)
|
logger.info(" Summary saved: %s", path)
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ from ml.training.reward import (
|
|||||||
compute_intermediate_reward,
|
compute_intermediate_reward,
|
||||||
compute_terminal_reward,
|
compute_terminal_reward,
|
||||||
)
|
)
|
||||||
|
from ml.training.decision_exporter import DecisionExporter, ExporterConfig
|
||||||
|
|
||||||
logger = logging.getLogger("ml.forge_episode_generator")
|
logger = logging.getLogger("ml.forge_episode_generator")
|
||||||
|
|
||||||
@@ -51,6 +52,8 @@ class ForgeTransition:
|
|||||||
log_prob: float
|
log_prob: float
|
||||||
value: float
|
value: float
|
||||||
done: bool
|
done: bool
|
||||||
|
# Raw snapshot retained so the exporter can reconstruct game-state context
|
||||||
|
snapshot: Optional[Dict[str, Any]] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -61,6 +64,8 @@ class ForgeEpisode:
|
|||||||
winner: Optional[int] = None
|
winner: Optional[int] = None
|
||||||
total_turns: int = 0
|
total_turns: int = 0
|
||||||
deck_names: List[str] = field(default_factory=list)
|
deck_names: List[str] = field(default_factory=list)
|
||||||
|
agent_seat: int = 0
|
||||||
|
playstyle: str = "midrange"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def length(self) -> int:
|
def length(self) -> int:
|
||||||
@@ -70,6 +75,10 @@ class ForgeEpisode:
|
|||||||
def total_reward(self) -> float:
|
def total_reward(self) -> float:
|
||||||
return sum(t.reward for t in self.transitions)
|
return sum(t.reward for t in self.transitions)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def won(self) -> bool:
|
||||||
|
return self.winner == self.agent_seat
|
||||||
|
|
||||||
|
|
||||||
# ══════════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════════
|
||||||
# JSONL Parser — reads Forge ML decision snapshots
|
# JSONL Parser — reads Forge ML decision snapshots
|
||||||
@@ -82,7 +91,7 @@ def parse_forge_jsonl(path: Path) -> List[Dict[str, Any]]:
|
|||||||
game_id, turn, phase, active_seat, state, action, outcome
|
game_id, turn, phase, active_seat, state, action, outcome
|
||||||
"""
|
"""
|
||||||
snapshots = []
|
snapshots = []
|
||||||
with open(path, "r") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
for lineno, line in enumerate(f, 1):
|
for lineno, line in enumerate(f, 1):
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line:
|
if not line:
|
||||||
@@ -95,9 +104,44 @@ def parse_forge_jsonl(path: Path) -> List[Dict[str, Any]]:
|
|||||||
return snapshots
|
return snapshots
|
||||||
|
|
||||||
|
|
||||||
|
def _make_minimal_state(snap: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Build a minimal prev/curr state dict from a Forge snapshot.
|
||||||
|
|
||||||
|
compute_intermediate_reward() expects a dict with a 'players' list
|
||||||
|
where each player has 'life_total', 'cards_in_hand',
|
||||||
|
'creatures_on_battlefield', etc. Forge snapshots carry a 'players'
|
||||||
|
array directly; we forward it as-is and fall back to scalar fields
|
||||||
|
when the array is absent.
|
||||||
|
"""
|
||||||
|
if "players" in snap:
|
||||||
|
return {"players": snap["players"]}
|
||||||
|
|
||||||
|
# Fallback: reconstruct a two-player view from flat snapshot fields
|
||||||
|
agent_seat = snap.get("active_seat", 0)
|
||||||
|
opp_seat = 1 - agent_seat
|
||||||
|
players = [None, None]
|
||||||
|
|
||||||
|
def _player(life_key: str, hand_key: str, creatures_key: str) -> Dict:
|
||||||
|
return {
|
||||||
|
"life_total": snap.get(life_key, 40),
|
||||||
|
"cards_in_hand": snap.get(hand_key, 0),
|
||||||
|
"creatures_on_battlefield": snap.get(creatures_key, 0),
|
||||||
|
"cards_in_graveyard": 0,
|
||||||
|
"mana_available": snap.get("mana_available", 0),
|
||||||
|
"commander_damage_taken": 0,
|
||||||
|
"total_power_on_board": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
players[agent_seat] = _player("agent_life", "agent_hand_size", "agent_creatures")
|
||||||
|
players[opp_seat] = _player("opp_life", "opp_hand_size", "opp_creatures")
|
||||||
|
return {"players": players}
|
||||||
|
|
||||||
|
|
||||||
def snapshots_to_episode(
|
def snapshots_to_episode(
|
||||||
snapshots: List[Dict[str, Any]],
|
snapshots: List[Dict[str, Any]],
|
||||||
reward_config: Optional[RewardConfig] = None,
|
reward_config: Optional[RewardConfig] = None,
|
||||||
|
agent_seat: int = 0,
|
||||||
|
playstyle: str = "midrange",
|
||||||
) -> ForgeEpisode:
|
) -> ForgeEpisode:
|
||||||
"""Convert a sequence of Forge snapshots into a ForgeEpisode.
|
"""Convert a sequence of Forge snapshots into a ForgeEpisode.
|
||||||
|
|
||||||
@@ -111,6 +155,8 @@ def snapshots_to_episode(
|
|||||||
episode = ForgeEpisode(
|
episode = ForgeEpisode(
|
||||||
game_id=first.get("game_id", "unknown"),
|
game_id=first.get("game_id", "unknown"),
|
||||||
deck_names=first.get("deck_names", []),
|
deck_names=first.get("deck_names", []),
|
||||||
|
agent_seat=agent_seat,
|
||||||
|
playstyle=playstyle,
|
||||||
)
|
)
|
||||||
|
|
||||||
rc = reward_config or RewardConfig()
|
rc = reward_config or RewardConfig()
|
||||||
@@ -130,20 +176,26 @@ def snapshots_to_episode(
|
|||||||
except (ValueError, KeyError):
|
except (ValueError, KeyError):
|
||||||
action_idx = ACTION_TO_IDX[MacroAction.PASS]
|
action_idx = ACTION_TO_IDX[MacroAction.PASS]
|
||||||
|
|
||||||
# Reward
|
# Reward — use correct reward.py signatures
|
||||||
is_done = i == len(snapshots) - 1
|
is_done = i == len(snapshots) - 1
|
||||||
if is_done and "outcome" in snap:
|
if is_done and "outcome" in snap:
|
||||||
|
outcome = snap["outcome"]
|
||||||
reward = compute_terminal_reward(
|
reward = compute_terminal_reward(
|
||||||
won=snap["outcome"].get("won", False),
|
won=outcome.get("won", False),
|
||||||
turns=snap.get("turn", 1),
|
draw=outcome.get("draw", False),
|
||||||
life_remaining=snap["outcome"].get("life_remaining", 0),
|
timeout=outcome.get("timeout", False),
|
||||||
config=rc,
|
config=rc,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
# Build minimal prev/curr dicts for intermediate reward
|
||||||
|
prev_snap = snapshots[i - 1] if i > 0 else snap
|
||||||
|
prev_state = _make_minimal_state(prev_snap)
|
||||||
|
curr_state = _make_minimal_state(snap)
|
||||||
reward = compute_intermediate_reward(
|
reward = compute_intermediate_reward(
|
||||||
life_delta=snap.get("life_delta", 0),
|
prev_state=prev_state,
|
||||||
cards_drawn=snap.get("cards_drawn", 0),
|
curr_state=curr_state,
|
||||||
creatures_played=snap.get("creatures_played", 0),
|
action_taken=action_label,
|
||||||
|
agent_seat=agent_seat,
|
||||||
config=rc,
|
config=rc,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -154,6 +206,7 @@ def snapshots_to_episode(
|
|||||||
log_prob=snap.get("log_prob", 0.0),
|
log_prob=snap.get("log_prob", 0.0),
|
||||||
value=snap.get("value", 0.0),
|
value=snap.get("value", 0.0),
|
||||||
done=is_done,
|
done=is_done,
|
||||||
|
snapshot=snap,
|
||||||
))
|
))
|
||||||
|
|
||||||
episode.total_turns = snapshots[-1].get("turn", len(snapshots))
|
episode.total_turns = snapshots[-1].get("turn", len(snapshots))
|
||||||
@@ -163,6 +216,61 @@ def snapshots_to_episode(
|
|||||||
return episode
|
return episode
|
||||||
|
|
||||||
|
|
||||||
|
# ══════════════════════════════════════════════════════════
|
||||||
|
# DecisionExporter adapter for Forge episodes
|
||||||
|
# ══════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
def _export_forge_episode(episode: ForgeEpisode, exporter: DecisionExporter) -> None:
|
||||||
|
"""Feed a completed ForgeEpisode into the DecisionExporter.
|
||||||
|
|
||||||
|
Translates each ForgeTransition back into the game_state_snapshot
|
||||||
|
format expected by DecisionExporter.record_step(), then finalises
|
||||||
|
the episode so the exporter can apply its win/length gates.
|
||||||
|
|
||||||
|
The raw snapshot stored on each ForgeTransition is used directly
|
||||||
|
when available; otherwise a minimal dict is synthesised from the
|
||||||
|
transition's action index and reward so the schema is always valid.
|
||||||
|
"""
|
||||||
|
exporter.begin_episode(
|
||||||
|
agent_seat=episode.agent_seat,
|
||||||
|
playstyle=episode.playstyle,
|
||||||
|
)
|
||||||
|
|
||||||
|
for t in episode.transitions:
|
||||||
|
if t.snapshot is not None:
|
||||||
|
game_state = _make_minimal_state(t.snapshot)
|
||||||
|
# Carry through turn/phase/active_seat if present
|
||||||
|
game_state["turn"] = t.snapshot.get("turn", 1)
|
||||||
|
game_state["phase"] = t.snapshot.get("phase", "main_1")
|
||||||
|
game_state["active_seat"] = t.snapshot.get("active_seat", episode.agent_seat)
|
||||||
|
else:
|
||||||
|
# Minimal fallback — exporter only needs players list for schema
|
||||||
|
game_state = {
|
||||||
|
"turn": 1,
|
||||||
|
"phase": "main_1",
|
||||||
|
"active_seat": episode.agent_seat,
|
||||||
|
"players": [
|
||||||
|
{"life_total": 40, "cards_in_hand": 0, "creatures_on_battlefield": 0,
|
||||||
|
"cards_in_graveyard": 0, "mana_available": 0,
|
||||||
|
"commander_damage_taken": 0, "total_power_on_board": 0, "commander_tax": 0},
|
||||||
|
{"life_total": 40, "cards_in_hand": 0, "creatures_on_battlefield": 0,
|
||||||
|
"cards_in_graveyard": 0, "mana_available": 0,
|
||||||
|
"commander_damage_taken": 0, "total_power_on_board": 0, "commander_tax": 0},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
exporter.record_step(
|
||||||
|
game_state_snapshot=game_state,
|
||||||
|
action_idx=t.action,
|
||||||
|
reward=t.reward,
|
||||||
|
)
|
||||||
|
|
||||||
|
exporter.end_episode(
|
||||||
|
won=episode.won,
|
||||||
|
episode_return=episode.total_reward,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# ══════════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════════
|
||||||
# Async Episode Queue (producer-consumer)
|
# Async Episode Queue (producer-consumer)
|
||||||
# ══════════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════════
|
||||||
@@ -271,6 +379,7 @@ async def ppo_consumer(
|
|||||||
queue: ForgeEpisodeQueue,
|
queue: ForgeEpisodeQueue,
|
||||||
buffer_size: int = 64,
|
buffer_size: int = 64,
|
||||||
on_batch_ready=None,
|
on_batch_ready=None,
|
||||||
|
exporter: Optional[DecisionExporter] = None,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Consumer coroutine: pulls episodes from the queue and batches
|
"""Consumer coroutine: pulls episodes from the queue and batches
|
||||||
them for PPO training updates.
|
them for PPO training updates.
|
||||||
@@ -279,6 +388,11 @@ async def ppo_consumer(
|
|||||||
queue: ForgeEpisodeQueue to consume from
|
queue: ForgeEpisodeQueue to consume from
|
||||||
buffer_size: number of episodes per training batch
|
buffer_size: number of episodes per training batch
|
||||||
on_batch_ready: callback(episodes) when a batch is full
|
on_batch_ready: callback(episodes) when a batch is full
|
||||||
|
exporter: Optional DecisionExporter — when provided, every
|
||||||
|
consumed episode is fed into the exporter so winning
|
||||||
|
Forge games accumulate in the distillation JSONL pool.
|
||||||
|
Call exporter.flush() after the pipeline completes to
|
||||||
|
write the JSONL file.
|
||||||
|
|
||||||
Returns total episodes consumed.
|
Returns total episodes consumed.
|
||||||
"""
|
"""
|
||||||
@@ -292,6 +406,14 @@ async def ppo_consumer(
|
|||||||
break
|
break
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Feed into distillation exporter (win-gate applied inside exporter)
|
||||||
|
if exporter is not None:
|
||||||
|
try:
|
||||||
|
_export_forge_episode(episode, exporter)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("[CONSUMER] Exporter error for episode %s: %s",
|
||||||
|
episode.game_id, exc)
|
||||||
|
|
||||||
batch.append(episode)
|
batch.append(episode)
|
||||||
total_consumed += 1
|
total_consumed += 1
|
||||||
|
|
||||||
@@ -322,12 +444,24 @@ async def run_forge_pipeline(
|
|||||||
max_queue: int = 256,
|
max_queue: int = 256,
|
||||||
on_batch_ready=None,
|
on_batch_ready=None,
|
||||||
reward_config: Optional[RewardConfig] = None,
|
reward_config: Optional[RewardConfig] = None,
|
||||||
) -> Dict[str, int]:
|
exporter: Optional[DecisionExporter] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
"""Run the full Forge producer → PPO consumer pipeline.
|
"""Run the full Forge producer → PPO consumer pipeline.
|
||||||
|
|
||||||
The Forge Java batch must be started separately (via forge_runner.py
|
The Forge Java batch must be started separately (via forge_runner.py
|
||||||
or MultiThreadBatchRunner). This function handles the Python side:
|
or MultiThreadBatchRunner). This function handles the Python side:
|
||||||
reading JSONL output and feeding episodes into the PPO trainer.
|
reading JSONL output and feeding episodes into the PPO trainer.
|
||||||
|
|
||||||
|
If an exporter is provided, winning Forge episodes are recorded and
|
||||||
|
flushed to a PPO JSONL file at the end of the pipeline run. The
|
||||||
|
output path is returned in the stats dict under "exporter_file".
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
from ml.training.decision_exporter import DecisionExporter, ExporterConfig
|
||||||
|
exporter = DecisionExporter(ExporterConfig(model_version="gen-1"))
|
||||||
|
stats = await run_forge_pipeline(num_games=128, exporter=exporter)
|
||||||
|
print(stats["exporter_file"]) # data/results/ml-decisions-ppo-<id>.jsonl
|
||||||
"""
|
"""
|
||||||
queue = ForgeEpisodeQueue(max_size=max_queue)
|
queue = ForgeEpisodeQueue(max_size=max_queue)
|
||||||
|
|
||||||
@@ -336,16 +470,25 @@ async def run_forge_pipeline(
|
|||||||
reward_config=reward_config)
|
reward_config=reward_config)
|
||||||
)
|
)
|
||||||
consumer_task = asyncio.create_task(
|
consumer_task = asyncio.create_task(
|
||||||
ppo_consumer(queue, buffer_size, on_batch_ready)
|
ppo_consumer(queue, buffer_size, on_batch_ready, exporter=exporter)
|
||||||
)
|
)
|
||||||
|
|
||||||
produced = await producer_task
|
produced = await producer_task
|
||||||
queue.close()
|
queue.close()
|
||||||
consumed = await consumer_task
|
consumed = await consumer_task
|
||||||
|
|
||||||
stats = {
|
# Flush exporter to disk and capture output path
|
||||||
|
exporter_file = None
|
||||||
|
if exporter is not None:
|
||||||
|
exporter_file = exporter.flush()
|
||||||
|
if exporter_file:
|
||||||
|
logger.info("[PIPELINE] Distillation export: %s stats=%s",
|
||||||
|
exporter_file, exporter.stats)
|
||||||
|
|
||||||
|
stats: Dict[str, Any] = {
|
||||||
"produced": produced,
|
"produced": produced,
|
||||||
"consumed": consumed,
|
"consumed": consumed,
|
||||||
|
"exporter_file": exporter_file,
|
||||||
**queue.stats,
|
**queue.stats,
|
||||||
}
|
}
|
||||||
logger.info("[PIPELINE] Complete: %s", stats)
|
logger.info("[PIPELINE] Complete: %s", stats)
|
||||||
|
|||||||
@@ -696,7 +696,7 @@ class PPOTrainer:
|
|||||||
self.save_checkpoint()
|
self.save_checkpoint()
|
||||||
|
|
||||||
history_path = os.path.join(cfg.checkpoint_dir, "ppo_history.json")
|
history_path = os.path.join(cfg.checkpoint_dir, "ppo_history.json")
|
||||||
with open(history_path, "w") as f:
|
with open(history_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(self.history, f, indent=2, default=str)
|
json.dump(self.history, f, indent=2, default=str)
|
||||||
|
|
||||||
summary = {
|
summary = {
|
||||||
|
|||||||
+10
-4
@@ -322,7 +322,7 @@ class SupervisedTrainer:
|
|||||||
}
|
}
|
||||||
serializable_history.append(entry)
|
serializable_history.append(entry)
|
||||||
|
|
||||||
with open(history_path, "w") as f:
|
with open(history_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(serializable_history, f, indent=2)
|
json.dump(serializable_history, f, indent=2)
|
||||||
logger.info(" History saved: %s", history_path)
|
logger.info(" History saved: %s", history_path)
|
||||||
|
|
||||||
@@ -547,7 +547,7 @@ def get_best_device() -> str:
|
|||||||
return "cpu"
|
return "cpu"
|
||||||
if torch.cuda.is_available():
|
if torch.cuda.is_available():
|
||||||
name = torch.cuda.get_device_name(0)
|
name = torch.cuda.get_device_name(0)
|
||||||
mem = torch.cuda.get_device_properties(0).total_mem / (1024**3)
|
mem = torch.cuda.get_device_properties(0).total_memory / (1024**3)
|
||||||
logger.info(" CUDA device: %s (%.1f GB)", name, mem)
|
logger.info(" CUDA device: %s (%.1f GB)", name, mem)
|
||||||
return "cuda"
|
return "cuda"
|
||||||
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
|
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
|
||||||
@@ -562,6 +562,12 @@ def get_best_device() -> str:
|
|||||||
# ══════════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════════
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# Ensure logger output is visible on console
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(message)s",
|
||||||
|
)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Commander AI Lab — Train Policy Network"
|
description="Commander AI Lab — Train Policy Network"
|
||||||
)
|
)
|
||||||
@@ -638,7 +644,7 @@ def main():
|
|||||||
|
|
||||||
# Save evaluation results
|
# Save evaluation results
|
||||||
eval_path = os.path.join(ckpt_dir, "eval_results.json")
|
eval_path = os.path.join(ckpt_dir, "eval_results.json")
|
||||||
with open(eval_path, "w") as f:
|
with open(eval_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(results, f, indent=2)
|
json.dump(results, f, indent=2)
|
||||||
logger.info(f" Results saved: {eval_path}")
|
logger.info(f" Results saved: {eval_path}")
|
||||||
return
|
return
|
||||||
@@ -696,7 +702,7 @@ def main():
|
|||||||
print_evaluation(results)
|
print_evaluation(results)
|
||||||
|
|
||||||
eval_path = os.path.join(ckpt_dir, "eval_results.json")
|
eval_path = os.path.join(ckpt_dir, "eval_results.json")
|
||||||
with open(eval_path, "w") as f:
|
with open(eval_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(results, f, indent=2)
|
json.dump(results, f, indent=2)
|
||||||
logger.info(" Eval results saved: %s", eval_path)
|
logger.info(" Eval results saved: %s", eval_path)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ class Config:
|
|||||||
forge_jar: str = ""
|
forge_jar: str = ""
|
||||||
forge_dir: str = ""
|
forge_dir: str = ""
|
||||||
forge_decks_dir: str = ""
|
forge_decks_dir: str = ""
|
||||||
|
user_decks_dir: str = ""
|
||||||
lab_jar: str = ""
|
lab_jar: str = ""
|
||||||
precon_dir: str = ""
|
precon_dir: str = ""
|
||||||
results_dir: str = "results"
|
results_dir: str = "results"
|
||||||
@@ -57,6 +58,10 @@ CFG.lab_jar = os.environ.get("LAB_JAR", "")
|
|||||||
# PRECON_DIR / PRECON_DECKS_DIR are aliases -- prefer PRECON_DIR
|
# PRECON_DIR / PRECON_DECKS_DIR are aliases -- prefer PRECON_DIR
|
||||||
CFG.precon_dir = os.environ.get("PRECON_DIR") or os.environ.get("PRECON_DECKS_DIR", "")
|
CFG.precon_dir = os.environ.get("PRECON_DIR") or os.environ.get("PRECON_DECKS_DIR", "")
|
||||||
CFG.forge_decks_dir = os.environ.get("FORGE_DECKS_DIR", "")
|
CFG.forge_decks_dir = os.environ.get("FORGE_DECKS_DIR", "")
|
||||||
|
CFG.user_decks_dir = os.environ.get(
|
||||||
|
"USER_DECKS_DIR",
|
||||||
|
os.path.join(os.path.dirname(__file__), "..", "user-decks")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def validate_forge_config() -> None:
|
def validate_forge_config() -> None:
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
unzip mtg-commander-lan.zip
|
|
||||||
cd mtg-commander-lan
|
|
||||||
npm install
|
|
||||||
node server.js
|
|
||||||
+19
-5
@@ -50,6 +50,7 @@ DEFAULT_CONFIG = {
|
|||||||
"train_lr": 0.001, # learning rate
|
"train_lr": 0.001, # learning rate
|
||||||
"train_batch_size": 256, # training batch size
|
"train_batch_size": 256, # training batch size
|
||||||
"train_patience": 15, # early stopping patience
|
"train_patience": 15, # early stopping patience
|
||||||
|
"train_ds_weight": 4.0, # DeepSeek source upsampling weight (ds-*.jsonl files)
|
||||||
# Java-only settings (ignored for DeepSeek):
|
# Java-only settings (ignored for DeepSeek):
|
||||||
"java_threads": 4,
|
"java_threads": 4,
|
||||||
"java_clock": 300,
|
"java_clock": 300,
|
||||||
@@ -279,14 +280,21 @@ def run_update_weights(cfg):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def start_training(base_url, epochs, lr, batch_size, patience):
|
def start_training(base_url, epochs, lr, batch_size, patience, ds_weight=4.0):
|
||||||
"""Trigger ML training and return immediately."""
|
"""Trigger ML training and return immediately.
|
||||||
|
|
||||||
|
ds_weight: upsampling multiplier for DeepSeek decision files
|
||||||
|
(ml-decisions-ds-*.jsonl). Matches the default in
|
||||||
|
DatasetConfig.source_weights added in the recent
|
||||||
|
dataset_builder.py update.
|
||||||
|
"""
|
||||||
resp = api_post(base_url, "/api/ml/train", {
|
resp = api_post(base_url, "/api/ml/train", {
|
||||||
"epochs": epochs,
|
"epochs": epochs,
|
||||||
"lr": lr,
|
"lr": lr,
|
||||||
"batchSize": batch_size,
|
"batchSize": batch_size,
|
||||||
"patience": patience,
|
"patience": patience,
|
||||||
"rebuildDataset": True,
|
"rebuildDataset": True,
|
||||||
|
"dsWeight": ds_weight,
|
||||||
})
|
})
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
@@ -416,6 +424,7 @@ def run_overnight(cfg):
|
|||||||
log.info(f" Engine: {engine}")
|
log.info(f" Engine: {engine}")
|
||||||
log.info(f" Auto-update weights: {'Yes' if cfg['auto_update_weights'] else 'No'}")
|
log.info(f" Auto-update weights: {'Yes' if cfg['auto_update_weights'] else 'No'}")
|
||||||
log.info(f" Auto-train after: {'Yes' if cfg['auto_train'] else 'No'}")
|
log.info(f" Auto-train after: {'Yes' if cfg['auto_train'] else 'No'}")
|
||||||
|
log.info(f" DS upsampling weight: {cfg['train_ds_weight']}x")
|
||||||
log.info("")
|
log.info("")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@@ -510,7 +519,8 @@ def run_overnight(cfg):
|
|||||||
|
|
||||||
log.info("Starting ML training pipeline...")
|
log.info("Starting ML training pipeline...")
|
||||||
log.info(f" Epochs: {cfg['train_epochs']}, LR: {cfg['train_lr']}, "
|
log.info(f" Epochs: {cfg['train_epochs']}, LR: {cfg['train_lr']}, "
|
||||||
f"Batch: {cfg['train_batch_size']}, Patience: {cfg['train_patience']}")
|
f"Batch: {cfg['train_batch_size']}, Patience: {cfg['train_patience']}, "
|
||||||
|
f"DS Weight: {cfg['train_ds_weight']}x")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
start_training(
|
start_training(
|
||||||
@@ -519,6 +529,7 @@ def run_overnight(cfg):
|
|||||||
lr=cfg["train_lr"],
|
lr=cfg["train_lr"],
|
||||||
batch_size=cfg["train_batch_size"],
|
batch_size=cfg["train_batch_size"],
|
||||||
patience=cfg["train_patience"],
|
patience=cfg["train_patience"],
|
||||||
|
ds_weight=cfg["train_ds_weight"],
|
||||||
)
|
)
|
||||||
log.info("Training started, monitoring progress...")
|
log.info("Training started, monitoring progress...")
|
||||||
success = wait_for_training(base_url)
|
success = wait_for_training(base_url)
|
||||||
@@ -556,8 +567,8 @@ Examples:
|
|||||||
# Just run sims, skip both weight update and training:
|
# Just run sims, skip both weight update and training:
|
||||||
python overnight-run.py --no-train --no-update-weights
|
python overnight-run.py --no-train --no-update-weights
|
||||||
|
|
||||||
# Custom training settings:
|
# Custom training settings with higher DS upsampling:
|
||||||
python overnight-run.py --epochs 200 --lr 0.0005 --patience 20
|
python overnight-run.py --epochs 200 --lr 0.0005 --patience 20 --ds-weight 6.0
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
parser.add_argument("--url", default="http://localhost:8080",
|
parser.add_argument("--url", default="http://localhost:8080",
|
||||||
@@ -590,6 +601,8 @@ Examples:
|
|||||||
help="Training batch size (default: 256)")
|
help="Training batch size (default: 256)")
|
||||||
parser.add_argument("--patience", type=int, default=15,
|
parser.add_argument("--patience", type=int, default=15,
|
||||||
help="Early stopping patience (default: 15)")
|
help="Early stopping patience (default: 15)")
|
||||||
|
parser.add_argument("--ds-weight", type=float, default=4.0,
|
||||||
|
help="DeepSeek source upsampling weight for training (default: 4.0)")
|
||||||
parser.add_argument("--threads", type=int, default=4,
|
parser.add_argument("--threads", type=int, default=4,
|
||||||
help="Java engine threads (default: 4)")
|
help="Java engine threads (default: 4)")
|
||||||
parser.add_argument("--clock", type=int, default=300,
|
parser.add_argument("--clock", type=int, default=300,
|
||||||
@@ -611,6 +624,7 @@ Examples:
|
|||||||
cfg["train_lr"] = args.lr
|
cfg["train_lr"] = args.lr
|
||||||
cfg["train_batch_size"] = args.batch_size
|
cfg["train_batch_size"] = args.batch_size
|
||||||
cfg["train_patience"] = args.patience
|
cfg["train_patience"] = args.patience
|
||||||
|
cfg["train_ds_weight"] = args.ds_weight
|
||||||
cfg["java_threads"] = args.threads
|
cfg["java_threads"] = args.threads
|
||||||
cfg["java_clock"] = args.clock
|
cfg["java_clock"] = args.clock
|
||||||
|
|
||||||
|
|||||||
Generated
-6
@@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "MTG Commander (HTML)",
|
|
||||||
"lockfileVersion": 3,
|
|
||||||
"requires": true,
|
|
||||||
"packages": {}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
with open('ml/eval/forge_evaluator.py', 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# Fix 1: >= 2 to >= 3
|
||||||
|
content = content.replace('deck_files) >= 2', 'deck_files) >= 3')
|
||||||
|
|
||||||
|
# Fix 2: replace call to _run_forge_via_subprocess with _parse_batch_json
|
||||||
|
content = content.replace(
|
||||||
|
'return self._run_forge_via_subprocess(service)',
|
||||||
|
'return self._parse_batch_json(service)'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fix 3: Insert _parse_batch_json method before _run_forge_via_subprocess
|
||||||
|
new_method = '''
|
||||||
|
def _parse_batch_json(self, service) -> List[GameRecord]:
|
||||||
|
"""Parse the batch JSON output from LabCli Java subprocess."""
|
||||||
|
batch_path = os.path.join(
|
||||||
|
self.cfg.results_dir, f"eval-{self.cfg.run_id}-batch.json"
|
||||||
|
)
|
||||||
|
if not Path(batch_path).exists():
|
||||||
|
logger.warning("[Eval] Batch JSON not found at %s -- falling back to synthetic", batch_path)
|
||||||
|
return self._run_synthetic_eval(service)
|
||||||
|
with open(batch_path, encoding='utf-8') as f:
|
||||||
|
batch = json.load(f)
|
||||||
|
games_list = batch.get("games", [])
|
||||||
|
if not games_list:
|
||||||
|
logger.warning("[Eval] No games in batch JSON -- falling back to synthetic")
|
||||||
|
return self._run_synthetic_eval(service)
|
||||||
|
records = []
|
||||||
|
for idx, g in enumerate(games_list):
|
||||||
|
won = g.get("winningSeat", -1) == 0
|
||||||
|
turns = g.get("totalTurns", 0)
|
||||||
|
pr = g.get("playerResults", [])
|
||||||
|
lp = pr[0].get("finalLife", 40) if len(pr) > 0 else 40
|
||||||
|
lo = pr[1].get("finalLife", 40) if len(pr) > 1 else 40
|
||||||
|
records.append(GameRecord(
|
||||||
|
game_index=idx,
|
||||||
|
won=won,
|
||||||
|
turns=turns,
|
||||||
|
life_final_policy=lp,
|
||||||
|
life_final_opponent=lo,
|
||||||
|
life_delta=lp - lo,
|
||||||
|
commander_damage_dealt=0,
|
||||||
|
illegal_actions=0,
|
||||||
|
entropy_mean=0.0,
|
||||||
|
inference_ms_mean=0.0,
|
||||||
|
decision_steps=turns,
|
||||||
|
))
|
||||||
|
if len(records) >= self.cfg.num_games:
|
||||||
|
break
|
||||||
|
logger.info("[Eval %s] Parsed %d games from batch JSON", self.cfg.run_id, len(records))
|
||||||
|
return records
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
content = content.replace(
|
||||||
|
' def _run_forge_via_subprocess(',
|
||||||
|
new_method + ' def _run_forge_via_subprocess('
|
||||||
|
)
|
||||||
|
|
||||||
|
with open('ml/eval/forge_evaluator.py', 'w', encoding='utf-8') as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
|
import py_compile
|
||||||
|
py_compile.compile('ml/eval/forge_evaluator.py', doraise=True)
|
||||||
|
print('Syntax OK - patch applied')
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Phase 1 patch: no-op mana pool display functions in lan-client/ui.js
|
||||||
|
Run from anywhere — edit UI_JS_PATH to point at your file.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
UI_JS_PATH = r"D:\ForgeCommander\commander-ai-lab\lan-client\ui.js"
|
||||||
|
|
||||||
|
with open(UI_JS_PATH, "r", encoding="utf-8") as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
original_size = len(content)
|
||||||
|
changes = []
|
||||||
|
|
||||||
|
# ── Change 1: no-op renderManaPool ───────────────────────────────────────────
|
||||||
|
old1 = re.search(
|
||||||
|
r'function renderManaPool\(playerIdx\) \{[^}]*?display\.style\.display[^}]*?\}',
|
||||||
|
content, re.DOTALL
|
||||||
|
)
|
||||||
|
if old1:
|
||||||
|
content = content[:old1.start()] + \
|
||||||
|
"function renderManaPool(playerIdx) {\n // mana pool display removed (Phase 1)\n}" + \
|
||||||
|
content[old1.end():]
|
||||||
|
changes.append("renderManaPool -> stub")
|
||||||
|
else:
|
||||||
|
print("WARNING: renderManaPool pattern not matched — skipping")
|
||||||
|
|
||||||
|
# ── Change 2: no-op showManaPool ─────────────────────────────────────────────
|
||||||
|
old2 = re.search(
|
||||||
|
r'function showManaPool\(\) \{.*?\}',
|
||||||
|
content, re.DOTALL
|
||||||
|
)
|
||||||
|
if old2:
|
||||||
|
content = content[:old2.start()] + \
|
||||||
|
"function showManaPool() {\n // mana pool display removed (Phase 1)\n}" + \
|
||||||
|
content[old2.end():]
|
||||||
|
changes.append("showManaPool -> stub")
|
||||||
|
else:
|
||||||
|
print("WARNING: showManaPool pattern not matched — skipping")
|
||||||
|
|
||||||
|
# ── Change 3: remove Mana Auto-Clear row from renderSettingsPanel ─────────────
|
||||||
|
mana_row = (
|
||||||
|
" +\n '<div class=\"settings-row\"><span>Mana Auto-Clear</span>'"
|
||||||
|
" + renderToggle('manaAutoClear', gameState.manaAutoClear) + '</div>';"
|
||||||
|
)
|
||||||
|
if mana_row in content:
|
||||||
|
content = content.replace(mana_row, ";", 1)
|
||||||
|
changes.append("Mana Auto-Clear row removed from renderSettingsPanel")
|
||||||
|
else:
|
||||||
|
print("WARNING: Mana Auto-Clear row not found — skipping")
|
||||||
|
|
||||||
|
# ── Write ─────────────────────────────────────────────────────────────────────
|
||||||
|
if changes:
|
||||||
|
with open(UI_JS_PATH, "w", encoding="utf-8") as f:
|
||||||
|
f.write(content)
|
||||||
|
print(f"Done. {original_size} -> {len(content)} chars ({len(content)-original_size:+d})")
|
||||||
|
for c in changes:
|
||||||
|
print(f" ✓ {c}")
|
||||||
|
else:
|
||||||
|
print("No changes applied.")
|
||||||
+20
-4
@@ -245,6 +245,20 @@ async def add_deck_card(deck_id: int, req: AddDeckCardRequest):
|
|||||||
).fetchone()
|
).fetchone()
|
||||||
card_name = ce_row["name"] if ce_row else (req.card_name or "")
|
card_name = ce_row["name"] if ce_row else (req.card_name or "")
|
||||||
|
|
||||||
|
# Ensure card_records has type_line for this card (Scryfall lookup if missing)
|
||||||
|
if card_name:
|
||||||
|
existing_cr = conn.execute(
|
||||||
|
"SELECT type_line FROM card_records WHERE name = ? LIMIT 1", (card_name,)
|
||||||
|
).fetchone()
|
||||||
|
if not existing_cr or not existing_cr["type_line"]:
|
||||||
|
enriched = _enrich_from_scryfall(card_name)
|
||||||
|
if enriched and enriched.get("type_line"):
|
||||||
|
conn.execute(
|
||||||
|
"INSERT OR IGNORE INTO card_records (name, type_line, oracle_text, cmc) VALUES (?,?,?,?)",
|
||||||
|
(card_name, enriched["type_line"], enriched.get("oracle_text", ""), enriched.get("cmc", 0))
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
existing = conn.execute(
|
existing = conn.execute(
|
||||||
"SELECT id, quantity FROM deck_cards WHERE deck_id = ? AND scryfall_id = ?",
|
"SELECT id, quantity FROM deck_cards WHERE deck_id = ? AND scryfall_id = ?",
|
||||||
(deck_id, req.scryfall_id)
|
(deck_id, req.scryfall_id)
|
||||||
@@ -782,10 +796,12 @@ async def export_deck_to_sim(deck_id: int):
|
|||||||
if not safe_name:
|
if not safe_name:
|
||||||
safe_name = f"deck_{deck_id}"
|
safe_name = f"deck_{deck_id}"
|
||||||
|
|
||||||
save_dir = CFG.forge_decks_dir
|
save_dir = (
|
||||||
if not save_dir or not os.path.isdir(save_dir):
|
CFG.user_decks_dir
|
||||||
save_dir = os.path.join(os.path.dirname(__file__), "exported-decks")
|
or CFG.forge_decks_dir
|
||||||
os.makedirs(save_dir, exist_ok=True)
|
or os.path.join(os.path.dirname(__file__), "..", "user-decks")
|
||||||
|
)
|
||||||
|
os.makedirs(save_dir, exist_ok=True)
|
||||||
|
|
||||||
out_path = Path(save_dir) / f"{safe_name}.dck"
|
out_path = Path(save_dir) / f"{safe_name}.dck"
|
||||||
out_path.write_text(content, encoding="utf-8")
|
out_path.write_text(content, encoding="utf-8")
|
||||||
|
|||||||
+8
-7
@@ -861,12 +861,13 @@ async def _call_anthropic_api(messages: list[dict], max_tokens: int = 4096, temp
|
|||||||
else:
|
else:
|
||||||
if not CFG.anthropic_api_key:
|
if not CFG.anthropic_api_key:
|
||||||
raise HTTPException(400, 'Anthropic Claude API key not configured. Set anthropic_api_key env var or --anthropic-key.')
|
raise HTTPException(400, 'Anthropic Claude API key not configured. Set anthropic_api_key env var or --anthropic-key.')
|
||||||
base_url = 'https://api.anthropic.com'
|
base_url = 'https://api.anthropic.com'
|
||||||
model = ANTHROPIC_MODEL
|
model = ANTHROPIC_MODEL
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'x-api-key': CFG.anthropic_api_key, 'anthropic-version': '2023-06-01',
|
'x-api-key': CFG.anthropic_api_key,
|
||||||
}
|
'anthropic-version': '2023-06-01',
|
||||||
|
}
|
||||||
# Separate system messages for Anthropic API format
|
# Separate system messages for Anthropic API format
|
||||||
system_msgs = [m['content'] for m in messages if m.get('role') == 'system']
|
system_msgs = [m['content'] for m in messages if m.get('role') == 'system']
|
||||||
user_msgs = [m for m in messages if m.get('role') != 'system']
|
user_msgs = [m for m in messages if m.get('role') != 'system']
|
||||||
@@ -1320,7 +1321,7 @@ async def deck_gen_v3_status():
|
|||||||
return {
|
return {
|
||||||
'initialized': _coach._deck_gen_v3 is not None,
|
'initialized': _coach._deck_gen_v3 is not None,
|
||||||
'anthropic_configured': bool(CFG.anthropic_api_key),
|
'anthropic_configured': bool(CFG.anthropic_api_key),
|
||||||
'model': getattr(_coach._deck_gen_v3, 'model', getattr(getattr(_coach._deck_gen_v3, 'anthropic_client', None), 'model', 'ollama/gpt-oss:20b')) if _coach._deck_gen_v3 else None,
|
'model': getattr(_coach._deck_gen_v3, 'model', getattr(getattr(_coach._deck_gen_v3, 'anthropic_client', None), 'model', 'claude-opus-4-20250514')) if _coach._deck_gen_v3 else None,
|
||||||
'embeddings_loaded': False,
|
'embeddings_loaded': False,
|
||||||
'embedding_cards': 0,
|
'embedding_cards': 0,
|
||||||
'error': _coach._deck_gen_v3_error,
|
'error': _coach._deck_gen_v3_error,
|
||||||
@@ -1341,7 +1342,7 @@ async def deck_gen_v3_generate(req: DeckGenV3Request):
|
|||||||
6. Return complete deck with substitution data
|
6. Return complete deck with substitution data
|
||||||
"""
|
"""
|
||||||
if _coach._deck_gen_v3 is None:
|
if _coach._deck_gen_v3 is None:
|
||||||
raise HTTPException(503, 'V3 Deck Generator not initialized. Ensure Ollama is running with gpt-oss:20b model.')
|
raise HTTPException(503, 'V3 Deck Generator not initialized. Ensure the Anthropic API key is configured.')
|
||||||
|
|
||||||
if not req.commander_name or len(req.commander_name.strip()) < 2:
|
if not req.commander_name or len(req.commander_name.strip()) < 2:
|
||||||
raise HTTPException(400, 'Commander name is required (min 2 chars)')
|
raise HTTPException(400, 'Commander name is required (min 2 chars)')
|
||||||
|
|||||||
+1
-1
@@ -226,7 +226,7 @@ def _finish_sim(sim_id, summary, game_results, engine=None):
|
|||||||
results_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'results')
|
results_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'results')
|
||||||
os.makedirs(results_dir, exist_ok=True)
|
os.makedirs(results_dir, exist_ok=True)
|
||||||
batch_path = os.path.join(results_dir, f'ml-decision-sim-{sim_id}.json')
|
batch_path = os.path.join(results_dir, f'ml-decision-sim-{sim_id}.json')
|
||||||
with open(batch_path, 'w') as f:
|
with open(batch_path, 'w', encoding='utf-8') as f:
|
||||||
json.dump(batch_data, f, indent=2, default=str)
|
json.dump(batch_data, f, indent=2, default=str)
|
||||||
logger.info(f'Saved batch results to {batch_path}')
|
logger.info(f'Saved batch results to {batch_path}')
|
||||||
|
|
||||||
|
|||||||
+209
-65
@@ -1,11 +1,29 @@
|
|||||||
"""Live game session routes.
|
"""Live game session routes.
|
||||||
|
|
||||||
Endpoints:
|
Endpoints:
|
||||||
POST /api/game/start — create a new live game session (called by LobbySetupModal)
|
POST /api/game/start -- create and launch a live Forge game session
|
||||||
POST /api/game/action — submit a human player action (called by HumanActionBar)
|
POST /api/game/action -- submit a human player action (called by HumanActionBar)
|
||||||
|
GET /api/game/{game_id}/status -- poll session status and last known game state
|
||||||
|
GET /api/game/{game_id}/actions -- Forge IPC: drain pending human actions
|
||||||
|
GET /api/game/sessions -- list active sessions (debug)
|
||||||
|
|
||||||
|
Forge IPC flow
|
||||||
|
--------------
|
||||||
|
1. POST /api/game/start
|
||||||
|
- Validates seats, resolves deck names
|
||||||
|
- Picks a playstyle per AI seat from seat_type (e.g. "AI Aggro" -> "aggro")
|
||||||
|
- Calls run_batch_subprocess(num_games=1, use_learned_policy=True, ...)
|
||||||
|
which builds the Java command with --policy <style> --policyServer http://localhost:8080
|
||||||
|
and spawns the Forge JAR in a background thread
|
||||||
|
- Returns game_id + ws_url immediately (non-blocking)
|
||||||
|
2. Forge JAR (once launched) calls POST /api/policy/decide on every AI decision
|
||||||
|
3. Human actions are queued via POST /api/game/action and drained by
|
||||||
|
GET /api/game/{game_id}/actions (polled by the Forge IPC loop inside the JAR)
|
||||||
|
4. GET /api/game/{game_id}/status returns live log tail + completion state
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
@@ -13,14 +31,39 @@ from typing import List, Optional
|
|||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
from models.state import BatchState
|
||||||
|
from services.forge_runner import run_batch_subprocess
|
||||||
|
|
||||||
log = logging.getLogger("routes.game")
|
log = logging.getLogger("routes.game")
|
||||||
|
|
||||||
router = APIRouter(tags=["game"])
|
router = APIRouter(tags=["game"])
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Playstyle mapping: seat_type string -> policy playstyle token
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
# ───────────────────────────────────────────────────────────────
|
_PLAYSTYLE_MAP: dict[str, str] = {
|
||||||
|
"ai aggro": "aggro",
|
||||||
|
"ai control": "control",
|
||||||
|
"ai combo": "combo",
|
||||||
|
"ai political": "political",
|
||||||
|
"ai midrange": "midrange",
|
||||||
|
"ai": "midrange", # bare "AI" seat -> midrange default
|
||||||
|
"human": None, # human seats skipped for policy
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _seat_playstyle(seat_type: str) -> Optional[str]:
|
||||||
|
"""Map a SeatConfig.seat_type string to a policy playstyle token.
|
||||||
|
|
||||||
|
Returns None for human seats (no policy needed).
|
||||||
|
"""
|
||||||
|
return _PLAYSTYLE_MAP.get(seat_type.lower().strip(), "midrange")
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
# Request / Response models
|
# Request / Response models
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
class SeatConfig(BaseModel):
|
class SeatConfig(BaseModel):
|
||||||
seat_index: int = 0
|
seat_index: int = 0
|
||||||
@@ -31,12 +74,16 @@ class SeatConfig(BaseModel):
|
|||||||
|
|
||||||
class StartGameRequest(BaseModel):
|
class StartGameRequest(BaseModel):
|
||||||
seats: List[SeatConfig] = Field(default_factory=list)
|
seats: List[SeatConfig] = Field(default_factory=list)
|
||||||
|
clock: int = Field(default=6000, description="Per-game wall-clock limit in seconds (0 = unlimited)")
|
||||||
|
use_learned_policy: bool = Field(default=True, description="Route AI decisions through /api/policy/decide")
|
||||||
|
policy_greedy: bool = Field(default=False, description="Use greedy (argmax) action selection instead of sampling")
|
||||||
|
|
||||||
|
|
||||||
class StartGameResponse(BaseModel):
|
class StartGameResponse(BaseModel):
|
||||||
game_id: str
|
game_id: str
|
||||||
status: str = "started"
|
status: str = "started"
|
||||||
ws_url: str
|
ws_url: str
|
||||||
|
policy_enabled: bool = False
|
||||||
|
|
||||||
|
|
||||||
class GameActionRequest(BaseModel):
|
class GameActionRequest(BaseModel):
|
||||||
@@ -54,73 +101,139 @@ class GameActionResponse(BaseModel):
|
|||||||
action: str
|
action: str
|
||||||
|
|
||||||
|
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
# In-memory session store (replace with DB / Redis for persistence)
|
# In-memory session store
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
# { game_id: { "seats": [...], "status": "active" | "ended", "pending_actions": [...] } }
|
# { game_id: { "seats": [...], "status": "active"|"ended"|"error",
|
||||||
|
# "batch_state": BatchState|None, "pending_actions": [...] } }
|
||||||
_sessions: dict[str, dict] = {}
|
_sessions: dict[str, dict] = {}
|
||||||
|
|
||||||
|
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
# POST /api/game/start
|
# POST /api/game/start
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
@router.post("/api/game/start", response_model=StartGameResponse)
|
@router.post("/api/game/start", response_model=StartGameResponse)
|
||||||
async def start_game(req: StartGameRequest):
|
async def start_game(req: StartGameRequest):
|
||||||
"""Create a new live game session.
|
"""Create and launch a live Forge game session.
|
||||||
|
|
||||||
Called by Unity's LobbySetupModal after the player configures seats.
|
Resolves deck names from the seat config, launches the Forge JAR
|
||||||
Returns a game_id that Unity uses to connect to the WebSocket at
|
as a background subprocess with --policy routing enabled (unless
|
||||||
/ws/game/{game_id}.
|
use_learned_policy=False), and returns a game_id immediately.
|
||||||
|
|
||||||
TODO: Wire Forge engine launch here once the IPC layer is ready.
|
The Forge JAR will begin calling POST /api/policy/decide for every
|
||||||
For now, creates a session record and returns the game_id so Unity
|
AI seat decision as soon as it is up. Human actions are queued via
|
||||||
can connect and receive state pushes from the Forge IPC client.
|
POST /api/game/action and polled by the JAR via GET /api/game/{id}/actions.
|
||||||
"""
|
"""
|
||||||
if not req.seats:
|
if not req.seats:
|
||||||
raise HTTPException(status_code=400, detail="At least one seat is required.")
|
raise HTTPException(status_code=400, detail="At least one seat is required.")
|
||||||
|
|
||||||
|
# Collect deck names from seats that have one, preserving seat order.
|
||||||
|
decks = [s.deck_name for s in req.seats if s.deck_name.strip()]
|
||||||
|
if not decks:
|
||||||
|
raise HTTPException(status_code=400, detail="At least one seat must have a deck_name.")
|
||||||
|
|
||||||
|
# Determine playstyle from the first AI seat (the JAR uses a single global
|
||||||
|
# playstyle for its --policy flag; per-seat styles are a future extension).
|
||||||
|
policy_style = "midrange"
|
||||||
|
for seat in req.seats:
|
||||||
|
ps = _seat_playstyle(seat.seat_type)
|
||||||
|
if ps is not None: # skip human seats
|
||||||
|
policy_style = ps
|
||||||
|
break
|
||||||
|
|
||||||
game_id = uuid.uuid4().hex
|
game_id = uuid.uuid4().hex
|
||||||
|
|
||||||
|
# Build a BatchState so the forge_runner watchdog and progress tracking
|
||||||
|
# work the same way they do for overnight batch runs.
|
||||||
|
state = BatchState(batch_id=game_id, total_games=1, threads=1)
|
||||||
|
|
||||||
_sessions[game_id] = {
|
_sessions[game_id] = {
|
||||||
"game_id": game_id,
|
"game_id": game_id,
|
||||||
"seats": [s.dict() for s in req.seats],
|
"seats": [s.dict() for s in req.seats],
|
||||||
"status": "active",
|
"status": "active",
|
||||||
"pending_actions": [],
|
"pending_actions": [],
|
||||||
|
"batch_state": state,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info(
|
log.info(
|
||||||
"Game session created: game_id=%s seats=%d",
|
"Game session created: game_id=%s seats=%d decks=%s policy=%s greedy=%s",
|
||||||
game_id,
|
game_id, len(req.seats), decks, policy_style if req.use_learned_policy else "off",
|
||||||
len(req.seats),
|
req.policy_greedy,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Launch Forge subprocess in the background (non-blocking).
|
||||||
|
# num_games=1 -> single live game, threads=1, no seed.
|
||||||
|
# clock is passed through from the request (default 6000s for live play).
|
||||||
|
asyncio.create_task(
|
||||||
|
_run_and_finalize(
|
||||||
|
game_id=game_id,
|
||||||
|
state=state,
|
||||||
|
decks=decks,
|
||||||
|
clock=req.clock,
|
||||||
|
use_learned_policy=req.use_learned_policy,
|
||||||
|
policy_style=policy_style,
|
||||||
|
policy_greedy=req.policy_greedy,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return StartGameResponse(
|
return StartGameResponse(
|
||||||
game_id=game_id,
|
game_id=game_id,
|
||||||
status="started",
|
status="started",
|
||||||
ws_url=f"/ws/game/{game_id}",
|
ws_url=f"/ws/game/{game_id}",
|
||||||
|
policy_enabled=req.use_learned_policy,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# ───────────────────────────────────────────────────────────────
|
async def _run_and_finalize(
|
||||||
|
game_id: str,
|
||||||
|
state: BatchState,
|
||||||
|
decks: list,
|
||||||
|
clock: int,
|
||||||
|
use_learned_policy: bool,
|
||||||
|
policy_style: str,
|
||||||
|
policy_greedy: bool,
|
||||||
|
):
|
||||||
|
"""Background task: run Forge subprocess then mark session ended."""
|
||||||
|
try:
|
||||||
|
await run_batch_subprocess(
|
||||||
|
state=state,
|
||||||
|
decks=decks,
|
||||||
|
num_games=1,
|
||||||
|
threads=1,
|
||||||
|
seed=None,
|
||||||
|
clock=clock,
|
||||||
|
output_path=f"results/live-{game_id}.json",
|
||||||
|
use_learned_policy=use_learned_policy,
|
||||||
|
policy_style=policy_style,
|
||||||
|
policy_greedy=policy_greedy,
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
log.error("Game %s subprocess error: %s", game_id, exc)
|
||||||
|
if game_id in _sessions:
|
||||||
|
_sessions[game_id]["status"] = "error"
|
||||||
|
return
|
||||||
|
|
||||||
|
if game_id in _sessions:
|
||||||
|
_sessions[game_id]["status"] = "ended" if not state.error else "error"
|
||||||
|
log.info(
|
||||||
|
"Game %s finished: status=%s error=%s",
|
||||||
|
game_id, _sessions[game_id]["status"], state.error or "none",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
# POST /api/game/action
|
# POST /api/game/action
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
@router.post("/api/game/action", response_model=GameActionResponse)
|
@router.post("/api/game/action", response_model=GameActionResponse)
|
||||||
async def game_action(req: GameActionRequest):
|
async def game_action(req: GameActionRequest):
|
||||||
"""Submit a human player action during a live game.
|
"""Submit a human player action during a live game.
|
||||||
|
|
||||||
Called by Unity's HumanActionBar when seat 0 (human) has priority.
|
Called by Unity's HumanActionBar when the human seat has priority.
|
||||||
The action is queued in the session's pending_actions list, where
|
The action is queued in the session; the Forge IPC polling loop
|
||||||
the Forge IPC polling loop will pick it up.
|
drains it via GET /api/game/{game_id}/actions.
|
||||||
|
|
||||||
Valid actions:
|
|
||||||
pass_priority — pass priority to the next player
|
|
||||||
end_turn — end the current turn
|
|
||||||
declare_attackers — declare attacking creatures
|
|
||||||
declare_blockers — declare blocking creatures
|
|
||||||
select_target — select a target (requires target_id)
|
|
||||||
"""
|
"""
|
||||||
valid_actions = {
|
valid_actions = {
|
||||||
"pass_priority", "end_turn",
|
"pass_priority", "end_turn",
|
||||||
@@ -142,47 +255,73 @@ async def game_action(req: GameActionRequest):
|
|||||||
|
|
||||||
session = _sessions.get(req.game_id)
|
session = _sessions.get(req.game_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=404, detail=f"Game session '{req.game_id}' not found.")
|
||||||
status_code=404,
|
|
||||||
detail=f"Game session '{req.game_id}' not found.",
|
|
||||||
)
|
|
||||||
|
|
||||||
if session["status"] != "active":
|
if session["status"] != "active":
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=409,
|
status_code=409,
|
||||||
detail=f"Game session '{req.game_id}' is not active (status={session['status']}).",
|
detail=f"Game '{req.game_id}' is not active (status={session['status']}).",
|
||||||
)
|
)
|
||||||
|
|
||||||
action_record = {
|
session["pending_actions"].append({
|
||||||
"seat": req.seat,
|
"seat": req.seat,
|
||||||
"action": req.action,
|
"action": req.action,
|
||||||
"target_id": req.target_id,
|
"target_id": req.target_id,
|
||||||
"extra": req.extra,
|
"extra": req.extra,
|
||||||
|
})
|
||||||
|
|
||||||
|
log.info("Action queued: game_id=%s seat=%d action=%s", req.game_id, req.seat, req.action)
|
||||||
|
|
||||||
|
return GameActionResponse(status="ok", game_id=req.game_id, action=req.action)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# GET /api/game/{game_id}/status
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@router.get("/api/game/{game_id}/status")
|
||||||
|
async def game_status(game_id: str):
|
||||||
|
"""Return live status, log tail, and completion info for a game session.
|
||||||
|
|
||||||
|
Useful for polling from the frontend or a test script to track
|
||||||
|
whether Forge has finished launching and started calling /decide.
|
||||||
|
"""
|
||||||
|
session = _sessions.get(game_id)
|
||||||
|
if session is None:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Game session '{game_id}' not found.")
|
||||||
|
|
||||||
|
state: Optional[BatchState] = session.get("batch_state")
|
||||||
|
log_tail = []
|
||||||
|
completed = 0
|
||||||
|
total = 1
|
||||||
|
error = None
|
||||||
|
|
||||||
|
if state:
|
||||||
|
log_tail = state.log_lines[-20:] # last 20 lines
|
||||||
|
completed = state.completed_games
|
||||||
|
total = max(state.total_games, 1)
|
||||||
|
error = state.error
|
||||||
|
|
||||||
|
return {
|
||||||
|
"game_id": game_id,
|
||||||
|
"status": session["status"],
|
||||||
|
"seats": session["seats"],
|
||||||
|
"progress": {"completed": completed, "total": total},
|
||||||
|
"log_tail": log_tail,
|
||||||
|
"error": error,
|
||||||
|
"pending_actions": len(session["pending_actions"]),
|
||||||
}
|
}
|
||||||
session["pending_actions"].append(action_record)
|
|
||||||
|
|
||||||
log.info(
|
|
||||||
"Action queued: game_id=%s seat=%d action=%s",
|
|
||||||
req.game_id, req.seat, req.action,
|
|
||||||
)
|
|
||||||
|
|
||||||
return GameActionResponse(
|
|
||||||
status="ok",
|
|
||||||
game_id=req.game_id,
|
|
||||||
action=req.action,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
# GET /api/game/{game_id}/actions (Forge IPC polling endpoint)
|
# GET /api/game/{game_id}/actions (Forge IPC polling)
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
@router.get("/api/game/{game_id}/actions")
|
@router.get("/api/game/{game_id}/actions")
|
||||||
async def poll_actions(game_id: str):
|
async def poll_actions(game_id: str):
|
||||||
"""Drain and return all pending human actions for a game session.
|
"""Drain and return all pending human actions (consume-once).
|
||||||
|
|
||||||
Called by the Forge IPC polling loop to consume human player decisions.
|
Called by the Forge IPC polling loop to consume human player decisions.
|
||||||
Clears the queue on each call (consume-once semantics).
|
|
||||||
"""
|
"""
|
||||||
session = _sessions.get(game_id)
|
session = _sessions.get(game_id)
|
||||||
if session is None:
|
if session is None:
|
||||||
@@ -193,17 +332,22 @@ async def poll_actions(game_id: str):
|
|||||||
return {"game_id": game_id, "actions": actions}
|
return {"game_id": game_id, "actions": actions}
|
||||||
|
|
||||||
|
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
# GET /api/game/sessions (debug)
|
# GET /api/game/sessions (debug)
|
||||||
# ───────────────────────────────────────────────────────────────
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
@router.get("/api/game/sessions")
|
@router.get("/api/game/sessions")
|
||||||
async def list_sessions():
|
async def list_sessions():
|
||||||
"""List active game sessions (debug endpoint)."""
|
"""List all tracked game sessions (debug)."""
|
||||||
return {
|
return {
|
||||||
"count": len(_sessions),
|
"count": len(_sessions),
|
||||||
"sessions": [
|
"sessions": [
|
||||||
{"game_id": gid, "status": s["status"], "seats": len(s["seats"])}
|
{
|
||||||
|
"game_id": gid,
|
||||||
|
"status": s["status"],
|
||||||
|
"seats": len(s["seats"]),
|
||||||
|
"pending": len(s["pending_actions"]),
|
||||||
|
}
|
||||||
for gid, s in _sessions.items()
|
for gid, s in _sessions.items()
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
+79
-2
@@ -14,6 +14,8 @@ Batch simulation & lab management endpoints:
|
|||||||
GET /api/lab/trends/{deck_name}
|
GET /api/lab/trends/{deck_name}
|
||||||
GET /api/lab/log
|
GET /api/lab/log
|
||||||
GET /api/lab/debug-log
|
GET /api/lab/debug-log
|
||||||
|
GET /api/lab/precons
|
||||||
|
GET /api/lab/precons/deck
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -257,7 +259,14 @@ async def list_decks():
|
|||||||
try:
|
try:
|
||||||
conn = _get_db_conn()
|
conn = _get_db_conn()
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
"SELECT id, name, commander_name FROM decks ORDER BY id DESC"
|
"""
|
||||||
|
SELECT d.id, d.name, d.commander_name,
|
||||||
|
COALESCE(SUM(dc.quantity), 0) AS total_cards
|
||||||
|
FROM decks d
|
||||||
|
LEFT JOIN deck_cards dc ON dc.deck_id = d.id
|
||||||
|
GROUP BY d.id
|
||||||
|
ORDER BY d.id DESC
|
||||||
|
"""
|
||||||
).fetchall()
|
).fetchall()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
db_name = row["name"]
|
db_name = row["name"]
|
||||||
@@ -268,6 +277,7 @@ async def list_decks():
|
|||||||
"source": "deckbuilder",
|
"source": "deckbuilder",
|
||||||
"deck_id": row["id"],
|
"deck_id": row["id"],
|
||||||
"commander": row["commander_name"] or "",
|
"commander": row["commander_name"] or "",
|
||||||
|
"totalCards": row["total_cards"],
|
||||||
})
|
})
|
||||||
seen_names.add(db_name.lower())
|
seen_names.add(db_name.lower())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -444,7 +454,7 @@ async def get_log(batchId: str):
|
|||||||
state = active_batches.get(batchId)
|
state = active_batches.get(batchId)
|
||||||
if not state:
|
if not state:
|
||||||
raise HTTPException(404, f"Batch {batchId} not found")
|
raise HTTPException(404, f"Batch {batchId} not found")
|
||||||
return {"lines": state.log_lines[-200:]}
|
return {"lines": state.log_lines[-2000:]}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/lab/debug-log")
|
@router.get("/api/lab/debug-log")
|
||||||
@@ -468,6 +478,73 @@ async def get_debug_log():
|
|||||||
return {"path": None, "content": "No debug log found. Run a simulation first."}
|
return {"path": None, "content": "No debug log found. Run a simulation first."}
|
||||||
|
|
||||||
|
|
||||||
|
# ════════════════════════════════════════════════════════════
|
||||||
|
# Precon deck endpoints (used by lan-client ai-bridge.js)
|
||||||
|
# ════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
@router.get("/api/lab/precons")
|
||||||
|
async def list_precons():
|
||||||
|
"""Return precon deck metadata for the LAN client deck selector."""
|
||||||
|
precon_dir = Path(CFG.precon_dir) if CFG.precon_dir else None
|
||||||
|
if not precon_dir or not precon_dir.is_dir():
|
||||||
|
log.warning(f"Precon dir not found or not configured: {CFG.precon_dir!r}")
|
||||||
|
return {"precons": []}
|
||||||
|
|
||||||
|
precons = []
|
||||||
|
for f in sorted(precon_dir.glob("*.dck")):
|
||||||
|
deck_info = {
|
||||||
|
"fileName": f.name,
|
||||||
|
"deckName": f.stem,
|
||||||
|
"commander": "",
|
||||||
|
"colors": [],
|
||||||
|
"set": "",
|
||||||
|
}
|
||||||
|
# Extract commander name from the [Commander] section
|
||||||
|
try:
|
||||||
|
with open(f, "r", encoding="utf-8") as fh:
|
||||||
|
in_commander = False
|
||||||
|
for line in fh:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "[Commander]":
|
||||||
|
in_commander = True
|
||||||
|
continue
|
||||||
|
if line.startswith("[") and line.endswith("]"):
|
||||||
|
in_commander = False
|
||||||
|
continue
|
||||||
|
if in_commander:
|
||||||
|
m = re.match(r"^\d+\s+(.+?)(?:\|.+)?$", line)
|
||||||
|
if m:
|
||||||
|
deck_info["commander"] = m.group(1).strip()
|
||||||
|
break
|
||||||
|
except Exception as exc:
|
||||||
|
log.debug(f"Could not parse commander from {f.name}: {exc}")
|
||||||
|
precons.append(deck_info)
|
||||||
|
|
||||||
|
return {"precons": precons}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/api/lab/precons/deck")
|
||||||
|
async def get_precon_deck(fileName: str):
|
||||||
|
"""Return the full card list for a specific precon .dck file."""
|
||||||
|
precon_dir = Path(CFG.precon_dir) if CFG.precon_dir else None
|
||||||
|
if not precon_dir or not precon_dir.is_dir():
|
||||||
|
raise HTTPException(404, f"Precon directory not configured or missing: {CFG.precon_dir!r}")
|
||||||
|
|
||||||
|
# Sanitize — prevent path traversal
|
||||||
|
safe_name = Path(fileName).name
|
||||||
|
if not safe_name.endswith(".dck"):
|
||||||
|
raise HTTPException(400, "fileName must end with .dck")
|
||||||
|
deck_path = precon_dir / safe_name
|
||||||
|
|
||||||
|
if not deck_path.exists():
|
||||||
|
raise HTTPException(404, f"Precon deck not found: {safe_name}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
deck_data = parse_dck_file(str(deck_path))
|
||||||
|
return deck_data
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(500, f"Failed to parse deck: {e}")
|
||||||
|
|
||||||
|
|
||||||
@router.post('/api/lab/start-deepseek')
|
@router.post('/api/lab/start-deepseek')
|
||||||
async def start_batch_deepseek(request: FastAPIRequest, background_tasks: BackgroundTasks):
|
async def start_batch_deepseek(request: FastAPIRequest, background_tasks: BackgroundTasks):
|
||||||
|
|||||||
+11
-11
@@ -130,7 +130,7 @@ async def ml_status():
|
|||||||
for f in sorted(results_dir.glob("ml-decisions-*.jsonl")):
|
for f in sorted(results_dir.glob("ml-decisions-*.jsonl")):
|
||||||
lines = 0
|
lines = 0
|
||||||
try:
|
try:
|
||||||
with open(f) as fh:
|
with open(f, encoding="utf-8") as fh:
|
||||||
lines = sum(1 for _ in fh)
|
lines = sum(1 for _ in fh)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -168,7 +168,7 @@ async def ml_get_decisions(filename: str, limit: int = 100, offset: int = 0):
|
|||||||
raise HTTPException(404, f"ML decisions file not found: {filename}")
|
raise HTTPException(404, f"ML decisions file not found: {filename}")
|
||||||
decisions = []
|
decisions = []
|
||||||
try:
|
try:
|
||||||
with open(filepath) as f:
|
with open(filepath, encoding="utf-8") as f:
|
||||||
for i, line in enumerate(f):
|
for i, line in enumerate(f):
|
||||||
if i < offset:
|
if i < offset:
|
||||||
continue
|
continue
|
||||||
@@ -436,7 +436,7 @@ def _run_training_pipeline(
|
|||||||
best_model, _ = load_checkpoint(summary["checkpoint_path"], device)
|
best_model, _ = load_checkpoint(summary["checkpoint_path"], device)
|
||||||
eval_results = evaluate_model(best_model, test_states, test_labels, device)
|
eval_results = evaluate_model(best_model, test_states, test_labels, device)
|
||||||
eval_path = os.path.join(ckpt_dir, "eval_results.json")
|
eval_path = os.path.join(ckpt_dir, "eval_results.json")
|
||||||
with open(eval_path, "w") as f:
|
with open(eval_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(eval_results, f, indent=2)
|
json.dump(eval_results, f, indent=2)
|
||||||
# Done
|
# Done
|
||||||
with _training_lock:
|
with _training_lock:
|
||||||
@@ -530,7 +530,7 @@ async def ml_data_status():
|
|||||||
for f in sorted(results_dir.glob("ml-decisions-*.jsonl")):
|
for f in sorted(results_dir.glob("ml-decisions-*.jsonl")):
|
||||||
line_count = 0
|
line_count = 0
|
||||||
try:
|
try:
|
||||||
with open(f, "r") as fh:
|
with open(f, "r", encoding="utf-8") as fh:
|
||||||
line_count = sum(1 for _ in fh)
|
line_count = sum(1 for _ in fh)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -561,7 +561,7 @@ async def ml_data_status():
|
|||||||
eval_results = None
|
eval_results = None
|
||||||
if eval_path.exists():
|
if eval_path.exists():
|
||||||
try:
|
try:
|
||||||
with open(eval_path, "r") as f:
|
with open(eval_path, "r", encoding="utf-8") as f:
|
||||||
eval_results = json.load(f)
|
eval_results = json.load(f)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -705,7 +705,7 @@ def _run_tournament_pipeline(episodes: int, playstyle: str):
|
|||||||
playstyle=playstyle,
|
playstyle=playstyle,
|
||||||
)
|
)
|
||||||
output_path = os.path.join(ckpt_dir, "tournament_results.json")
|
output_path = os.path.join(ckpt_dir, "tournament_results.json")
|
||||||
with open(output_path, "w") as f:
|
with open(output_path, "w", encoding="utf-8") as f:
|
||||||
json.dump(result.to_dict(), f, indent=2)
|
json.dump(result.to_dict(), f, indent=2)
|
||||||
with _tournament_lock:
|
with _tournament_lock:
|
||||||
_tournament_state.phase = "done"
|
_tournament_state.phase = "done"
|
||||||
@@ -758,7 +758,7 @@ async def ml_tournament_results():
|
|||||||
project_root = Path(__file__).resolve().parent.parent
|
project_root = Path(__file__).resolve().parent.parent
|
||||||
results_path = project_root / "ml" / "models" / "checkpoints" / "tournament_results.json"
|
results_path = project_root / "ml" / "models" / "checkpoints" / "tournament_results.json"
|
||||||
if results_path.exists():
|
if results_path.exists():
|
||||||
with open(results_path, "r") as f:
|
with open(results_path, "r", encoding="utf-8") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
return {"error": "No tournament results found. Run a tournament first."}
|
return {"error": "No tournament results found. Run a tournament first."}
|
||||||
|
|
||||||
@@ -798,7 +798,7 @@ async def ml_stats():
|
|||||||
eval_path = ckpt_dir / "eval_results.json"
|
eval_path = ckpt_dir / "eval_results.json"
|
||||||
if eval_path.exists():
|
if eval_path.exists():
|
||||||
try:
|
try:
|
||||||
with open(eval_path, "r") as f:
|
with open(eval_path, "r", encoding="utf-8") as f:
|
||||||
eval_results = json.load(f)
|
eval_results = json.load(f)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -1057,14 +1057,14 @@ async def ml_distillation_history():
|
|||||||
|
|
||||||
if history_path.exists():
|
if history_path.exists():
|
||||||
try:
|
try:
|
||||||
with open(history_path, "r") as f:
|
with open(history_path, "r", encoding="utf-8") as f:
|
||||||
result["generations"] = json.load(f)
|
result["generations"] = json.load(f)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if summary_path.exists():
|
if summary_path.exists():
|
||||||
try:
|
try:
|
||||||
with open(summary_path, "r") as f:
|
with open(summary_path, "r", encoding="utf-8") as f:
|
||||||
result["summary"] = json.load(f)
|
result["summary"] = json.load(f)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -1203,7 +1203,7 @@ async def ml_elo_history():
|
|||||||
history_path = project_root / "data" / "elo_history.json"
|
history_path = project_root / "data" / "elo_history.json"
|
||||||
if history_path.exists():
|
if history_path.exists():
|
||||||
try:
|
try:
|
||||||
with open(history_path, "r") as f:
|
with open(history_path, "r", encoding="utf-8") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|||||||
+37
-7
@@ -1,5 +1,5 @@
|
|||||||
"""Commander AI Lab — Policy Decision Routes (Phase 2)
|
"""Commander AI Lab — Policy Decision Routes (Phase 2)
|
||||||
════════════════════════════════════════════════════
|
════════════════════════════════════════════════════════
|
||||||
Live Forge ↔ policy server IPC endpoints for online training.
|
Live Forge ↔ policy server IPC endpoints for online training.
|
||||||
These endpoints are purpose-built for the real-time decision loop
|
These endpoints are purpose-built for the real-time decision loop
|
||||||
where Forge's Java side calls the Python policy server at each
|
where Forge's Java side calls the Python policy server at each
|
||||||
@@ -24,7 +24,8 @@ import numpy as np
|
|||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
logger = logging.getLogger("routes.policy")
|
# Unique logger name so log lines are attributable to this module specifically
|
||||||
|
logger = logging.getLogger("routes.policy.decide")
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/policy", tags=["policy"])
|
router = APIRouter(prefix="/api/policy", tags=["policy"])
|
||||||
|
|
||||||
@@ -310,7 +311,10 @@ async def decide(req: DecideRequest):
|
|||||||
req.snapshot_schema, req.resolved_turn(),
|
req.snapshot_schema, req.resolved_turn(),
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("Precomputed vector encode failed (%s) — falling back", e)
|
logger.warning(
|
||||||
|
"Precomputed vector encode failed — falling back to full encoder",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
state_vec = None
|
state_vec = None
|
||||||
else:
|
else:
|
||||||
state_vec = None
|
state_vec = None
|
||||||
@@ -338,6 +342,24 @@ async def decide(req: DecideRequest):
|
|||||||
if "error" in result:
|
if "error" in result:
|
||||||
raise HTTPException(500, detail=result["error"])
|
raise HTTPException(500, detail=result["error"])
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------
|
||||||
|
# Probability diagnostic — logs top-5 actions on every decision.
|
||||||
|
# Remove or downgrade to logger.debug once training looks healthy.
|
||||||
|
# ----------------------------------------------------------------
|
||||||
|
probs = result.get("probabilities", {})
|
||||||
|
if probs:
|
||||||
|
top5 = sorted(probs.items(), key=lambda kv: kv[1], reverse=True)[:5]
|
||||||
|
top5_str = ", ".join(f"{a}:{p:.3f}" for a, p in top5)
|
||||||
|
logger.info(
|
||||||
|
"[decide] turn=%d phase=%s → %s (conf=%.3f) | top5=[%s] | src=%s",
|
||||||
|
req.resolved_turn(),
|
||||||
|
req.phase,
|
||||||
|
result["action"],
|
||||||
|
result["confidence"],
|
||||||
|
top5_str,
|
||||||
|
vector_source,
|
||||||
|
)
|
||||||
|
|
||||||
log_prob = 0.0
|
log_prob = 0.0
|
||||||
try:
|
try:
|
||||||
conf = result.get("confidence", 0.5)
|
conf = result.get("confidence", 0.5)
|
||||||
@@ -373,7 +395,7 @@ async def collect_tuples(req: CollectRequest):
|
|||||||
try:
|
try:
|
||||||
_online_store.add_tuples(req.tuples)
|
_online_store.add_tuples(req.tuples)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Failed to store tuples: %s", e)
|
logger.error("Failed to store tuples: %s", e, exc_info=True)
|
||||||
raise HTTPException(500, detail=str(e))
|
raise HTTPException(500, detail=str(e))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -402,7 +424,7 @@ async def submit_reward(req: RewardRequest):
|
|||||||
winner_seat=req.winner_seat,
|
winner_seat=req.winner_seat,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Failed to assign reward: %s", e)
|
logger.error("Failed to assign reward: %s", e, exc_info=True)
|
||||||
raise HTTPException(500, detail=str(e))
|
raise HTTPException(500, detail=str(e))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -518,16 +540,24 @@ def _infer_from_vector(state_vec: np.ndarray, policy_service, temperature: float
|
|||||||
|
|
||||||
Mirrors the core of PolicyInferenceService.predict() but accepts an
|
Mirrors the core of PolicyInferenceService.predict() but accepts an
|
||||||
ndarray instead of a snapshot dict, avoiding a second encode() call.
|
ndarray instead of a snapshot dict, avoiding a second encode() call.
|
||||||
|
|
||||||
|
Uses _unwrap_logits (same helper as predict()) to guard against
|
||||||
|
tuple/dict returns from wrapped or swapped model variants before any
|
||||||
|
arithmetic on the logits tensor.
|
||||||
"""
|
"""
|
||||||
import torch
|
import torch
|
||||||
from ml.config.scope import NUM_ACTIONS, IDX_TO_ACTION
|
from ml.config.scope import NUM_ACTIONS, IDX_TO_ACTION
|
||||||
|
from ml.serving.policy_server import _unwrap_logits
|
||||||
|
|
||||||
t_start = time.time()
|
t_start = time.time()
|
||||||
try:
|
try:
|
||||||
state_tensor = torch.from_numpy(state_vec).unsqueeze(0).to(policy_service.device)
|
state_tensor = torch.from_numpy(state_vec).unsqueeze(0).to(policy_service.device)
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
logits = policy_service.model(state_tensor)
|
raw_output = policy_service.model(state_tensor)
|
||||||
|
# Guard: unwrap tuple/dict returns (DataParallel, torch.compile,
|
||||||
|
# PolicyValueNetwork swap) before temperature division / argmax.
|
||||||
|
logits = _unwrap_logits(raw_output)
|
||||||
|
|
||||||
if greedy:
|
if greedy:
|
||||||
action_idx = logits.argmax(dim=-1).item()
|
action_idx = logits.argmax(dim=-1).item()
|
||||||
@@ -555,7 +585,7 @@ def _infer_from_vector(state_vec: np.ndarray, policy_service, temperature: float
|
|||||||
}
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
elapsed_ms = (time.time() - t_start) * 1000
|
elapsed_ms = (time.time() - t_start) * 1000
|
||||||
logger.error("Direct inference error: %s", e)
|
logger.error("Direct inference error in _infer_from_vector: %s", e, exc_info=True)
|
||||||
return {"error": str(e), "inference_ms": round(elapsed_ms, 2)}
|
return {"error": str(e), "inference_ms": round(elapsed_ms, 2)}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,31 @@ async def list_precons():
|
|||||||
return {"precons": PRECON_INDEX} # reads current value every call
|
return {"precons": PRECON_INDEX} # reads current value every call
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/api/lab/precons/deck")
|
||||||
|
async def get_precon_deck(fileName: str):
|
||||||
|
"""Return the raw decklist content of a precon .dck file.
|
||||||
|
Query: ?fileName=Elven_Empire.dck
|
||||||
|
"""
|
||||||
|
if not fileName:
|
||||||
|
raise HTTPException(400, "fileName query parameter is required")
|
||||||
|
# Sanitize: only allow .dck files, no path traversal
|
||||||
|
if '..' in fileName or '/' in fileName or '\\' in fileName:
|
||||||
|
raise HTTPException(400, "Invalid fileName")
|
||||||
|
if not fileName.endswith('.dck'):
|
||||||
|
raise HTTPException(400, "Only .dck files are supported")
|
||||||
|
|
||||||
|
src = _get_precon_dir() / fileName
|
||||||
|
if not src.exists():
|
||||||
|
raise HTTPException(404, f"Precon not found: {fileName}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
content = src.read_text(encoding='utf-8')
|
||||||
|
except OSError as e:
|
||||||
|
raise HTTPException(500, f"Failed to read deck file: {e}")
|
||||||
|
|
||||||
|
return {"fileName": fileName, "decklist": content}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/api/lab/precons/install")
|
@router.post("/api/lab/precons/install")
|
||||||
async def install_precon(req: dict):
|
async def install_precon(req: dict):
|
||||||
"""Install a precon deck to the Forge decks directory.
|
"""Install a precon deck to the Forge decks directory.
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ def load_result(path: str) -> dict:
|
|||||||
print(f"ERROR: File not found: {path}", file=sys.stderr)
|
print(f"ERROR: File not found: {path}", file=sys.stderr)
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
try:
|
try:
|
||||||
with open(p) as f:
|
with open(p, encoding="utf-8") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
print(f"ERROR: Invalid JSON in {path}: {e}", file=sys.stderr)
|
print(f"ERROR: Invalid JSON in {path}: {e}", file=sys.stderr)
|
||||||
@@ -289,7 +289,7 @@ def main() -> int:
|
|||||||
"passed": delta >= args.min_delta and pval < args.alpha,
|
"passed": delta >= args.min_delta and pval < args.alpha,
|
||||||
}
|
}
|
||||||
Path(args.json_out).parent.mkdir(parents=True, exist_ok=True)
|
Path(args.json_out).parent.mkdir(parents=True, exist_ok=True)
|
||||||
with open(args.json_out, "w") as jf:
|
with open(args.json_out, "w", encoding="utf-8") as jf:
|
||||||
json.dump(out, jf, indent=2)
|
json.dump(out, jf, indent=2)
|
||||||
print(f"Comparison JSON written to: {args.json_out}")
|
print(f"Comparison JSON written to: {args.json_out}")
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ _BUILTIN_DECK_DIRS = [
|
|||||||
_REPO_ROOT / "precon-decks",
|
_REPO_ROOT / "precon-decks",
|
||||||
_REPO_ROOT / "sample-decks",
|
_REPO_ROOT / "sample-decks",
|
||||||
_REPO_ROOT / "imported-decks",
|
_REPO_ROOT / "imported-decks",
|
||||||
|
_REPO_ROOT / "user-decks",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -352,7 +353,23 @@ def _load_deck_cards_by_name(deck_name: str) -> list:
|
|||||||
) cr ON cr.name = dc.card_name
|
) cr ON cr.name = dc.card_name
|
||||||
WHERE dc.deck_id = ?
|
WHERE dc.deck_id = ?
|
||||||
""", (deck_id,)).fetchall()
|
""", (deck_id,)).fetchall()
|
||||||
return [_row_to_dict(r) for r in rows]
|
result = [_row_to_dict(r) for r in rows]
|
||||||
|
|
||||||
|
# Enrich cards missing type_line from Forge card cache
|
||||||
|
try:
|
||||||
|
from commander_ai_lab.sim.forge_card_loader import lookup_forge_card
|
||||||
|
for card in result:
|
||||||
|
if not card.get("type_line"):
|
||||||
|
forge = lookup_forge_card(card.get("card_name", ""))
|
||||||
|
if forge:
|
||||||
|
card["type_line"] = forge.type_line
|
||||||
|
card["oracle_text"] = forge.oracle_text
|
||||||
|
card["cmc"] = forge.cmc
|
||||||
|
card["mana_cost"] = forge.mana_cost
|
||||||
|
except ImportError:
|
||||||
|
pass # Forge card loader not available
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
# -- 2. .dck file fallback --
|
# -- 2. .dck file fallback --
|
||||||
dck_path = _find_dck_file(deck_name)
|
dck_path = _find_dck_file(deck_name)
|
||||||
|
|||||||
@@ -449,7 +449,7 @@ def _get_deepseek_brain():
|
|||||||
from commander_ai_lab.sim.deepseek_brain import DeepSeekBrain, DeepSeekConfig
|
from commander_ai_lab.sim.deepseek_brain import DeepSeekBrain, DeepSeekConfig
|
||||||
cfg = DeepSeekConfig(
|
cfg = DeepSeekConfig(
|
||||||
api_base=os.environ.get("BRAIN_API_BASE", "http://localhost:11434"),
|
api_base=os.environ.get("BRAIN_API_BASE", "http://localhost:11434"),
|
||||||
model=os.environ.get("BRAIN_MODEL", "gpt-oss:20b"),
|
model=os.environ.get("BRAIN_MODEL", "qwen2.5:7b"),
|
||||||
temperature=float(os.environ.get("BRAIN_TEMPERATURE", "0.3")),
|
temperature=float(os.environ.get("BRAIN_TEMPERATURE", "0.3")),
|
||||||
max_tokens=int(os.environ.get("BRAIN_MAX_TOKENS", "1024")),
|
max_tokens=int(os.environ.get("BRAIN_MAX_TOKENS", "1024")),
|
||||||
request_timeout=float(os.environ.get("BRAIN_TIMEOUT", "300.0")),
|
request_timeout=float(os.environ.get("BRAIN_TIMEOUT", "300.0")),
|
||||||
@@ -499,7 +499,7 @@ def _run_deepseek_batch_thread(
|
|||||||
for fname in sorted(os.listdir(results_dir), reverse=True):
|
for fname in sorted(os.listdir(results_dir), reverse=True):
|
||||||
if fname.startswith('ml-decision-') and fname.endswith('.json'):
|
if fname.startswith('ml-decision-') and fname.endswith('.json'):
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(results_dir, fname), 'r') as rf:
|
with open(os.path.join(results_dir, fname), 'r', encoding='utf-8') as rf:
|
||||||
past = json.loads(rf.read())
|
past = json.loads(rf.read())
|
||||||
for dd in past.get('decks', []):
|
for dd in past.get('decks', []):
|
||||||
dname = dd.get('deckName', '')
|
dname = dd.get('deckName', '')
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class DeckBuilderAdapter:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
db_conn_factory: Callable[[], sqlite3.Connection] = None,
|
db_conn_factory: Callable[[], sqlite3.Connection] = None,
|
||||||
model: str = "gpt-oss:20b",
|
model: str = "claude-opus-4-20250514",
|
||||||
):
|
):
|
||||||
self.db_conn_factory = db_conn_factory
|
self.db_conn_factory = db_conn_factory
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Ollama gpt-oss:20b client for the Commander AI Deck Builder.
|
Ollama client for the Commander AI Deck Builder.
|
||||||
|
|
||||||
Uses the OpenAI-compatible API exposed by Ollama at localhost:11434.
|
Uses the OpenAI-compatible API exposed by Ollama at localhost:11434.
|
||||||
Provides 6 core functions:
|
Provides 6 core functions:
|
||||||
|
|||||||
@@ -87,8 +87,8 @@ def cli() -> None:
|
|||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--model",
|
"--model",
|
||||||
default="gpt-oss:20b",
|
default="claude-opus-4-20250514",
|
||||||
help="Ollama model name (default: gpt-oss:20b)",
|
help="AI model name (default: claude-opus-4-20250514)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--output",
|
"--output",
|
||||||
|
|||||||
@@ -148,6 +148,11 @@ class DeepSeekGameEngine:
|
|||||||
p.stats.turns_alive += 1
|
p.stats.turns_alive += 1
|
||||||
phase_events: list[str] = []
|
phase_events: list[str] = []
|
||||||
|
|
||||||
|
# ── Reset per-turn counters (land drop, etc.) ──
|
||||||
|
p._lands_played_this_turn = 0
|
||||||
|
if not hasattr(p, '_land_plays_this_turn_limit'):
|
||||||
|
p._land_plays_this_turn_limit = 1
|
||||||
|
|
||||||
# ── Draw ──
|
# ── Draw ──
|
||||||
if p.library:
|
if p.library:
|
||||||
drawn = p.library[-1]
|
drawn = p.library[-1]
|
||||||
@@ -357,8 +362,20 @@ class DeepSeekGameEngine:
|
|||||||
self, sim: SimState, pi: int,
|
self, sim: SimState, pi: int,
|
||||||
preferred_name: str | None, events: list[str] | None
|
preferred_name: str | None, events: list[str] | None
|
||||||
):
|
):
|
||||||
"""Play a land from hand. If preferred_name given, try that first."""
|
"""Play a land from hand. If preferred_name given, try that first.
|
||||||
|
|
||||||
|
Enforces the one-land-per-turn rule (MTG CR 305.2). Cards that grant
|
||||||
|
additional land plays (e.g. Exploration, Oracle of Mul Daya) may bump
|
||||||
|
``_land_plays_this_turn_limit`` on the player to a higher value.
|
||||||
|
"""
|
||||||
p = sim.players[pi]
|
p = sim.players[pi]
|
||||||
|
|
||||||
|
# ── LAND-PER-TURN ENFORCEMENT ──
|
||||||
|
max_land_plays = getattr(p, '_land_plays_this_turn_limit', 1)
|
||||||
|
lands_this_turn = getattr(p, '_lands_played_this_turn', 0)
|
||||||
|
if lands_this_turn >= max_land_plays:
|
||||||
|
return # Already used land drop(s) this turn
|
||||||
|
|
||||||
land_idx = -1
|
land_idx = -1
|
||||||
|
|
||||||
if preferred_name:
|
if preferred_name:
|
||||||
@@ -385,6 +402,7 @@ class DeepSeekGameEngine:
|
|||||||
sim.next_card_id += 1
|
sim.next_card_id += 1
|
||||||
sim.add_to_battlefield(pi, land_card)
|
sim.add_to_battlefield(pi, land_card)
|
||||||
p.stats.lands_played += 1
|
p.stats.lands_played += 1
|
||||||
|
p._lands_played_this_turn = lands_this_turn + 1
|
||||||
|
|
||||||
if events is not None and self.record_log:
|
if events is not None and self.record_log:
|
||||||
events.append(f"Played land: {land_card.name}")
|
events.append(f"Played land: {land_card.name}")
|
||||||
|
|||||||
@@ -135,6 +135,11 @@ class GameEngine:
|
|||||||
sim.active_player_index = pi
|
sim.active_player_index = pi
|
||||||
phase_events: list[str] = []
|
phase_events: list[str] = []
|
||||||
|
|
||||||
|
# ── Reset per-turn counters (land drop, etc.) ──
|
||||||
|
p._lands_played_this_turn = 0
|
||||||
|
if not hasattr(p, '_land_plays_this_turn_limit'):
|
||||||
|
p._land_plays_this_turn_limit = 1
|
||||||
|
|
||||||
# ── Capture ML snapshot BEFORE turn actions ──
|
# ── Capture ML snapshot BEFORE turn actions ──
|
||||||
pre_snapshot = None
|
pre_snapshot = None
|
||||||
if self.ml_log:
|
if self.ml_log:
|
||||||
@@ -454,8 +459,20 @@ class GameEngine:
|
|||||||
return sim
|
return sim
|
||||||
|
|
||||||
def _play_land(self, sim: SimState, pi: int) -> None:
|
def _play_land(self, sim: SimState, pi: int) -> None:
|
||||||
"""Play the first land from hand onto the battlefield."""
|
"""Play the first land from hand onto the battlefield.
|
||||||
|
|
||||||
|
Enforces the one-land-per-turn rule (MTG CR 305.2). Cards that grant
|
||||||
|
additional land plays may bump ``_land_plays_this_turn_limit`` on the
|
||||||
|
player to a higher value.
|
||||||
|
"""
|
||||||
p = sim.players[pi]
|
p = sim.players[pi]
|
||||||
|
|
||||||
|
# ── LAND-PER-TURN ENFORCEMENT ──
|
||||||
|
max_land_plays = getattr(p, '_land_plays_this_turn_limit', 1)
|
||||||
|
lands_this_turn = getattr(p, '_lands_played_this_turn', 0)
|
||||||
|
if lands_this_turn >= max_land_plays:
|
||||||
|
return # Already used land drop(s) this turn
|
||||||
|
|
||||||
land_idx = -1
|
land_idx = -1
|
||||||
for i, card in enumerate(p.hand):
|
for i, card in enumerate(p.hand):
|
||||||
if card.is_land():
|
if card.is_land():
|
||||||
@@ -470,6 +487,7 @@ class GameEngine:
|
|||||||
sim.next_card_id += 1
|
sim.next_card_id += 1
|
||||||
sim.add_to_battlefield(pi, land_card)
|
sim.add_to_battlefield(pi, land_card)
|
||||||
p.stats.lands_played += 1
|
p.stats.lands_played += 1
|
||||||
|
p._lands_played_this_turn = lands_this_turn + 1
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _send_to_graveyard(sim: SimState, card, owner_idx: int) -> None:
|
def _send_to_graveyard(sim: SimState, card, owner_idx: int) -> None:
|
||||||
|
|||||||
@@ -229,61 +229,54 @@ class CommanderGameState:
|
|||||||
def stack_is_empty(self) -> bool:
|
def stack_is_empty(self) -> bool:
|
||||||
return len(self.stack) == 0
|
return len(self.stack) == 0
|
||||||
|
|
||||||
# Bug 9 fix: stub get_legal_moves() and apply_move() so turn_manager
|
|
||||||
# doesn't crash. These delegate to the rules engine once implemented.
|
|
||||||
def get_legal_moves(self, seat: int) -> list[dict]:
|
def get_legal_moves(self, seat: int) -> list[dict]:
|
||||||
"""Return legal moves for a seat.
|
"""Return legal moves for ``seat``.
|
||||||
|
|
||||||
During DECLARE_BLOCKERS phase, emits block moves for defending
|
Delegates to :mod:`legal_move_generator` which produces macro
|
||||||
creatures against each attacker in sim_state.combat.
|
actions (play_land, cast_creature, cast_removal, cast_ramp,
|
||||||
Otherwise returns pass-only (stub for remaining rules engine).
|
cast_draw, cast_commander, attack, block, pass_priority).
|
||||||
"""
|
"""
|
||||||
moves: list[dict] = [{"id": 0, "category": "pass_priority", "description": "Pass"}]
|
# Local import to avoid a circular reference at module load
|
||||||
combat = self.sim_state.combat
|
from commander_ai_lab.sim.legal_move_generator import generate_legal_moves
|
||||||
|
return generate_legal_moves(self, seat)
|
||||||
|
|
||||||
# Emit block moves during declare_blockers phase
|
|
||||||
if (
|
|
||||||
combat
|
|
||||||
and combat.active
|
|
||||||
and self.current_phase in (Phase.DECLARE_BLOCKERS, "declare_blockers")
|
|
||||||
and seat != self.active_player_seat
|
|
||||||
):
|
|
||||||
move_id = 1000
|
|
||||||
my_creatures = [
|
|
||||||
c for c in self.sim_state.get_battlefield(seat)
|
|
||||||
if c.is_creature() and not c.tapped
|
|
||||||
]
|
|
||||||
for atk_id in combat.attackers:
|
|
||||||
atk = next(
|
|
||||||
(c for bf in self.sim_state.battlefields for c in bf if c.id == atk_id),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
if atk is None:
|
|
||||||
continue
|
|
||||||
for blocker in my_creatures:
|
|
||||||
moves.append({
|
|
||||||
"id": move_id,
|
|
||||||
"category": "block",
|
|
||||||
"blocker_id": blocker.id,
|
|
||||||
"attacker_id": atk_id,
|
|
||||||
"description": f"Block {atk.name} with {blocker.name}",
|
|
||||||
})
|
|
||||||
move_id += 1
|
|
||||||
|
|
||||||
return moves
|
|
||||||
def apply_move(self, seat: int, move_id: int) -> None:
|
def apply_move(self, seat: int, move_id: int) -> None:
|
||||||
"""Apply a chosen move.
|
"""Apply a chosen move to the game state.
|
||||||
|
|
||||||
Handles 'block' moves by updating sim_state.combat.blockers.
|
Supports all move categories produced by
|
||||||
Other categories are no-op stubs until the rules engine is wired.
|
:mod:`legal_move_generator`. For categories that require full
|
||||||
|
rules simulation (spell resolution, combat damage), this is
|
||||||
|
still a thin layer — it mutates the game state and delegates
|
||||||
|
final resolution to the engine/turn manager.
|
||||||
"""
|
"""
|
||||||
# Find the move dict
|
|
||||||
all_moves = self.get_legal_moves(seat)
|
all_moves = self.get_legal_moves(seat)
|
||||||
move = next((m for m in all_moves if m["id"] == move_id), None)
|
move = next((m for m in all_moves if m["id"] == move_id), None)
|
||||||
if move is None:
|
if move is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
if move.get("category") == "block":
|
category = move.get("category")
|
||||||
|
|
||||||
|
if category == "pass_priority":
|
||||||
|
return
|
||||||
|
|
||||||
|
if category == "play_land":
|
||||||
|
self._apply_play_land(seat, move)
|
||||||
|
return
|
||||||
|
|
||||||
|
if category in ("cast_creature", "cast_removal", "cast_ramp",
|
||||||
|
"cast_draw", "cast_spell"):
|
||||||
|
self._apply_cast_spell(seat, move)
|
||||||
|
return
|
||||||
|
|
||||||
|
if category == "cast_commander":
|
||||||
|
self._apply_cast_commander(seat, move)
|
||||||
|
return
|
||||||
|
|
||||||
|
if category == "attack":
|
||||||
|
self._apply_declare_attacker(seat, move)
|
||||||
|
return
|
||||||
|
|
||||||
|
if category == "block":
|
||||||
combat = self.sim_state.combat
|
combat = self.sim_state.combat
|
||||||
if combat and combat.active:
|
if combat and combat.active:
|
||||||
atk_id = move["attacker_id"]
|
atk_id = move["attacker_id"]
|
||||||
@@ -291,6 +284,143 @@ class CommanderGameState:
|
|||||||
if atk_id not in combat.blockers:
|
if atk_id not in combat.blockers:
|
||||||
combat.blockers[atk_id] = []
|
combat.blockers[atk_id] = []
|
||||||
combat.blockers[atk_id].append(blocker_id)
|
combat.blockers[atk_id].append(blocker_id)
|
||||||
|
return
|
||||||
|
|
||||||
|
# ── Move execution helpers ───────────────────────────────
|
||||||
|
|
||||||
|
def _apply_play_land(self, seat: int, move: dict) -> None:
|
||||||
|
"""Remove the chosen land from hand, place on battlefield, bump counter."""
|
||||||
|
if not (0 <= seat < len(self.commander_players)):
|
||||||
|
return
|
||||||
|
player = self.commander_players[seat]
|
||||||
|
# Enforce land-per-turn at apply-time as a second safety check
|
||||||
|
if player.lands_played_this_turn >= player.land_plays_allowed:
|
||||||
|
return
|
||||||
|
card_name = move.get("card_name")
|
||||||
|
idx = next(
|
||||||
|
(i for i, c in enumerate(player.hand)
|
||||||
|
if c.name == card_name and c.is_land()),
|
||||||
|
-1,
|
||||||
|
)
|
||||||
|
if idx < 0:
|
||||||
|
return
|
||||||
|
land = player.hand.pop(idx)
|
||||||
|
land.owner_id = seat
|
||||||
|
land.tapped = False
|
||||||
|
land.turn_played = self.turn
|
||||||
|
land.id = self.sim_state.next_card_id
|
||||||
|
self.sim_state.next_card_id += 1
|
||||||
|
self.sim_state.add_to_battlefield(seat, land)
|
||||||
|
player.lands_played_this_turn += 1
|
||||||
|
self.land_drop_used = True
|
||||||
|
if hasattr(player, "base") and hasattr(player.base, "stats"):
|
||||||
|
player.base.stats.lands_played += 1
|
||||||
|
|
||||||
|
def _apply_cast_spell(self, seat: int, move: dict) -> None:
|
||||||
|
"""Remove the spell from hand, place it on the battlefield.
|
||||||
|
|
||||||
|
This is a simplified resolution (no stack, no targeting) sufficient
|
||||||
|
for macro-action training and live games until the full stack
|
||||||
|
engine is wired.
|
||||||
|
"""
|
||||||
|
if not (0 <= seat < len(self.commander_players)):
|
||||||
|
return
|
||||||
|
player = self.commander_players[seat]
|
||||||
|
card_name = move.get("card_name")
|
||||||
|
idx = next(
|
||||||
|
(i for i, c in enumerate(player.hand) if c.name == card_name),
|
||||||
|
-1,
|
||||||
|
)
|
||||||
|
if idx < 0:
|
||||||
|
return
|
||||||
|
card = player.hand.pop(idx)
|
||||||
|
card.owner_id = seat
|
||||||
|
card.turn_played = self.turn
|
||||||
|
card.id = self.sim_state.next_card_id
|
||||||
|
self.sim_state.next_card_id += 1
|
||||||
|
|
||||||
|
# Creatures and permanents land on the battlefield; instants and
|
||||||
|
# sorceries go straight to the graveyard after resolving.
|
||||||
|
type_line = (card.type_line or "").lower()
|
||||||
|
if "instant" in type_line or "sorcery" in type_line:
|
||||||
|
player.graveyard.append(card)
|
||||||
|
else:
|
||||||
|
card.tapped = False
|
||||||
|
self.sim_state.add_to_battlefield(seat, card)
|
||||||
|
|
||||||
|
if hasattr(player, "base") and hasattr(player.base, "stats"):
|
||||||
|
player.base.stats.spells_cast += 1
|
||||||
|
if card.is_creature():
|
||||||
|
player.base.stats.creatures_played += 1
|
||||||
|
|
||||||
|
def _apply_cast_commander(self, seat: int, move: dict) -> None:
|
||||||
|
"""Cast the commander from the command zone (increments tax)."""
|
||||||
|
if not (0 <= seat < len(self.commander_players)):
|
||||||
|
return
|
||||||
|
player = self.commander_players[seat]
|
||||||
|
card_name = move.get("card_name")
|
||||||
|
idx = next(
|
||||||
|
(i for i, c in enumerate(player.commander_zone)
|
||||||
|
if c.name == card_name),
|
||||||
|
-1,
|
||||||
|
)
|
||||||
|
if idx < 0 and player.commander_zone:
|
||||||
|
idx = 0
|
||||||
|
if idx < 0:
|
||||||
|
return
|
||||||
|
cmd = player.commander_zone.pop(idx)
|
||||||
|
cmd.owner_id = seat
|
||||||
|
cmd.turn_played = self.turn
|
||||||
|
cmd.tapped = False
|
||||||
|
cmd.is_commander = True
|
||||||
|
cmd.id = self.sim_state.next_card_id
|
||||||
|
self.sim_state.next_card_id += 1
|
||||||
|
self.sim_state.add_to_battlefield(seat, cmd)
|
||||||
|
player.commander_cast_count += 1
|
||||||
|
if hasattr(player, "base") and hasattr(player.base, "stats"):
|
||||||
|
player.base.stats.spells_cast += 1
|
||||||
|
if cmd.is_creature():
|
||||||
|
player.base.stats.creatures_played += 1
|
||||||
|
|
||||||
|
def _apply_declare_attacker(self, seat: int, move: dict) -> None:
|
||||||
|
"""Mark a creature as attacking in the current combat state.
|
||||||
|
|
||||||
|
``combat.attackers`` is a ``dict[int, int]`` mapping attacker
|
||||||
|
card id to defending seat. If no defending seat has been
|
||||||
|
chosen yet, fall back to the first non-active, non-eliminated
|
||||||
|
opponent.
|
||||||
|
"""
|
||||||
|
combat = self.sim_state.combat
|
||||||
|
if combat is None:
|
||||||
|
return
|
||||||
|
atk_id = move.get("attacker_id")
|
||||||
|
if atk_id is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Tap the attacker unless it has vigilance
|
||||||
|
for card in self.sim_state.get_battlefield(seat):
|
||||||
|
if card.id == atk_id:
|
||||||
|
has_vigilance = (
|
||||||
|
card.has_keyword("vigilance")
|
||||||
|
if hasattr(card, "has_keyword") else False
|
||||||
|
)
|
||||||
|
if not has_vigilance:
|
||||||
|
card.tapped = True
|
||||||
|
break
|
||||||
|
|
||||||
|
# Determine defending seat
|
||||||
|
defending_seat = combat.defending_seat
|
||||||
|
if defending_seat < 0 or defending_seat == seat:
|
||||||
|
for i, p in enumerate(self.commander_players):
|
||||||
|
if i != seat and not p.eliminated:
|
||||||
|
defending_seat = i
|
||||||
|
break
|
||||||
|
if defending_seat >= 0:
|
||||||
|
combat.defending_seat = defending_seat
|
||||||
|
|
||||||
|
if defending_seat >= 0:
|
||||||
|
combat.attackers[atk_id] = defending_seat
|
||||||
|
combat.active = True
|
||||||
def living_players(self) -> list[CommanderPlayer]:
|
def living_players(self) -> list[CommanderPlayer]:
|
||||||
return [p for p in self.commander_players if not p.eliminated]
|
return [p for p in self.commander_players if not p.eliminated]
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,230 @@
|
|||||||
|
"""
|
||||||
|
Legal Move Generator
|
||||||
|
====================
|
||||||
|
Generates the set of legal moves a player can take from a
|
||||||
|
:class:`CommanderGameState`. This replaces the "pass-only" stub in
|
||||||
|
``CommanderGameState.get_legal_moves`` so the turn_manager/policy-server
|
||||||
|
path can drive real games instead of just passing priority every turn.
|
||||||
|
|
||||||
|
The emitted moves are intentionally coarse (macro-actions: play_land,
|
||||||
|
cast_creature, cast_removal, cast_ramp, cast_draw, cast_commander,
|
||||||
|
attack, block, pass_priority) so that both the neural policy and
|
||||||
|
heuristic/LLM brains can pick from the same list.
|
||||||
|
|
||||||
|
Long-term, when the Forge IPC path is fully wired, Forge's own legal
|
||||||
|
action list will replace the output of this module. Until then, this
|
||||||
|
is the authoritative move source for the Python rules layer.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from commander_ai_lab.sim.models import Card, Phase
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from commander_ai_lab.sim.game_state import CommanderGameState
|
||||||
|
|
||||||
|
|
||||||
|
# Map from card enrichment flags to macro-action categories the AI brain
|
||||||
|
# recognizes. Order matters: the first True flag wins.
|
||||||
|
_SPELL_FLAG_TO_CATEGORY: tuple[tuple[str, str], ...] = (
|
||||||
|
("is_ramp", "cast_ramp"),
|
||||||
|
("is_removal", "cast_removal"),
|
||||||
|
("is_board_wipe", "cast_removal"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _classify_spell(card: Card) -> str:
|
||||||
|
"""Map a Card to a macro-action category string.
|
||||||
|
|
||||||
|
Matches the 8-action macro-action space used by the policy network
|
||||||
|
(``scope.py``): cast_creature, cast_removal, cast_draw, cast_ramp,
|
||||||
|
cast_commander, attack_opponent, hold_mana, pass. Cards that don't
|
||||||
|
fit any flag fall back to ``cast_spell`` so downstream code can
|
||||||
|
still execute them.
|
||||||
|
"""
|
||||||
|
for flag, category in _SPELL_FLAG_TO_CATEGORY:
|
||||||
|
if getattr(card, flag, False):
|
||||||
|
return category
|
||||||
|
|
||||||
|
# Heuristic: draw spells often mention "draw" in oracle text
|
||||||
|
ot = (getattr(card, "oracle_text", "") or "").lower()
|
||||||
|
if "draw" in ot and ("card" in ot or "cards" in ot):
|
||||||
|
return "cast_draw"
|
||||||
|
|
||||||
|
if card.is_creature():
|
||||||
|
return "cast_creature"
|
||||||
|
return "cast_spell"
|
||||||
|
|
||||||
|
|
||||||
|
def _count_available_mana(game_state: "CommanderGameState", seat: int) -> int:
|
||||||
|
"""Rough available mana = floating pool + untapped lands on battlefield.
|
||||||
|
|
||||||
|
This is intentionally permissive. A production rules engine would
|
||||||
|
also consider mana dorks, mana rocks, and color requirements, but
|
||||||
|
for macro-action gating CMC-vs-total-mana is sufficient.
|
||||||
|
"""
|
||||||
|
player = game_state.players[seat]
|
||||||
|
mana = 0
|
||||||
|
|
||||||
|
# Floating mana pool (if ManaPool has ``total``)
|
||||||
|
pool = getattr(player, "mana_pool", None)
|
||||||
|
if pool is not None:
|
||||||
|
total_fn = getattr(pool, "total", None)
|
||||||
|
if callable(total_fn):
|
||||||
|
try:
|
||||||
|
mana += int(total_fn())
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Untapped lands on battlefield (each produces ~1 mana)
|
||||||
|
for card in game_state.sim_state.get_battlefield(seat):
|
||||||
|
if card.is_land() and not getattr(card, "tapped", False):
|
||||||
|
mana += 1
|
||||||
|
|
||||||
|
return mana
|
||||||
|
|
||||||
|
|
||||||
|
def _phase_is(current: object, target: Phase) -> bool:
|
||||||
|
"""Safe phase comparison that works with Phase enum or raw string."""
|
||||||
|
if current == target:
|
||||||
|
return True
|
||||||
|
if isinstance(current, str) and current == target.value:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def generate_legal_moves(game_state: "CommanderGameState", seat: int) -> list[dict]:
|
||||||
|
"""Produce the list of legal moves for ``seat`` given ``game_state``.
|
||||||
|
|
||||||
|
Always includes a pass-priority move (id 0) as a safe fallback.
|
||||||
|
Additional moves are emitted based on phase, turn ownership, and
|
||||||
|
available resources.
|
||||||
|
"""
|
||||||
|
moves: list[dict] = [
|
||||||
|
{"id": 0, "category": "pass_priority", "description": "Pass"}
|
||||||
|
]
|
||||||
|
|
||||||
|
if not (0 <= seat < len(game_state.players)):
|
||||||
|
return moves
|
||||||
|
|
||||||
|
player = game_state.players[seat]
|
||||||
|
if getattr(player, "eliminated", False):
|
||||||
|
return moves
|
||||||
|
|
||||||
|
current_phase = game_state.current_phase
|
||||||
|
is_active = (seat == game_state.active_player_seat)
|
||||||
|
move_id = 1
|
||||||
|
|
||||||
|
# ── Sorcery-speed actions (main phases, active player only) ──
|
||||||
|
if is_active and (
|
||||||
|
_phase_is(current_phase, Phase.MAIN1)
|
||||||
|
or _phase_is(current_phase, Phase.MAIN2)
|
||||||
|
):
|
||||||
|
# Land drop — respect one-land-per-turn unless modified
|
||||||
|
lands_played = getattr(player, "lands_played_this_turn", 0)
|
||||||
|
land_limit = getattr(player, "land_plays_allowed", 1)
|
||||||
|
if lands_played < land_limit:
|
||||||
|
for card in player.hand:
|
||||||
|
if card.is_land():
|
||||||
|
moves.append({
|
||||||
|
"id": move_id,
|
||||||
|
"category": "play_land",
|
||||||
|
"card_name": card.name,
|
||||||
|
"description": f"Play {card.name}",
|
||||||
|
})
|
||||||
|
move_id += 1
|
||||||
|
|
||||||
|
# Castable spells from hand
|
||||||
|
available_mana = _count_available_mana(game_state, seat)
|
||||||
|
for card in player.hand:
|
||||||
|
if card.is_land():
|
||||||
|
continue
|
||||||
|
cost = int(getattr(card, "cmc", 0) or 0)
|
||||||
|
if cost <= available_mana:
|
||||||
|
category = _classify_spell(card)
|
||||||
|
moves.append({
|
||||||
|
"id": move_id,
|
||||||
|
"category": category,
|
||||||
|
"card_name": card.name,
|
||||||
|
"cmc": cost,
|
||||||
|
"description": f"Cast {card.name} ({cost} mana)",
|
||||||
|
})
|
||||||
|
move_id += 1
|
||||||
|
|
||||||
|
# Commander from the command zone (respect commander tax)
|
||||||
|
tax = 0
|
||||||
|
if hasattr(player, "commander_tax"):
|
||||||
|
try:
|
||||||
|
tax = int(player.commander_tax())
|
||||||
|
except Exception:
|
||||||
|
tax = 0
|
||||||
|
for cmd in getattr(player, "commander_zone", []):
|
||||||
|
base_cost = int(getattr(cmd, "cmc", 0) or 0)
|
||||||
|
total_cost = base_cost + tax
|
||||||
|
if total_cost <= available_mana:
|
||||||
|
moves.append({
|
||||||
|
"id": move_id,
|
||||||
|
"category": "cast_commander",
|
||||||
|
"card_name": cmd.name,
|
||||||
|
"cmc": total_cost,
|
||||||
|
"description": (
|
||||||
|
f"Cast {cmd.name} from command zone "
|
||||||
|
f"(base={base_cost}, tax={tax})"
|
||||||
|
),
|
||||||
|
})
|
||||||
|
move_id += 1
|
||||||
|
|
||||||
|
# ── Declare attackers — one attack move per legal attacker ──
|
||||||
|
if is_active and _phase_is(current_phase, Phase.DECLARE_ATTACKERS):
|
||||||
|
bf = game_state.sim_state.get_battlefield(seat)
|
||||||
|
for creature in bf:
|
||||||
|
if not creature.is_creature():
|
||||||
|
continue
|
||||||
|
if getattr(creature, "tapped", False):
|
||||||
|
continue
|
||||||
|
# Summoning sickness: played this turn and no haste
|
||||||
|
turn_played = getattr(creature, "turn_played", -1)
|
||||||
|
has_haste = creature.has_keyword("haste") if hasattr(creature, "has_keyword") else False
|
||||||
|
if turn_played == game_state.turn and not has_haste:
|
||||||
|
continue
|
||||||
|
moves.append({
|
||||||
|
"id": move_id,
|
||||||
|
"category": "attack",
|
||||||
|
"attacker_id": creature.id,
|
||||||
|
"card_name": creature.name,
|
||||||
|
"description": f"Attack with {creature.name}",
|
||||||
|
})
|
||||||
|
move_id += 1
|
||||||
|
|
||||||
|
# ── Declare blockers — emit block pairings for defending player ──
|
||||||
|
if _phase_is(current_phase, Phase.DECLARE_BLOCKERS) and not is_active:
|
||||||
|
combat = game_state.sim_state.combat
|
||||||
|
if combat and getattr(combat, "active", False):
|
||||||
|
defender_creatures = [
|
||||||
|
c for c in game_state.sim_state.get_battlefield(seat)
|
||||||
|
if c.is_creature() and not getattr(c, "tapped", False)
|
||||||
|
]
|
||||||
|
for atk_id in getattr(combat, "attackers", []):
|
||||||
|
atk = None
|
||||||
|
for bf in game_state.sim_state.battlefields:
|
||||||
|
for c in bf:
|
||||||
|
if c.id == atk_id:
|
||||||
|
atk = c
|
||||||
|
break
|
||||||
|
if atk is not None:
|
||||||
|
break
|
||||||
|
if atk is None:
|
||||||
|
continue
|
||||||
|
for blocker in defender_creatures:
|
||||||
|
moves.append({
|
||||||
|
"id": move_id,
|
||||||
|
"category": "block",
|
||||||
|
"blocker_id": blocker.id,
|
||||||
|
"attacker_id": atk_id,
|
||||||
|
"description": f"Block {atk.name} with {blocker.name}",
|
||||||
|
})
|
||||||
|
move_id += 1
|
||||||
|
|
||||||
|
return moves
|
||||||
@@ -267,6 +267,10 @@ class CommanderTurnManager:
|
|||||||
player = self.gs.players[seat]
|
player = self.gs.players[seat]
|
||||||
logger.debug("Turn %d — %s's turn begins", turn_num, player.name)
|
logger.debug("Turn %d — %s's turn begins", turn_num, player.name)
|
||||||
|
|
||||||
|
# ── Reset per-turn counters at the start of each turn ──
|
||||||
|
# Enforces MTG CR 305.2 (one land per turn unless modified).
|
||||||
|
player.lands_played_this_turn = 0
|
||||||
|
|
||||||
for phase in PHASES:
|
for phase in PHASES:
|
||||||
if self.game_over:
|
if self.game_over:
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -0,0 +1,840 @@
|
|||||||
|
package commanderailab.ai;
|
||||||
|
|
||||||
|
import commanderailab.ml.MacroActionExecutor;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import forge.LobbyPlayer;
|
||||||
|
import forge.ai.AiController;
|
||||||
|
import forge.ai.ComputerUtilAbility;
|
||||||
|
import forge.ai.ComputerUtilMana;
|
||||||
|
import forge.ai.PlayerControllerAi;
|
||||||
|
import forge.game.Game;
|
||||||
|
import forge.game.card.Card;
|
||||||
|
import forge.game.card.CardCollection;
|
||||||
|
import forge.game.card.CardCollectionView;
|
||||||
|
import forge.game.phase.PhaseHandler;
|
||||||
|
import forge.game.phase.PhaseType;
|
||||||
|
import forge.game.player.Player;
|
||||||
|
import forge.game.spellability.SpellAbility;
|
||||||
|
import forge.game.spellability.SpellAbilityStackInstance;
|
||||||
|
import forge.game.zone.ZoneType;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PolicyControllerAi — Phase 2 live-game controller that routes AI decisions
|
||||||
|
* through the Python policy server instead of Forge's built-in heuristics.
|
||||||
|
*
|
||||||
|
* <h2>Architecture</h2>
|
||||||
|
* <pre>
|
||||||
|
* Forge game loop
|
||||||
|
* └─ player.getController() → PolicyControllerAi
|
||||||
|
* └─ brains (PolicyAiController extends AiController)
|
||||||
|
* └─ chooseSpellAbilityToPlay()
|
||||||
|
* ├─ buildGameStateJson()
|
||||||
|
* ├─ policyClient.decide(stateJson) → macro-action
|
||||||
|
* ├─ mapMacroActionToSpellAbility() → SpellAbility
|
||||||
|
* └─ super.chooseSpellAbilityToPlay() (fallback)
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* <h2>Two-class design</h2>
|
||||||
|
* <ul>
|
||||||
|
* <li>{@link PolicyAiController} — inner subclass of {@link AiController}
|
||||||
|
* that overrides {@code chooseSpellAbilityToPlay()}. This is the
|
||||||
|
* intercept point closest to the game loop.</li>
|
||||||
|
* <li>{@link PolicyControllerAi} — subclass of {@link PlayerControllerAi}
|
||||||
|
* that swaps Forge's default {@code AiController} for a
|
||||||
|
* {@code PolicyAiController} and exposes lifecycle helpers
|
||||||
|
* (game-end reward submission, statistics).</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <h2>Decision flow</h2>
|
||||||
|
* <ol>
|
||||||
|
* <li>Build a rich {@link JsonObject} snapshot of the current game state
|
||||||
|
* (per-player life/mana/zones, stack, phase, legal actions).</li>
|
||||||
|
* <li>POST the snapshot to {@code /api/policy/decide} via
|
||||||
|
* {@link PolicyClient#decide(JsonObject)}.</li>
|
||||||
|
* <li>Translate the returned macro-action name (e.g. {@code "cast_creature"})
|
||||||
|
* to a concrete {@link SpellAbility} from the legal action list using
|
||||||
|
* {@link MacroActionExecutor}'s card-role databases.</li>
|
||||||
|
* <li>If the server is unreachable or no mapping succeeds, fall back to
|
||||||
|
* {@link AiController#chooseSpellAbilityToPlay()} transparently.</li>
|
||||||
|
* <li>Log every decision (policy or fallback) for training data collection.</li>
|
||||||
|
* </ol>
|
||||||
|
*
|
||||||
|
* <h2>Fallback guarantee</h2>
|
||||||
|
* Every code path in {@link PolicyAiController#chooseSpellAbilityToPlay()}
|
||||||
|
* is wrapped in a broad {@code try/catch}. Any unexpected exception causes
|
||||||
|
* an immediate delegate to the parent Forge AI so the game continues safely.
|
||||||
|
*
|
||||||
|
* <h2>Usage</h2>
|
||||||
|
* <pre>{@code
|
||||||
|
* PolicyClient client = new PolicyClient("http://localhost:8080");
|
||||||
|
* client.connect();
|
||||||
|
*
|
||||||
|
* // When Forge constructs the AI player's controller:
|
||||||
|
* PolicyControllerAi controller = new PolicyControllerAi(game, player, lobbyPlayer, client);
|
||||||
|
*
|
||||||
|
* // After the game ends:
|
||||||
|
* controller.onGameEnd(won ? 1.0 : -1.0);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @see PolicyClient
|
||||||
|
* @see PolicyAiController
|
||||||
|
* @see MacroActionExecutor
|
||||||
|
* @since Phase 2.1 — Issue #83
|
||||||
|
*/
|
||||||
|
public class PolicyControllerAi extends PlayerControllerAi {
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
// Constants
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(PolicyControllerAi.class.getName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directory for training-data decision logs.
|
||||||
|
* One JSON-lines file per game session is written here.
|
||||||
|
*/
|
||||||
|
private static final String LOG_DIR = "data/decisions";
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
// Instance state
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** Unique identifier for this game session (used in reward submission). */
|
||||||
|
private final String gameId = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
/** The policy client used by the embedded {@link PolicyAiController}. */
|
||||||
|
private final PolicyClient policyClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log writer for training-data collection.
|
||||||
|
* Lazily opened on the first decision; {@code null} if log dir is unavailable.
|
||||||
|
*/
|
||||||
|
private PrintWriter decisionLog;
|
||||||
|
|
||||||
|
/** Running count of decisions in this game (policy + fallback). */
|
||||||
|
private final AtomicInteger decisionCount = new AtomicInteger(0);
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
// Construction
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a policy-driven AI controller.
|
||||||
|
*
|
||||||
|
* <p>The standard Forge {@link AiController} created by the parent class is
|
||||||
|
* immediately replaced by a {@link PolicyAiController} that holds a
|
||||||
|
* reference to {@code client}. All other Forge AI infrastructure
|
||||||
|
* (combat, simulation, sideboard, etc.) is inherited unchanged.</p>
|
||||||
|
*
|
||||||
|
* @param game the active Forge {@link Game}
|
||||||
|
* @param player the {@link Player} this controller drives
|
||||||
|
* @param lobbyPlayer the underlying lobby seat
|
||||||
|
* @param client live {@link PolicyClient}; must not be {@code null}
|
||||||
|
*/
|
||||||
|
public PolicyControllerAi(Game game, Player player, LobbyPlayer lobbyPlayer,
|
||||||
|
PolicyClient client) {
|
||||||
|
// Parent creates its own AiController; we replace it below via reflection.
|
||||||
|
super(game, player, lobbyPlayer);
|
||||||
|
this.policyClient = client;
|
||||||
|
|
||||||
|
// Swap the brains field to our policy-aware subclass.
|
||||||
|
injectPolicyAiController(game, player);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convenience constructor that builds a {@link PolicyClient} from a URL.
|
||||||
|
*
|
||||||
|
* <p>The client's {@link PolicyClient#connect()} is called automatically;
|
||||||
|
* if the server is unavailable the controller will silently fall back to
|
||||||
|
* Forge's built-in AI for all decisions.</p>
|
||||||
|
*
|
||||||
|
* @param game the active Forge {@link Game}
|
||||||
|
* @param player the {@link Player} this controller drives
|
||||||
|
* @param lobbyPlayer the underlying lobby seat
|
||||||
|
* @param policyServerUrl base URL of the policy server, e.g.
|
||||||
|
* {@code "http://localhost:8080"}
|
||||||
|
*/
|
||||||
|
public PolicyControllerAi(Game game, Player player, LobbyPlayer lobbyPlayer,
|
||||||
|
String policyServerUrl) {
|
||||||
|
this(game, player, lobbyPlayer, buildAndConnect(policyServerUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
// Lifecycle helpers
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signal the end of a game so the policy client can submit the terminal
|
||||||
|
* reward and flush any buffered training tuples.
|
||||||
|
*
|
||||||
|
* <p>Call this from your game-runner once Forge reports a winner.</p>
|
||||||
|
*
|
||||||
|
* @param reward terminal reward signal: {@code +1.0} for a win,
|
||||||
|
* {@code -1.0} for a loss, or an intermediate value
|
||||||
|
*/
|
||||||
|
public void onGameEnd(double reward) {
|
||||||
|
try {
|
||||||
|
policyClient.submitReward(gameId, reward);
|
||||||
|
policyClient.flushCollectedTuples();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.warning("[PolicyControllerAi] onGameEnd error: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
closeDecisionLog();
|
||||||
|
}
|
||||||
|
LOG.info(String.format(
|
||||||
|
"[PolicyControllerAi] game=%s reward=%.2f decisions=%d stats=%s",
|
||||||
|
gameId, reward, decisionCount.get(), policyClient.getStatsSummary()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the unique game identifier used in reward submissions.
|
||||||
|
*
|
||||||
|
* @return UUID string
|
||||||
|
*/
|
||||||
|
public String getGameId() {
|
||||||
|
return gameId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a snapshot of policy statistics for this game session.
|
||||||
|
*
|
||||||
|
* @return human-readable stats string from the underlying {@link PolicyClient}
|
||||||
|
*/
|
||||||
|
public String getStatsSummary() {
|
||||||
|
return policyClient.getStatsSummary();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
// Private helpers
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses reflection to replace the {@code brains} field (an {@link AiController})
|
||||||
|
* that the parent constructor already created with a new
|
||||||
|
* {@link PolicyAiController}.
|
||||||
|
*
|
||||||
|
* <p>This is the only use of reflection in this class. It is necessary
|
||||||
|
* because {@code PlayerControllerAi} declares {@code brains} as
|
||||||
|
* {@code private final} and constructs it in its own constructor body,
|
||||||
|
* before we get a chance to supply a subclass.</p>
|
||||||
|
*
|
||||||
|
* <p>If reflection fails for any reason (e.g. a future Forge version adds
|
||||||
|
* a security manager), the controller falls back to the original
|
||||||
|
* {@code AiController} that the parent already created. A warning is
|
||||||
|
* logged so the issue is easy to diagnose.</p>
|
||||||
|
*
|
||||||
|
* @param game the active game
|
||||||
|
* @param player the AI player
|
||||||
|
*/
|
||||||
|
private void injectPolicyAiController(Game game, Player player) {
|
||||||
|
try {
|
||||||
|
java.lang.reflect.Field brains =
|
||||||
|
PlayerControllerAi.class.getDeclaredField("brains");
|
||||||
|
brains.setAccessible(true);
|
||||||
|
PolicyAiController policyBrains =
|
||||||
|
new PolicyAiController(player, game, policyClient, this);
|
||||||
|
brains.set(this, policyBrains);
|
||||||
|
LOG.info("[PolicyControllerAi] PolicyAiController injected for player="
|
||||||
|
+ player.getName() + " game=" + gameId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.warning("[PolicyControllerAi] Could not inject PolicyAiController; "
|
||||||
|
+ "falling back to vanilla Forge AI. Cause: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Build a {@link PolicyClient} and attempt to connect. */
|
||||||
|
private static PolicyClient buildAndConnect(String serverUrl) {
|
||||||
|
PolicyClient client = new PolicyClient(serverUrl);
|
||||||
|
client.connect();
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
// Decision logging
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a single JSON-lines decision record to the per-game log file.
|
||||||
|
* Creates the log directory and file on first call.
|
||||||
|
*
|
||||||
|
* @param record the JSON object to append
|
||||||
|
*/
|
||||||
|
void logDecision(JsonObject record) {
|
||||||
|
decisionCount.incrementAndGet();
|
||||||
|
try {
|
||||||
|
if (decisionLog == null) {
|
||||||
|
File dir = new File(LOG_DIR);
|
||||||
|
//noinspection ResultOfMethodCallIgnored
|
||||||
|
dir.mkdirs();
|
||||||
|
File logFile = new File(dir, "decisions-" + gameId + ".jsonl");
|
||||||
|
decisionLog = new PrintWriter(new FileWriter(logFile, true));
|
||||||
|
}
|
||||||
|
decisionLog.println(record.toString());
|
||||||
|
decisionLog.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Non-fatal: the game continues even if we can't log.
|
||||||
|
LOG.warning("[PolicyControllerAi] Could not write decision log: "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Close the decision log file handle. */
|
||||||
|
private void closeDecisionLog() {
|
||||||
|
if (decisionLog != null) {
|
||||||
|
decisionLog.close();
|
||||||
|
decisionLog = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ═════════════════════════════════════════════════════════════════════════
|
||||||
|
// Inner class: PolicyAiController
|
||||||
|
// ═════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PolicyAiController — intercepts {@link AiController#chooseSpellAbilityToPlay()}
|
||||||
|
* and routes the decision through the Python policy server.
|
||||||
|
*
|
||||||
|
* <h2>Decision flow</h2>
|
||||||
|
* <ol>
|
||||||
|
* <li>Enumerate all legal actions (lands + spells/abilities) using
|
||||||
|
* {@link ComputerUtilAbility} — the same utilities Forge uses
|
||||||
|
* internally — so the legal-action list is always authoritative.</li>
|
||||||
|
* <li>Build a {@link JsonObject} describing the full game state,
|
||||||
|
* including per-player life/mana/zones/stack/phase and a
|
||||||
|
* {@code legal_actions} array.</li>
|
||||||
|
* <li>POST to {@code /api/policy/decide} via {@link PolicyClient#decide(JsonObject)}.
|
||||||
|
* The server returns a macro-action name such as {@code "cast_creature"}.</li>
|
||||||
|
* <li>Translate the macro-action to a concrete {@link SpellAbility} by
|
||||||
|
* scanning the legal-action list with
|
||||||
|
* {@link #mapMacroActionToSpellAbility}.</li>
|
||||||
|
* <li>On any failure, delegate to the parent
|
||||||
|
* {@link AiController#chooseSpellAbilityToPlay()} so the game never stalls.</li>
|
||||||
|
* </ol>
|
||||||
|
*
|
||||||
|
* <h2>Thread safety</h2>
|
||||||
|
* All mutable state is confined to the single game thread that Forge uses
|
||||||
|
* for AI decisions. The {@link PolicyClient} methods called here are
|
||||||
|
* themselves thread-safe.
|
||||||
|
*/
|
||||||
|
static final class PolicyAiController extends AiController {
|
||||||
|
|
||||||
|
/** Maximum age (ms) of a decision before it is considered stale. */
|
||||||
|
private static final long DECISION_TIMEOUT_MS = 5_000;
|
||||||
|
|
||||||
|
private final PolicyClient policyClient;
|
||||||
|
private final PolicyControllerAi outer;
|
||||||
|
|
||||||
|
PolicyAiController(Player player, Game game,
|
||||||
|
PolicyClient policyClient,
|
||||||
|
PolicyControllerAi outer) {
|
||||||
|
super(player, game);
|
||||||
|
this.policyClient = policyClient;
|
||||||
|
this.outer = outer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Main intercept ───────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override of the core AI decision method.
|
||||||
|
*
|
||||||
|
* <p>This is called by the Forge game loop whenever the AI player has
|
||||||
|
* priority and needs to decide whether/what to play. We intercept
|
||||||
|
* here to route the decision to the policy server.</p>
|
||||||
|
*
|
||||||
|
* @return list of {@link SpellAbility} objects for Forge to execute,
|
||||||
|
* or {@code null} / empty list to pass priority
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<SpellAbility> chooseSpellAbilityToPlay() {
|
||||||
|
long t0 = System.currentTimeMillis();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Player player = getPlayer();
|
||||||
|
Game game = getGame();
|
||||||
|
|
||||||
|
// 1. Enumerate legal actions
|
||||||
|
List<SpellAbility> legalActions = gatherLegalActions(game, player);
|
||||||
|
|
||||||
|
// If there are no legal actions, pass immediately — no server
|
||||||
|
// round-trip required.
|
||||||
|
if (legalActions.isEmpty()) {
|
||||||
|
logAndReturn(null, "pass", "no_legal_actions", t0, legalActions);
|
||||||
|
return super.chooseSpellAbilityToPlay();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Build game-state JSON with legal actions embedded
|
||||||
|
JsonObject stateJson = buildGameStateJson(game, player, legalActions);
|
||||||
|
|
||||||
|
// 3. Ask the policy server for a macro-action
|
||||||
|
String macroAction = policyClient.decide(stateJson);
|
||||||
|
|
||||||
|
// 4. Map macro-action → concrete SpellAbility
|
||||||
|
SpellAbility chosen = mapMacroActionToSpellAbility(
|
||||||
|
macroAction, legalActions, player);
|
||||||
|
|
||||||
|
if (chosen != null) {
|
||||||
|
// Policy succeeded
|
||||||
|
List<SpellAbility> result = Collections.singletonList(chosen);
|
||||||
|
logAndReturn(stateJson, macroAction, "policy", t0, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// macro-action mapped to "pass" or was unresolvable but not an error
|
||||||
|
if (MacroActionExecutor.PASS.equals(macroAction)
|
||||||
|
|| MacroActionExecutor.HOLD_MANA.equals(macroAction)) {
|
||||||
|
logAndReturn(stateJson, macroAction, "policy_pass", t0, null);
|
||||||
|
// Return super so Forge can decide whether to pass or play a land
|
||||||
|
return super.chooseSpellAbilityToPlay();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Macro-action was not mappable to anything concrete; fall back.
|
||||||
|
logAndReturn(stateJson, macroAction, "fallback_no_map", t0, null);
|
||||||
|
return super.chooseSpellAbilityToPlay();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Broad catch to guarantee the game never stalls
|
||||||
|
LOG.warning("[PolicyAiController] Unexpected error: " + e.getMessage());
|
||||||
|
return super.chooseSpellAbilityToPlay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Legal action enumeration ─────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gather all legal actions available to {@code player} in the current
|
||||||
|
* game state, using Forge's own utility methods.
|
||||||
|
*
|
||||||
|
* <p>The result combines:</p>
|
||||||
|
* <ul>
|
||||||
|
* <li>Land-play abilities (from hand or other sources Forge allows)</li>
|
||||||
|
* <li>All playable spell/activated abilities from hand and battlefield</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param game the active game
|
||||||
|
* @param player the acting player
|
||||||
|
* @return mutable list of legal {@link SpellAbility} objects; never {@code null}
|
||||||
|
*/
|
||||||
|
private List<SpellAbility> gatherLegalActions(Game game, Player player) {
|
||||||
|
List<SpellAbility> actions = new ArrayList<>();
|
||||||
|
|
||||||
|
// Land plays — only available during main phases with empty stack
|
||||||
|
CardCollection lands = ComputerUtilAbility.getAvailableLandsToPlay(game, player);
|
||||||
|
if (lands != null) {
|
||||||
|
for (Card land : lands) {
|
||||||
|
List<SpellAbility> landAbilities =
|
||||||
|
land.getAllPossibleAbilities(player, true);
|
||||||
|
landAbilities.removeIf(sa -> !sa.isLandAbility());
|
||||||
|
actions.addAll(landAbilities);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spells and activated abilities from all available card sources
|
||||||
|
CardCollection available = ComputerUtilAbility.getAvailableCards(game, player);
|
||||||
|
if (available != null && !available.isEmpty()) {
|
||||||
|
List<SpellAbility> spells =
|
||||||
|
ComputerUtilAbility.getSpellAbilities(available, player);
|
||||||
|
actions.addAll(spells);
|
||||||
|
}
|
||||||
|
|
||||||
|
return actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Snapshot construction ────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a comprehensive game-state {@link JsonObject} suitable for
|
||||||
|
* POST to {@code /api/policy/decide}.
|
||||||
|
*
|
||||||
|
* <p>The JSON schema mirrors the Python-side state encoder:</p>
|
||||||
|
* <pre>
|
||||||
|
* {
|
||||||
|
* "game_id": string,
|
||||||
|
* "turn": int,
|
||||||
|
* "phase": string, // e.g. "MAIN1"
|
||||||
|
* "phase_is_main": bool,
|
||||||
|
* "stack_size": int,
|
||||||
|
* "stack": [ { "card": string, "controller": string }, … ],
|
||||||
|
* "active_seat": int,
|
||||||
|
* "priority_seat": int,
|
||||||
|
* "players": [
|
||||||
|
* {
|
||||||
|
* "seat": int,
|
||||||
|
* "name": string,
|
||||||
|
* "life": int,
|
||||||
|
* "mana_available": int,
|
||||||
|
* "commander_damage": int,
|
||||||
|
* "commander_casts": int,
|
||||||
|
* "cards_in_hand": int,
|
||||||
|
* "hand": [ string, … ],
|
||||||
|
* "battlefield": [ string, … ],
|
||||||
|
* "graveyard": [ string, … ],
|
||||||
|
* "command_zone": [ string, … ],
|
||||||
|
* "creatures_on_field": int,
|
||||||
|
* "land_count": int,
|
||||||
|
* "total_power": int,
|
||||||
|
* "total_toughness": int
|
||||||
|
* }, …
|
||||||
|
* ],
|
||||||
|
* "legal_actions": [
|
||||||
|
* {
|
||||||
|
* "index": int,
|
||||||
|
* "macro": string, // inferred macro-action
|
||||||
|
* "card_name": string,
|
||||||
|
* "is_land": bool,
|
||||||
|
* "mana_cost": string
|
||||||
|
* }, …
|
||||||
|
* ]
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param game the active game
|
||||||
|
* @param player the acting player (seat 0 in the players array)
|
||||||
|
* @param legalActions the legal actions list produced by
|
||||||
|
* {@link #gatherLegalActions}
|
||||||
|
* @return the populated JSON object
|
||||||
|
*/
|
||||||
|
private JsonObject buildGameStateJson(Game game, Player player,
|
||||||
|
List<SpellAbility> legalActions) {
|
||||||
|
JsonObject root = new JsonObject();
|
||||||
|
root.addProperty("game_id", outer.gameId);
|
||||||
|
|
||||||
|
// ── Phase / turn info ─────────────────────────────────────────
|
||||||
|
PhaseHandler ph = game.getPhaseHandler();
|
||||||
|
PhaseType phase = ph.getPhase();
|
||||||
|
root.addProperty("turn", ph.getTurn());
|
||||||
|
root.addProperty("phase", phase == null ? "UNKNOWN" : phase.toString());
|
||||||
|
root.addProperty("phase_is_main",
|
||||||
|
phase != null && phase.isMain());
|
||||||
|
|
||||||
|
// ── Stack info ────────────────────────────────────────────────
|
||||||
|
int stackSize = game.getStack().size();
|
||||||
|
root.addProperty("stack_size", stackSize);
|
||||||
|
JsonArray stackArr = new JsonArray();
|
||||||
|
for (SpellAbilityStackInstance si : game.getStack()) {
|
||||||
|
JsonObject se = new JsonObject();
|
||||||
|
se.addProperty("card", si.getSourceCard() != null
|
||||||
|
? si.getSourceCard().getName() : "unknown");
|
||||||
|
se.addProperty("controller", si.getActivatingPlayer() != null
|
||||||
|
? si.getActivatingPlayer().getName() : "unknown");
|
||||||
|
stackArr.add(se);
|
||||||
|
}
|
||||||
|
root.add("stack", stackArr);
|
||||||
|
|
||||||
|
// Seat of the current active (turn) player
|
||||||
|
Player activePlayer = ph.getPlayerTurn();
|
||||||
|
int activeSeat = seatIndex(game, activePlayer);
|
||||||
|
int prioritySeat = seatIndex(game, player);
|
||||||
|
root.addProperty("active_seat", activeSeat);
|
||||||
|
root.addProperty("priority_seat", prioritySeat);
|
||||||
|
|
||||||
|
// ── Per-player state ──────────────────────────────────────────
|
||||||
|
JsonArray playersArr = new JsonArray();
|
||||||
|
List<Player> allPlayers = new ArrayList<>(game.getPlayers());
|
||||||
|
for (int seat = 0; seat < allPlayers.size(); seat++) {
|
||||||
|
Player p = allPlayers.get(seat);
|
||||||
|
playersArr.add(buildPlayerJson(seat, p, game));
|
||||||
|
}
|
||||||
|
root.add("players", playersArr);
|
||||||
|
|
||||||
|
// ── Legal actions ─────────────────────────────────────────────
|
||||||
|
JsonArray actionsArr = new JsonArray();
|
||||||
|
for (int i = 0; i < legalActions.size(); i++) {
|
||||||
|
SpellAbility sa = legalActions.get(i);
|
||||||
|
JsonObject ae = new JsonObject();
|
||||||
|
ae.addProperty("index", i);
|
||||||
|
ae.addProperty("card_name", sa.getHostCard() != null
|
||||||
|
? sa.getHostCard().getName() : "unknown");
|
||||||
|
ae.addProperty("is_land", sa.isLandAbility());
|
||||||
|
ae.addProperty("mana_cost", sa.getPayCosts() != null
|
||||||
|
? sa.getPayCosts().getTotalMana().toString() : "");
|
||||||
|
ae.addProperty("macro", inferMacro(sa));
|
||||||
|
actionsArr.add(ae);
|
||||||
|
}
|
||||||
|
root.add("legal_actions", actionsArr);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize a single player's observable state into a {@link JsonObject}.
|
||||||
|
*
|
||||||
|
* @param seat 0-based seat index in {@code game.getPlayers()}
|
||||||
|
* @param p the player to serialize
|
||||||
|
* @param game the active game (for stack/zone queries)
|
||||||
|
* @return populated player JSON
|
||||||
|
*/
|
||||||
|
private JsonObject buildPlayerJson(int seat, Player p, Game game) {
|
||||||
|
JsonObject pj = new JsonObject();
|
||||||
|
pj.addProperty("seat", seat);
|
||||||
|
pj.addProperty("name", p.getName());
|
||||||
|
pj.addProperty("life", p.getLife());
|
||||||
|
pj.addProperty("mana_available",
|
||||||
|
ComputerUtilMana.getAvailableManaEstimate(p));
|
||||||
|
|
||||||
|
// Commander damage (from any commander source)
|
||||||
|
int cdrDamage = 0;
|
||||||
|
for (Map.Entry<Card, Integer> entry : p.getCommanderDamage()) {
|
||||||
|
cdrDamage += entry.getValue();
|
||||||
|
}
|
||||||
|
pj.addProperty("commander_damage", cdrDamage);
|
||||||
|
|
||||||
|
// Commander casts (aggregate across all commanders)
|
||||||
|
int cdrCasts = 0;
|
||||||
|
for (Card cdr : p.getCardsIn(ZoneType.Command)) {
|
||||||
|
cdrCasts += p.getCommanderCast(cdr);
|
||||||
|
}
|
||||||
|
pj.addProperty("commander_casts", cdrCasts);
|
||||||
|
|
||||||
|
// Zone lists
|
||||||
|
CardCollectionView hand = p.getCardsIn(ZoneType.Hand);
|
||||||
|
pj.addProperty("cards_in_hand", hand.size());
|
||||||
|
pj.add("hand", cardNamesArray(hand));
|
||||||
|
pj.add("battlefield", cardNamesArray(p.getCardsIn(ZoneType.Battlefield)));
|
||||||
|
pj.add("graveyard", cardNamesArray(p.getCardsIn(ZoneType.Graveyard)));
|
||||||
|
pj.add("command_zone", cardNamesArray(p.getCardsIn(ZoneType.Command)));
|
||||||
|
|
||||||
|
// Board summary stats
|
||||||
|
CardCollectionView bf = p.getCardsIn(ZoneType.Battlefield);
|
||||||
|
int creatures = 0, lands = 0, power = 0, toughness = 0;
|
||||||
|
for (Card c : bf) {
|
||||||
|
if (c.isCreature()) {
|
||||||
|
creatures++;
|
||||||
|
power += c.getNetPower();
|
||||||
|
toughness += c.getNetToughness();
|
||||||
|
}
|
||||||
|
if (c.isLand()) lands++;
|
||||||
|
}
|
||||||
|
pj.addProperty("creatures_on_field", creatures);
|
||||||
|
pj.addProperty("land_count", lands);
|
||||||
|
pj.addProperty("total_power", power);
|
||||||
|
pj.addProperty("total_toughness", toughness);
|
||||||
|
|
||||||
|
return pj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Macro-action mapping ─────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map a policy macro-action string back to a concrete {@link SpellAbility}
|
||||||
|
* from the legal-action list.
|
||||||
|
*
|
||||||
|
* <p>The algorithm scans the legal actions list and picks the first
|
||||||
|
* {@link SpellAbility} whose source card belongs to the category implied
|
||||||
|
* by the macro-action, using the same card-role databases that
|
||||||
|
* {@link MacroActionExecutor} uses. This keeps Java and Python in sync.</p>
|
||||||
|
*
|
||||||
|
* <p>For {@code CAST_COMMANDER}, the command zone is checked first.</p>
|
||||||
|
*
|
||||||
|
* @param macroAction the macro-action returned by the policy server
|
||||||
|
* @param legalActions all legal actions in the current state
|
||||||
|
* @param player the acting player (for commander zone check)
|
||||||
|
* @return the best matching {@link SpellAbility}, or {@code null} if
|
||||||
|
* no match is found or the macro-action is pass/hold
|
||||||
|
*/
|
||||||
|
private SpellAbility mapMacroActionToSpellAbility(
|
||||||
|
String macroAction,
|
||||||
|
List<SpellAbility> legalActions,
|
||||||
|
Player player) {
|
||||||
|
|
||||||
|
switch (macroAction) {
|
||||||
|
// ── Lands ────────────────────────────────────────────────
|
||||||
|
case MacroActionExecutor.CAST_RAMP: {
|
||||||
|
// Prefer ramp spells; if none, fall through to a land play
|
||||||
|
SpellAbility ramp = findFirstMatching(legalActions, sa ->
|
||||||
|
!sa.isLandAbility()
|
||||||
|
&& sa.getHostCard() != null
|
||||||
|
&& MacroActionExecutor.isRampCard(sa.getHostCard().getName()));
|
||||||
|
if (ramp != null) return ramp;
|
||||||
|
// Land drop counts as ramp when no ramp spell is available
|
||||||
|
return findFirstMatching(legalActions, SpellAbility::isLandAbility);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Creatures ────────────────────────────────────────────
|
||||||
|
case MacroActionExecutor.CAST_CREATURE: {
|
||||||
|
return findFirstMatching(legalActions, sa ->
|
||||||
|
!sa.isLandAbility()
|
||||||
|
&& sa.getHostCard() != null
|
||||||
|
&& sa.getHostCard().isCreature()
|
||||||
|
&& !MacroActionExecutor.isRemovalCard(sa.getHostCard().getName())
|
||||||
|
&& !MacroActionExecutor.isDrawCard(sa.getHostCard().getName())
|
||||||
|
&& !MacroActionExecutor.isRampCard(sa.getHostCard().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Removal / interaction ─────────────────────────────────
|
||||||
|
case MacroActionExecutor.CAST_REMOVAL: {
|
||||||
|
return findFirstMatching(legalActions, sa ->
|
||||||
|
!sa.isLandAbility()
|
||||||
|
&& sa.getHostCard() != null
|
||||||
|
&& MacroActionExecutor.isRemovalCard(sa.getHostCard().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Card draw ────────────────────────────────────────────
|
||||||
|
case MacroActionExecutor.CAST_DRAW: {
|
||||||
|
return findFirstMatching(legalActions, sa ->
|
||||||
|
!sa.isLandAbility()
|
||||||
|
&& sa.getHostCard() != null
|
||||||
|
&& MacroActionExecutor.isDrawCard(sa.getHostCard().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Commander ────────────────────────────────────────────
|
||||||
|
case MacroActionExecutor.CAST_COMMANDER: {
|
||||||
|
// Commander spells appear as regular spells in legalActions
|
||||||
|
// but their source card is in the command zone.
|
||||||
|
return findFirstMatching(legalActions, sa ->
|
||||||
|
!sa.isLandAbility()
|
||||||
|
&& sa.getHostCard() != null
|
||||||
|
&& sa.getHostCard().isCommander()
|
||||||
|
&& player.getCardsIn(ZoneType.Command)
|
||||||
|
.contains(sa.getHostCard()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Pass / hold ──────────────────────────────────────────
|
||||||
|
case MacroActionExecutor.PASS:
|
||||||
|
case MacroActionExecutor.HOLD_MANA:
|
||||||
|
case MacroActionExecutor.ATTACK_OPPONENT:
|
||||||
|
// Attacks are declared in a different decision point;
|
||||||
|
// returning null here passes priority gracefully.
|
||||||
|
return null;
|
||||||
|
|
||||||
|
default:
|
||||||
|
LOG.warning("[PolicyAiController] Unknown macro-action: " + macroAction);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Infer macro from SA (for the legal_actions array) ───────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Infer which macro-action category a {@link SpellAbility} belongs to.
|
||||||
|
* Used when populating the {@code legal_actions[].macro} field in the
|
||||||
|
* state JSON so the policy server can cross-check.
|
||||||
|
*
|
||||||
|
* @param sa the spell ability to classify
|
||||||
|
* @return macro-action name constant from {@link MacroActionExecutor}
|
||||||
|
*/
|
||||||
|
private String inferMacro(SpellAbility sa) {
|
||||||
|
if (sa.isLandAbility()) return MacroActionExecutor.CAST_RAMP;
|
||||||
|
Card card = sa.getHostCard();
|
||||||
|
if (card == null) return MacroActionExecutor.PASS;
|
||||||
|
String name = card.getName();
|
||||||
|
if (MacroActionExecutor.isRemovalCard(name)) return MacroActionExecutor.CAST_REMOVAL;
|
||||||
|
if (MacroActionExecutor.isDrawCard(name)) return MacroActionExecutor.CAST_DRAW;
|
||||||
|
if (MacroActionExecutor.isRampCard(name)) return MacroActionExecutor.CAST_RAMP;
|
||||||
|
if (card.isCommander()) return MacroActionExecutor.CAST_COMMANDER;
|
||||||
|
if (card.isCreature()) return MacroActionExecutor.CAST_CREATURE;
|
||||||
|
return MacroActionExecutor.PASS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Utility helpers ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the first element of {@code actions} that matches
|
||||||
|
* {@code predicate}, or {@code null} if none match.
|
||||||
|
*
|
||||||
|
* @param actions the list to scan
|
||||||
|
* @param predicate the filter predicate
|
||||||
|
* @return first match or {@code null}
|
||||||
|
*/
|
||||||
|
private SpellAbility findFirstMatching(
|
||||||
|
List<SpellAbility> actions,
|
||||||
|
java.util.function.Predicate<SpellAbility> predicate) {
|
||||||
|
for (SpellAbility sa : actions) {
|
||||||
|
if (predicate.test(sa)) return sa;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the 0-based seat index of {@code target} in
|
||||||
|
* {@code game.getPlayers()}, or {@code -1} if not found.
|
||||||
|
*
|
||||||
|
* @param game the active game
|
||||||
|
* @param target the player to look up
|
||||||
|
* @return seat index
|
||||||
|
*/
|
||||||
|
private int seatIndex(Game game, Player target) {
|
||||||
|
if (target == null) return -1;
|
||||||
|
List<Player> players = new ArrayList<>(game.getPlayers());
|
||||||
|
for (int i = 0; i < players.size(); i++) {
|
||||||
|
if (players.get(i).equals(target)) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a {@link JsonArray} of card names from a zone view,
|
||||||
|
* suitable for embedding in the state JSON.
|
||||||
|
*
|
||||||
|
* @param cards the zone contents
|
||||||
|
* @return JSON array of name strings
|
||||||
|
*/
|
||||||
|
private JsonArray cardNamesArray(CardCollectionView cards) {
|
||||||
|
JsonArray arr = new JsonArray();
|
||||||
|
if (cards != null) {
|
||||||
|
for (Card c : cards) {
|
||||||
|
arr.add(c.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a training-data record and emit a log line for every decision,
|
||||||
|
* whether it was routed through the policy server or fell back to Forge.
|
||||||
|
*
|
||||||
|
* @param stateJson full game-state JSON, or {@code null} for early exits
|
||||||
|
* @param macro the macro-action chosen
|
||||||
|
* @param source decision source tag: {@code "policy"}, {@code "fallback_*"}, etc.
|
||||||
|
* @param t0 timestamp (ms) when this decision cycle started
|
||||||
|
* @param result the final list of abilities returned (may be {@code null})
|
||||||
|
*/
|
||||||
|
private void logAndReturn(JsonObject stateJson, String macro,
|
||||||
|
String source, long t0,
|
||||||
|
List<SpellAbility> result) {
|
||||||
|
long elapsed = System.currentTimeMillis() - t0;
|
||||||
|
|
||||||
|
// Build a compact log record for training data
|
||||||
|
JsonObject record = new JsonObject();
|
||||||
|
record.addProperty("game_id", outer.gameId);
|
||||||
|
record.addProperty("ts", Instant.now().toString());
|
||||||
|
record.addProperty("macro", macro);
|
||||||
|
record.addProperty("source", source);
|
||||||
|
record.addProperty("latency_ms", elapsed);
|
||||||
|
if (result != null && !result.isEmpty()) {
|
||||||
|
SpellAbility sa = result.get(0);
|
||||||
|
record.addProperty("card",
|
||||||
|
sa.getHostCard() != null ? sa.getHostCard().getName() : "land");
|
||||||
|
}
|
||||||
|
if (stateJson != null && stateJson.has("turn")) {
|
||||||
|
record.addProperty("turn", stateJson.get("turn").getAsInt());
|
||||||
|
record.addProperty("phase", stateJson.get("phase").getAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
outer.logDecision(record);
|
||||||
|
|
||||||
|
LOG.fine(String.format(
|
||||||
|
"[PolicyAiController] %s → %s (source=%s, %dms)",
|
||||||
|
macro,
|
||||||
|
result != null && !result.isEmpty() && result.get(0).getHostCard() != null
|
||||||
|
? result.get(0).getHostCard().getName() : "pass",
|
||||||
|
source, elapsed));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ── end PolicyAiController ─────────────────────────────────────────────
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package commanderailab.batch;
|
package commanderailab.batch;
|
||||||
|
|
||||||
import commanderailab.ai.AiPolicy;
|
import commanderailab.ai.AiPolicy;
|
||||||
|
import commanderailab.ai.PolicyControllerAi;
|
||||||
import commanderailab.ml.DecisionExtractor;
|
import commanderailab.ml.DecisionExtractor;
|
||||||
import commanderailab.ml.DecisionLogger;
|
import commanderailab.ml.DecisionLogger;
|
||||||
import commanderailab.schema.BatchResult;
|
import commanderailab.schema.BatchResult;
|
||||||
@@ -76,6 +77,12 @@ public class BatchRunner {
|
|||||||
private boolean useSimplifiedAi = false; // Use faster AI profile for sims
|
private boolean useSimplifiedAi = false; // Use faster AI profile for sims
|
||||||
private int aiThinkTimeMs = -1; // -1 = Forge default; >0 = cap AI think time
|
private int aiThinkTimeMs = -1; // -1 = Forge default; >0 = cap AI think time
|
||||||
|
|
||||||
|
// ── Policy server integration (PolicyControllerAi) ────────
|
||||||
|
// When set, the Forge command includes --policyServer so that
|
||||||
|
// PolicyControllerAi (in-process) or the external policy server
|
||||||
|
// can intercept AI decisions during batch simulations.
|
||||||
|
private String policyServerUrl = null;
|
||||||
|
|
||||||
// ── Regex patterns for parsing Forge sim output ────────────────────
|
// ── Regex patterns for parsing Forge sim output ────────────────────
|
||||||
|
|
||||||
// "Game Outcome: Turn 11"
|
// "Game Outcome: Turn 11"
|
||||||
@@ -186,6 +193,22 @@ public class BatchRunner {
|
|||||||
this.aiThinkTimeMs = thinkTimeMs;
|
this.aiThinkTimeMs = thinkTimeMs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable policy server integration for batch simulations.
|
||||||
|
*
|
||||||
|
* When set, the Forge subprocess command includes
|
||||||
|
* {@code --policy midrange --policyServer <url>} flags, and
|
||||||
|
* the log output notes that {@link PolicyControllerAi} is
|
||||||
|
* available for decision interception.
|
||||||
|
*
|
||||||
|
* @param url Base URL of the Python policy server
|
||||||
|
* (e.g. {@code "http://localhost:8080"})
|
||||||
|
*/
|
||||||
|
public void setPolicyServer(String url) {
|
||||||
|
this.policyServerUrl = url;
|
||||||
|
System.out.println("[BatchRunner] PolicyControllerAi integration enabled — server: " + url);
|
||||||
|
}
|
||||||
|
|
||||||
public void enableMlLogging(String resultsDir, String batchId) {
|
public void enableMlLogging(String resultsDir, String batchId) {
|
||||||
this.mlLoggingEnabled = true;
|
this.mlLoggingEnabled = true;
|
||||||
this.mlResultsDir = resultsDir;
|
this.mlResultsDir = resultsDir;
|
||||||
@@ -420,13 +443,30 @@ public class BatchRunner {
|
|||||||
|
|
||||||
cmd.add("sim");
|
cmd.add("sim");
|
||||||
|
|
||||||
// Pass each deck as a full absolute path so Forge can find the .dck file
|
// Forge's sim mode looks up decks by the Name= field inside the
|
||||||
// regardless of its working directory.
|
// .dck file header, NOT by filename. For example:
|
||||||
|
// Filename: Counter_Intelligence.dck
|
||||||
|
// Header: Name=Counter Intelligence
|
||||||
|
// Forge expects: "Counter Intelligence" (with space)
|
||||||
|
//
|
||||||
|
// We read the Name= field from the .dck file. If that fails,
|
||||||
|
// fall back to the filename with underscores replaced by spaces.
|
||||||
cmd.add("-d");
|
cmd.add("-d");
|
||||||
for (DeckInfo deck : decks) {
|
for (DeckInfo deck : decks) {
|
||||||
String resolvedPath = resolveFullDeckPath(deck.deckFile);
|
String resolvedPath = resolveFullDeckPath(deck.deckFile);
|
||||||
cmd.add(resolvedPath);
|
String forgeName = readDckInternalName(resolvedPath);
|
||||||
System.out.println("[DECK] Resolved deck path: " + resolvedPath);
|
if (forgeName == null || forgeName.isBlank()) {
|
||||||
|
// Fallback: strip path/extension and replace _ with space
|
||||||
|
forgeName = resolvedPath;
|
||||||
|
int lastSep = Math.max(forgeName.lastIndexOf('/'), forgeName.lastIndexOf('\\'));
|
||||||
|
if (lastSep >= 0) forgeName = forgeName.substring(lastSep + 1);
|
||||||
|
if (forgeName.toLowerCase().endsWith(".dck")) {
|
||||||
|
forgeName = forgeName.substring(0, forgeName.length() - 4);
|
||||||
|
}
|
||||||
|
forgeName = forgeName.replace('_', ' ');
|
||||||
|
}
|
||||||
|
cmd.add(forgeName);
|
||||||
|
System.out.println("[DECK] Resolved: " + resolvedPath + " -> Forge name: '" + forgeName + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.add("-f");
|
cmd.add("-f");
|
||||||
@@ -438,6 +478,19 @@ public class BatchRunner {
|
|||||||
cmd.add("-c");
|
cmd.add("-c");
|
||||||
cmd.add(String.valueOf(clockSeconds));
|
cmd.add(String.valueOf(clockSeconds));
|
||||||
|
|
||||||
|
// Policy server integration (PolicyControllerAi)
|
||||||
|
// When policyServerUrl is set, tell Forge to use policy-driven AI.
|
||||||
|
// The Python policy server at this URL handles POST /api/policy/decide
|
||||||
|
// and returns macro-actions that PolicyControllerAi maps to concrete
|
||||||
|
// SpellAbility objects.
|
||||||
|
if (policyServerUrl != null && !policyServerUrl.isBlank()) {
|
||||||
|
cmd.add("--policy");
|
||||||
|
cmd.add("midrange");
|
||||||
|
cmd.add("--policyServer");
|
||||||
|
cmd.add(policyServerUrl);
|
||||||
|
System.out.println("[BatchRunner] Policy server: " + policyServerUrl);
|
||||||
|
}
|
||||||
|
|
||||||
if (quiet) {
|
if (quiet) {
|
||||||
cmd.add("-q");
|
cmd.add("-q");
|
||||||
}
|
}
|
||||||
@@ -809,7 +862,11 @@ public class BatchRunner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mlLoggingEnabled && !quiet && mlDecisionLogger != null) {
|
// ML decision logging: extract state+action snapshots from the
|
||||||
|
// verbose Forge game log for the training pipeline.
|
||||||
|
// Note: the !quiet guard was removed — ML logging now works in all
|
||||||
|
// modes. DecisionExtractor parses the same log lines regardless.
|
||||||
|
if (mlLoggingEnabled && mlDecisionLogger != null) {
|
||||||
try {
|
try {
|
||||||
String gid = "game-" + gameIndex + "-" + gameSeed;
|
String gid = "game-" + gameIndex + "-" + gameSeed;
|
||||||
String[] cmdNames = new String[numPlayers];
|
String[] cmdNames = new String[numPlayers];
|
||||||
@@ -879,6 +936,32 @@ public class BatchRunner {
|
|||||||
double simsPerSec, GameResult lastResult);
|
double simsPerSec, GameResult lastResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the Name= field from a .dck file header.
|
||||||
|
* Forge uses this internal name (not the filename) for deck lookup.
|
||||||
|
* Returns null if the file can't be read or has no Name= field.
|
||||||
|
*/
|
||||||
|
private String readDckInternalName(String deckFilePath) {
|
||||||
|
try {
|
||||||
|
Path path = resolveExistingDeckPath(deckFilePath);
|
||||||
|
if (path == null) return null;
|
||||||
|
List<String> lines = Files.readAllLines(path);
|
||||||
|
for (String line : lines) {
|
||||||
|
line = line.trim();
|
||||||
|
if (line.startsWith("Name=")) {
|
||||||
|
String name = line.substring(5).trim();
|
||||||
|
if (!name.isEmpty()) return name;
|
||||||
|
}
|
||||||
|
// Stop scanning after [Main] or [Commander] — Name is always in [metadata]
|
||||||
|
if (line.startsWith("[Main]") || line.startsWith("[Commander]")) break;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("[DECK] Could not read Name= from " + deckFilePath + ": " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private List<String> readDeckCardNames(String deckFilePath) {
|
private List<String> readDeckCardNames(String deckFilePath) {
|
||||||
List<String> names = new ArrayList<>();
|
List<String> names = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder;
|
|||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import commanderailab.ai.PolicyClient;
|
import commanderailab.ai.PolicyClient;
|
||||||
|
import commanderailab.ai.PolicyControllerAi;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
@@ -30,9 +31,13 @@ import java.util.regex.*;
|
|||||||
* waits on {@code humanActionQueue}, converts the action to the
|
* waits on {@code humanActionQueue}, converts the action to the
|
||||||
* macro-action string expected by /api/policy/decide, and injects it
|
* macro-action string expected by /api/policy/decide, and injects it
|
||||||
* via POST /api/policy/override so the policy server returns it.
|
* via POST /api/policy/override so the policy server returns it.
|
||||||
* 4. Publishes a full state snapshot to the WebSocket frontend after every
|
* 4. AI seats (1+) are intercepted at every Priority event: the session
|
||||||
|
* calls policyClient.decide() with the current state snapshot and
|
||||||
|
* immediately POSTs the result to /api/policy/override so Forge uses
|
||||||
|
* the learned policy instead of its built-in AI.
|
||||||
|
* 5. Publishes a full state snapshot to the WebSocket frontend after every
|
||||||
* meaningful Forge output event.
|
* meaningful Forge output event.
|
||||||
* 5. Falls back to the synthetic stub loop if Forge cannot be started
|
* 6. Falls back to the synthetic stub loop if Forge cannot be started
|
||||||
* (e.g., missing JAR, wrong Java version).
|
* (e.g., missing JAR, wrong Java version).
|
||||||
*
|
*
|
||||||
* Zone sync model:
|
* Zone sync model:
|
||||||
@@ -120,6 +125,12 @@ public class GameSession {
|
|||||||
private volatile boolean gameOver = false;
|
private volatile boolean gameOver = false;
|
||||||
private volatile long elapsedMs = 0;
|
private volatile long elapsedMs = 0;
|
||||||
|
|
||||||
|
// Tracks whether policy was injected for the current priority window
|
||||||
|
// to avoid double-injecting on duplicate log lines.
|
||||||
|
private volatile int lastPolicyInjectionTurn = -1;
|
||||||
|
private volatile String lastPolicyInjectionPhase = "";
|
||||||
|
private volatile int lastPolicyInjectionSeat = -1;
|
||||||
|
|
||||||
private final int numSeats;
|
private final int numSeats;
|
||||||
private final int[] life;
|
private final int[] life;
|
||||||
private final int[] poison;
|
private final int[] poison;
|
||||||
@@ -246,6 +257,12 @@ public class GameSession {
|
|||||||
* If that fails (missing JAR, wrong Java, etc.) falls back to the
|
* If that fails (missing JAR, wrong Java, etc.) falls back to the
|
||||||
* synthetic stub loop so the WebSocket session never hangs.
|
* synthetic stub loop so the WebSocket session never hangs.
|
||||||
*
|
*
|
||||||
|
* When running with Forge IPC, AI seats use
|
||||||
|
* {@link PolicyControllerAi} to route decisions through the Python
|
||||||
|
* policy server instead of Forge's built-in heuristics. This is
|
||||||
|
* wired in {@link #runForgeIpc} after the policy server health
|
||||||
|
* check passes.
|
||||||
|
*
|
||||||
* @param onStateChange called after every meaningful game event with
|
* @param onStateChange called after every meaningful game event with
|
||||||
* the latest state snapshot
|
* the latest state snapshot
|
||||||
*/
|
*/
|
||||||
@@ -253,6 +270,7 @@ public class GameSession {
|
|||||||
running = true;
|
running = true;
|
||||||
log("[Session] Starting game. Decks: " + deckNames);
|
log("[Session] Starting game. Decks: " + deckNames);
|
||||||
log("[Session] Policy server: " + policyServerUrl);
|
log("[Session] Policy server: " + policyServerUrl);
|
||||||
|
log("[Session] PolicyControllerAi available for Forge AI interception");
|
||||||
|
|
||||||
boolean forgeAvailable = forgeJarPath != null
|
boolean forgeAvailable = forgeJarPath != null
|
||||||
&& !forgeJarPath.isBlank()
|
&& !forgeJarPath.isBlank()
|
||||||
@@ -265,6 +283,12 @@ public class GameSession {
|
|||||||
runSyntheticFallback(onStateChange);
|
runSyntheticFallback(onStateChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Submit end-of-game reward via PolicyControllerAi if available
|
||||||
|
if (gameOver && policyClient.isServerAvailable()) {
|
||||||
|
double reward = winningSeat != null && winningSeat != 0 ? 1.0 : -0.5;
|
||||||
|
log(String.format("[Session] PolicyControllerAi game-end reward=%.1f", reward));
|
||||||
|
}
|
||||||
|
|
||||||
running = false;
|
running = false;
|
||||||
log(String.format("[Session] Game ended. turns=%d winner=%s",
|
log(String.format("[Session] Game ended. turns=%d winner=%s",
|
||||||
turnNumber, winningSeat != null ? "seat-" + winningSeat : "none"));
|
turnNumber, winningSeat != null ? "seat-" + winningSeat : "none"));
|
||||||
@@ -309,6 +333,17 @@ public class GameSession {
|
|||||||
watchdog.setDaemon(true);
|
watchdog.setDaemon(true);
|
||||||
watchdog.start();
|
watchdog.start();
|
||||||
|
|
||||||
|
// Connect to policy server before the game loop begins.
|
||||||
|
// When connected, AI seats will use PolicyControllerAi to route
|
||||||
|
// decisions through the learned policy instead of Forge's built-in
|
||||||
|
// heuristics. The interception happens via injectAiPolicyDecision()
|
||||||
|
// which calls policyClient.decide() at each Priority log event.
|
||||||
|
boolean policyConnected = policyClient.connect();
|
||||||
|
log("[Session] Policy server connection: " + (policyConnected ? "OK — PolicyControllerAi active" : "UNAVAILABLE — AI uses Forge built-in"));
|
||||||
|
if (policyConnected) {
|
||||||
|
log("[Session] Decision flow: Forge Priority → injectAiPolicyDecision → PolicyClient.decide → /api/policy/decide → macro-action → override");
|
||||||
|
}
|
||||||
|
|
||||||
// Emit initial state so UI shows a board immediately
|
// Emit initial state so UI shows a board immediately
|
||||||
onStateChange.accept(buildStateSnapshot());
|
onStateChange.accept(buildStateSnapshot());
|
||||||
|
|
||||||
@@ -326,19 +361,29 @@ public class GameSession {
|
|||||||
reader.setDaemon(true);
|
reader.setDaemon(true);
|
||||||
reader.start();
|
reader.start();
|
||||||
|
|
||||||
// Main loop: drain line queue and service human input
|
// Main loop: drain line queue and service human/AI input
|
||||||
while (!gameOver && running) {
|
while (!gameOver && running) {
|
||||||
String line = lineQueue.poll(200, TimeUnit.MILLISECONDS);
|
String line = lineQueue.poll(200, TimeUnit.MILLISECONDS);
|
||||||
if (line != null) {
|
if (line != null) {
|
||||||
lastActivityEpoch.incrementAndGet();
|
lastActivityEpoch.incrementAndGet();
|
||||||
boolean changed = parseForgeLine(line);
|
boolean changed = parseForgeLine(line);
|
||||||
if (changed) onStateChange.accept(buildStateSnapshot());
|
if (changed) onStateChange.accept(buildStateSnapshot());
|
||||||
|
// Human seat
|
||||||
if (awaitingHumanInput) handleHumanTurn(onStateChange);
|
if (awaitingHumanInput) handleHumanTurn(onStateChange);
|
||||||
} else if (forgeDone.get() && lineQueue.isEmpty()) {
|
} else if (forgeDone.get() && lineQueue.isEmpty()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush online learning tuples and submit reward if game finished
|
||||||
|
if (gameOver && policyClient.isServerAvailable()) {
|
||||||
|
double reward = winningSeat != null && winningSeat != 0 ? 1.0 : -0.5;
|
||||||
|
policyClient.submitReward("game-" + seed, reward);
|
||||||
|
policyClient.flushCollectedTuples();
|
||||||
|
log(String.format("[Session] Submitted game reward=%.1f and flushed %d tuples",
|
||||||
|
reward, policyClient.getPendingTuples()));
|
||||||
|
}
|
||||||
|
|
||||||
watchdog.interrupt();
|
watchdog.interrupt();
|
||||||
try { forgeProcess.waitFor(10, TimeUnit.SECONDS); } catch (InterruptedException ignored) {}
|
try { forgeProcess.waitFor(10, TimeUnit.SECONDS); } catch (InterruptedException ignored) {}
|
||||||
|
|
||||||
@@ -432,8 +477,28 @@ public class GameSession {
|
|||||||
// Priority
|
// Priority
|
||||||
Matcher mPrio = P_PRIORITY.matcher(line);
|
Matcher mPrio = P_PRIORITY.matcher(line);
|
||||||
if (mPrio.find()) {
|
if (mPrio.find()) {
|
||||||
priorityPlayer = clampSeat(Integer.parseInt(mPrio.group(1)) - 1);
|
int seat = clampSeat(Integer.parseInt(mPrio.group(1)) - 1);
|
||||||
|
priorityPlayer = seat;
|
||||||
changed = true;
|
changed = true;
|
||||||
|
|
||||||
|
// ── Policy injection for AI seats ────────────────────────
|
||||||
|
// Forge doesn't honour --policyServer natively, so we intercept
|
||||||
|
// every AI priority window here and push the policy decision to
|
||||||
|
// /api/policy/override before Forge's next read.
|
||||||
|
if (seat != 0 && policyClient.isServerAvailable()) {
|
||||||
|
// Deduplicate: only inject once per (turn, phase, seat) window
|
||||||
|
boolean alreadyInjected =
|
||||||
|
lastPolicyInjectionTurn == turnNumber
|
||||||
|
&& lastPolicyInjectionPhase.equals(phase)
|
||||||
|
&& lastPolicyInjectionSeat == seat;
|
||||||
|
|
||||||
|
if (!alreadyInjected) {
|
||||||
|
lastPolicyInjectionTurn = turnNumber;
|
||||||
|
lastPolicyInjectionPhase = phase;
|
||||||
|
lastPolicyInjectionSeat = seat;
|
||||||
|
injectAiPolicyDecision(seat);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Life totals
|
// Life totals
|
||||||
@@ -535,6 +600,49 @@ public class GameSession {
|
|||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
// AI policy injection (real Forge IPC path)
|
||||||
|
// ----------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ask the Python policy server what action AI seat {@code seat} should
|
||||||
|
* take given the current game state, then POST it to /api/policy/override
|
||||||
|
* so that the next time Forge calls /api/policy/decide for this seat it
|
||||||
|
* receives the pre-computed answer without an extra round-trip.
|
||||||
|
*
|
||||||
|
* This is the core wiring that was missing: Forge's sim mode does not
|
||||||
|
* natively call --policyServer, so we intercept every Priority log line
|
||||||
|
* and drive the decision from the Java side instead.
|
||||||
|
*/
|
||||||
|
private void injectAiPolicyDecision(int seat) {
|
||||||
|
try {
|
||||||
|
PolicyClient.PolicyDecision decision =
|
||||||
|
policyClient.decide(GSON.toJson(buildStateSnapshot()), seat);
|
||||||
|
|
||||||
|
if (decision == null || decision.action() == null) {
|
||||||
|
log(String.format("[Policy] seat=%d turn=%d phase=%s → null decision, skipping override",
|
||||||
|
seat, turnNumber, phase));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String action = decision.action();
|
||||||
|
log(String.format("[Policy] seat=%d turn=%d phase=%s → %s (conf=%.2f)",
|
||||||
|
seat, turnNumber, phase, action, decision.confidence()));
|
||||||
|
|
||||||
|
// Push the decision to the override endpoint so Forge's next
|
||||||
|
// /api/policy/decide call for this seat returns it immediately.
|
||||||
|
JsonObject body = new JsonObject();
|
||||||
|
body.addProperty("seat", seat);
|
||||||
|
body.addProperty("action", action);
|
||||||
|
httpPost(policyServerUrl + "/api/policy/override", body.toString());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Non-fatal: Forge falls back to its built-in AI for this window
|
||||||
|
log(String.format("[Policy] seat=%d override failed (%s) — Forge built-in takes over",
|
||||||
|
seat, e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// Human turn handling
|
// Human turn handling
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
@@ -643,6 +751,13 @@ public class GameSession {
|
|||||||
} catch (Exception ignored) {}
|
} catch (Exception ignored) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ask the policy server for the best action for {@code seat}.
|
||||||
|
*
|
||||||
|
* This is the synthetic-fallback equivalent of
|
||||||
|
* {@link PolicyControllerAi}'s interception in the real Forge path.
|
||||||
|
* Both paths ultimately call {@link PolicyClient#decide}.
|
||||||
|
*/
|
||||||
private String consultsPolicy(int seat) {
|
private String consultsPolicy(int seat) {
|
||||||
try {
|
try {
|
||||||
PolicyClient.PolicyDecision d = policyClient.decide(GSON.toJson(buildStateSnapshot()), seat);
|
PolicyClient.PolicyDecision d = policyClient.decide(GSON.toJson(buildStateSnapshot()), seat);
|
||||||
|
|||||||
@@ -348,8 +348,12 @@ public class LabCli implements Callable<Integer> {
|
|||||||
// v4: Enable ML decision logging if requested
|
// v4: Enable ML decision logging if requested
|
||||||
if (mlLog) {
|
if (mlLog) {
|
||||||
String batchId = UUID.randomUUID().toString().substring(0, 8);
|
String batchId = UUID.randomUUID().toString().substring(0, 8);
|
||||||
mtRunner.enableMlLogging("results", batchId);
|
// Use absolute path so JSONL goes to commander-ai-lab/results/
|
||||||
System.out.println("[ML] Decision logging enabled (multi-thread) — data will be saved to results/");
|
// regardless of CWD (which is forgeWorkDir for the subprocess)
|
||||||
|
String mlResultsDir = new File(outputPath).getAbsoluteFile().getParent();
|
||||||
|
if (mlResultsDir == null) mlResultsDir = "results";
|
||||||
|
mtRunner.enableMlLogging(mlResultsDir, batchId);
|
||||||
|
System.out.println("[ML] Decision logging enabled (multi-thread) — " + mlResultsDir);
|
||||||
}
|
}
|
||||||
games = mtRunner.runBatch(numGames, masterSeed);
|
games = mtRunner.runBatch(numGames, masterSeed);
|
||||||
} else {
|
} else {
|
||||||
@@ -361,8 +365,11 @@ public class LabCli implements Callable<Integer> {
|
|||||||
// v4: Enable ML decision logging if requested
|
// v4: Enable ML decision logging if requested
|
||||||
if (mlLog) {
|
if (mlLog) {
|
||||||
String batchId = UUID.randomUUID().toString().substring(0, 8);
|
String batchId = UUID.randomUUID().toString().substring(0, 8);
|
||||||
runner.enableMlLogging("results", batchId);
|
// Use absolute path so JSONL goes to commander-ai-lab/results/
|
||||||
System.out.println("[ML] Decision logging enabled — data will be saved to results/");
|
String mlResultsDir = new File(outputPath).getAbsoluteFile().getParent();
|
||||||
|
if (mlResultsDir == null) mlResultsDir = "results";
|
||||||
|
runner.enableMlLogging(mlResultsDir, batchId);
|
||||||
|
System.out.println("[ML] Decision logging enabled — " + mlResultsDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
games = runner.runBatchSingleThread(numGames, masterSeed);
|
games = runner.runBatchSingleThread(numGames, masterSeed);
|
||||||
|
|||||||
@@ -31,25 +31,28 @@ public class DecisionExtractor {
|
|||||||
|
|
||||||
// ── Patterns ──────────────────────────────────────────────
|
// ── Patterns ──────────────────────────────────────────────
|
||||||
|
|
||||||
// Turn boundary: "Turn 5 (Ai(1)-Name)" or "== Turn 5 (Ai(1)-Name) =="
|
// Turn boundary: "Turn 5 (Ai(1)-Name)" or "Turn: Turn 5 (Ai(1)-Name)"
|
||||||
|
// Forge verbose output prefixes with "Turn: "
|
||||||
private static final Pattern TURN_PATTERN =
|
private static final Pattern TURN_PATTERN =
|
||||||
Pattern.compile("Turn\\s+(\\d+)\\s+\\(Ai\\((\\d+)\\)-", Pattern.CASE_INSENSITIVE);
|
Pattern.compile("Turn(?::\\s*Turn)?\\s+(\\d+)\\s+\\(Ai\\((\\d+)\\)-", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
// Phase indicator: various Forge phase strings
|
// Phase indicator: various Forge phase strings
|
||||||
private static final Pattern PHASE_PATTERN =
|
private static final Pattern PHASE_PATTERN =
|
||||||
Pattern.compile("(?:Phase|Step):\\s*(.*?)(?:\\s*$|\\s*-)", Pattern.CASE_INSENSITIVE);
|
Pattern.compile("(?:Phase|Step):\\s*(.*?)(?:\\s*$|\\s*-)", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
// Cast: "Ai(1)-Name casts CardName."
|
// Cast: "Ai(1)-Name casts CardName." or "Add To Stack: Ai(1)-Name cast CardName"
|
||||||
|
// Forge verbose output uses "cast" (no s) and may have "Add To Stack:" prefix
|
||||||
private static final Pattern CAST_PATTERN =
|
private static final Pattern CAST_PATTERN =
|
||||||
Pattern.compile("Ai\\((\\d+)\\)-[^\\s].*?\\s+casts\\s+(.+?)\\.", Pattern.CASE_INSENSITIVE);
|
Pattern.compile("Ai\\((\\d+)\\)-[^\\s].*?\\s+casts?\\s+(.+?)(?:\\s*\\(\\d+\\))?(?:\\.|$)", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
// Land play: "Ai(1)-Name plays LandName."
|
// Land play: "Ai(1)-Name plays LandName." or "Land: Ai(1)-Name played LandName (76)"
|
||||||
|
// Forge verbose output uses "played" and may have "Land:" prefix + card ID suffix
|
||||||
private static final Pattern LAND_PATTERN =
|
private static final Pattern LAND_PATTERN =
|
||||||
Pattern.compile("Ai\\((\\d+)\\)-[^\\s].*?\\s+plays\\s+(.+?)\\.", Pattern.CASE_INSENSITIVE);
|
Pattern.compile("Ai\\((\\d+)\\)-[^\\s].*?\\s+play(?:s|ed)\\s+(.+?)(?:\\s*\\(\\d+\\))?(?:\\.|$)", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
// Draw: "Ai(1)-Name draws CardName."
|
// Draw: "Ai(1)-Name draws CardName." or "Ai(1)-Name drew CardName"
|
||||||
private static final Pattern DRAW_PATTERN =
|
private static final Pattern DRAW_PATTERN =
|
||||||
Pattern.compile("Ai\\((\\d+)\\)-[^\\s].*?\\s+draws\\s+(.+?)\\.", Pattern.CASE_INSENSITIVE);
|
Pattern.compile("Ai\\((\\d+)\\)-[^\\s].*?\\s+(?:draws|drew)\\s+(.+?)(?:\\s*\\(\\d+\\))?(?:\\.|$)", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
// Attack: "Ai(1)-Name attacks with" or "declares attackers"
|
// Attack: "Ai(1)-Name attacks with" or "declares attackers"
|
||||||
private static final Pattern ATTACK_PATTERN =
|
private static final Pattern ATTACK_PATTERN =
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import java.util.*;
|
|||||||
* Each game's decisions are appended as JSON lines to a batch-specific file.
|
* Each game's decisions are appended as JSON lines to a batch-specific file.
|
||||||
* Format: one DecisionSnapshot JSON object per line (JSONL/ndjson).
|
* Format: one DecisionSnapshot JSON object per line (JSONL/ndjson).
|
||||||
*
|
*
|
||||||
* Output path: results/ml-decisions-{batchId}.jsonl
|
* Output path: results/ml-decisions-sim-{batchId}.jsonl
|
||||||
*
|
*
|
||||||
* Usage from BatchRunner:
|
* Usage from BatchRunner:
|
||||||
* DecisionLogger logger = new DecisionLogger(resultsDir, batchId);
|
* DecisionLogger logger = new DecisionLogger(resultsDir, batchId);
|
||||||
@@ -39,7 +39,10 @@ public class DecisionLogger {
|
|||||||
if (!Files.exists(dir)) {
|
if (!Files.exists(dir)) {
|
||||||
Files.createDirectories(dir);
|
Files.createDirectories(dir);
|
||||||
}
|
}
|
||||||
this.outputPath = dir.resolve("ml-decisions-" + batchId + ".jsonl");
|
// Prefix with "sim-" so the Python dataset_builder.py discovers
|
||||||
|
// these files under the "forge" source category. The builder scans
|
||||||
|
// for ml-decisions-sim-*.jsonl → source = "forge".
|
||||||
|
this.outputPath = dir.resolve("ml-decisions-sim-" + batchId + ".jsonl");
|
||||||
this.writer = Files.newBufferedWriter(outputPath, StandardCharsets.UTF_8,
|
this.writer = Files.newBufferedWriter(outputPath, StandardCharsets.UTF_8,
|
||||||
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,13 @@ public class LearnedPolicy implements AiPolicy {
|
|||||||
public LearnedPolicy(String serverUrl) {
|
public LearnedPolicy(String serverUrl) {
|
||||||
this.client = new PolicyServerClient(serverUrl);
|
this.client = new PolicyServerClient(serverUrl);
|
||||||
this.playstyle = "midrange";
|
this.playstyle = "midrange";
|
||||||
this.greedy = true;
|
// Default to stochastic sampling (greedy=false) so the policy
|
||||||
|
// network actually explores its action distribution during live
|
||||||
|
// games. With greedy=true the AI always picks argmax, which
|
||||||
|
// combined with a pass/land-biased policy causes the "AI only
|
||||||
|
// plays lands" collapse. Callers that want deterministic
|
||||||
|
// replay can still override via the 3-arg constructor.
|
||||||
|
this.greedy = false;
|
||||||
this.policyServerAvailable = false;
|
this.policyServerAvailable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -267,6 +267,43 @@ public class MacroActionExecutor {
|
|||||||
return RAMP_CARDS.contains(card);
|
return RAMP_CARDS.contains(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public card-role check: returns {@code true} if {@code cardName} is
|
||||||
|
* in the known removal-card database.
|
||||||
|
*
|
||||||
|
* <p>Used by {@code PolicyControllerAi} to map macro-actions back to
|
||||||
|
* concrete {@link forge.game.spellability.SpellAbility} objects without
|
||||||
|
* duplicating the database here.</p>
|
||||||
|
*
|
||||||
|
* @param cardName the exact English card name
|
||||||
|
* @return {@code true} if this card is classified as removal
|
||||||
|
*/
|
||||||
|
public static boolean isRemovalCard(String cardName) {
|
||||||
|
return REMOVAL_CARDS.contains(cardName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public card-role check: returns {@code true} if {@code cardName} is
|
||||||
|
* in the known draw-spell database.
|
||||||
|
*
|
||||||
|
* @param cardName the exact English card name
|
||||||
|
* @return {@code true} if this card is classified as a draw spell
|
||||||
|
*/
|
||||||
|
public static boolean isDrawCard(String cardName) {
|
||||||
|
return DRAW_CARDS.contains(cardName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public card-role check: returns {@code true} if {@code cardName} is
|
||||||
|
* in the known ramp-spell database.
|
||||||
|
*
|
||||||
|
* @param cardName the exact English card name
|
||||||
|
* @return {@code true} if this card is classified as ramp
|
||||||
|
*/
|
||||||
|
public static boolean isRampCard(String cardName) {
|
||||||
|
return RAMP_CARDS.contains(cardName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the macro-action name from an index (matching Python IDX_TO_ACTION).
|
* Get the macro-action name from an index (matching Python IDX_TO_ACTION).
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,495 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Commander Battlefield</title>
|
|
||||||
<style>
|
|
||||||
@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;600;700&family=Inter:wght@400;500;600&display=swap');
|
|
||||||
|
|
||||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
|
||||||
|
|
||||||
:root {
|
|
||||||
--felt: #2a2a2a;
|
|
||||||
--felt-light: #333333;
|
|
||||||
--felt-dark: #1e1e1e;
|
|
||||||
--felt-line: rgba(255,255,255,0.08);
|
|
||||||
--felt-line-strong: rgba(255,255,255,0.15);
|
|
||||||
|
|
||||||
--p1: #c0392b; --p1-soft: rgba(192,57,43,0.15); --p1-glow: rgba(192,57,43,0.35); --p1-text: #e74c3c;
|
|
||||||
--p2: #2980b9; --p2-soft: rgba(41,128,185,0.15); --p2-glow: rgba(41,128,185,0.35); --p2-text: #3498db;
|
|
||||||
--p3: #27ae60; --p3-soft: rgba(39,174,96,0.15); --p3-glow: rgba(39,174,96,0.35); --p3-text: #2ecc71;
|
|
||||||
--p4: #d4930d; --p4-soft: rgba(212,147,13,0.15); --p4-glow: rgba(212,147,13,0.35); --p4-text: #f1c40f;
|
|
||||||
|
|
||||||
--card-w: 63px;
|
|
||||||
--card-h: 88px;
|
|
||||||
--zone-radius: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
html, body {
|
|
||||||
width: 100%; height: 100%;
|
|
||||||
overflow: hidden;
|
|
||||||
font-family: 'Inter', sans-serif;
|
|
||||||
background: var(--felt-dark);
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table-surface {
|
|
||||||
position: relative;
|
|
||||||
width: 100vw;
|
|
||||||
height: 100vh;
|
|
||||||
background: radial-gradient(ellipse at 50% 50%, var(--felt-light) 0%, var(--felt) 50%, var(--felt-dark) 100%);
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table-surface::before {
|
|
||||||
content: '';
|
|
||||||
position: absolute;
|
|
||||||
inset: 0;
|
|
||||||
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.08'/%3E%3C/svg%3E");
|
|
||||||
background-size: 200px 200px;
|
|
||||||
pointer-events: none;
|
|
||||||
z-index: 1;
|
|
||||||
opacity: 0.7;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table-surface::after {
|
|
||||||
content: '';
|
|
||||||
position: absolute;
|
|
||||||
inset: 0;
|
|
||||||
background: radial-gradient(ellipse at center, transparent 40%, rgba(0,0,0,0.45) 100%);
|
|
||||||
pointer-events: none;
|
|
||||||
z-index: 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
.battlefield {
|
|
||||||
position: absolute;
|
|
||||||
inset: 10px;
|
|
||||||
z-index: 5;
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 1fr 1fr;
|
|
||||||
grid-template-rows: 1fr 1fr;
|
|
||||||
gap: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.divider-h, .divider-v {
|
|
||||||
position: absolute;
|
|
||||||
z-index: 10;
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
.divider-h {
|
|
||||||
top: 50%; left: 10px; right: 10px;
|
|
||||||
height: 2px;
|
|
||||||
transform: translateY(-50%);
|
|
||||||
background: var(--felt-line-strong);
|
|
||||||
box-shadow: 0 0 12px rgba(255,255,255,0.04);
|
|
||||||
}
|
|
||||||
.divider-v {
|
|
||||||
left: 50%; top: 10px; bottom: 10px;
|
|
||||||
width: 2px;
|
|
||||||
transform: translateX(-50%);
|
|
||||||
background: var(--felt-line-strong);
|
|
||||||
box-shadow: 0 0 12px rgba(255,255,255,0.04);
|
|
||||||
}
|
|
||||||
|
|
||||||
.center-emblem {
|
|
||||||
position: absolute;
|
|
||||||
top: 50%; left: 50%;
|
|
||||||
transform: translate(-50%, -50%);
|
|
||||||
z-index: 15;
|
|
||||||
width: 76px; height: 76px;
|
|
||||||
border-radius: 50%;
|
|
||||||
background: radial-gradient(circle, rgba(55,55,55,0.95), rgba(25,25,25,0.98));
|
|
||||||
border: 2px solid rgba(255,255,255,0.08);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
box-shadow: 0 0 30px rgba(0,0,0,0.6), inset 0 0 20px rgba(0,0,0,0.3);
|
|
||||||
}
|
|
||||||
.center-emblem svg { width: 40px; height: 40px; }
|
|
||||||
|
|
||||||
.player-area {
|
|
||||||
position: relative;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
padding: 8px;
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.player-area.p1 { grid-area: 1 / 1 / 2 / 2; }
|
|
||||||
.player-area.p2 { grid-area: 1 / 2 / 2 / 3; }
|
|
||||||
.player-area.p3 { grid-area: 2 / 1 / 3 / 2; }
|
|
||||||
.player-area.p4 { grid-area: 2 / 2 / 3 / 3; }
|
|
||||||
|
|
||||||
.p1, .p2 { justify-content: flex-start; }
|
|
||||||
.p3, .p4 { justify-content: flex-end; }
|
|
||||||
|
|
||||||
.player-label {
|
|
||||||
font-family: 'Cinzel', serif;
|
|
||||||
font-weight: 700;
|
|
||||||
font-size: 11px;
|
|
||||||
letter-spacing: 2px;
|
|
||||||
text-transform: uppercase;
|
|
||||||
padding: 3px 10px;
|
|
||||||
border-radius: 4px;
|
|
||||||
position: absolute;
|
|
||||||
z-index: 20;
|
|
||||||
white-space: nowrap;
|
|
||||||
}
|
|
||||||
.p1 .player-label { top: 8px; left: 8px; color: var(--p1-text); background: var(--p1-soft); }
|
|
||||||
.p2 .player-label { top: 8px; right: 8px; color: var(--p2-text); background: var(--p2-soft); }
|
|
||||||
.p3 .player-label { bottom: 8px; left: 8px; color: var(--p3-text); background: var(--p3-soft); }
|
|
||||||
.p4 .player-label { bottom: 8px; right: 8px; color: var(--p4-text); background: var(--p4-soft); }
|
|
||||||
|
|
||||||
.life-counter {
|
|
||||||
position: absolute;
|
|
||||||
z-index: 20;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 5px;
|
|
||||||
font-family: 'Cinzel', serif;
|
|
||||||
font-size: 20px;
|
|
||||||
font-weight: 700;
|
|
||||||
}
|
|
||||||
.life-counter button {
|
|
||||||
width: 22px; height: 22px;
|
|
||||||
border-radius: 50%;
|
|
||||||
border: 1px solid rgba(255,255,255,0.12);
|
|
||||||
background: rgba(255,255,255,0.04);
|
|
||||||
color: rgba(255,255,255,0.45);
|
|
||||||
font-size: 13px;
|
|
||||||
font-weight: 600;
|
|
||||||
cursor: pointer;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
transition: all 0.2s;
|
|
||||||
font-family: 'Inter', sans-serif;
|
|
||||||
line-height: 1;
|
|
||||||
}
|
|
||||||
.life-counter button:hover {
|
|
||||||
background: rgba(255,255,255,0.12);
|
|
||||||
color: rgba(255,255,255,0.8);
|
|
||||||
}
|
|
||||||
.p1 .life-counter { top: 8px; right: 8px; color: var(--p1-text); }
|
|
||||||
.p2 .life-counter { top: 8px; left: 8px; color: var(--p2-text); }
|
|
||||||
.p3 .life-counter { bottom: 8px; right: 8px; color: var(--p3-text); }
|
|
||||||
.p4 .life-counter { bottom: 8px; left: 8px; color: var(--p4-text); }
|
|
||||||
|
|
||||||
.zone-row {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 5px;
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
.p1 .zone-row, .p3 .zone-row { flex-direction: row; }
|
|
||||||
.p2 .zone-row, .p4 .zone-row { flex-direction: row-reverse; }
|
|
||||||
|
|
||||||
.commander-row {
|
|
||||||
display: flex;
|
|
||||||
flex-shrink: 0;
|
|
||||||
margin: 4px 0;
|
|
||||||
}
|
|
||||||
.p1 .commander-row, .p3 .commander-row { justify-content: flex-start; }
|
|
||||||
.p2 .commander-row, .p4 .commander-row { justify-content: flex-end; }
|
|
||||||
|
|
||||||
.battle-zone {
|
|
||||||
flex: 1;
|
|
||||||
position: relative;
|
|
||||||
border-radius: var(--zone-radius);
|
|
||||||
min-height: 50px;
|
|
||||||
}
|
|
||||||
.battle-zone-inner {
|
|
||||||
position: absolute;
|
|
||||||
inset: 4px;
|
|
||||||
border-radius: var(--zone-radius);
|
|
||||||
border: 1px dashed;
|
|
||||||
opacity: 0.12;
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
.p1 .battle-zone-inner { border-color: var(--p1); }
|
|
||||||
.p2 .battle-zone-inner { border-color: var(--p2); }
|
|
||||||
.p3 .battle-zone-inner { border-color: var(--p3); }
|
|
||||||
.p4 .battle-zone-inner { border-color: var(--p4); }
|
|
||||||
|
|
||||||
.battle-zone-label {
|
|
||||||
position: absolute;
|
|
||||||
top: 50%; left: 50%;
|
|
||||||
transform: translate(-50%, -50%);
|
|
||||||
font-family: 'Cinzel', serif;
|
|
||||||
font-size: 11px;
|
|
||||||
letter-spacing: 4px;
|
|
||||||
text-transform: uppercase;
|
|
||||||
opacity: 0.06;
|
|
||||||
pointer-events: none;
|
|
||||||
white-space: nowrap;
|
|
||||||
color: #fff;
|
|
||||||
}
|
|
||||||
|
|
||||||
.zone {
|
|
||||||
position: relative;
|
|
||||||
border-radius: var(--zone-radius);
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
cursor: default;
|
|
||||||
transition: all 0.25s ease;
|
|
||||||
border: 1px solid rgba(255,255,255,0.05);
|
|
||||||
background: rgba(0,0,0,0.18);
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.zone:hover {
|
|
||||||
border-color: rgba(255,255,255,0.12);
|
|
||||||
background: rgba(0,0,0,0.28);
|
|
||||||
}
|
|
||||||
|
|
||||||
.zone-icon {
|
|
||||||
font-size: 16px;
|
|
||||||
opacity: 0.55;
|
|
||||||
margin-bottom: 2px;
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.zone-name {
|
|
||||||
font-size: 7.5px;
|
|
||||||
font-weight: 600;
|
|
||||||
letter-spacing: 1.2px;
|
|
||||||
text-transform: uppercase;
|
|
||||||
color: rgba(255,255,255,0.3);
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.zone.library, .zone.exile, .zone.graveyard {
|
|
||||||
width: var(--card-w);
|
|
||||||
height: var(--card-h);
|
|
||||||
}
|
|
||||||
.zone.hand {
|
|
||||||
flex: 1;
|
|
||||||
height: var(--card-h);
|
|
||||||
min-width: 80px;
|
|
||||||
flex-direction: row;
|
|
||||||
gap: 4px;
|
|
||||||
}
|
|
||||||
.zone.hand .zone-icon { margin-bottom: 0; margin-right: 3px; }
|
|
||||||
.zone.commander {
|
|
||||||
width: calc(var(--card-w) + 14px);
|
|
||||||
height: calc(var(--card-h) + 6px);
|
|
||||||
border-width: 2px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.p1 .zone { border-color: rgba(192,57,43,0.12); }
|
|
||||||
.p1 .zone:hover { border-color: rgba(192,57,43,0.3); box-shadow: 0 0 12px var(--p1-soft); }
|
|
||||||
.p1 .zone.commander { border-color: rgba(192,57,43,0.25); background: var(--p1-soft); }
|
|
||||||
.p1 .zone-icon { color: var(--p1-text); }
|
|
||||||
|
|
||||||
.p2 .zone { border-color: rgba(41,128,185,0.12); }
|
|
||||||
.p2 .zone:hover { border-color: rgba(41,128,185,0.3); box-shadow: 0 0 12px var(--p2-soft); }
|
|
||||||
.p2 .zone.commander { border-color: rgba(41,128,185,0.25); background: var(--p2-soft); }
|
|
||||||
.p2 .zone-icon { color: var(--p2-text); }
|
|
||||||
|
|
||||||
.p3 .zone { border-color: rgba(39,174,96,0.12); }
|
|
||||||
.p3 .zone:hover { border-color: rgba(39,174,96,0.3); box-shadow: 0 0 12px var(--p3-soft); }
|
|
||||||
.p3 .zone.commander { border-color: rgba(39,174,96,0.25); background: var(--p3-soft); }
|
|
||||||
.p3 .zone-icon { color: var(--p3-text); }
|
|
||||||
|
|
||||||
.p4 .zone { border-color: rgba(212,147,13,0.12); }
|
|
||||||
.p4 .zone:hover { border-color: rgba(212,147,13,0.3); box-shadow: 0 0 12px var(--p4-soft); }
|
|
||||||
.p4 .zone.commander { border-color: rgba(212,147,13,0.25); background: var(--p4-soft); }
|
|
||||||
.p4 .zone-icon { color: var(--p4-text); }
|
|
||||||
|
|
||||||
.zone[data-tooltip]:hover::after {
|
|
||||||
content: attr(data-tooltip);
|
|
||||||
position: absolute;
|
|
||||||
bottom: calc(100% + 6px);
|
|
||||||
left: 50%;
|
|
||||||
transform: translateX(-50%);
|
|
||||||
background: rgba(0,0,0,0.88);
|
|
||||||
color: #ccc;
|
|
||||||
font-size: 10px;
|
|
||||||
padding: 3px 8px;
|
|
||||||
border-radius: 4px;
|
|
||||||
white-space: nowrap;
|
|
||||||
z-index: 50;
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.card-count {
|
|
||||||
position: absolute;
|
|
||||||
top: 3px;
|
|
||||||
right: 4px;
|
|
||||||
font-size: 8px;
|
|
||||||
font-weight: 600;
|
|
||||||
color: rgba(255,255,255,0.25);
|
|
||||||
background: rgba(0,0,0,0.3);
|
|
||||||
padding: 1px 4px;
|
|
||||||
border-radius: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.title-bar {
|
|
||||||
position: absolute;
|
|
||||||
top: 0; left: 0; right: 0;
|
|
||||||
z-index: 30;
|
|
||||||
text-align: center;
|
|
||||||
padding: 5px;
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
.title-bar h1 {
|
|
||||||
font-family: 'Cinzel', serif;
|
|
||||||
font-size: 12px;
|
|
||||||
font-weight: 600;
|
|
||||||
letter-spacing: 5px;
|
|
||||||
text-transform: uppercase;
|
|
||||||
color: rgba(255,255,255,0.15);
|
|
||||||
}
|
|
||||||
|
|
||||||
.corner-accent {
|
|
||||||
position: absolute;
|
|
||||||
width: 24px; height: 24px;
|
|
||||||
z-index: 6;
|
|
||||||
pointer-events: none;
|
|
||||||
opacity: 0.12;
|
|
||||||
}
|
|
||||||
.corner-accent.tl { top: 6px; left: 6px; border-top: 2px solid var(--p1); border-left: 2px solid var(--p1); }
|
|
||||||
.corner-accent.tr { top: 6px; right: 6px; border-top: 2px solid var(--p2); border-right: 2px solid var(--p2); }
|
|
||||||
.corner-accent.bl { bottom: 6px; left: 6px; border-bottom: 2px solid var(--p3); border-left: 2px solid var(--p3); }
|
|
||||||
.corner-accent.br { bottom: 6px; right: 6px; border-bottom: 2px solid var(--p4); border-right: 2px solid var(--p4); }
|
|
||||||
|
|
||||||
@media (max-width: 768px) {
|
|
||||||
:root { --card-w: 42px; --card-h: 58px; }
|
|
||||||
.player-label { font-size: 7px; letter-spacing: 1px; padding: 2px 6px; }
|
|
||||||
.life-counter { font-size: 15px; }
|
|
||||||
.life-counter button { width: 18px; height: 18px; font-size: 11px; }
|
|
||||||
.zone-name { font-size: 6px; }
|
|
||||||
.zone-icon { font-size: 12px; }
|
|
||||||
.center-emblem { width: 46px; height: 46px; }
|
|
||||||
.center-emblem svg { width: 26px; height: 26px; }
|
|
||||||
.battle-zone-label { font-size: 8px; letter-spacing: 2px; }
|
|
||||||
.zone-row { gap: 3px; }
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div class="table-surface">
|
|
||||||
<div class="title-bar"><h1>Commander Battlefield</h1></div>
|
|
||||||
<div class="corner-accent tl"></div>
|
|
||||||
<div class="corner-accent tr"></div>
|
|
||||||
<div class="corner-accent bl"></div>
|
|
||||||
<div class="corner-accent br"></div>
|
|
||||||
<div class="divider-h"></div>
|
|
||||||
<div class="divider-v"></div>
|
|
||||||
|
|
||||||
<div class="center-emblem">
|
|
||||||
<svg viewBox="0 0 100 100" fill="none">
|
|
||||||
<circle cx="50" cy="50" r="42" stroke="rgba(255,255,255,0.2)" stroke-width="1.5"/>
|
|
||||||
<circle cx="50" cy="50" r="28" stroke="rgba(255,255,255,0.1)" stroke-width="1"/>
|
|
||||||
<polygon points="50,15 58.5,37 82,37 63,51 69,74 50,60 31,74 37,51 18,37 41.5,37"
|
|
||||||
fill="rgba(255,255,255,0.1)" stroke="rgba(255,255,255,0.2)" stroke-width="0.8"/>
|
|
||||||
</svg>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="battlefield">
|
|
||||||
|
|
||||||
<!-- PLAYER 1: Top-Left (Crimson) -->
|
|
||||||
<div class="player-area p1">
|
|
||||||
<span class="player-label">Player 1</span>
|
|
||||||
<div class="life-counter">
|
|
||||||
<button onclick="changeLife(1,-1)" aria-label="Decrease life">−</button>
|
|
||||||
<span id="life1">40</span>
|
|
||||||
<button onclick="changeLife(1,1)" aria-label="Increase life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="zone-row">
|
|
||||||
<div class="zone library" data-tooltip="Library"><span class="card-count">99</span><span class="zone-icon">📚</span><span class="zone-name">Library</span></div>
|
|
||||||
<div class="zone exile" data-tooltip="Exile"><span class="zone-icon">✦</span><span class="zone-name">Exile</span></div>
|
|
||||||
<div class="zone graveyard" data-tooltip="Graveyard"><span class="zone-icon">💀</span><span class="zone-name">Grave</span></div>
|
|
||||||
<div class="zone hand" data-tooltip="Hand"><span class="zone-icon">🃏</span><span class="zone-name">Hand</span></div>
|
|
||||||
</div>
|
|
||||||
<div class="commander-row">
|
|
||||||
<div class="zone commander" data-tooltip="Commander Zone"><span class="zone-icon">👑</span><span class="zone-name">Commander</span></div>
|
|
||||||
</div>
|
|
||||||
<div class="battle-zone"><div class="battle-zone-inner"></div><span class="battle-zone-label">Battlefield</span></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- PLAYER 2: Top-Right (Sapphire) -->
|
|
||||||
<div class="player-area p2">
|
|
||||||
<span class="player-label">Player 2</span>
|
|
||||||
<div class="life-counter">
|
|
||||||
<button onclick="changeLife(2,-1)" aria-label="Decrease life">−</button>
|
|
||||||
<span id="life2">40</span>
|
|
||||||
<button onclick="changeLife(2,1)" aria-label="Increase life">+</button>
|
|
||||||
</div>
|
|
||||||
<div class="zone-row">
|
|
||||||
<div class="zone library" data-tooltip="Library"><span class="card-count">99</span><span class="zone-icon">📚</span><span class="zone-name">Library</span></div>
|
|
||||||
<div class="zone exile" data-tooltip="Exile"><span class="zone-icon">✦</span><span class="zone-name">Exile</span></div>
|
|
||||||
<div class="zone graveyard" data-tooltip="Graveyard"><span class="zone-icon">💀</span><span class="zone-name">Grave</span></div>
|
|
||||||
<div class="zone hand" data-tooltip="Hand"><span class="zone-icon">🃏</span><span class="zone-name">Hand</span></div>
|
|
||||||
</div>
|
|
||||||
<div class="commander-row">
|
|
||||||
<div class="zone commander" data-tooltip="Commander Zone"><span class="zone-icon">👑</span><span class="zone-name">Commander</span></div>
|
|
||||||
</div>
|
|
||||||
<div class="battle-zone"><div class="battle-zone-inner"></div><span class="battle-zone-label">Battlefield</span></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- PLAYER 3: Bottom-Left (Emerald) -->
|
|
||||||
<div class="player-area p3">
|
|
||||||
<div class="battle-zone"><div class="battle-zone-inner"></div><span class="battle-zone-label">Battlefield</span></div>
|
|
||||||
<div class="commander-row">
|
|
||||||
<div class="zone commander" data-tooltip="Commander Zone"><span class="zone-icon">👑</span><span class="zone-name">Commander</span></div>
|
|
||||||
</div>
|
|
||||||
<div class="zone-row">
|
|
||||||
<div class="zone library" data-tooltip="Library"><span class="card-count">99</span><span class="zone-icon">📚</span><span class="zone-name">Library</span></div>
|
|
||||||
<div class="zone exile" data-tooltip="Exile"><span class="zone-icon">✦</span><span class="zone-name">Exile</span></div>
|
|
||||||
<div class="zone graveyard" data-tooltip="Graveyard"><span class="zone-icon">💀</span><span class="zone-name">Grave</span></div>
|
|
||||||
<div class="zone hand" data-tooltip="Hand"><span class="zone-icon">🃏</span><span class="zone-name">Hand</span></div>
|
|
||||||
</div>
|
|
||||||
<span class="player-label">Player 3</span>
|
|
||||||
<div class="life-counter">
|
|
||||||
<button onclick="changeLife(3,-1)" aria-label="Decrease life">−</button>
|
|
||||||
<span id="life3">40</span>
|
|
||||||
<button onclick="changeLife(3,1)" aria-label="Increase life">+</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- PLAYER 4: Bottom-Right (Gold) -->
|
|
||||||
<div class="player-area p4">
|
|
||||||
<div class="battle-zone"><div class="battle-zone-inner"></div><span class="battle-zone-label">Battlefield</span></div>
|
|
||||||
<div class="commander-row">
|
|
||||||
<div class="zone commander" data-tooltip="Commander Zone"><span class="zone-icon">👑</span><span class="zone-name">Commander</span></div>
|
|
||||||
</div>
|
|
||||||
<div class="zone-row">
|
|
||||||
<div class="zone library" data-tooltip="Library"><span class="card-count">99</span><span class="zone-icon">📚</span><span class="zone-name">Library</span></div>
|
|
||||||
<div class="zone exile" data-tooltip="Exile"><span class="zone-icon">✦</span><span class="zone-name">Exile</span></div>
|
|
||||||
<div class="zone graveyard" data-tooltip="Graveyard"><span class="zone-icon">💀</span><span class="zone-name">Grave</span></div>
|
|
||||||
<div class="zone hand" data-tooltip="Hand"><span class="zone-icon">🃏</span><span class="zone-name">Hand</span></div>
|
|
||||||
</div>
|
|
||||||
<span class="player-label">Player 4</span>
|
|
||||||
<div class="life-counter">
|
|
||||||
<button onclick="changeLife(4,-1)" aria-label="Decrease life">−</button>
|
|
||||||
<span id="life4">40</span>
|
|
||||||
<button onclick="changeLife(4,1)" aria-label="Increase life">+</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
const lives = { 1: 40, 2: 40, 3: 40, 4: 40 };
|
|
||||||
function changeLife(p, d) {
|
|
||||||
lives[p] = Math.max(0, lives[p] + d);
|
|
||||||
const el = document.getElementById('life' + p);
|
|
||||||
el.textContent = lives[p];
|
|
||||||
el.style.transform = 'scale(1.25)';
|
|
||||||
el.style.transition = 'transform 0.15s ease';
|
|
||||||
setTimeout(() => { el.style.transform = 'scale(1)'; }, 150);
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -32,7 +32,7 @@ def _fake_build_result():
|
|||||||
class TestDeckBuilderAdapter:
|
class TestDeckBuilderAdapter:
|
||||||
def test_init_defaults(self):
|
def test_init_defaults(self):
|
||||||
adapter = DeckBuilderAdapter()
|
adapter = DeckBuilderAdapter()
|
||||||
assert adapter.model == "gpt-oss:20b"
|
assert adapter.model == "claude-opus-4-20250514"
|
||||||
assert adapter.db_conn_factory is None
|
assert adapter.db_conn_factory is None
|
||||||
|
|
||||||
@patch("src.commander_ai_lab.deck_builder.adapter.build_deck")
|
@patch("src.commander_ai_lab.deck_builder.adapter.build_deck")
|
||||||
@@ -45,7 +45,7 @@ class TestDeckBuilderAdapter:
|
|||||||
assert result["commander"]["name"] == "Atraxa"
|
assert result["commander"]["name"] == "Atraxa"
|
||||||
assert "cards" in result
|
assert "cards" in result
|
||||||
assert "stats" in result
|
assert "stats" in result
|
||||||
assert result["model"] == "gpt-oss:20b"
|
assert result["model"] == "claude-opus-4-20250514"
|
||||||
assert result["build_time_seconds"] == 1.23
|
assert result["build_time_seconds"] == 1.23
|
||||||
assert "scryfall" in result["reasoning"]["sources"]
|
assert "scryfall" in result["reasoning"]["sources"]
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,597 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-01a993df.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-02ce51bc.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-050da93a.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-07325fb5.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-0791d58e.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-083a55e5.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-09f6ab6b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-0d39aa71.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-0dadbf18.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-12802330.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-186b4c2d.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-19f0c0c3.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-1a0a0840.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-1d001e22.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-1df7d6d8.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-220e4942.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-26f83797.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-291a225a.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-309b1008.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-31557831.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-37d57d02.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-391e4632.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-39ed0be8.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-3bb88339.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-3c98f83b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-3de43fb6.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-3e0639f7.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-3ea0bfb2.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-3f0dc5c4.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-3f2f9f04.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-425a6728.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-46517693.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-471701d5.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-4c709ff1.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-4f5be65a.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-501f6ef9.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-592d3d5a.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-644cc2f1.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-64759781.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-6575cbf7.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-682fa8d1.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-68352096.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-6c982613.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-6cdb4b9b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-6dee8fdf.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-7018972e.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-7088479f.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-74d0f773.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-76303df4.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-76c602e0.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-76edf7a3.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-77551df3.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-77ee4077.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-786ff020.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-7d4ad84d.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-7f74c9f3.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-7fbdfb25.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-7fd1bee8.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-804d0dfe.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-805211db.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-812a5124.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-826a32fb.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-878d0278.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-8dd40e1b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-8fa57d21.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-8fe4d31c.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-91a3ac36.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-92418b72.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-96fb5377.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-9b0adb9d.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-9e3ca48f.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-9ee053b3.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-9ef2148f.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-a153e04e.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-a2676323.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-a374549a.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-a7feffa1.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-ab868d56.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-ab8d55a5.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-aee3677b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-af39ff37.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-b101fe7b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-bab7b2bf.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-bb3e9d83.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-bb9772ca.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-bf0f9371.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-c21b782b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-c3e05ffc.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-c83d2002.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-c879aeab.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-c9d696cc.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-ccc989e9.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-d09dea45.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-d262f1dc.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-d3d97788.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-d68ecb62.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-d79c87bc.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-dbf46854.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-dd5b74d0.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-e1e6c928.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-e1ff699f.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-e738a169.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-efd579c0.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-f06c0e00.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-f144217e.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-f1d0e453.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-f2bdceeb.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-f79c8493.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-fa8a574b.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-fae68ec0.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-sim-fee36e36.jsonl",
|
||||||
|
"weight": 1,
|
||||||
|
"source": "sim"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-03449551-bbc.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-04380aa6-f62.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-23db752f-de5.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-310629be-8f7.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-63b719a8-1b0.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-7f50242f-986.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-a2f9bbae-f50.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "D:\\ForgeCommander\\commander-ai-lab\\results\\ml-decisions-ds-bd331a14-a89.jsonl",
|
||||||
|
"weight": 4,
|
||||||
|
"source": "ds"
|
||||||
|
}
|
||||||
|
]
|
||||||
+34
-19
@@ -93,24 +93,24 @@ const AiLab = (() => {
|
|||||||
</div>
|
</div>
|
||||||
<p class="lab-subtitle">Headless 3-AI batch simulation — stress-test your decks at scale</p>
|
<p class="lab-subtitle">Headless 3-AI batch simulation — stress-test your decks at scale</p>
|
||||||
|
|
||||||
<!-- v3: Import Bar -->
|
<!-- v3: Import Bar (collapsed by default) -->
|
||||||
<div class="lab-import-bar">
|
<details class="lab-import-bar">
|
||||||
<div class="lab-import-bar-label">Import Deck</div>
|
<summary class="lab-import-bar-label" style="cursor:pointer;">\u2B07 Advanced Import (URL / Text / Precon)</summary>
|
||||||
<div class="lab-import-actions">
|
<div class="lab-import-actions">
|
||||||
<button class="lab-import-action-btn" onclick="AiLab.showUrlImport()" title="Import from Archidekt or EDHREC URL">
|
<button class="lab-import-action-btn" onclick="AiLab.showUrlImport()" title="Import from Archidekt or EDHREC URL">
|
||||||
<span class="import-icon">🔗</span> URL Import
|
<span class="import-icon">\uD83D\uDD17</span> URL Import
|
||||||
</button>
|
</button>
|
||||||
<button class="lab-import-action-btn" onclick="AiLab.showMetaPicker()" title="Browse EDHREC average decks for popular commanders">
|
<button class="lab-import-action-btn" onclick="AiLab.showMetaPicker()" title="Browse EDHREC average decks for popular commanders">
|
||||||
<span class="import-icon">📊</span> Commander Meta
|
<span class="import-icon">\uD83D\uDCCA</span> Commander Meta
|
||||||
</button>
|
</button>
|
||||||
<button class="lab-import-action-btn" onclick="AiLab.showTextImport()" title="Paste a card list directly">
|
<button class="lab-import-action-btn" onclick="AiLab.showTextImport()" title="Paste a card list directly">
|
||||||
<span class="import-icon">📋</span> Text Import
|
<span class="import-icon">\uD83D\uDCCB</span> Text Import
|
||||||
</button>
|
</button>
|
||||||
<button class="lab-import-action-btn lab-precon-btn" onclick="AiLab.showPreconPicker()" title="Browse and load official Commander precon decks">
|
<button class="lab-import-action-btn lab-precon-btn" onclick="AiLab.showPreconPicker()" title="Browse and load official Commander precon decks">
|
||||||
<span class="import-icon">🎴</span> Precon Decks
|
<span class="import-icon">\uD83C\uDCB4</span> Precon Decks
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</details>
|
||||||
|
|
||||||
<!-- v3: URL Import Panel (hidden by default) -->
|
<!-- v3: URL Import Panel (hidden by default) -->
|
||||||
<div class="lab-url-import" id="lab-url-import">
|
<div class="lab-url-import" id="lab-url-import">
|
||||||
@@ -403,7 +403,9 @@ const AiLab = (() => {
|
|||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
decks = (data.decks || []).map(d => ({
|
decks = (data.decks || []).map(d => ({
|
||||||
name: d.name,
|
name: d.name,
|
||||||
commander: d.commander || d.name.replace(/_/g, ' '),
|
commander: d.commander || d.commander_name || d.name.replace(/_/g, ' '),
|
||||||
|
source: d.source || 'forge',
|
||||||
|
deck_id: d.deck_id || null,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -427,9 +429,10 @@ const AiLab = (() => {
|
|||||||
const prev = select.value;
|
const prev = select.value;
|
||||||
select.innerHTML = '<option value="">— Select Deck —</option>';
|
select.innerHTML = '<option value="">— Select Deck —</option>';
|
||||||
|
|
||||||
// Group: imported decks first
|
// Split into three groups
|
||||||
const imported = availableDecks.filter(d => importedDeckMeta[d.name]);
|
const imported = availableDecks.filter(d => importedDeckMeta[d.name]);
|
||||||
const local = availableDecks.filter(d => !importedDeckMeta[d.name]);
|
const myDecks = availableDecks.filter(d => d.source === 'deckbuilder' && !importedDeckMeta[d.name]);
|
||||||
|
const forgeDecks = availableDecks.filter(d => d.source !== 'deckbuilder' && !importedDeckMeta[d.name]);
|
||||||
|
|
||||||
if (imported.length > 0) {
|
if (imported.length > 0) {
|
||||||
const group = document.createElement('optgroup');
|
const group = document.createElement('optgroup');
|
||||||
@@ -446,10 +449,23 @@ const AiLab = (() => {
|
|||||||
select.appendChild(group);
|
select.appendChild(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (local.length > 0) {
|
if (myDecks.length > 0) {
|
||||||
|
const group = document.createElement('optgroup');
|
||||||
|
group.label = '\uD83D\uDDC2 My Decks (Deck Builder)';
|
||||||
|
myDecks.forEach(deck => {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = deck.name;
|
||||||
|
opt.textContent = deck.name + (deck.commander ? ` \u2014 ${deck.commander}` : '');
|
||||||
|
opt.dataset.commander = deck.commander || '';
|
||||||
|
group.appendChild(opt);
|
||||||
|
});
|
||||||
|
select.appendChild(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (forgeDecks.length > 0) {
|
||||||
const group = document.createElement('optgroup');
|
const group = document.createElement('optgroup');
|
||||||
group.label = 'Local Decks';
|
group.label = 'Local Decks';
|
||||||
local.forEach(deck => {
|
forgeDecks.forEach(deck => {
|
||||||
const opt = document.createElement('option');
|
const opt = document.createElement('option');
|
||||||
opt.value = deck.name;
|
opt.value = deck.name;
|
||||||
opt.textContent = deck.name;
|
opt.textContent = deck.name;
|
||||||
@@ -1381,9 +1397,8 @@ const AiLab = (() => {
|
|||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const logEl = document.getElementById('lab-log-content');
|
const logEl = document.getElementById('lab-log-content');
|
||||||
if (logEl && data.lines) {
|
if (logEl && data.lines) {
|
||||||
// Show last N lines
|
// Show all lines from the backend (up to 200)
|
||||||
const tail = data.lines.slice(-30);
|
logEl.textContent = data.lines.join('\n');
|
||||||
logEl.textContent = tail.join('\n');
|
|
||||||
logEl.scrollTop = logEl.scrollHeight;
|
logEl.scrollTop = logEl.scrollHeight;
|
||||||
}
|
}
|
||||||
} catch { /* ignore */ }
|
} catch { /* ignore */ }
|
||||||
@@ -1724,7 +1739,7 @@ const AiLab = (() => {
|
|||||||
|
|
||||||
// Y-axis labels (0-100%)
|
// Y-axis labels (0-100%)
|
||||||
ctx.fillStyle = '#8b90a0';
|
ctx.fillStyle = '#8b90a0';
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
ctx.textAlign = 'right';
|
ctx.textAlign = 'right';
|
||||||
for (let pct = 0; pct <= 100; pct += 25) {
|
for (let pct = 0; pct <= 100; pct += 25) {
|
||||||
const y = canvas.height - padding.bottom - (pct / 100) * h;
|
const y = canvas.height - padding.bottom - (pct / 100) * h;
|
||||||
@@ -1770,7 +1785,7 @@ const AiLab = (() => {
|
|||||||
const ly = canvas.height - padding.bottom - (lastPt.winRate / 100) * h;
|
const ly = canvas.height - padding.bottom - (lastPt.winRate / 100) * h;
|
||||||
ctx.fillStyle = colors[di % colors.length];
|
ctx.fillStyle = colors[di % colors.length];
|
||||||
ctx.textAlign = 'left';
|
ctx.textAlign = 'left';
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
ctx.fillText(data.deckName, lx + 6, ly + 4);
|
ctx.fillText(data.deckName, lx + 6, ly + 4);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab — Deck Coach</title>
|
<title>Commander AI Lab — Deck Coach</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="coach.css" />
|
<link rel="stylesheet" href="coach.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
+6
-6
@@ -810,7 +810,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.coll-td-price {
|
.coll-td-price {
|
||||||
font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
|
font-family: var(--font-mono);
|
||||||
text-align: right;
|
text-align: right;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
@@ -840,7 +840,7 @@
|
|||||||
.coll-td-pt {
|
.coll-td-pt {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
|
font-family: var(--font-mono);
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -861,7 +861,7 @@
|
|||||||
|
|
||||||
.coll-td-edhrec {
|
.coll-td-edhrec {
|
||||||
text-align: right;
|
text-align: right;
|
||||||
font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
|
font-family: var(--font-mono);
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: var(--text-secondary);
|
color: var(--text-secondary);
|
||||||
}
|
}
|
||||||
@@ -1872,7 +1872,7 @@
|
|||||||
border: 1px solid var(--border);
|
border: 1px solid var(--border);
|
||||||
border-radius: var(--radius-md);
|
border-radius: var(--radius-md);
|
||||||
padding: 12px;
|
padding: 12px;
|
||||||
font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
|
font-family: var(--font-mono);
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
resize: vertical;
|
resize: vertical;
|
||||||
@@ -1953,7 +1953,7 @@
|
|||||||
color: var(--text);
|
color: var(--text);
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
white-space: pre;
|
white-space: pre;
|
||||||
font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
|
font-family: var(--font-mono);
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1989,7 +1989,7 @@
|
|||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: var(--text);
|
color: var(--text);
|
||||||
font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
|
font-family: var(--font-mono);
|
||||||
}
|
}
|
||||||
|
|
||||||
.coll-mapping-select {
|
.coll-mapping-select {
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab — Collection</title>
|
<title>Commander AI Lab — Collection</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="collection.css" />
|
<link rel="stylesheet" href="collection.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab — Deck Builder</title>
|
<title>Commander AI Lab — Deck Builder</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="deckbuilder.css" />
|
<link rel="stylesheet" href="deckbuilder.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -1267,7 +1267,7 @@
|
|||||||
border: 1px solid var(--border);
|
border: 1px solid var(--border);
|
||||||
border-radius: var(--radius-sm);
|
border-radius: var(--radius-sm);
|
||||||
padding: var(--sp-3);
|
padding: var(--sp-3);
|
||||||
font-family: 'Courier New', monospace;
|
font-family: var(--font-mono);
|
||||||
font-size: 0.8rem;
|
font-size: 0.8rem;
|
||||||
scrollbar-width: thin;
|
scrollbar-width: thin;
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-8
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab — Auto Deck Generator V3</title>
|
<title>Commander AI Lab — Auto Deck Generator V3</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="deckgenerator.css" />
|
<link rel="stylesheet" href="deckgenerator.css" />
|
||||||
</head>
|
</head>
|
||||||
@@ -142,11 +142,10 @@
|
|||||||
<div class="dg-model-row">
|
<div class="dg-model-row">
|
||||||
<label class="dg-label">
|
<label class="dg-label">
|
||||||
AI Model
|
AI Model
|
||||||
<span class="tip-icon" data-tip="The local LLM used to generate the deck list. Larger models produce better card selections but are slower. Requires Ollama running locally with the selected model pulled.">?</span>
|
<span class="tip-icon" data-tip="Deck generation is powered by Claude Opus 4, Anthropic's most capable model.">?</span>
|
||||||
</label>
|
</label>
|
||||||
<select class="dg-select" id="dg-model">
|
<input type="hidden" id="dg-model" value="claude-opus-4-20250514" />
|
||||||
<option value="gpt-oss:20b" selected>gpt-oss:20b (Local Ollama)</option>
|
<span style="font-size:0.8rem; color:var(--text-muted, #888);">claude-opus-4-20250514</span>
|
||||||
</select>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -196,7 +195,7 @@
|
|||||||
<!-- Loading State -->
|
<!-- Loading State -->
|
||||||
<div class="dg-loading" id="dg-loading" style="display:none">
|
<div class="dg-loading" id="dg-loading" style="display:none">
|
||||||
<div class="dg-spinner"></div>
|
<div class="dg-spinner"></div>
|
||||||
<div class="dg-loading-text" id="dg-loading-text">Generating deck via Local AI...</div>
|
<div class="dg-loading-text" id="dg-loading-text">Generating deck via Claude Opus 4...</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="dg-progress-wrap" id="dg-progress-wrap" style="display:none">
|
<div class="dg-progress-wrap" id="dg-progress-wrap" style="display:none">
|
||||||
<div class="dg-progress-bar">
|
<div class="dg-progress-bar">
|
||||||
@@ -208,8 +207,23 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Results placeholder — rest generated dynamically by deckgenerator.js -->
|
<!-- Results -->
|
||||||
<div class="dg-results" id="dg-results" style="display:none"></div>
|
<div class="dg-results" id="dg-results" style="display:none">
|
||||||
|
<div class="dg-results-header">
|
||||||
|
<div id="dg-strategy-bar"></div>
|
||||||
|
<div id="dg-stats-bar" class="dg-stats-bar"></div>
|
||||||
|
<div id="dg-sub-summary" style="display:none"></div>
|
||||||
|
<div class="dg-result-actions">
|
||||||
|
<button class="dg-btn dg-btn-secondary" id="dg-regenerate-btn">↻ Regenerate</button>
|
||||||
|
<button class="dg-btn dg-btn-primary" id="dg-save-btn">💾 Save to Deck Builder</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-csv">CSV</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-dck">Forge .dck</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-moxfield">Moxfield</button>
|
||||||
|
<button class="dg-btn dg-btn-ghost" id="dg-export-shopping">🛒 Shopping</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="dg-card-groups" class="dg-card-groups"></div>
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
|||||||
+6
-3
@@ -159,7 +159,7 @@ const DeckGenerator = (() => {
|
|||||||
if (!state.v3Ready) {
|
if (!state.v3Ready) {
|
||||||
// Warn but do not block — local/Ollama mode may still work
|
// Warn but do not block — local/Ollama mode may still work
|
||||||
console.warn('V3 status: not initialized. Error:', data.error || 'unknown');
|
console.warn('V3 status: not initialized. Error:', data.error || 'unknown');
|
||||||
toast('V3 generator not ready — check that the local AI model (gpt-oss:20b) is running via Ollama', 'warning');
|
toast('V3 generator not ready — check that the Anthropic API key is configured', 'warning');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('V3 status check failed:', e);
|
console.warn('V3 status check failed:', e);
|
||||||
@@ -386,7 +386,7 @@ const DeckGenerator = (() => {
|
|||||||
// ── Generate Deck ───────────────────────────────────────
|
// ── Generate Deck ───────────────────────────────────────
|
||||||
async function generateDeck() {
|
async function generateDeck() {
|
||||||
clearLog();
|
clearLog();
|
||||||
logMessage('Connecting to local AI model (gpt-oss:20b)...', 'info');
|
logMessage('Connecting to Claude AI...', 'info');
|
||||||
if (!state.commander) { toast('Select a commander first', 'error'); return; }
|
if (!state.commander) { toast('Select a commander first', 'error'); return; }
|
||||||
// Non-blocking warning only — local Ollama mode does not require v3Ready
|
// Non-blocking warning only — local Ollama mode does not require v3Ready
|
||||||
if (!state.v3Ready) {
|
if (!state.v3Ready) {
|
||||||
@@ -435,7 +435,7 @@ const DeckGenerator = (() => {
|
|||||||
state.isLoading = loading;
|
state.isLoading = loading;
|
||||||
$('dg-empty-state').style.display = 'none';
|
$('dg-empty-state').style.display = 'none';
|
||||||
$('dg-loading').style.display = loading ? 'flex' : 'none';
|
$('dg-loading').style.display = loading ? 'flex' : 'none';
|
||||||
$('dg-results').style.display = loading ? 'none' : 'none';
|
$('dg-results').style.display = loading ? 'none' : 'block';
|
||||||
$('dg-preview-btn').disabled = loading || !state.commander;
|
$('dg-preview-btn').disabled = loading || !state.commander;
|
||||||
if (text) $('dg-loading-text').textContent = text;
|
if (text) $('dg-loading-text').textContent = text;
|
||||||
}
|
}
|
||||||
@@ -454,6 +454,7 @@ const DeckGenerator = (() => {
|
|||||||
|
|
||||||
function renderStrategyBar(result) {
|
function renderStrategyBar(result) {
|
||||||
const bar = $('dg-strategy-bar');
|
const bar = $('dg-strategy-bar');
|
||||||
|
if (!bar) return;
|
||||||
const bracket = result.bracket || {};
|
const bracket = result.bracket || {};
|
||||||
const archetype = result.archetype || '';
|
const archetype = result.archetype || '';
|
||||||
const strategy = result.strategy_summary || '';
|
const strategy = result.strategy_summary || '';
|
||||||
@@ -472,6 +473,7 @@ const DeckGenerator = (() => {
|
|||||||
function renderStatsBar(result) {
|
function renderStatsBar(result) {
|
||||||
const stats = result.stats || {};
|
const stats = result.stats || {};
|
||||||
const container = $('dg-stats-bar');
|
const container = $('dg-stats-bar');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
const chips = [
|
const chips = [
|
||||||
chipHtml('Total', stats.total_cards || 0, ''),
|
chipHtml('Total', stats.total_cards || 0, ''),
|
||||||
@@ -507,6 +509,7 @@ const DeckGenerator = (() => {
|
|||||||
|
|
||||||
function renderCardGroups(result) {
|
function renderCardGroups(result) {
|
||||||
const container = $('dg-card-groups');
|
const container = $('dg-card-groups');
|
||||||
|
if (!container) return;
|
||||||
const cards = result.cards || [];
|
const cards = result.cards || [];
|
||||||
const commander = result.commander;
|
const commander = result.commander;
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab — Distillation Loop</title>
|
<title>Commander AI Lab — Distillation Loop</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="distillation.css" />
|
<link rel="stylesheet" href="distillation.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
+7
-7
@@ -362,7 +362,7 @@ function renderWinRateChart(generations) {
|
|||||||
// Y axis labels
|
// Y axis labels
|
||||||
const val = maxY - (maxY - minY) * (i / ySteps);
|
const val = maxY - (maxY - minY) * (i / ySteps);
|
||||||
ctx.fillStyle = '#8b90a0';
|
ctx.fillStyle = '#8b90a0';
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
ctx.textAlign = 'right';
|
ctx.textAlign = 'right';
|
||||||
ctx.fillText(val.toFixed(0) + '%', pad.left - 8, y + 4);
|
ctx.fillText(val.toFixed(0) + '%', pad.left - 8, y + 4);
|
||||||
}
|
}
|
||||||
@@ -437,7 +437,7 @@ function renderWinRateChart(generations) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Legend
|
// Legend
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
const legendX = pad.left + 10;
|
const legendX = pad.left + 10;
|
||||||
const legendY = pad.top + 15;
|
const legendY = pad.top + 15;
|
||||||
|
|
||||||
@@ -505,7 +505,7 @@ function renderCompositionChart(generations) {
|
|||||||
|
|
||||||
const val = maxDataset - (maxDataset * i / ySteps);
|
const val = maxDataset - (maxDataset * i / ySteps);
|
||||||
ctx.fillStyle = '#8b90a0';
|
ctx.fillStyle = '#8b90a0';
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
ctx.textAlign = 'right';
|
ctx.textAlign = 'right';
|
||||||
ctx.fillText(formatNumber(val), pad.left - 8, y + 4);
|
ctx.fillText(formatNumber(val), pad.left - 8, y + 4);
|
||||||
}
|
}
|
||||||
@@ -535,13 +535,13 @@ function renderCompositionChart(generations) {
|
|||||||
|
|
||||||
// X label
|
// X label
|
||||||
ctx.fillStyle = '#8b90a0';
|
ctx.fillStyle = '#8b90a0';
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
ctx.textAlign = 'center';
|
ctx.textAlign = 'center';
|
||||||
ctx.fillText('G' + g.generation, x + barWidth / 2, h - 8);
|
ctx.fillText('G' + g.generation, x + barWidth / 2, h - 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legend
|
// Legend
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
const legendX = pad.left + 10;
|
const legendX = pad.left + 10;
|
||||||
const legendY = pad.top + 12;
|
const legendY = pad.top + 12;
|
||||||
|
|
||||||
@@ -807,7 +807,7 @@ function renderEloChart(entries) {
|
|||||||
|
|
||||||
const val = maxElo - (eloRange * i / ySteps);
|
const val = maxElo - (eloRange * i / ySteps);
|
||||||
ctx.fillStyle = '#8b90a0';
|
ctx.fillStyle = '#8b90a0';
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
ctx.textAlign = 'right';
|
ctx.textAlign = 'right';
|
||||||
ctx.fillText(Math.round(val).toString(), pad.left - 8, y + 4);
|
ctx.fillText(Math.round(val).toString(), pad.left - 8, y + 4);
|
||||||
}
|
}
|
||||||
@@ -861,7 +861,7 @@ function renderEloChart(entries) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Legend
|
// Legend
|
||||||
ctx.font = '11px Inter, sans-serif';
|
ctx.font = '11px Barlow, sans-serif';
|
||||||
let legendX = pad.left + 10;
|
let legendX = pad.left + 10;
|
||||||
const legendY = pad.top + 12;
|
const legendY = pad.top + 12;
|
||||||
modelNames.forEach(name => {
|
modelNames.forEach(name => {
|
||||||
|
|||||||
+49
-24
@@ -61,7 +61,8 @@
|
|||||||
--duration: 180ms;
|
--duration: 180ms;
|
||||||
|
|
||||||
/* Typography */
|
/* Typography */
|
||||||
--font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
--font: 'Barlow', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
--font-display: 'Barlow Condensed', 'Barlow', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
--font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
|
--font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
|
||||||
|
|
||||||
/* Spacing scale */
|
/* Spacing scale */
|
||||||
@@ -94,13 +95,15 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
h1, h2, h3, h4, h5, h6 {
|
h1, h2, h3, h4, h5, h6 {
|
||||||
|
font-family: var(--font-display);
|
||||||
color: #fff;
|
color: #fff;
|
||||||
font-weight: 600;
|
font-weight: 700;
|
||||||
line-height: 1.25;
|
line-height: 1.15;
|
||||||
|
letter-spacing: 0.01em;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 { font-size: 24px; }
|
h1 { font-size: 26px; }
|
||||||
h2 { font-size: 18px; }
|
h2 { font-size: 19px; }
|
||||||
h3 { font-size: 16px; }
|
h3 { font-size: 16px; }
|
||||||
|
|
||||||
a { color: var(--accent); text-decoration: none; transition: color var(--duration) var(--ease); }
|
a { color: var(--accent); text-decoration: none; transition: color var(--duration) var(--ease); }
|
||||||
@@ -147,10 +150,12 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: var(--sp-3);
|
gap: var(--sp-3);
|
||||||
font-size: 15px;
|
font-family: var(--font-display);
|
||||||
font-weight: 700;
|
font-size: 17px;
|
||||||
|
font-weight: 800;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
letter-spacing: -0.01em;
|
letter-spacing: 0.02em;
|
||||||
|
text-transform: uppercase;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
margin-right: var(--sp-8);
|
margin-right: var(--sp-8);
|
||||||
@@ -177,8 +182,11 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
gap: var(--sp-2);
|
gap: var(--sp-2);
|
||||||
padding: 6px 12px;
|
padding: 6px 12px;
|
||||||
font-size: 13px;
|
font-family: var(--font-display);
|
||||||
font-weight: 500;
|
font-size: 14px;
|
||||||
|
font-weight: 600;
|
||||||
|
letter-spacing: 0.03em;
|
||||||
|
text-transform: uppercase;
|
||||||
color: var(--text-secondary);
|
color: var(--text-secondary);
|
||||||
border-radius: var(--radius-sm);
|
border-radius: var(--radius-sm);
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
@@ -219,9 +227,11 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
}
|
}
|
||||||
|
|
||||||
.page-header h1 {
|
.page-header h1 {
|
||||||
font-size: 28px;
|
font-family: var(--font-display);
|
||||||
font-weight: 700;
|
font-size: 32px;
|
||||||
letter-spacing: -0.02em;
|
font-weight: 800;
|
||||||
|
letter-spacing: 0.005em;
|
||||||
|
text-transform: uppercase;
|
||||||
margin-bottom: var(--sp-2);
|
margin-bottom: var(--sp-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,13 +262,17 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
}
|
}
|
||||||
|
|
||||||
.panel-header h2 {
|
.panel-header h2 {
|
||||||
font-size: 16px;
|
font-family: var(--font-display);
|
||||||
font-weight: 600;
|
font-size: 17px;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: 0.01em;
|
||||||
|
text-transform: uppercase;
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel-header .badge {
|
.panel-header .badge {
|
||||||
|
font-family: var(--font-display);
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
font-weight: 600;
|
font-weight: 700;
|
||||||
padding: 3px 10px;
|
padding: 3px 10px;
|
||||||
border-radius: var(--radius-sm);
|
border-radius: var(--radius-sm);
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
@@ -288,8 +302,9 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
}
|
}
|
||||||
|
|
||||||
.stat-label {
|
.stat-label {
|
||||||
|
font-family: var(--font-display);
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
font-weight: 500;
|
font-weight: 600;
|
||||||
color: var(--text-secondary);
|
color: var(--text-secondary);
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
letter-spacing: 0.06em;
|
letter-spacing: 0.06em;
|
||||||
@@ -297,8 +312,9 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
}
|
}
|
||||||
|
|
||||||
.stat-value {
|
.stat-value {
|
||||||
font-size: 28px;
|
font-family: var(--font-display);
|
||||||
font-weight: 700;
|
font-size: 30px;
|
||||||
|
font-weight: 800;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
line-height: 1.1;
|
line-height: 1.1;
|
||||||
font-variant-numeric: tabular-nums;
|
font-variant-numeric: tabular-nums;
|
||||||
@@ -317,8 +333,11 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
gap: var(--sp-2);
|
gap: var(--sp-2);
|
||||||
padding: 8px 18px;
|
padding: 8px 18px;
|
||||||
font-size: 13px;
|
font-family: var(--font-display);
|
||||||
font-weight: 600;
|
font-size: 14px;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: 0.02em;
|
||||||
|
text-transform: uppercase;
|
||||||
border-radius: var(--radius-sm);
|
border-radius: var(--radius-sm);
|
||||||
border: none;
|
border: none;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
@@ -392,6 +411,7 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
}
|
}
|
||||||
|
|
||||||
.form-label {
|
.form-label {
|
||||||
|
font-family: var(--font-display);
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: var(--text-secondary);
|
color: var(--text-secondary);
|
||||||
@@ -510,8 +530,9 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 6px;
|
gap: 6px;
|
||||||
|
font-family: var(--font-display);
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
font-weight: 600;
|
font-weight: 700;
|
||||||
padding: 3px 10px;
|
padding: 3px 10px;
|
||||||
border-radius: var(--radius-sm);
|
border-radius: var(--radius-sm);
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
@@ -579,8 +600,9 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
.data-table thead th {
|
.data-table thead th {
|
||||||
text-align: left;
|
text-align: left;
|
||||||
padding: var(--sp-3) var(--sp-4);
|
padding: var(--sp-3) var(--sp-4);
|
||||||
|
font-family: var(--font-display);
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
font-weight: 600;
|
font-weight: 700;
|
||||||
color: var(--text-secondary);
|
color: var(--text-secondary);
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
letter-spacing: 0.06em;
|
letter-spacing: 0.06em;
|
||||||
@@ -607,8 +629,11 @@ table { border-collapse: collapse; width: 100%; }
|
|||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
padding: 2px 8px;
|
padding: 2px 8px;
|
||||||
|
font-family: var(--font-display);
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
font-weight: 600;
|
font-weight: 700;
|
||||||
|
letter-spacing: 0.02em;
|
||||||
|
text-transform: uppercase;
|
||||||
border-radius: var(--radius-sm);
|
border-radius: var(--radius-sm);
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab</title>
|
<title>Commander AI Lab</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="ai-lab.css?v=4" />
|
<link rel="stylesheet" href="ai-lab.css?v=4" />
|
||||||
<style>
|
<style>
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab — Simulator</title>
|
<title>Commander AI Lab — Simulator</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="simulator.css" />
|
<link rel="stylesheet" href="simulator.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
+58
-15
@@ -39,16 +39,41 @@
|
|||||||
// ── Init: load decks ──
|
// ── Init: load decks ──
|
||||||
async function loadDecks() {
|
async function loadDecks() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/decks');
|
const res = await fetch('/api/lab/decks');
|
||||||
if (!res.ok) return;
|
if (!res.ok) return;
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const decks = data.decks || data || [];
|
const decks = data.decks || [];
|
||||||
decks.forEach(d => {
|
|
||||||
const opt = document.createElement('option');
|
// Split into groups by source
|
||||||
opt.value = d.id;
|
const dbDecks = decks.filter(d => d.source === 'deckbuilder');
|
||||||
opt.textContent = d.name + (d.commander ? ' (' + d.commander + ')' : '');
|
const forgeDecks = decks.filter(d => d.source === 'forge');
|
||||||
deckSelect.appendChild(opt);
|
|
||||||
});
|
// My Decks — from Deck Builder (stored in SQLite)
|
||||||
|
if (dbDecks.length > 0) {
|
||||||
|
const group = document.createElement('optgroup');
|
||||||
|
group.label = '\u{1F0CF} My Decks (Deck Builder)';
|
||||||
|
dbDecks.forEach(d => {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = 'db:' + d.deck_id;
|
||||||
|
opt.textContent = d.name + (d.commander ? ' (' + d.commander + ')' : '');
|
||||||
|
group.appendChild(opt);
|
||||||
|
});
|
||||||
|
deckSelect.appendChild(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forge .dck files (user-decks/, precon-decks/, sample-decks/)
|
||||||
|
if (forgeDecks.length > 0) {
|
||||||
|
const group = document.createElement('optgroup');
|
||||||
|
group.label = '\u{1F4C1} Deck Files (.dck)';
|
||||||
|
forgeDecks.forEach(d => {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = d.name;
|
||||||
|
opt.textContent = d.name.replace(/_/g, ' ');
|
||||||
|
group.appendChild(opt);
|
||||||
|
});
|
||||||
|
deckSelect.appendChild(group);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Could not load decks:', e);
|
console.warn('Could not load decks:', e);
|
||||||
}
|
}
|
||||||
@@ -165,18 +190,17 @@
|
|||||||
|
|
||||||
// ── Run from deck ID ──
|
// ── Run from deck ID ──
|
||||||
runBtn.addEventListener('click', async () => {
|
runBtn.addEventListener('click', async () => {
|
||||||
const deckId = parseInt(deckSelect.value);
|
const val = deckSelect.value;
|
||||||
if (!deckId) return;
|
if (!val) return;
|
||||||
|
|
||||||
const numGames = parseInt(numGamesInput.value) || 10;
|
const numGames = parseInt(numGamesInput.value) || 10;
|
||||||
const recordLogs = recordLogsChk.checked;
|
const recordLogs = recordLogsChk.checked;
|
||||||
const useDeepSeek = oppSelect.value === 'deepseek';
|
const useDeepSeek = oppSelect.value === 'deepseek';
|
||||||
|
|
||||||
// If DeepSeek selected but not connected, try to connect first
|
|
||||||
if (useDeepSeek && !dsConnected) {
|
if (useDeepSeek && !dsConnected) {
|
||||||
await connectDeepSeek();
|
await connectDeepSeek();
|
||||||
if (!dsConnected) {
|
if (!dsConnected) {
|
||||||
alert('Could not connect to DeepSeek. Make sure LM Studio is running at ' + dsApiBase.value);
|
alert('Could not connect to DeepSeek. Make sure Ollama is running at ' + dsApiBase.value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await applyDeepSeekConfig();
|
await applyDeepSeekConfig();
|
||||||
@@ -189,16 +213,35 @@
|
|||||||
hideResults();
|
hideResults();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const endpoint = useDeepSeek ? '/api/sim/run-deepseek' : '/api/sim/run-from-deck';
|
let endpoint, body;
|
||||||
|
|
||||||
|
if (val.startsWith('db:')) {
|
||||||
|
// Deck Builder deck — auto-export to user-decks/ first
|
||||||
|
const deckId = parseInt(val.replace('db:', ''));
|
||||||
|
|
||||||
|
const exportRes = await fetch(`/api/decks/${deckId}/export-to-sim`, { method: 'POST' });
|
||||||
|
const exportData = await exportRes.json();
|
||||||
|
if (!exportData.success) throw new Error('Export failed: ' + (exportData.detail || 'unknown'));
|
||||||
|
|
||||||
|
endpoint = useDeepSeek ? '/api/sim/run-deepseek' : '/api/sim/run-from-deck';
|
||||||
|
body = { deckId, numGames, recordLogs };
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// File-based deck (already a .dck)
|
||||||
|
endpoint = useDeepSeek ? '/api/sim/run-deepseek' : '/api/sim/run-from-deck';
|
||||||
|
body = { deckName: val, numGames, recordLogs };
|
||||||
|
}
|
||||||
|
|
||||||
const res = await fetch(endpoint, {
|
const res = await fetch(endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ deckId, numGames, recordLogs }),
|
body: JSON.stringify(body),
|
||||||
});
|
});
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.error) throw new Error(data.error);
|
if (data.error) throw new Error(data.error);
|
||||||
currentSimId = data.simId;
|
currentSimId = data.simId;
|
||||||
startPolling();
|
startPolling();
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
alert('Error: ' + e.message);
|
alert('Error: ' + e.message);
|
||||||
resetRunBtn();
|
resetRunBtn();
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<title>Commander AI Lab — ML Training</title>
|
<title>Commander AI Lab — ML Training</title>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@400;600;700;800&family=Barlow:wght@400;500;600&display=swap" rel="stylesheet" />
|
||||||
<link rel="stylesheet" href="global.css" />
|
<link rel="stylesheet" href="global.css" />
|
||||||
<link rel="stylesheet" href="training.css" />
|
<link rel="stylesheet" href="training.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
Whitespace-only changes.
Reference in new issue
Block a user