@zaisma/cli 0.29.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/package.json +1 -1
  3. package/zaisma.mjs +66 -11
package/README.md CHANGED
@@ -6,6 +6,8 @@ Run it with `npx @zaisma/cli <command>`, or install it once with `npm install -g
6
6
 
7
7
  Before anything else, `zaisma check ./build` lists anything in the game Zaisma would refuse or that won't work on Zaisma (threaded engine builds, `alert()`, forms, servers the game hasn't listed…), each with how to fix it. It needs no key and sends nothing.
8
8
 
9
+ To play a multiplayer game on your own computer before uploading it, run `zaisma play-locally` in its folder: the game and its game server run here, and the test bench's Add a player opens another tab as a new player, each on Zaisma's own game page. It needs no key and no internet.
10
+
9
11
  1. Make an API key at https://zaisma.com/dashboard/settings, under Keys, and set it: `export ZAISMA_KEY=zsk_…`
10
12
  2. Make a game: `zaisma new "Sky Hopper"`
11
13
  3. Upload your build folder (the one with `index.html`): `zaisma upload ./build --game sky-hopper --patch-notes "First version"`. Only new or changed files are sent. It runs the check first, and in what it sends (never in your folder) it downloads scripts, styles and fonts the game loads from other sites and unpacks pre-compressed builds (`.gz`, `.br`, `.unityweb`), then says what it changed. Once the scan passes you get a private preview link.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zaisma/cli",
3
- "version": "0.29.0",
3
+ "version": "0.31.0",
4
4
  "description": "Upload and publish browser games on Zaisma from the command line.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://zaisma.com",
package/zaisma.mjs CHANGED
@@ -20,7 +20,8 @@ const KEY = process.env.ZAISMA_KEY || "";
20
20
  const CLI_VERSION = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")).version;
21
21
  /** Files bigger than this go in parts; the server says how big each part is. */
22
22
  const DIRECT_BYTES = 95 * 1024 * 1024;
23
- const JUNK = new Set([".DS_Store", "Thumbs.db", "desktop.ini", "__MACOSX", ".git", "node_modules"]);
23
+ // .zaisma-local holds what play-locally keeps (its keys, saves and database): never sent.
24
+ const JUNK = new Set([".DS_Store", "Thumbs.db", "desktop.ini", "__MACOSX", ".git", "node_modules", ".zaisma-local"]);
24
25
  /** How long upload waits for the scan (200 checks, 3 seconds apart) before it says it's still scanning. ZAISMA_SCAN_CHECK_INTERVAL_MS is for tests. */
25
26
  const SCAN_CHECK_INTERVAL_MS = Number(process.env.ZAISMA_SCAN_CHECK_INTERVAL_MS) || 3000;
26
27
  const SCAN_CHECK_COUNT = 200;
@@ -104,6 +105,7 @@ const COMMAND_ERROR_CODES = {
104
105
  shader_texture_not_approved: "shader, check: a texture sent for the shader was rejected, or still waits for a person, so the shader wasn't sent.",
105
106
  no_chrome: "check: there's no Chrome on this computer to draw the shader in, and no --game with ZAISMA_KEY to draw it on Zaisma.",
106
107
  local_chrome_failed: "check: the Chrome on this computer couldn't draw the shader.",
108
+ game_server_not_run: "play-locally: npx couldn't run @zaisma/game-server, which runs the game on this computer.",
107
109
  server_unreachable: "Zaisma couldn't be reached: no connection, or a wrong ZAISMA_API.",
108
110
  unexpected_answer: "Zaisma answered without JSON, like a page from a network in between.",
109
111
  };
@@ -132,6 +134,18 @@ const COMMANDS = {
132
134
  "draw-on-zaisma": { hasValue: false, about: "A shader's folder only: draw it on Zaisma's own renderer, with --game and ZAISMA_KEY, even when there's a Chrome here" },
133
135
  },
134
136
  },
137
+ "play-locally": {
138
+ about:
139
+ "Play your game on this computer, with its game server and a test bench: Add a player opens another tab as someone else, so you play together, with made-up accounts, friends, blocks and items between them, and Zaisma's buy sheet with a test card that's never charged; " +
140
+ "needs no key and no internet. A file change reloads the game; a change to the game server starts it again with its rooms kept. It runs @zaisma/game-server's play-locally",
141
+ arguments: [{ name: "folder", isOptional: true }],
142
+ flags: {
143
+ port: { hasValue: true, placeholder: "<port>", about: "The first of four ports, 4800 unless given: the bench, the game's site, the stand-in rooms and your game server take it and the next three" },
144
+ game: { hasValue: true, placeholder: "<slug>", about: "The game's slug; with ZAISMA_KEY set, each start brings in the game's items from Zaisma" },
145
+ "on-network": { hasValue: false, about: "Let a phone on the same Wi-Fi open the bench too; it prints the address and a QR code" },
146
+ "no-open": { hasValue: false, about: "Don't open the bench in the browser" },
147
+ },
148
+ },
135
149
  upload: {
136
150
  about: "Upload a version; only changed files are sent. It runs the check first, and downloads files the game loads from outside sites and unpacks pre-compressed builds in what it sends, never in the folder",
137
151
  arguments: [{ name: "folder" }],
@@ -1737,15 +1751,20 @@ function* sqliteDumpStatements(sqlite) {
1737
1751
  )
1738
1752
  .all();
1739
1753
  yield "PRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;\n";
1740
- for (const table of entries.filter((entry) => entry.type === "table" && (entry.table_kind === "table" || entry.table_kind === "virtual") && !entry.name.startsWith("sqlite_"))) {
1741
- yield `${table.sql};\n`;
1754
+ const tables = entries.filter((entry) => entry.type === "table" && (entry.table_kind === "table" || entry.table_kind === "virtual") && !entry.name.startsWith("sqlite_"));
1755
+ // Every table is made before any row goes in, so a row can refer to a table made after it.
1756
+ for (const table of tables) yield `${table.sql};\n`;
1757
+ for (const table of tables) {
1742
1758
  const columns = sqlite.prepare("SELECT name, hidden = 0 AS is_insertable, pk AS primary_key_number FROM pragma_table_xinfo(?) ORDER BY cid").all(table.name);
1743
1759
  const insertedColumns = columns.filter((column) => column.is_insertable === 1);
1744
- const insertInto = `INSERT INTO ${quoteName(table.name)}${insertedColumns.length < columns.length ? `(${insertedColumns.map((column) => quoteName(column.name)).join(",")})` : ""} VALUES(`;
1745
- const valuesSql = insertedColumns.map((column, i) => `quote(${quoteName(column.name)}) AS value_${i}`).join(", ");
1760
+ // A search table's rows are known by their rowids, so they go in with them.
1761
+ const isVirtual = table.table_kind === "virtual";
1762
+ const insertInto = `INSERT INTO ${quoteName(table.name)}${insertedColumns.length < columns.length || isVirtual ? `(${[...(isVirtual ? ["rowid"] : []), ...insertedColumns.map((column) => quoteName(column.name))].join(",")})` : ""} VALUES(`;
1763
+ const valuesSql = [...(isVirtual ? ["rowid AS paging_rowid"] : []), ...insertedColumns.map((column, i) => `quote(${quoteName(column.name)}) AS value_${i}`)].join(", ");
1746
1764
  const primaryKey = columns.filter((column) => column.primary_key_number > 0).sort((first, second) => first.primary_key_number - second.primary_key_number);
1747
- const order = table.is_without_rowid ? primaryKey.map((column) => quoteName(column.name)).join(", ") : "rowid";
1748
- for (const row of sqlite.prepare(`SELECT ${valuesSql} FROM ${quoteName(table.name)} ORDER BY ${order}`).iterate()) yield `${insertInto}${insertedColumns.map((_, i) => row[`value_${i}`]).join(",")});\n`;
1765
+ // The key's order, rowid last where there is one, as Zaisma's export reads it (packages/game-server/src/dump.ts).
1766
+ const order = primaryKey.length ? [...primaryKey.map((column) => `${quoteName(table.name)}.${quoteName(column.name)}`), ...(table.is_without_rowid ? [] : [`${quoteName(table.name)}.rowid`])].join(", ") : "rowid";
1767
+ for (const row of sqlite.prepare(`SELECT ${valuesSql} FROM ${quoteName(table.name)} ORDER BY ${order}`).iterate()) yield `${insertInto}${[...(isVirtual ? [row.paging_rowid] : []), ...insertedColumns.map((_, i) => row[`value_${i}`])].join(",")});\n`;
1749
1768
  }
1750
1769
  if (entries.some((entry) => entry.name === "sqlite_sequence")) {
1751
1770
  yield "DELETE FROM sqlite_sequence;\n";
@@ -1832,7 +1851,7 @@ async function secrets(command, values) {
1832
1851
  }
1833
1852
 
1834
1853
  /** The fields of an item the file gives and set_item takes. */
1835
- const ITEM_FILE_FIELDS = ["item_name", "title", "description", "item_kind", "item_category", "price_minor_units", "currency_code", "local_prices"];
1854
+ const ITEM_FILE_FIELDS = ["item_name", "title", "description", "item_kind", "item_category", "price_minor_units", "currency_code", "local_prices", "is_on_game_page"];
1836
1855
 
1837
1856
  /** A price as people read it, from minor units, for the list. */
1838
1857
  function itemPriceText(gameItem) {
@@ -1861,7 +1880,7 @@ async function items(command, values) {
1861
1880
  } catch {
1862
1881
  wanted = null;
1863
1882
  }
1864
- const fileFix = 'Give a JSON list of whole items, like [{ "item_name": "revive", "title": "Revive", "description": "", "item_kind": "consumable", "item_category": "gameplay", "price_minor_units": 199, "currency_code": "usd", "local_prices": [] }].';
1883
+ const fileFix = 'Give a JSON list of whole items, like [{ "item_name": "revive", "title": "Revive", "description": "", "item_kind": "consumable", "item_category": "gameplay", "price_minor_units": 199, "currency_code": "usd", "local_prices": [], "is_on_game_page": false }].';
1865
1884
  if (!Array.isArray(wanted) || wanted.some((gameItem) => !gameItem || typeof gameItem !== "object" || typeof gameItem.item_name !== "string")) refuse("bad_item", { message: `${values.file ?? "What was piped in"} isn't a JSON list of items.`, field: "file", fix: fileFix });
