- TypeScript 59.5%
- JavaScript 31.8%
- Go 4.5%
- CSS 3.5%
- HTML 0.6%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| admin | ||
| client-terminal | ||
| data | ||
| deploy | ||
| docs | ||
| public | ||
| src | ||
| tests | ||
| tools | ||
| .dockerignore | ||
| .gitignore | ||
| docker-compose.yml | ||
| Dockerfile | ||
| index.html | ||
| Makefile | ||
| package-lock.json | ||
| package.json | ||
| PATCHNOTES.md | ||
| README.md | ||
| tsconfig.json | ||
| tsconfig.server.json | ||
| vite.config.ts | ||
Battleground Tactics
Turn-based fantasy tactics on a grid for 2 to 4 armies (eight colors), played in a phone browser: against the AI, or online with friends through a small server. Version 1.3.0 (see PATCHNOTES.md).
It is two things in one repository:
- an engine for grid tactics games in the browser (
src/engine/): grid geometry, seeded randomness, data-driven content checked file by file, translations, sprite sheets, terrain autotiling, a canvas board sized for fingers, an interface skinned from pictures, audio, a versioned user database with migrations and backups, and the pieces of a dependency-free Node server (JSON, event streams, static files); - the game built on it (
src/game/,src/client/,src/server/): its rules, AI, content model, screens, map editor and developer tools, and its data (data/).
Plain TypeScript and Canvas in the browser, plain Node on the server: no framework, no runtime dependency. The internal
name, used by the npm package, the Docker image and the browser's save keys, is tbsg.
Documentation
| Read | For |
|---|---|
| docs/engine.md | the core engine features, and how a game is built on them |
| docs/architecture.md | the code's layers and modules, what runs when |
| docs/data.md | the data folder: units, terrain, rules, sprites, tiles, the skin, languages, maps, packs |
| docs/game.md | the game: battles, modes, online play, the map editor, the developer tools |
| docs/ai-gaps.md | game mechanics the AI ignores or plays only in part, to update later |
| client-terminal/README.md | the command-line client: online versus games in a terminal, with browser players (docker build -t tbsg-client client-terminal/) |
Run (Docker)
Production runs from deploy/docker-compose.yml, the file the CD pipeline uses. The dev deployment is the
docker-compose.yml at the root.
make test # the tests, before deploying
docker compose -f deploy/docker-compose.yml up -d --build # prod on host port 8080 (TBSG_PORT), or: make prod
docker compose -f deploy/docker-compose.yml -f deploy/traefik.yml up -d --build # prod behind Traefik on TBSG_HOST
docker compose up -d --build # dev on 8081 (TBSG_DEV_PORT), or: make dev
Settings for prod come from the pipeline's environment or deploy/.env (copy deploy/.env.example): TBSG_ADMIN_PASSWORD,
TBSG_PORT, TBSG_IMAGE (a pipeline that builds and pushes the image sets the tag, then runs pull and up -d --no-build),
and for deploy/traefik.yml TBSG_HOST plus the Traefik network, entry point and cert resolver. The prod compose project is
always tbsg and its state volume is always tbsg_tbsg-state, so a deploy from any checkout replaces the running server and
keeps its state.
Plain HTTP on that port; put your reverse proxy and TLS in front (the online games' event streams must not be buffered:
the server sends X-Accel-Buffering: no). Health check: GET /healthz. The server reads data/ once at start and
refuses a broken file, naming it; it serves public/ (pictures, sounds, sprite sheets) from disk. Changing data or art
doesn't recompile the code, and building the image runs no tests (make test does, see Develop). Mount the folders to skip the rebuild (restart for
data/, nothing for public/):
volumes:
- ./data:/app/data:ro
- ./public:/app/public:ro
Admin page. Set an admin password in deploy/.env (prod) or the root .env (dev), then (re)start:
echo 'TBSG_ADMIN_PASSWORD=<a long password>' >> deploy/.env && docker compose -f deploy/docker-compose.yml up -d
/admin/ then shows, once signed in with it, the state of the server (version, uptime, memory, data), the online games
being played (seats, players online, round and turn; the admin can close one), and every device that has opened the game
there: the name it plays online under, its visits and online games, and what it keeps saved on the device (campaign
progress, maps, challenges, Marathon, crowns, replays: counts only, sent by the page when it opens and when it is left),
and the maps and data packs players shared in Community (it can remove any of them; they are kept in STATE_DIR/community).
That list is kept in STATE_DIR (/app/state, a volume in both compose files). Without a password the admin page
stays shut. The sign-in is a cookie (HttpOnly, SameSite=Strict, Secure behind an HTTPS proxy) that lasts 12 hours;
five wrong passwords in a row make that client wait a minute.
A dev deployment, which can change the game's own data from the game, runs beside it: see docs/game.md › Developer tools. The developer tools need the same password.
Offline play. Players can install the game (Add to Home screen, or the browser's Install button) and play it with no
network: a service worker keeps a copy of the whole game (the page, the code, /api/data, everything in public/). It
needs HTTPS (or localhost), as every service worker does. Whenever there is a network the game is loaded from the server as
usual, and a new version (a new image, new data/ after a restart, a changed file of a mounted public/) is downloaded
in the background; a page still running the older copy offers to reload. Online games need the server. A dev deployment has no service worker.
See docs/game.md › Offline play.
Develop
No Node is needed on the host; everything runs in a container:
docker run --rm -it -v "$PWD":/app -w /app -p 5173:5173 node:24-alpine npm run dev # http://<host>:5173
docker run --rm -v "$PWD":/app -w /app node:24-alpine sh -c "npm run typecheck && npm test"
make test runs the whole suite in a container with only Docker (the Dockerfile's test stage: type checks, build,
then tests/ against data/): what CI runs. It fails when a test does; building the image never runs the tests.
| Script | Does |
|---|---|
npm run dev |
the Vite dev server, with online games, data packs and the developer tools |
npm run typecheck / npm run build |
type checks / the client into dist/client, the server into dist/node |
npm test |
compiles the server side and runs tests/ (node --test): rules, AI, data checks, saves, the architecture |
npm start |
the built server |
npm run i18n |
translation coverage, --sync <code>, --new <code> |
npm run art:units (:death, :fx, :icons, :terrain, :ui, :interludes, :worlds, :sound) |
the placeholder art |
npm run art:appicon |
the installed app's PNG icons (public/app/), drawn like public/icon.svg |
npm run solve |
the challenges' solver |
npm run duel |
the AI against itself (or --baseline an older build of it) on the versus maps: who wins and in how many rounds, how much it ferries, refuels, gathers, and what each kind of unit deals for its price (tools/ai-duel.mjs) |
Saves
The game keeps a player's maps, progress and settings in the browser (localStorage), stamped with the version that wrote
them (tbsg.version). A later version upgrades older saves, and older backup files, with its migrations:
docs/engine.md › The user database.