Duello arcade
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
Two home-built arcade cabinets, each with a Raspberry Pi, stand in two different homes on two different networks and play the same two-player game against each other. The cabinets talk to each other directly, Pi to Pi: cabinet one accepts the session and runs the game, cabinet two opens the session to it and sends its player's inputs. Each cabinet also brings a companion screen for the phone, a device UI that the Connectivitizer app shows: the live match as a small animated playfield, the scoreboard and match history of the two homes, who is at the other cabinet, how the two cabinets are connected (direct or relayed, round-trip time), and a challenge button that makes the other cabinet light up. Neither Pi needs an open port or a public address.
The cabinets are simulated. The program ships with a simulator: computer players hold the sticks and nothing is drawn on a cabinet screen, so the example runs with nothing attached. Where a real stick, button, screen and lights would be connected is marked in the code and below; that part is not implemented.
| Folder | What it is |
|---|---|
pi/arcade.py | The program of one cabinet, in either role: --role host (cabinet one) or --role guest (cabinet two). |
pi/game.py | The game, Rimshock, shared by both roles, and the computer player. Runs on its own without a screen. |
pi/cabinet.py | The cabinet's stick, button, screen and lights behind the class Cabinet, with the simulator. |
pi/server.py | The HTTP and WebSocket server on 127.0.0.1 (standard library only). |
ui/arcade.html | The companion screen: one self-contained page. |
ui/manifest.json | What a cabinet answers at /.cz/ui.json: the name of the screen and the colours of the app's bar. |
ui/demo-data.js | A pretend cabinet for the demo on the landing page (below). |
Rimshock is a game of its own, made for this example. Two skimmers glide on a round plate that floats in the dark: a lime dart (cabinet one) and a pink box (cabinet two). Whoever leaves the plate loses the round.
The whole game is in pi/game.py, a few hundred lines. The host steps it 60 times a second.
| Cabinet one, the host | Cabinet two, the guest | |
|---|---|---|
| Runs | arcade.py --role host | arcade.py --role guest |
| In Connectivitizer | A device, cabinet-one, with the service api. | A device too, cabinet-two, with the service api for its own phone. As that device it opens a session to cabinet-one. |
| The game | Runs it: reads its own stick, takes the guest's inputs, decides everything. Keeps the scoreboard in pi/scores.json. | Sends its stick and button 30 times a second and shows the snapshots that come back, 30 a second. |
The guest opens the session itself, device to device, over its own connection to the platform (agent.connect in the SDK); it needs no API key, its device key carries the right to open sessions. Through the session it opens one WebSocket to the host, /play on the service api. The session is end-to-end encrypted with the shared key both cabinets hold, and direct, peer to peer, where the two networks allow it, otherwise through the relay. The guest measures the round-trip time on that WebSocket once a second and tells the host, so both companion screens show the same number. Whether the session is direct or relayed is what the SDK on the guest reports (session.info.mode); the host shows what the guest tells it.
The guest does not predict anything: it shows the game as the host sent it. What the player at cabinet two feels is therefore the round trip between the cabinets, which is why a direct connection matters here.
pip install -r pi/requirements.txt, which is huelake-connectivitizer) and keys from the web app (below).cd src/examples/arcade/pi
python arcade.py --role hostArcade cabinet 'cabinet-one' (host, simulator): Player one at Home one, on http://127.0.0.1:8141
the companion page: http://127.0.0.1:8141/.cz/ui.html
the match: http://127.0.0.1:8141/state
SIMULATOR: a computer player holds this cabinet's stick, and nothing is drawn on a screen.
Local only: this cabinet is NOT a Connectivitizer device (no keys: fill in secrets.json, or set CZ_DEVICE_KEY and CZ_SHARED_KEY).Without keys it runs local only. In simulator mode a second computer player takes the other seat as long as no other cabinet is connected, so there is always a match to watch: open the companion page in a browser on the same machine, or ask curl http://127.0.0.1:8141/state.
The second cabinet on the same machine, in another terminal:
python arcade.py --role guest --local 127.0.0.1:8141 --player Robin --home "The Shed"--local makes the guest reach the host over plain TCP instead of a Connectivitizer session. It is there to try both roles on one machine; the companion page then shows the link as LOCAL and not end-to-end encrypted. The guest's own page is at http://127.0.0.1:8142/.cz/ui.html. Both programs listen on the loopback address only.
The game alone, two computer players without a screen, as fast as the machine goes:
python game.py --matches 3| Option | Default | |
|---|---|---|
--role (or ARCADE_ROLE, or role in secrets.json) | none | host or guest. |
--port (or ARCADE_PORT) | 8141 host, 8142 guest | The local port; 0 takes any free one. |
--player, --home | Player one at Home one, Player two at Home two | Who plays at this cabinet, and what its home is called on the scoreboard. |
--cabinet | simulator | hardware ends with "not implemented". |
--local HOST:PORT | none | Guest only: plain TCP to a host on the same machine. |
Both cabinets belong to the same workspace and hold the same shared key.
cd src/examples/arcade/pi
python -m venv .venv && .venv/bin/pip install -r requirements.txt # .venv\Scripts\pip on Windows
cp secrets.host.example.json secrets.json # on cabinet two: secrets.guest.example.json
.venv/bin/python arcade.py # the role comes from secrets.jsonsecrets.json (not committed, see pi/.gitignore), or the same as environment variables:
secrets.json | Variable | Host | Guest | |
|---|---|---|---|---|
sharedKey | CZ_SHARED_KEY | yes | yes | czs_...; web app: Credentials › Generate a shared key. The same one on both cabinets and on the phones. |
deviceKey | CZ_DEVICE_KEY | yes | yes | czd_...; web app: Credentials › Create a device key. It makes the cabinet a device, which accepts sessions: on the host from the guest and from the phone, on the guest from its own phone. The guest also opens its session to the host with it, so the guest's device key needs "Its devices may open sessions to other devices" switched on; the host's does not. One device key with that setting for both cabinets works as well. |
deviceName | CZ_DEVICE_NAME | yes | yes | How the cabinet is called in the workspace. Default cabinet-one for the host, cabinet-two for the guest. |
hostDevice | CZ_HOST_DEVICE | no | yes | The host's device name. Default cabinet-one. |
role, home, player | ARCADE_ROLE, ARCADE_HOME, ARCADE_PLAYER | The same as the options above. |
A device appears in the workspace the first time it connects. A guest whose device key does not have "Its devices may open sessions to other devices" switched on is refused by the platform with FORBIDDEN; the guest writes that on its console and tries again. The SDK forwards each stream of a session as plain TCP to 127.0.0.1:<port>, so the program's own server answers the phone and the other cabinet; nothing else on the Pi is reachable. When the session between the cabinets ends, the guest opens a new one, waiting 2 to 30 seconds between tries. A host with a real cabinet (not the simulator) waits with the game until the guest is back.
In the Connectivitizer app on Android, signed in to the same workspace and holding the same shared key: pick cabinet-one (or cabinet-two). The app asks it for /.cz/ui.json, loads /.cz/ui.html through the session and shows it full screen, with the bar above it in the colours of the manifest. In the app the page gets the match over a WebSocket (connectivitizer.webSocket("/match")). Opened in a browser straight from the cabinet it has no such bridge and asks /state four times a second instead, which is coarser, and it shows no answer to a challenge. An iPhone and a PC do not show device UIs yet (device UIs, "Not yet").
Challenge sends a message to the other cabinet over the session between the two. That cabinet lights up (the simulator writes a line on its console), tells its own companion screen, and answers that it did; the button then says so. With no other cabinet connected, the cabinet answers 409 and the page shows why.
The page shows two connections that are easy to mix up: the card Cabinet link is the session between the two Pis. How the phone itself reaches its cabinet is shown by the app, in its bar.
All under the service api. Both roles answer the same; the guest answers from what the host sent it.
| Request | Answer |
|---|---|
GET /state | { "game": "Rimshock", "role": "host" | "guest", "seat": 0 | 1, "players": [ player, player ], "link", "match" }. seat is this cabinet's place in players and in the match: 0 the host, 1 the guest. A player is { "cabinet", "home", "player", "present", "computer" }: the cabinet's device name, the names on the scoreboard, whether that cabinet is connected, and whether a computer player holds its stick. link is { "connected", "path": "direct" | "relayed" | "local" | null, "endToEndEncrypted": true | false | null, "rttMs": number | null }: the connection between the two cabinets. match is a snapshot (below). |
GET /scores | { "homes": [ { "home", "player", "wins" }, { … } ], "history": [ { "at", "winner", "score": [a, b], "rounds", "seconds", "computer" }, … ] }. history is the last 20 matches, newest first; at in seconds since 1970, winner a seat, seconds the time played, computer whether a computer player took part. |
POST /challenge | { "ok": true, "to": { "cabinet", "player" } } once the message is on its way. 409 with { "error" } when the other cabinet is not connected. |
WebSocket /match | For the companion page. { "type": "hello", "state", "scores" } when it opens (GET /state and GET /scores); { "type": "state", …snapshot } 20 times a second on the host, 15 on the guest; { "type": "event", "event", "t", … } as things happen; { "type": "info", "players", "link" } when a cabinet comes or goes and with every new round-trip time; { "type": "scores", "homes", "history" } after a match. Nothing is expected from the page. |
WebSocket /play | Host only: the other cabinet (below). |
GET /.cz/ui.json, GET /.cz/ui.html | The manifest and the page, read from ui/ on every request, so the page can be edited while the program runs. |
A snapshot: { "t", "phase": "waiting" | "ready" | "play" | "point" | "over", "round", "clock", "countdown", "rim", "score": [a, b], "target": 5, "p": [ skimmer, skimmer ], "spark": [x, y] | null }. t is the game's own clock in seconds, clock the seconds played in this round, countdown the seconds left of a phase that is not play. Lengths are in plate radii around the centre (0, 0), x to the right and y down; rim is the radius of the plate now. A skimmer is { "x", "y", "vx", "vy", "charge" (0 to 1), "boost", "out" }.
Events, each with the game's time t: round (round, score), go (round), shock (seat, x, y, reach, boost), hit (seat of the skimmer that was hit, by, x, y, strength), bump (x, y, speed), spark (seat, x, y), fall (seat of the loser, x, y, score, round), draw (round), match (winner, score, rounds, seconds). And two that are not part of the game: challenge (seat, player: the other cabinet challenges this one) and lit (seat: that cabinet lit up for a challenge).
/playJSON text messages on one WebSocket.
| Guest to host | |
|---|---|
{ "type": "hello", "cabinet", "home", "player", "computer", "path", "endToEndEncrypted" } | First. path and endToEndEncrypted as the guest's SDK reports the session. The host starts a new match. |
{ "type": "in", "x", "y", "b" } | The stick (each -1 to 1) and the button, 30 times a second. An input older than half a second counts as a stick let go. |
{ "type": "ping", "n", "rttMs" } | Once a second; rttMs is the last round trip the guest measured. |
{ "type": "challenge", "from" }, { "type": "lit" } | A challenge from the guest's phone; the answer to one from the host. |
| Host to guest | |
|---|---|
{ "type": "welcome", "cabinet", "home", "player", "computer", "scores" } | The answer to hello. |
{ "type": "state", …snapshot } | 30 times a second. |
{ "type": "event", … }, { "type": "scores", … } | As on /match. |
{ "type": "pong", "n" } | The answer to ping, at once. |
{ "type": "challenge", "from" }, { "type": "lit" } | As above, the other way round. |
{ "type": "busy", "error" } | Then the socket closes: another cabinet is already playing. |
A guest that says nothing for five seconds counts as gone, and so does a host. Anybody who can open a session to the host's service api can open /play; that is everybody who holds the shared key and may open sessions to the host in the workspace: with an API key, with a device key that has "Its devices may open sessions to other devices" switched on, or signed in to the app.
The cabinet is behind the class Cabinet in pi/cabinet.py; the rest of the program only knows that class. SimulatedCabinet implements it with a computer player. HardwareCabinet is the place for a real one: it raises "not implemented" and has never been run on a cabinet. Its comment describes what would have to be filled in:
read_input(): the stick's four switches and the button on GPIO pins.draw(): a full-screen window that draws a snapshot, 60 times a second on the host and from the 30 snapshots a second on the guest.event(): the flash and the sound for a shock, a hit, a fall and the end of a match.light_up(): marquee lights or an LED strip for a challenge.None of it was written or tried. How the game feels with real hands on two sticks, and at which round-trip time it stops being fun for the guest, is therefore not known.
The landing page of the web app shows the companion page without any hardware (demo kit): ui/demo-data.js is a pretend cabinet one, with the game from pi/game.py once more in JavaScript, two computer players at the sticks, and the same answers and /match messages. It opens in the fourth round of a match. The players, the homes, the earlier matches and the round-trip time in it are invented.
cd src/connectivitizer
node scripts/build-demos.mjs --only arcade --out <folder> # writes <folder>/arcade/ui.html, which opens from the file systemgame.py) on a Windows PC with Python 3.12: 40 matches between two computer players. Every round ended, the longest after 15.8 seconds, and every match reached 5.--local: the guest joined, a whole match was played to its end and written to scores.json, both cabinets answered every request in the table above, a challenge in each direction arrived and was answered, and the host went on with a computer player after the guest left./play over TCP, which is what a session delivers to the local port: hello, inputs, ping, snapshots and close.