# ⚽ Reverse Engineering & Specification: FUT.GG & FUTBin Squad Builders

## 1. Executive Summary & Architectural Comparison

This specification documents the reverse-engineering analysis of **FUT.GG Squad Builder** (`https://www.fut.gg/squad-builder/`) and **FUTBin Squad Builder** (`https://www.futbin.com/27/squad-builder`), alongside the architecture of our native **BXC Autonomous AI Squad Builder** integrated into `@aphrody/fut`.

### Architectural Comparison Matrix

| Capability | FUT.GG (`/squad-builder/`) | FUTBin (`/27/squad-builder`) | BXC Native Engine (`@aphrody/fut/squad`) |
| :--- | :--- | :--- | :--- |
| **Frontend Framework** | TanStack Router / TanStack Start + Tailwind | Server-Side PHP / Blade + jQuery / Vanilla JS | Headless Bun / TypeScript Engine + CLI & SVG Render |
| **Chemistry Engine** | Modern EA Sports FC (33-point system) | Modern FC 33-pt + legacy chem links | Official EA FC 33-pt system + Icon/Hero/Manager boosts |
| **FC IQ Tactics** | Role, Role+, Role++, Tactical Presets | Limited / Legacy Workrates | Full FC IQ: 35 PlayStyles, Role++, Focus & Balance Analyzer |
| **Player Database** | REST v2 API + Client Cache | Internal API / Ajax autocomplete | Offline-first SQLite WAL (7,500+ crawled FC 27 cards) |
| **Pitch Coordinates** | Absolute % positioning (SVG / CSS) | CSS Grid / Absolute slot cards | Scalable (x, y) % coordinates with ASCII & SVG export |
| **Pricing Sources** | Console / PC live market sync | Community BIN bisections | Real-time pricing with rating-curve heuristic fallback |
| **WAF / Anti-Bot** | Cloudflare Standard (Passed via TLS Chrome 131) | Cloudflare Managed Turnstile (`challenges.cloudflare.com`) | `bxc` stealth profile + libcurl impersonate FFI |
| **AI Agent Usability** | Manual UI interaction | Manual UI interaction | Programmatic `SquadSolver` API & CLI `bxc fut squad` |

---

## 2. Reverse Engineering of FUT.GG Squad Builder

