# tssbot backend

Rust backend API service for Toothless' TSS Bot.

It reads two SQLite databases:

- `TSS_BATTLES_DB` for `tss_battles.db` (matches, players, and the `match_logs` table)
- `TSS_TEAMS_DB` for `tss_teams.db`
- `TSS_TOURNAMENTS_DB` for `tss_tournaments.db`

If any of these are unset, the backend first looks under `STORAGE_VOL_PATH`
before falling back to the current working directory.
- `BACKEND_HOST` bind host, default `127.0.0.1`
- `BACKEND_ALLOWED_ORIGINS` comma-separated browser origins allowed by CORS

Both paths can be absolute or relative to the repo root when run through the root scripts/PM2.

## Vehicle translation + icons

At startup the backend loads two cache files (built by the bots, shared under
`STORAGE/CACHE`) into memory to translate `player_games_hist.vehicle_internal`
(the WT cdk) into localized vehicle names and icon filenames for the game scoreboard:

- `VEHICLE_TRANSLATIONS_JSON` → `vehicle_translations.json` (`{ cdk: { en, ru, ... } }`).
  The `/api/tss/games/:id` endpoint honors `?lang=` (default `en`), falling back
  `lang → en → raw cdk`.
- `VEHICLE_DATA_CACHE_JSON` → `vehicle_data_cache_all.json` (`[cdk, name, icon, tags]`),
  used for icon filenames (fallback `<cdk>.png`).

The icon PNGs themselves are served statically by the frontend at `/vehicle-icons`
(deploy-time copy/symlink of `SHARED/ICONS/VEHICLES`).

It currently exposes:

- `GET /health`
- `GET /api/tss/leaderboard/teams?limit=100`
- `GET /api/tss/leaderboard/players?limit=100`
- `GET /api/tss/games/recent?limit=50`
- `GET /api/tss/games/:session_id?lang=en` — scoreboard (teams, players, vehicle lineup)
- `GET /api/tss/games/:session_id/logs` — chat + battle logs from `match_logs`
- `GET /api/tss/teams/resolve?name=...`
- `GET /api/tss/teams/search?q=...&limit=10`
- `GET /api/tss/teams/:team`
- `GET /api/tss/teams/:team/history`
- `GET /api/tss/teams/:team/games`
- `GET /api/tss/player/:uid`

## Local development

```sh
npm run dev:backend
```

The backend listens on <http://127.0.0.1:6000> by default. Override with `BACKEND_PORT` and `BACKEND_HOST`.

## Production build

```sh
npm run build:backend
```
