siegebreak/web/README.md
caelum0x 6c7cea7592 feat: build out backend services, web client, and game roster
Backend (services/): make the app testable and fill real gaps.
- Extract buildApp() factory; index.ts owns listen()/matcher tick
- Real Apple/Google identity verification via jose JWKS (iss/aud/sub)
- Guest -> OAuth account linking (POST /auth/link), rebind in place
- POST /matches/:id/complete: record match + participants, update
  leaderboard, idempotent, guarded by server-to-server secret
- Extract pure matchmaking pairing (computePairings); fix ticket
  deckId loss on match; map ZodError -> 400; @fastify/cors
- vitest suite (40 tests), initial Prisma migration, prisma schema path

Web (web/): new Vite + React + TS client.
- Typed API client with one-shot 401 refresh/retry, localStorage auth
- Login (guest), deck builder (slot-0 hero rule), queue polling,
  leaderboard; 11 tests, clean build

Game (Assets/, Unity C#): champion-select layout now fits the roster
size; add Golem and Bandit champions.

Docs: root DEVELOPMENT.md tying game + services + web together.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-25 18:15:12 +03:00

89 lines
3.6 KiB
Markdown

# FP Clash — Web Client
Companion web client for **Siegebreak / FP Clash**, a Clash-Royale-style game.
Built with Vite + React + TypeScript. It talks to the game backend in
`../services/`.
## Quick start
```bash
cd web
npm install
npm run dev # http://localhost:5173
```
Set the backend origin (optional — defaults to `http://localhost:8080`):
```bash
cp .env.example .env
# edit VITE_API_URL if your backend runs elsewhere
```
## Scripts
| Command | What it does |
| ----------------- | ----------------------------------------- |
| `npm run dev` | Start the Vite dev server |
| `npm run build` | Type-check (`tsc --noEmit`) + `vite build`|
| `npm run preview` | Serve the production build locally |
| `npm test` | Run the Vitest suite once |
| `npm run typecheck` | Type-check only |
## Backend connection
The app reads the API origin from `import.meta.env.VITE_API_URL`
(default `http://localhost:8080`) and sends **full-origin** requests to it.
This is the cleaner choice for this repo: the app and backend stay decoupled,
and there is no proxy config to keep in sync.
> **CORS:** the backend in `../services/` ships `@fastify/cors`. It reflects any
> origin by default, or restricts to a comma-separated `CORS_ORIGINS` list (set
> `CORS_ORIGINS=http://localhost:5173` in `services/.env` to pin it to this dev
> server). So cross-origin dev calls (`localhost:5173` → `localhost:8080`) work
> out of the box. A same-origin `server.proxy` block in `vite.config.ts` remains
> available as an alternative.
### Endpoint map
| UI area | Backend endpoint(s) |
| ------------------ | -------------------------------------------------------------- |
| Login (guest) | `POST /auth/guest`, `POST /auth/refresh` |
| Collection / Decks | `GET /cards`, `GET /players/me/decks`, `POST /players/me/decks`|
| Nav wallet | `GET /economy/wallet` |
| Queue | `POST /matchmaking/queue`, `GET /matchmaking/status`, `POST /matchmaking/cancel` |
| Leaderboard | `GET /leaderboard/:seasonId` |
| Analytics | `POST /analytics/event` (fired on login, best-effort) |
## Auth model
- Guest login generates and persists a stable random `deviceId` in
`localStorage`, then calls `POST /auth/guest`.
- Access + refresh tokens are stored in `localStorage`.
- The API client attaches `Authorization: Bearer <accessToken>` to protected
calls. On a `401` it performs **one** refresh via the refresh token and
retries the request; if the refresh fails it clears the session (forcing a
return to Login).
## Project layout
```
src/
api/client.ts Typed fetch wrapper + 401 refresh/retry + endpoint fns
auth/ Session store (localStorage) + React context/hook
components/ NavBar, WalletBadge, ProtectedRoute, CardTile
deck/deckSlots.ts Pure deck-slot helpers (slot-0 = hero rule)
pages/ Login, Collection (deck builder), Queue, Leaderboard
types.ts Domain types mirroring the backend contract
```
## Tests
`npm test` covers:
- **api client** — bearer token attachment, single refresh + retry on 401,
session clearing when refresh fails (mocked `fetch`).
- **deck builder** — slot 0 rejects non-hero cards and blocks saving.
- **deck slots** — pure helper behaviour (immutability, completion, validation).
- **Login** — renders and calls `login()` on click.
```