ECHO GARDEN / STUDY / LEVEL 7 OF 7
Two players,
one garden.
Each headset keeps its own copy of the garden. They stay the same because every change travels as a message through the course server, using the framework's server.init, server.send and server.sync, the style of the course's construct.js. This page simulates two headsets and the network between them, runs the game's own gardenNet.js, and shows the bug that made the first version drift apart.
Two headsets
Headset A
Headset B
The network
break it on purposeMessages in flight and in B's inbox
The shared beat
which link plays now?Every half second each headset plays one link. They never send the beat: they read their own clock and pick links[beat % links.length].
- A: beat · link
- B: beat · link
The course framework
server/client.jsTwo pieces matter. When a message arrives, its value is assigned to a global variable (first excerpt). sync sends my outbox and hands each client's inbox to the callback, but only when it runs, once per frame (second excerpt). A second batch that arrives before that frame replaces the first.
The fix
gardenNet.jsEach inbox gets a setter, so an arriving batch is added to what is waiting instead of replacing it. The framework's three-second full-state broadcast is turned off with server.syncInterval(-3), because a slightly old snapshot could erase a note the master had not heard about yet.
The beat
gardenAudio.js · echoGarden.jsWhat you should be able to explain
- Copies, not one garden. There is no garden on the server. Each headset applies the same messages to its own copy with the same rules (level 1), so the copies agree as long as no message is lost.
- The outbox and the inbox.
server.sendonly queues.server.sync, once per frame, broadcasts the queue as one batch and calls your callback with every client's latest batch, including your own. That is why your own change also appears through the callback. - The lost batch. A batch is stored by plain assignment. If two batches from A reach B before B's next frame, the first is overwritten. A paused tab runs no frames, so it keeps only the last batch. Pick "server.sync only", pause B, and let A plant 4 quickly.
- The fix.
gardenNet.jsreplaces each inbox with a property whose setter appends. The framework still assigns, but nothing is lost. The scene also runsnet.frame()from a timer every 0.5 s, so a hidden tab keeps receiving. - Agreeing without messages. Links come from the garden (level 4) and the beat comes from the wall clock. Two headsets agree on both without sending them.
model.timecould not do this: it starts when each headset loads the scene.
Exercises: ① Choose "server.sync only", tick "B's frames are paused", press "A plants 4 quickly", then untick. How many notes does B have? Repeat with gardenNet.js. ② With latency at 400 ms and no pause, can the first version still lose a batch? (Hint: how often does each headset run a frame?) ③ Set B's clock off by 300 ms: how often do the two headsets play different links? What would you need to fix that?