claude-of-tanks / src/net
Purpose
Provide one authoritative match path for campaign, LAN, private, and ranked
play without importing Three.js rendering or DOM state.
Mental model & key files
protocol.tsowns the strict wire vocabulary, envelopes, sequence arithmetic,
and untrusted input validation.
lobby.tsowns the strict canonical room model: teams, capacity, readiness,
permissions, host migration, round state, and start policy.
lobbyRuntime.tsowns typed lobby transport sequencing, payload admission,
broadcast, and the lossless lobby-to-match channel handoff.
playerNames.ts,roomInvite.ts, andsignalEndpoint.tsown strict commander
identity, share-link parsing, and deployment-aware signaling URL policy.
signalingClient.tsowns strict request correlation, durable event polling,
reconnect backoff, room-seat resume, and RTC-session epoch rotation.
matchRuntime.tsowns fixed ticks, input ordering, snapshots, and client time.inputCadence.tsbounds replaceable input uploads independently from display
refresh while preserving immediate control edges.
browserInputRuntime.tscomposes finite-point aim, action edges, and cadence
behind explicit multiplayer intent; solo boot must not import it.
snapshot.tsowns quantization, visibility filtering, and interpolation.snapshotWireCodec.tsowns strict compact binary snapshot rows; protocol v2 uses
explicit snapshot acknowledgements, per-peer deltas, and periodic keyframes.
loopbackTransport.ts,channelTransport.ts, andwebrtcPeer.tsimplement
the same bounded transport contract.
localSession.tsproves solo play traverses the real host/client path.localTankPrediction.tsowns typed local input replay and presentation-only
correction. Its reconciliation stages authority seeding, input
acknowledgement/replay, error accounting, bounded presentation correction,
and terminal cleanup; it never owns combat or match results.
networkFramePump.tsowns browser host/client frame order, snapshot/event
application, input cadence, snapshot barriers, and network diagnostics.
networkBattleBarrier.tsowns first-authority and peer-ready predicates plus
the identity-bound idempotent READY retry lease.
networkRoomCoordinator.tsowns browser room subscriptions, garage/menu/chat
presentation, selection commands, readiness, and rematch admission.
networkLobbyPreloader.tscoalesces joined-room transfers, retries failed
optional chunks, and warms new roster builders. It reasserts canonical map
intent on waiting-room packets; the world coordinator owns in-flight/cache
deduplication and retry admission, not a permanent map-ID latch. Garage
browsing must preserve joined-room preparation, including while Not Ready.
networkBattleLaunchRuntime.tsowns private/LAN, retained-room rematch, and
ranked launch policy, including cold-loader presentation and terminal cleanup.
networkBattlePresentationRuntime.tsowns the shared cold-client path from
opaque loader through parallel module/world/transport acquisition, hidden
roster preparation, initial authority, warmup, atomic visual activation,
black-frame validation, reveal, and then all-peer readiness.
networkBattlePresentationAccess.tskeeps that deep multiplayer-only owner
out of Garage/solo boot and retries failed intent transfers.
networkBattleActivationRuntime.tsowns the atomic prepared-visual transfer
into live player or spectator presentation: world/HUD/FX/result reset, phase
publication, camera ownership, and Garage shutdown.
connectionRecovery.tsowns reconnect status and the single bounded failure
edge; transport replacement remains below it.
rankedServiceClient.tsowns service-scoped ladder identity and queue polling;
dedicatedClient.ts owns authenticated WebSocket handoff and reconnect.
privateRoomSession.tsowns typed lobby WebRTC composition and
rtcIceLease.ts owns expiring TURN generations;
privateMatchHandoff.ts is the strict lobby-to-match boundary: it
deterministically fills open team slots with bots
and releases those same channels to match authority.
browserBattleBridge.tsis presentation-only and must stay lazy from main.
Patterns and invariants
- Player/entity identity is independent from
specId. - Authority accepts controls only; it computes every gameplay result.
- Spotting filters data before serialization.
- Queues, extrapolation, catch-up, sequences, and payload sizes are bounded.
- WebRTC control/events stay reliable and ordered; replaceable snapshots and
live input use the unordered zero-retransmit state lane. Fire/consumable
edges repeat until acknowledged and authority deduplicates them. WebSocket
snapshots and input coalesce under backpressure so stale state cannot
consume control headroom.
- Initial RTC recovery replays pending SDP before creating a new ICE
generation. Duplicate descriptions must be idempotent; never overlap offers
merely because a fresh browser is slow.
- Local prediction replays the exact shared movement path. Reconciliation error
is presentation-only: horizontal hull motion, terrain support/tilt, and live
turret aim use separate bounded decay channels. Contacts may extend smoothing
but must never change authority, collision, or ballistic state. Keep the
collision adapter stable for the predictor lifetime; do not allocate a new
closure for every replayed fixed step.
- Modules remain Node-runnable with no DOM/WebGL dependency.
- Visual activation must remain one operation after roster/snapshot preparation
and warmup; do not publish battle phase or camera state piecemeal from
main.ts. Send READY only after the verified battlefield frame and awaited
loader fade. Authority still holds gameplay until every peer is ready and
its countdown expires; visible loading peers show WAITING FOR COMMANDERS.
- Failed entry keeps an opaque loader through Garage restoration and its first
paint. Settle in-flight world activation before restoring Garage, without
waiting on an unrelated stalled transport.
- Both first-entry and retained-rematch hosts must acquire the selected world's
collision before preparing authority. Guest module, world and connection work
stays concurrent; a retained transport does not remove the host dependency.
- Unexpected black-frame watchdog rejection fails closed after awaited resource
draining; the watchdog itself owns compatibility fallback. Preserve the
cancellation checkpoint before reporting graphics failure. A null lobby
callback is also used for successful match handoff, so it must not blindly
cancel the room's in-flight map build.
- A bridge must remain private until its exact roster and viewer-bearing first
snapshot are ready. Keep that order in networkBattlePresentationRuntime.ts;
failed unpublished bridges are disposed before the launcher handles cleanup.
- Tests exercise the public host/client interface, not private internals.
Verification
Run node src/net/browserInputRuntime.selftest.mjs, node src/net/net.selftest.mjs,
node src/net/privateMatchHandoff.selftest.mjs, then npm test and
npm run build. Network adapters additionally require browser-pair proof.