Skip to content

five-on-it / apps/server/src/level-files

apps/server/src/level-files ​

The levels a game server serves by their hash. A client that asks for it (the web client, ?levels=hash on its connection: LEVELS_BY_HASH in @foi/protocol) is welcomed with a big level's hash and length instead of the level (welcomeLevel), fetches it at /levels/<hash>.json (serveLevelFile) and keeps it in its browser by that hash: the second time it plays that level it loads it from there, and a welcome no longer costs the server the level's JSON (7.7 MB on Riverbend) for every player who joins.

A level's JSON, its hash and its gzip are made once per level object (rooms on the same level share it) and forgotten with it; the server serves only the levels of its open rooms. The files are immutable (a level's hash names its bytes), cached for a year, and open to any page (a client served by Vite or from a file talks to the server across origins).

A room makes its level's file as it opens (GameServer's constructor), never on a join: a join is handled on the server's loop between two steps, and Riverbend's JSON and hash take 70 ms, which held every player's snapshots when the first client asked for the level by its hash (join.integration.test.ts). A welcome that carries the level splices the same JSON in (LevelFile.text) rather than making the level's JSON again.

Interfaces ​

Variables ​

Functions ​