five-on-it / apps/server/src/input-queue
apps/server/src/input-queue
A client's inputs on the server, waiting for their ticks. A client's input n is its tick n, and the server runs exactly one of a client's ticks with each of its own, from its first input on (game-server.ts); this is the queue between the two.
Inputs come in any order and more than once: over a transport that loses frames each one is sent in several frames (@foi/net-client's InputHistory), and frames are reordered. So an input is put in its place by its number (one older than the newest queued but not yet run is not late), and one already taken is skipped: a press counts once. When the input for the tick due has not come, the tick runs on a guess (the last input held without its one-shot presses and its trigger, a grenade's pin staying out) and is spent: the input meant for it, arriving later, is too late to steer, and only its presses count, on the next tick run. The queue remembers which of the last HANDLED_WINDOW ticks' inputs it took (run on their tick, their presses carried, or given up), so a duplicate of a late input never presses twice; an input older than that is ignored. When a burst overflows the queue the oldest input is given up, its presses carried.