1866
1885
  for (const gameItem of wanted) {
1867
1886
  const unknown = Object.keys(gameItem).find((key) => !ITEM_FILE_FIELDS.includes(key) && !["card_fee_note", "created_at", "updated_at"].includes(key));
@@ -1871,13 +1890,15 @@ async function items(command, values) {
1871
1890
  const repeated = names.find((name, i) => names.indexOf(name) !== i);
1872
1891
  if (repeated) refuse("bad_item", { message: `${repeated} is in the file twice.`, field: "file", fix: "Give each item once." });
1873
1892
  const current = (await api("GET", path)).items;
1874
- const same = (gameItem, existing) => ITEM_FILE_FIELDS.every((field) => JSON.stringify(gameItem[field] ?? null) === JSON.stringify(existing[field] ?? null));
1893
+ // An item file that leaves is_on_game_page out means false, as items set sends it.
1894
+ const valueOf = (gameItem, field) => gameItem[field] ?? (field === "is_on_game_page" ? false : null);
1895
+ const same = (gameItem, existing) => ITEM_FILE_FIELDS.every((field) => JSON.stringify(valueOf(gameItem, field)) === JSON.stringify(valueOf(existing, field)));
1875
1896
  const changed = wanted.filter((gameItem) => { const existing = current.find((other) => other.item_name === gameItem.item_name); return !existing || !same(gameItem, existing); });
1876
1897
  const gone = current.filter((existing) => !names.includes(existing.item_name));
1877
1898
  const setNames = [];
1878
1899
  for (const gameItem of changed) {
1879
1900
  const { item_name: itemName, ...fields } = gameItem;
1880
- for (const field of ITEM_FILE_FIELDS.slice(1)) if (!(field in fields)) fields[field] = field === "local_prices" ? [] : field === "description" ? "" : null;
1901
+ for (const field of ITEM_FILE_FIELDS.slice(1)) if (!(field in fields)) fields[field] = field === "local_prices" ? [] : field === "description" ? "" : field === "is_on_game_page" ? false : null;
1881
1902
  for (const key of ["card_fee_note", "created_at", "updated_at"]) delete fields[key];
1882
1903
  await api("PUT", `${path}/${encodeURIComponent(itemName)}`, fields);
1883
1904
  setNames.push(itemName);
@@ -2100,6 +2121,7 @@ async function run({ command, values, flags, globalFlags, rest }) {
2100
2121
  return EXIT_CODES.done;
2101
2122
  }
2102
2123
  if (command === "check") return checkFolder(values, flags);
2124
+ if (command === "play-locally") return playLocally(values, flags);
2103
2125
  if (command === "export") return exportGame(values, flags);
2104
2126
  if (command === "upload") return upload(values, flags);
2105
2127
  if (command === "shader") return sendShader(values, flags);
@@ -2153,6 +2175,39 @@ async function run({ command, values, flags, globalFlags, rest }) {
2153
2175
  return EXIT_CODES.done;
2154
2176
  }
2155
2177
 
2178
+ /**
2179
+ * play-locally: the game server package's command, run through npx so this command stays free of dependencies
2180
+ * (ZAISMA_GAME_SERVER_BIN, a path to its bin/node.js, is for tests and the repository). Its output and exit code
2181
+ * come through as they are; with --json it prints { bench_url, game_site_url, rooms_url, network_url } once listening.
2182
+ */
2183
+ async function playLocally(values, flags) {
2184
+ const { spawn } = await import("node:child_process");
2185
+ const given = [values.folder ?? ".", ...(flags.port !== undefined ? ["--port", flags.port] : []), ...(flags.game !== undefined ? ["--game", flags.game] : []), ...(flags["on-network"] ? ["--on-network"] : []), ...(flags["no-open"] ? ["--no-open"] : []), ...(isJson ? ["--json"] : [])];
2186
+ const { existsSync: isThere } = await import("node:fs");
2187
+ const { dirname: folderOf, join: joinPath } = await import("node:path");
2188
+ const local = process.env.ZAISMA_GAME_SERVER_BIN;
2189
+ // npm's npx run through this Node, where npm keeps it beside Node (Windows) or under lib/ (macOS, Linux): a .cmd can't be spawned without a shell on Windows.
2190
+ const npxScript = [joinPath(folderOf(process.execPath), "node_modules/npm/bin/npx-cli.js"), joinPath(folderOf(process.execPath), "../lib/node_modules/npm/bin/npx-cli.js")].find((path) => isThere(path));
2191
+ const npxArguments = ["--yes", "@zaisma/game-server@1", "play-locally", ...given];
2192
+ const [file, list] = local ? [process.execPath, [local, "play-locally", ...given]] : npxScript ? [process.execPath, [npxScript, ...npxArguments]] : ["npx", npxArguments];
2193
+ let child;
2194
+ try {
2195
+ child = spawn(file, list, { stdio: "inherit" });
2196
+ } catch {
2197
+ refuse("game_server_not_run", { message: "npx couldn't run @zaisma/game-server.", fix: "Check that Node 22.18 or newer and npm are installed, and that npm can reach its registry once to download the package." }, EXIT_CODES.failed);
2198
+ }
2199
+ // Ctrl-C reaches the child from the terminal by itself; a SIGTERM sent to this command alone is passed on.
2200
+ process.on("SIGINT", () => {});
2201
+ process.on("SIGTERM", () => child.kill("SIGTERM"));
2202
+ const exitCode = await new Promise((done) => {
2203
+ child.on("error", () => done(null));
2204
+ // Stopped by a signal (Ctrl-C) is a stop, not a failure.
2205
+ child.on("exit", (code, signal) => done(code ?? (signal ? 0 : 1)));
2206
+ });
2207
+ if (exitCode === null) refuse("game_server_not_run", { message: "npx couldn't run @zaisma/game-server.", fix: "Check that Node 22.18 or newer and npm are installed, and that npm can reach its registry once to download the package." }, EXIT_CODES.failed);
2208
+ return exitCode === 0 ? EXIT_CODES.done : { exitCode, meaning: "play-locally stopped" };
2209
+ }
2210
+
2156
2211
  /** A failure, in the API's shape: with --json the { error } on stdout, otherwise one line on stderr. */
2157
2212
  function printError(error) {
2158
2213
  if (isJson) printJson({ error });