ravensight-playtest 0.8.3 → 0.10.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 (66) hide show
  1. package/README.md +280 -66
  2. package/addons/ravensight_driver/driver.gd +666 -21
  3. package/docs/ADAPTERS.md +11 -7
  4. package/docs/NATIVE_RUNNER.md +3 -1
  5. package/docs/godot-driver.md +127 -13
  6. package/package.json +8 -6
  7. package/src/api/client.js +4 -3
  8. package/src/cli.js +84 -25
  9. package/src/commands/check.js +30 -4
  10. package/src/commands/fakeCore.js +1 -0
  11. package/src/commands/hosted.js +41 -12
  12. package/src/commands/init.js +17 -1
  13. package/src/commands/run.js +209 -14
  14. package/src/commands/upload.js +6 -2
  15. package/src/commands/validate.js +1 -1
  16. package/src/commands/validateHosted.js +50 -5
  17. package/src/config.js +33 -2
  18. package/src/hosted/archivePreflight.js +7 -7
  19. package/src/hosted/buildUpload.js +89 -8
  20. package/src/hosted/recordedRun.js +141 -26
  21. package/src/hosted/status.js +19 -10
  22. package/src/run/actionSelector.js +14 -3
  23. package/src/run/aggregate.js +57 -3
  24. package/src/run/anygame/controlbook.js +179 -0
  25. package/src/run/anygame/genre.js +179 -0
  26. package/src/run/anygame/index.js +258 -0
  27. package/src/run/anygame/language.js +336 -0
  28. package/src/run/anygame/memory.js +330 -0
  29. package/src/run/anygame/shared-hashes.json +10 -0
  30. package/src/run/anygame/shared.js +24 -0
  31. package/src/run/anygame/sound.js +342 -0
  32. package/src/run/args.js +75 -8
  33. package/src/run/choiceCheck.js +312 -0
  34. package/src/run/context.js +254 -13
  35. package/src/run/drivers/godot-observation.js +93 -6
  36. package/src/run/drivers/godot.js +769 -46
  37. package/src/run/drivers/recording.js +24 -4
  38. package/src/run/drivers/web.js +1242 -77
  39. package/src/run/edgePass.js +731 -0
  40. package/src/run/fixLoop.js +5 -4
  41. package/src/run/focus.js +65 -0
  42. package/src/run/gamepad.js +108 -0
  43. package/src/run/godotEdgePass.js +235 -0
  44. package/src/run/godotTesterProbes.js +192 -0
  45. package/src/run/headings.js +21 -0
  46. package/src/run/honesty.js +544 -0
  47. package/src/run/inputs.js +256 -0
  48. package/src/run/marks.js +142 -0
  49. package/src/run/personaLoop.js +1000 -79
  50. package/src/run/phone.js +7 -0
  51. package/src/run/pixels.js +215 -0
  52. package/src/run/playClock.js +162 -0
  53. package/src/run/readiness.js +585 -0
  54. package/src/run/refusal.js +67 -0
  55. package/src/run/regions.js +119 -0
  56. package/src/run/report.js +14 -2
  57. package/src/run/reportChecks.js +472 -0
  58. package/src/run/resume.js +3 -2
  59. package/src/run/screenshots.js +161 -0
  60. package/src/run/strip.js +114 -0
  61. package/src/run/testerLedger.js +312 -0
  62. package/src/run/testerProbes.js +584 -0
  63. package/src/run/thinRun.js +102 -0
  64. package/src/run/timeFreeze.js +79 -0
  65. package/src/run/tools.js +454 -29
  66. package/src/runDefaults.js +24 -2
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The screen a `--phone` run is played on: a phone held upright. Kept apart
3
+ * from the browser driver so the command can name it without loading one.
4
+ */
5
+ export const PHONE_VIEWPORT = Object.freeze({ width: 390, height: 844 });
6
+
7
+ export default { PHONE_VIEWPORT };
@@ -0,0 +1,215 @@
1
+ // What a screenshot shows, measured without an image library.
2
+ //
3
+ // Ported from the cloud runner's change check. A byte hash calls a blinking
4
+ // text cursor a change and calls two saves of one frame the same only when the
5
+ // encoder happened to write the same bytes. Here two screens are compared as
6
+ // BLOCK by BLOCK pixel averages, and a difference counts only when it covers at
7
+ // least MIN_CHANGE_AREA_PX or its bounding box is at least MIN_CHANGE_SIDE_PX
8
+ // on both sides: a caret (about 2 by 30 pixels) is neither, one typed character
9
+ // (about 10 by 16) is.
10
+ //
11
+ // The decoder reads the 8-bit, non-interlaced RGB and RGBA PNGs that Chromium
12
+ // and Godot write. Anything else answers null, and every caller falls back to
13
+ // the bytes.
14
+ import { createHash } from 'node:crypto';
15
+ import zlib from 'node:zlib';
16
+
17
+ const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
18
+
19
+ /** The most pixels decoded: a larger image is not a game screenshot. */
20
+ export const MAX_PIXELS = 4096 * 4096;
21
+
22
+ /**
23
+ * @param {Buffer} buf
24
+ * @returns {{width: number, height: number, channels: number, data: Buffer}|null}
25
+ */
26
+ export function decodePng(buf) {
27
+ if (!Buffer.isBuffer(buf) || buf.length < 33 || !buf.subarray(0, 8).equals(SIGNATURE)) return null;
28
+ let off = 8;
29
+ let width = 0;
30
+ let height = 0;
31
+ let channels = 0;
32
+ const idat = [];
33
+ while (off + 8 <= buf.length) {
34
+ const len = buf.readUInt32BE(off);
35
+ const type = buf.toString('latin1', off + 4, off + 8);
36
+ const body = buf.subarray(off + 8, off + 8 + len);
37
+ if (type === 'IHDR') {
38
+ if (body.length < 13) return null;
39
+ width = body.readUInt32BE(0);
40
+ height = body.readUInt32BE(4);
41
+ const depth = body[8];
42
+ const colour = body[9];
43
+ const interlace = body[12];
44
+ if (depth !== 8 || interlace !== 0 || ![2, 6].includes(colour)) return null;
45
+ if (width * height > MAX_PIXELS) return null;
46
+ channels = colour === 6 ? 4 : 3;
47
+ } else if (type === 'IDAT') idat.push(body);
48
+ else if (type === 'IEND') break;
49
+ off += 12 + len;
50
+ }
51
+ if (!width || !height || !channels || idat.length === 0) return null;
52
+ let raw;
53
+ try {
54
+ raw = zlib.inflateSync(Buffer.concat(idat));
55
+ } catch {
56
+ return null;
57
+ }
58
+ const stride = width * channels;
59
+ if (raw.length < height * (stride + 1)) return null;
60
+ const data = Buffer.alloc(height * stride);
61
+ for (let y = 0; y < height; y += 1) {
62
+ const filter = raw[y * (stride + 1)];
63
+ const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
64
+ const out = data.subarray(y * stride, (y + 1) * stride);
65
+ const prev = y ? data.subarray((y - 1) * stride, y * stride) : null;
66
+ for (let x = 0; x < stride; x += 1) {
67
+ const a = x >= channels ? out[x - channels] : 0;
68
+ const b = prev ? prev[x] : 0;
69
+ const c = prev && x >= channels ? prev[x - channels] : 0;
70
+ let v = line[x];
71
+ if (filter === 1) v += a;
72
+ else if (filter === 2) v += b;
73
+ else if (filter === 3) v += (a + b) >> 1;
74
+ else if (filter === 4) {
75
+ const p = a + b - c;
76
+ const pa = Math.abs(p - a);
77
+ const pb = Math.abs(p - b);
78
+ const pc = Math.abs(p - c);
79
+ v += pa <= pb && pa <= pc ? a : (pb <= pc ? b : c);
80
+ } else if (filter !== 0) return null;
81
+ out[x] = v & 0xff;
82
+ }
83
+ }
84
+ return { width, height, channels, data };
85
+ }
86
+
87
+ export const BLOCK = 4;
88
+ export const BLOCK_TOLERANCE = 12;
89
+ export const MIN_CHANGE_AREA_PX = 300;
90
+ export const MIN_CHANGE_SIDE_PX = 10;
91
+
92
+ /**
93
+ * Block averages of a decoded image.
94
+ *
95
+ * @returns {{cols: number, rows: number, block: number, width: number, height: number, rgb: Uint8Array}|null}
96
+ */
97
+ export function fingerprint(img, block = BLOCK) {
98
+ if (!img) return null;
99
+ const cols = Math.ceil(img.width / block);
100
+ const rows = Math.ceil(img.height / block);
101
+ const sum = new Uint32Array(cols * rows * 3);
102
+ const count = new Uint32Array(cols * rows);
103
+ for (let y = 0; y < img.height; y += 1) {
104
+ const r = (y / block) | 0;
105
+ for (let x = 0; x < img.width; x += 1) {
106
+ const c = r * cols + ((x / block) | 0);
107
+ const i = (y * img.width + x) * img.channels;
108
+ sum[c * 3] += img.data[i];
109
+ sum[c * 3 + 1] += img.data[i + 1];
110
+ sum[c * 3 + 2] += img.data[i + 2];
111
+ count[c] += 1;
112
+ }
113
+ }
114
+ const rgb = new Uint8Array(cols * rows * 3);
115
+ for (let c = 0; c < cols * rows; c += 1) {
116
+ for (let k = 0; k < 3; k += 1) rgb[c * 3 + k] = Math.round(sum[c * 3 + k] / count[c]);
117
+ }
118
+ return { cols, rows, block, width: img.width, height: img.height, rgb };
119
+ }
120
+
121
+ /** The fingerprint of a PNG buffer, or null when this reader cannot decode it. */
122
+ export function pngFingerprint(png) {
123
+ return fingerprint(decodePng(png));
124
+ }
125
+
126
+ /**
127
+ * What differs between two fingerprints of the same size: the changed area in
128
+ * pixels and the changed blocks' bounding box, or null when the two cannot be
129
+ * compared (different sizes).
130
+ */
131
+ export function screenDelta(a, b, tolerance = BLOCK_TOLERANCE) {
132
+ if (!a || !b || a.cols !== b.cols || a.rows !== b.rows || a.block !== b.block) return null;
133
+ let n = 0;
134
+ let x0 = Infinity;
135
+ let y0 = Infinity;
136
+ let x1 = -1;
137
+ let y1 = -1;
138
+ for (let c = 0; c < a.cols * a.rows; c += 1) {
139
+ const i = c * 3;
140
+ if (Math.abs(a.rgb[i] - b.rgb[i]) <= tolerance && Math.abs(a.rgb[i + 1] - b.rgb[i + 1]) <= tolerance && Math.abs(a.rgb[i + 2] - b.rgb[i + 2]) <= tolerance) continue;
141
+ n += 1;
142
+ const x = c % a.cols;
143
+ const y = (c / a.cols) | 0;
144
+ if (x < x0) x0 = x;
145
+ if (x > x1) x1 = x;
146
+ if (y < y0) y0 = y;
147
+ if (y > y1) y1 = y;
148
+ }
149
+ const s = a.block;
150
+ return n ? { area: n * s * s, width: (x1 - x0 + 1) * s, height: (y1 - y0 + 1) * s, x: x0 * s, y: y0 * s } : { area: 0, width: 0, height: 0, x: 0, y: 0 };
151
+ }
152
+
153
+ /** A difference a player would notice: not nothing, not a lone blinking caret. */
154
+ export function significant(delta) {
155
+ if (!delta || !delta.area) return false;
156
+ return delta.area >= MIN_CHANGE_AREA_PX || (delta.width >= MIN_CHANGE_SIDE_PX && delta.height >= MIN_CHANGE_SIDE_PX);
157
+ }
158
+
159
+ /**
160
+ * Whether two saved screenshots are the same screen to a player.
161
+ *
162
+ * Identical bytes are. Two PNGs this reader decodes are when nothing a player
163
+ * would notice differs between them. Anything else (another format, another
164
+ * size) is a different screen: when in doubt the file is kept.
165
+ *
166
+ * @param {Buffer} a
167
+ * @param {Buffer} b
168
+ * @returns {boolean}
169
+ */
170
+ export function sameScreen(a, b) {
171
+ if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) return false;
172
+ if (a.equals(b)) return true;
173
+ const delta = screenDelta(pngFingerprint(a), pngFingerprint(b));
174
+ return delta !== null && !significant(delta);
175
+ }
176
+
177
+ /**
178
+ * Distinct screens by the same measure: a screenshot is new when it differs
179
+ * noticeably from every distinct one kept so far. A PNG this reader cannot
180
+ * decode falls back to its byte hash.
181
+ */
182
+ export function createDistinct(limit = 300) {
183
+ const kept = [];
184
+ const hashes = new Set();
185
+ return {
186
+ /** @returns {boolean} true when the screen was not seen before */
187
+ add(png) {
188
+ const fp = pngFingerprint(png);
189
+ if (!fp) {
190
+ const hash = Buffer.isBuffer(png) ? createHash('sha256').update(png).digest('hex') : String(png);
191
+ if (hashes.has(hash)) return false;
192
+ hashes.add(hash);
193
+ return true;
194
+ }
195
+ if (kept.some(k => {
196
+ const delta = screenDelta(k, fp);
197
+ return delta !== null && !significant(delta);
198
+ })) return false;
199
+ if (kept.length < limit) kept.push(fp);
200
+ return true;
201
+ },
202
+ get count() {
203
+ return kept.length + hashes.size;
204
+ }
205
+ };
206
+ }
207
+
208
+ /** How many of the screenshots are distinct screens. */
209
+ export function countDistinct(pngs) {
210
+ const distinct = createDistinct();
211
+ for (const png of pngs) distinct.add(png);
212
+ return distinct.count;
213
+ }
214
+
215
+ export default { decodePng, fingerprint, pngFingerprint, screenDelta, significant, sameScreen, createDistinct, countDistinct };
@@ -0,0 +1,162 @@
1
+ /**
2
+ * The clock a run sold by the minute stops on.
3
+ *
4
+ * A local run on a cloud tier (`--tier`, `--minutes`) is sold minutes of
5
+ * play, and the server stops serving the run at its own wall clock
6
+ * (`limits.wall_seconds` on the registered run). The play loop counted
7
+ * actions, so such a run was given an action count guessed from the wall
8
+ * clock (six seconds an action) and stopped there, early or late.
9
+ *
10
+ * The rule, stated once:
11
+ *
12
+ * - **Play time is time the game ran.** On a driver that freezes the game
13
+ * between actions (`godot_driver` in lockstep, a canvas game in the
14
+ * browser), that is the time inputs and waits took to play: the model
15
+ * thinking costs no play time, because the game stood still. On a driver
16
+ * whose game keeps running (`--realtime`, a page played from its text),
17
+ * every second since play began is play time, because the game ran.
18
+ * - **Play ends when the play time sold is used**, when one was sold
19
+ * (`playSeconds`).
20
+ * - **Play also ends when the wall clock is nearly used**, whatever the play
21
+ * time: the server refuses the run after `wallSeconds`, and the report has
22
+ * to be written inside it. The reserve kept for the report is a fifth of
23
+ * the wall clock, at least 60 seconds and at most 240.
24
+ *
25
+ * With neither limit the clock never stops anything; a standard run still
26
+ * ends on its action budget.
27
+ */
28
+
29
+ /** The share of the wall clock kept for the report, and its bounds in seconds. */
30
+ export const REPORT_RESERVE_SHARE = 0.2;
31
+ export const REPORT_RESERVE_MIN_SECONDS = 60;
32
+ export const REPORT_RESERVE_MAX_SECONDS = 240;
33
+
34
+ /** The persona is told once that time is short when this share of the limit is left. */
35
+ export const NEARLY_DUE_SHARE = 0.15;
36
+
37
+ /**
38
+ * Seconds of the wall clock kept back for the report.
39
+ *
40
+ * @param {number} wallSeconds
41
+ * @returns {number}
42
+ */
43
+ export function reportReserveSeconds(wallSeconds) {
44
+ return Math.min(REPORT_RESERVE_MAX_SECONDS, Math.max(REPORT_RESERVE_MIN_SECONDS, Math.round(wallSeconds * REPORT_RESERVE_SHARE)));
45
+ }
46
+
47
+ /**
48
+ * The limits of a run, from what the server registered and what was asked for.
49
+ *
50
+ * `runLength` is `tier:<tier>:<minutes>` when the play length was chosen
51
+ * (`--tier medium --minutes 30`); the minutes are the play time sold. A tier
52
+ * with no minutes, a long run and a standard run have a wall clock only.
53
+ *
54
+ * @param {{runLength?: string}|null} options
55
+ * @param {{limits?: {wall_seconds?: number}}|null} run the run as register answered it
56
+ * @returns {{playSeconds: number|null, wallSeconds: number|null}|null} null when the run has no clock to stop on
57
+ */
58
+ export function playClockFor(options, run) {
59
+ const length = options && typeof options.runLength === 'string' ? options.runLength : '';
60
+ const tier = /^tier:(?:jam|small|medium|large)(?::([1-9][0-9]{0,2}))?$/.exec(length);
61
+ // Only a run sold by the minute stops on the clock: a standard or a long
62
+ // run is sold by its action budget, as it always was.
63
+ if (!tier) return null;
64
+ const wall = Number(run && run.limits && run.limits.wall_seconds);
65
+ const minutes = tier[1] ? Number(tier[1]) : null;
66
+ const limits = { playSeconds: minutes ? minutes * 60 : null, wallSeconds: Number.isFinite(wall) && wall > 0 ? wall : null };
67
+ return limits.playSeconds === null && limits.wallSeconds === null ? null : limits;
68
+ }
69
+
70
+ /**
71
+ * @param {Object} [options]
72
+ * @param {number|null} [options.playSeconds] play time sold, or null
73
+ * @param {number|null} [options.wallSeconds] the server's wall clock for the run, or null
74
+ * @param {number} [options.startedAt] when the run's wall clock began, in ms; now when left out
75
+ * @param {() => number} [options.now]
76
+ */
77
+ export function createPlayClock({ playSeconds = null, wallSeconds = null, startedAt = null, now = Date.now } = {}) {
78
+ const began = Number.isFinite(startedAt) ? startedAt : now();
79
+ let ranMs = 0;
80
+ let thoughtMs = 0;
81
+ let frozenMs = 0;
82
+ const playLimit = Number.isFinite(playSeconds) && playSeconds > 0 ? playSeconds : null;
83
+ const wallLimit = Number.isFinite(wallSeconds) && wallSeconds > 0 ? wallSeconds : null;
84
+ const wallStop = wallLimit === null ? null : Math.max(1, wallLimit - reportReserveSeconds(wallLimit));
85
+ const playedSeconds = () => (ranMs + thoughtMs) / 1000;
86
+ const wallUsed = () => (now() - began) / 1000;
87
+ return {
88
+ /** Count time the game ran under an input or a wait. */
89
+ ran(ms) {
90
+ if (Number.isFinite(ms) && ms > 0) ranMs += ms;
91
+ },
92
+ /**
93
+ * Count the time the model took to answer: play time when the game ran
94
+ * through it, none when the game stood still.
95
+ *
96
+ * @param {number} ms
97
+ * @param {boolean} frozen the game stood still meanwhile
98
+ */
99
+ thought(ms, frozen) {
100
+ if (!Number.isFinite(ms) || ms <= 0) return;
101
+ if (frozen) frozenMs += ms;
102
+ else thoughtMs += ms;
103
+ },
104
+ /** @returns {'play'|'wall'|null} which limit is used up, or null */
105
+ due() {
106
+ if (playLimit !== null && playedSeconds() >= playLimit) return 'play';
107
+ if (wallStop !== null && wallUsed() >= wallStop) return 'wall';
108
+ return null;
109
+ },
110
+ /** True once when little of either limit is left. */
111
+ nearlyDue() {
112
+ if (playLimit !== null && playedSeconds() >= playLimit * (1 - NEARLY_DUE_SHARE)) return true;
113
+ if (wallStop !== null && wallUsed() >= wallStop * (1 - NEARLY_DUE_SHARE)) return true;
114
+ return false;
115
+ },
116
+ read() {
117
+ return {
118
+ play_seconds: Number(playedSeconds().toFixed(1)),
119
+ play_seconds_max: playLimit,
120
+ wall_seconds: Number(wallUsed().toFixed(1)),
121
+ wall_seconds_max: wallLimit,
122
+ wall_seconds_stop: wallStop,
123
+ frozen_seconds: Number((frozenMs / 1000).toFixed(1)),
124
+ counts: frozenMs > thoughtMs ? 'the time the game ran under inputs and waits, the game standing still while the model thought' : 'the time inputs and waits took and the time the model thought, the game running throughout'
125
+ };
126
+ }
127
+ };
128
+ }
129
+
130
+ const minutes = seconds => {
131
+ const value = seconds / 60;
132
+ return Number.isInteger(value) ? String(value) : value.toFixed(1);
133
+ };
134
+
135
+ /**
136
+ * Why play stopped on the clock, as report.json's quit_detail.
137
+ *
138
+ * @param {'play'|'wall'} which
139
+ * @param {Object} read the clock's own reading
140
+ * @returns {string}
141
+ */
142
+ export function clockStopDetail(which, read) {
143
+ if (which === 'play') return `the ${minutes(read.play_seconds_max)} minutes of play this run was sold ran out (${minutes(Math.round(read.play_seconds))} minutes played, counting ${read.counts})`;
144
+ return `the run's wall clock of ${minutes(read.wall_seconds_max)} minutes was nearly used, and the rest is kept for the report (${minutes(Math.round(read.play_seconds))} minutes of play, counting ${read.counts})`;
145
+ }
146
+
147
+ /**
148
+ * What the persona is told once when time is short.
149
+ *
150
+ * @param {Object} read
151
+ * @returns {string}
152
+ */
153
+ export function timeLeftAside(read) {
154
+ const left = [];
155
+ if (read.play_seconds_max !== null) left.push(Math.max(0, read.play_seconds_max - read.play_seconds));
156
+ if (read.wall_seconds_stop !== null) left.push(Math.max(0, read.wall_seconds_stop - read.wall_seconds));
157
+ const seconds = left.length > 0 ? Math.min(...left) : 0;
158
+ const about = seconds >= 90 ? `about ${Math.round(seconds / 60)} minutes` : seconds >= 45 ? 'about a minute' : 'less than a minute';
159
+ return `The time this session has is nearly used: ${about} of play are left. Spend them on what matters most, and write a note for anything you saw and have not written down. When the time is up the report is written from your notes.`;
160
+ }
161
+
162
+ export default { createPlayClock, playClockFor, clockStopDetail, timeLeftAside, reportReserveSeconds };