### 2.1 Full-Screen Capture & Visual Artifacts
- **HD Full-Screen Screenshot**: [`packages/fut/src/docs/screenshots/futgg_squad_builder_hd.png`](file:///home/ubuntu/bxc/packages/fut/src/docs/screenshots/futgg_squad_builder_hd.png)
- **Extracted DOM Metadata**: [`packages/fut/src/docs/screenshots/futgg_squad_builder_meta.json`](file:///home/ubuntu/bxc/packages/fut/src/docs/screenshots/futgg_squad_builder_meta.json)

### 2.2 Text Fields & Inputs
1. **Search Bar**:
   - Selector: `input[placeholder='Search everything…']`
   - Role: Global search triggering autocomplete modal for player card insertion.
2. **Squad Name Input**:
   - Inline editable heading for saving custom squad configurations.
3. **Formation Selector**:
   - Dropdown modal with 29 EA FC formations categorised by 3-at-the-back, 4-at-the-back, and 5-at-the-back.

### 2.3 Action Buttons & Controls
- **`Tactics`**: Opens the FC IQ tactical drawer (Build-up Style, Defensive Approach, Formation, Player Roles).
- **`Save`**: Saves the active squad JSON to the user's FUT.GG profile.
- **`Share`**: Generates a permalink `/squad-builder/:squadUuid` with OpenGraph meta tags.
- **`OVR` / `CHEM` / `GGR`**: Card overlay toggle switching between Overall Rating, Chemistry points (0-3), and FUT.GG Meta Rating.
- **Card Slot Action (`+`)**: Clickable plus sign opening the player picker modal with filters for position, version, price, club, league, and nation.

### 2.4 Pitch Slot Placements & 2D Coordinates
Slots are positioned relative to a standard pitch bounding box (1000 x 1350 px):
- **GK**: `{ x: 50%, y: 90% }`
- **Defenders (LB / CB / RB)**: `{ y: 72% - 76% }`, X evenly distributed from 12% to 88%.
- **Midfielders (CDM / CM / CAM)**: `{ y: 35% - 58% }`.
- **Attackers (LW / ST / RW)**: `{ y: 12% - 22% }`.

---

## 3. Reverse Engineering of FUTBin Squad Builder

### 3.1 Full-Screen Capture & Anti-Bot Behavior
- **HD Screenshot**: [`packages/fut/src/docs/screenshots/futbin_squad_builder_hd.png`](file:///home/ubuntu/bxc/packages/fut/src/docs/screenshots/futbin_squad_builder_hd.png)
- **Turnstile Challenge**: FUTBin enforces Cloudflare Managed Challenge (`cType: 'managed'`, Ray ID validation) on `https://www.futbin.com/25/squad-builder` and `/27/squad-builder`. Requests without authentic session cookies and valid browser canvas fingerprints receive `403 Just a moment...`.

### 3.2 UI Structure & Features
- **Legacy Formation Links**: Connects neighboring slots with visual green/orange/red link lines (historical FIFA feature maintained for visual clarity).
- **Price Counter**: Separate tallies for PlayStation/Xbox (Console) and PC (Origin).
- **Bench & Reserves**: Two expandable rows below the pitch for 7 substitutes and 5 reserve players.
- **Manager Slot**: Positioned to the bottom-left of the pitch with Country and League dropdowns.

---

## 4. BXC Native Squad Builder Engine Architecture

The native engine is located in [`packages/fut/src/squad/`](file:///home/ubuntu/bxc/packages/fut/src/squad/) and exported via `@aphrody/fut`:

```
packages/fut/src/squad/
├── types.ts          # Complete TypeScript types (Squad, Slots, Player, Manager, Stats)
├── formations.ts     # 29 EA FC Formations with slot coordinates & role defaults
├── chemistry.ts      # Exact EA Sports FC chemistry calculation engine (0-33)
├── roles.ts          # FC IQ Tactical Roles & Squad Balance Analyzer
├── squad.ts          # Core Squad instance class with mutations & serialization
├── solver.ts         # Autonomous AI Agent Solver querying local SQLite database
├── renderer.ts       # ASCII terminal renderer & high-res SVG graphic generator
└── index.ts          # Barrel export
```

### 4.1 Chemistry Calculation Algorithm
Individual chemistry is determined in 5 phases:
1. **Position Verification**: A player is in position if `slot.defaultPosition` equals `player.primaryPosition` or is contained in `player.alternativePositions`. Out-of-position players get **0 chemistry** and contribute 0 points to club, nation, and league counts.
2. **Threshold Contributions**:
   - **Clubs**: 2 players = +1, 4 players = +2, 7 players = +3.
   - **Nations**: 2 players = +1, 5 players = +2, 8 players = +3.
   - **Leagues**: 3 players = +1, 5 players = +2, 8 players = +3.
3. **Special Item Modifiers**:
   - **Icons**: Receive 3/3 chemistry automatically; contribute +2 to their nation and +1 to *every league* present in the starting 11.
   - **Heroes**: Receive 3/3 chemistry automatically; contribute +2 to their league and +1 to their nation.
   - **Manager**: Contributes +1 to their nation and +1 to their league.
4. **Summation**: Player Chemistry = $\min(3, \text{ClubPts} + \text{NationPts} + \text{LeaguePts})$.
5. **Team Chemistry**: Sum of all 11 starters ($\max = 33$).

### 4.2 FC IQ Tactical Balance Analyzer
Evaluates tactical synergy:
- Checks presence of an anchor midfielder (`Holding` / Sentinelle).
- Validates creative playmaking (`Playmaker` / Meneur de Jeu).
- Checks offensive width and prevents suicidal dual-attacking fullback setups.

---

## 5. Generated Méta Squads for the User

### Squad 1 : Budget ~100k — Formation 4-4-1-1 (Chimie 33/33)
- **Fichier SVG généré** : [`packages/fut/src/docs/screenshots/squad_100k_4411.svg`](file:///home/ubuntu/bxc/packages/fut/src/docs/screenshots/squad_100k_4411.svg)
- **Fichier JSON** : [`packages/fut/src/data/squad_100k_4411.json`](file:///home/ubuntu/bxc/packages/fut/src/data/squad_100k_4411.json)
- **Composition** :
  - **GK** : Thibaut Courtois (90) — Sweeper Keeper (Chem: 3)
  - **RB** : Míchel Salgado (87) — Fullback (Chem: 3)
  - **CB** : Rafael Márquez (88) — Defender (Chem: 3)
  - **CB** : Javier Mascherano (88) — Defender (Chem: 3)
  - **LB** : Marc Cucurella (86) — Fullback (Chem: 3)
  - **RM** : Lamine Yamal (90) — Inside Forward (Chem: 3)
  - **CM** : Aitana Bonmatí (90) — Holding (Chem: 3)
  - **CM** : Jude Bellingham (90) — Box-to-Box (Chem: 3)
  - **LM** : Claudia Pina (89) — Inside Forward (Chem: 3)
  - **CAM** : Ewa Pajor (89) — Shadow Striker (Chem: 3)
  - **ST** : Kylian Mbappé (91) — Advanced Forward (Chem: 3)
  - **Manager** : Carlo Ancelotti (Italy / LALIGA)
  - **Total Chimie** : **33/33** | **Note Générale** : **89**

### Squad 2 : Budget ~500k — Formation 4-4-2 (Chimie 33/33)
- **Fichier SVG généré** : [`packages/fut/src/docs/screenshots/squad_500k_442.svg`](file:///home/ubuntu/bxc/packages/fut/src/docs/screenshots/squad_500k_442.svg)
- **Fichier JSON** : [`packages/fut/src/data/squad_500k_442.json`](file:///home/ubuntu/bxc/packages/fut/src/data/squad_500k_442.json)
- **Composition** :
  - **GK** : Thibaut Courtois (90)
  - **RB** : Míchel Salgado (87)
  - **CB** : Rafael Márquez (88)
  - **CB** : Javier Mascherano (88)
  - **LB** : Marc Cucurella (86)
  - **RM** : Lamine Yamal (90)
  - **CM** : Aitana Bonmatí (90)
  - **CM** : Pedri (90)
  - **LM** : Claudia Pina (89)
  - **ST** : Ewa Pajor (89)
  - **ST** : Kylian Mbappé (91)
  - **Total Chimie** : **33/33** | **Note Générale** : **89**

### Squad 3 : Budget ~5M — Formation 4-2-1-3 (Chimie 33/33)
- **Fichier SVG généré** : [`packages/fut/src/docs/screenshots/squad_5000k_4213.svg`](file:///home/ubuntu/bxc/packages/fut/src/docs/screenshots/squad_5000k_4213.svg)
- **Fichier JSON** : [`packages/fut/src/data/squad_5000k_4213.json`](file:///home/ubuntu/bxc/packages/fut/src/data/squad_5000k_4213.json)
- **Composition** :
  - **GK** : Thibaut Courtois (90)
  - **RB** : Cafu (91) — Base Icon
  - **CB** : Rafael Márquez (88) — Hero
  - **CB** : Javier Mascherano (88) — Hero
  - **LB** : Roberto Carlos (90) — Base Icon
  - **CDM** : Pedri (90)
  - **CDM** : Rodri (90)
  - **CAM** : Aitana Bonmatí (90)
  - **RW** : Lamine Yamal (90)
  - **LW** : Claudia Pina (89)
  - **ST** : Kylian Mbappé (91)
  - **Total Chimie** : **33/33** | **Note Générale** : **90** | **ATT** : **90** | **MID** : **90** | **DEF** : **89**

---

## 6. CLI Usage & Programmatic Integration

```bash
# Lister les formations disponibles
bun run ./src/cli/index.ts fut squad formations

# Construire et optimiser automatiquement une équipe
bun run ./src/cli/index.ts fut squad build --formation 4-4-1-1 --budget 500000 --svg pitch.svg --json squad.json

# Rendre un squad existant en ASCII et exporter en SVG
bun run ./src/cli/index.ts fut squad render squad.json --svg render.svg
```
