194 lines
8.0 KiB
Markdown
194 lines
8.0 KiB
Markdown
# Protokoll — Replay-Format v1 & Server-API
|
||
|
||
| Feld | Wert |
|
||
|---|---|
|
||
| Version | 1 (Dokument 0.1) |
|
||
| Status | Entwurf für MVP-2 (Server) |
|
||
| Bezug | [ADR 0002](ADR/0002-determinismus-fixed-point.md) · [GDD](GDD.md) |
|
||
|
||
---
|
||
|
||
## 1. Grundprinzip
|
||
|
||
Der Client ist **nie** autoritativ. Er erzeugt einen **Command-Log**; Server und Client führen
|
||
denselben `sim`-Crate aus. Ein Match ist gültig, wenn beide denselben `final_hash` errechnen.
|
||
|
||
```
|
||
Client: seed + content_hash + commands ──► sim ──► final_hash / Replay
|
||
Server: seed + content_hash + commands ──► sim ──► final_hash (Vergleich)
|
||
```
|
||
|
||
Alle Zeitangaben sind **Ticks** (60 Hz), keine Millisekunden — Zeit muss deterministisch sein.
|
||
|
||
## 2. Replay v1
|
||
|
||
```json
|
||
{
|
||
"v": 1,
|
||
"sim_version": "1.0.0",
|
||
"content_hash": "b3:9f2c...",
|
||
"seed": 1234567890123,
|
||
"mode": "ranked",
|
||
"players": [
|
||
{ "tank": "vanguard", "loadout": [1, 4, 7] },
|
||
{ "tank": "longshot", "loadout": [2, 3, 7] }
|
||
],
|
||
"commands": [
|
||
{ "t": 12, "p": 0, "c": { "kind": "fire", "angle": 45250, "power": 61000, "w": 4 } },
|
||
{ "t": 980, "p": 1, "c": { "kind": "fire", "angle": 132000, "power": 58000, "w": 2 } }
|
||
],
|
||
"final_hash": 12345678901234567890,
|
||
"duration_ticks": 4120
|
||
}
|
||
```
|
||
|
||
| Feld | Typ | Bedeutung |
|
||
|---|---|---|
|
||
| `v` | u8 | Formatversion (steigt bei Strukturänderung) |
|
||
| `sim_version` | SemVer | Sim-Version; Änderung an Formeln/Balance erhöht sie |
|
||
| `content_hash` | String | `b3:<hex>` über kanonisches Content-JSON (Waffen, Panzer, Muster) |
|
||
| `seed` | u64 | Match-Seed (Server-erzeugt in Ranked) |
|
||
| `mode` | Enum | `ranked` \| `async` \| `daily` \| `hotseat` \| `campaign` |
|
||
| `players[]` | Array | `tank` (ID), `loadout` (max. 3 Waffen-IDs) |
|
||
| `commands[]` | Array | geordnet, aufsteigend in `t` |
|
||
| `final_hash` | u64 | FNV-1a-Kette über alle Ticks (ADR 0002) – **im HTTP-Protokoll als 16-stelliger Hex-String** |
|
||
| `duration_ticks` | u32 | Match-Länge; Cap 18000 (= 5 Minuten) |
|
||
|
||
**Größenbudget:** typisch 2–5 KB, hartes Limit 256 KB (Server lehnt sonst mit `too_long` ab).
|
||
|
||
## 3. Commands
|
||
|
||
| `kind` | Felder | Wirkung |
|
||
|---|---|---|
|
||
| `fire` | `angle` (u16, 1/1000°), `power` (i32 Q16.16), `w` (u8 Waffen-ID) | Schuss abfeuern, beendet den Zug |
|
||
| `move` | `dir` (−1/0/+1), `ticks` (u16) | Panzer fährt (begrenzter Tank pro Zug) |
|
||
| `shield` | – | Schild aktivieren, beendet den Zug |
|
||
| `ultimate` | – | Panzer-Ultimate, beendet den Zug |
|
||
| `end_turn` | – | Zug abgeben (nur wenn Timer abläuft) |
|
||
|
||
**Validierungsregeln (Client und Server identisch, in `sim::apply`):**
|
||
- `angle` ∈ [0, 359999]; `power` ∈ [0.05, 1.00] in Q16.16
|
||
- `w` muss im Loadout des Spielers liegen und Munition > 0 haben
|
||
- Command nur akzeptiert, wenn `t` ≥ aktueller Tick und Spieler am Zug ist
|
||
- Pro Zug genau **ein** `fire`/`shield`/`ultimate`/`end_turn`
|
||
- Verstöße ⇒ `SimError` (Client zeigt "ungültig", Server antwortet `illegal_command`)
|
||
|
||
## 4. Server-API
|
||
|
||
Basis: `https://api.ironarc.example/v1` · Transport: HTTPS, JSON (UTF-8) · Auth: `Authorization: Bearer <token>`.
|
||
|
||
| Methode | Pfad | Zweck |
|
||
|---|---|---|
|
||
| `GET` | `/health` | Liveness (`{status:"ok",sim_version:"1.0.0"}`) |
|
||
| `GET` | `/content/{version}` | Content-Pinning; 404 bei unbekannter Version |
|
||
| `POST` | `/matches` | Match anlegen (Seed serverseitig) |
|
||
| `GET` | `/matches/{id}` | Match-Zustand + Züge |
|
||
| `POST` | `/matches/{id}/turn` | Zug einreichen (Live-PvP) |
|
||
| `POST` | `/matches/{id}/validate` | **Replay validieren** (async/offline) |
|
||
| `GET` | `/leaderboard/{season}` | Rangliste (Top 100 + eigener Rang) |
|
||
|
||
### 4.1 Match anlegen
|
||
|
||
```http
|
||
POST /v1/matches
|
||
{ "mode": "ranked", "content_hash": "b3:9f2c…", "sim_version": "1.0.0",
|
||
"players": [{"tank":"vanguard","loadout":[1,4,7]},{"tank":"longshot","loadout":[2,3,7]}] }
|
||
```
|
||
|
||
```json
|
||
201 Created
|
||
{ "match_id": "m_8f3a…", "seed": 1234567890123, "turn": 0, "state_hash": 9182736455544332211 }
|
||
```
|
||
|
||
### 4.2 Zug einreichen
|
||
|
||
```http
|
||
POST /v1/matches/m_8f3a…/turn
|
||
{ "expect_hash": 9182736455544332211, "command": { "kind":"fire", "angle":45250, "power":61000, "w":4 } }
|
||
```
|
||
|
||
```json
|
||
200 OK
|
||
{ "accepted": true, "turn": 1, "state_hash": 11223344556677889900, "events": [{"type":"explosion","x":812,"y":430}] }
|
||
```
|
||
|
||
`expect_hash` ist optimistische Nebenläufigkeitskontrolle: passt er nicht, antwortet der Server `409 state_conflict`
|
||
(Client synchronisiert neu). So können keine Züge "überschrieben" werden.
|
||
|
||
### 4.3 Replay validieren
|
||
|
||
```http
|
||
POST /v1/matches/m_8f3a…/validate
|
||
{ "replay": { …Replay v1 aus Abschnitt 2… } }
|
||
```
|
||
|
||
| Status | Body | Bedeutung |
|
||
|---|---|---|
|
||
| `200` | `{ "valid": true, "final_hash": 12…, "duration_ms": 41, "rewards": {"scrap":120,"xp":35} }` | Replay akzeptiert, Belohnung freigegeben |
|
||
| `409` | `{ "valid": false, "reason": "hash_mismatch", "expected": 12…, "actual": 99… }` | Manipulation oder Sim-Drift |
|
||
| `409` | `{ "valid": false, "reason": "sim_version_mismatch" }` | Client zu alt/neu → Update-Hinweis |
|
||
| `409` | `{ "valid": false, "reason": "content_mismatch" }` | Content-Version passt nicht |
|
||
| `422` | `{ "valid": false, "reason": "illegal_command", "at": 12 }` | Command außerhalb der Regeln |
|
||
| `413` | `{ "valid": false, "reason": "too_long" }` | > 256 KB oder > 18000 Ticks |
|
||
|
||
## 4.4 Hash-Kodierung im HTTP-Protokoll
|
||
|
||
Alle 64-Bit-Hashes (`state_hash`, `expect_hash`, `final_hash`) werden als **16-stellige
|
||
Hex-Strings** übertragen, z. B. `"0000000000000063"`.
|
||
|
||
**Grund:** JSON-Zahlen sind in JavaScript/Dart auf 2^53 exakt; u64-Hashes überschreiten das
|
||
regelmäßig. Als Zahl übertragen würde stillschweigend Präzision verloren gehen und jeder
|
||
Vergleich fehlschlagen. Rust parst mit `u64::from_str_radix(s, 16)`, Dart mit
|
||
`BigInt.parse(s, radix: 16).toSigned(64)`.
|
||
|
||
## 5. Fehlerformat
|
||
|
||
```json
|
||
{ "error": { "code": "rate_limited", "message": "Too many requests", "retry_after_s": 12 } }
|
||
```
|
||
|
||
Codes: `unauthorized` · `forbidden` · `not_found` · `state_conflict` · `rate_limited` · `payload_too_large` · `internal`.
|
||
|
||
## 6. Auth & Rate-Limits
|
||
|
||
| Aspekt | MVP-2 | Später |
|
||
|---|---|---|
|
||
| Identität | Device-ID + HMAC-signierter Token (Server stellt aus) | Firebase Auth / Play Games Services + Play Integrity |
|
||
| Token-Laufzeit | 30 Tage, rotierbar | kurzlebig + Refresh |
|
||
| Rate-Limit | 60 req/min pro Token (`tower-governor`) | + Burst-Schutz pro IP/ASN |
|
||
| Validate-Limit | 120 Replays/Stunde pro Token | dynamisch via Remote Config |
|
||
|
||
## 7. Offline-Queue & Belohnungen
|
||
|
||
1. Match wird **offline** gespielt und lokal als Replay gespeichert (`pending`).
|
||
2. Bei Netz: `POST /validate` (max. 5 Replays pro Batch, exponentielles Backoff).
|
||
3. **Belohnungen erst nach `valid:true`** (Scrap/XP/Trophäen).
|
||
4. UI zeigt „wird geprüft" — kein stiller Verlust; Retry bis 7 Tage.
|
||
5. Nach 3 fehlgeschlagenen Validierungen (`hash_mismatch`) wird der Account für Ranked gesperrt
|
||
und ein Review-Flag gesetzt (kein Auto-Ban).
|
||
|
||
## 8. Anti-Cheat-Regeln (Server)
|
||
|
||
| Regel | Verhalten |
|
||
|---|---|
|
||
| `sim_version` ≠ Server-Version | `409 sim_version_mismatch` |
|
||
| `content_hash` ≠ aktiv | `409 content_mismatch` |
|
||
| Command-Felder außerhalb Wertebereichen | `422 illegal_command` |
|
||
| Replay-Länge/Zeit-Cap überschritten | `413 too_long` |
|
||
| `final_hash` ≠ nachgerechnet | `409 hash_mismatch` + Flag |
|
||
| Unmögliche Siegquote/Trophäen-Sprünge | Soft-Flag + Review, nie Auto-Ban |
|
||
| Rate-Limit | `429 rate_limited` |
|
||
|
||
## 9. Versionierung
|
||
|
||
- `v` (Replay-Format): Erhöhung nur bei Strukturänderung; Server akzeptiert `v:1` und antwortet bei `v:2` mit `upgrade_required`.
|
||
- `sim_version` (SemVer): Jede Balance-/Formeländerung erhöht `MINOR`; Server hält die letzten **3** Sim-Versionen parallel kompiliert.
|
||
- `content_hash`: Bei LiveOps-Änderung (Waffenwerte) neuer Hash; alte Replays bleiben mit alter Content-Version validierbar.
|
||
|
||
## 10. Offene Punkte
|
||
|
||
1. Hosting: Cloud Run (min-instances 0/1) vs. Fly.io — Entscheidung in M4.
|
||
2. Persistenz: SQLite für MVP, Postgres ab > 5k DAU (Migration ohne API-Bruch).
|
||
3. Live-PvP-Transport: HTTP-Polling (MVP) vs. WebSocket/SSE (später).
|
||
4. Push-Benachrichtigung „Gegner hat gespielt" via FCM (M5, optional).
|