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.
- package/README.md +280 -66
- package/addons/ravensight_driver/driver.gd +666 -21
- package/docs/ADAPTERS.md +11 -7
- package/docs/NATIVE_RUNNER.md +3 -1
- package/docs/godot-driver.md +127 -13
- package/package.json +8 -6
- package/src/api/client.js +4 -3
- package/src/cli.js +84 -25
- package/src/commands/check.js +30 -4
- package/src/commands/fakeCore.js +1 -0
- package/src/commands/hosted.js +41 -12
- package/src/commands/init.js +17 -1
- package/src/commands/run.js +209 -14
- package/src/commands/upload.js +6 -2
- package/src/commands/validate.js +1 -1
- package/src/commands/validateHosted.js +50 -5
- package/src/config.js +33 -2
- package/src/hosted/archivePreflight.js +7 -7
- package/src/hosted/buildUpload.js +89 -8
- package/src/hosted/recordedRun.js +141 -26
- package/src/hosted/status.js +19 -10
- package/src/run/actionSelector.js +14 -3
- package/src/run/aggregate.js +57 -3
- package/src/run/anygame/controlbook.js +179 -0
- package/src/run/anygame/genre.js +179 -0
- package/src/run/anygame/index.js +258 -0
- package/src/run/anygame/language.js +336 -0
- package/src/run/anygame/memory.js +330 -0
- package/src/run/anygame/shared-hashes.json +10 -0
- package/src/run/anygame/shared.js +24 -0
- package/src/run/anygame/sound.js +342 -0
- package/src/run/args.js +75 -8
- package/src/run/choiceCheck.js +312 -0
- package/src/run/context.js +254 -13
- package/src/run/drivers/godot-observation.js +93 -6
- package/src/run/drivers/godot.js +769 -46
- package/src/run/drivers/recording.js +24 -4
- package/src/run/drivers/web.js +1242 -77
- package/src/run/edgePass.js +731 -0
- package/src/run/fixLoop.js +5 -4
- package/src/run/focus.js +65 -0
- package/src/run/gamepad.js +108 -0
- package/src/run/godotEdgePass.js +235 -0
- package/src/run/godotTesterProbes.js +192 -0
- package/src/run/headings.js +21 -0
- package/src/run/honesty.js +544 -0
- package/src/run/inputs.js +256 -0
- package/src/run/marks.js +142 -0
- package/src/run/personaLoop.js +1000 -79
- package/src/run/phone.js +7 -0
- package/src/run/pixels.js +215 -0
- package/src/run/playClock.js +162 -0
- package/src/run/readiness.js +585 -0
- package/src/run/refusal.js +67 -0
- package/src/run/regions.js +119 -0
- package/src/run/report.js +14 -2
- package/src/run/reportChecks.js +472 -0
- package/src/run/resume.js +3 -2
- package/src/run/screenshots.js +161 -0
- package/src/run/strip.js +114 -0
- package/src/run/testerLedger.js +312 -0
- package/src/run/testerProbes.js +584 -0
- package/src/run/thinRun.js +102 -0
- package/src/run/timeFreeze.js +79 -0
- package/src/run/tools.js +454 -29
- package/src/runDefaults.js +24 -2
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// The inputs a persona can send beyond a click and a key tap, in one place:
|
|
2
|
+
// the limits, the names a model writes for keys, and the shape checks both
|
|
3
|
+
// drivers share. A driver turns these into its own events; nothing here
|
|
4
|
+
// touches a game.
|
|
5
|
+
|
|
6
|
+
/** The longest a key or a mouse button may be held in one action. */
|
|
7
|
+
export const MAX_HOLD_MS = 5000;
|
|
8
|
+
|
|
9
|
+
/** Extra keys one key action may hold together with `key`: six keys in all. */
|
|
10
|
+
export const MAX_WITH_KEYS = 5;
|
|
11
|
+
|
|
12
|
+
/** A held key starts repeating after this long, as a keyboard does. */
|
|
13
|
+
export const KEY_REPEAT_DELAY_MS = 500;
|
|
14
|
+
|
|
15
|
+
/** A repeating key repeats this often (about thirty a second). */
|
|
16
|
+
export const KEY_REPEAT_INTERVAL_MS = 33;
|
|
17
|
+
|
|
18
|
+
/** Two presses this close together are a double click. */
|
|
19
|
+
export const DOUBLE_CLICK_GAP_MS = 80;
|
|
20
|
+
|
|
21
|
+
/** The fewest and most intermediate pointer moves in one drag. */
|
|
22
|
+
export const MIN_DRAG_STEPS = 2;
|
|
23
|
+
export const MAX_DRAG_STEPS = 60;
|
|
24
|
+
export const DEFAULT_DRAG_STEPS = 12;
|
|
25
|
+
|
|
26
|
+
/** How long a drag takes from press to release, by default and at most. */
|
|
27
|
+
export const DEFAULT_DRAG_MS = 400;
|
|
28
|
+
export const MAX_DRAG_MS = 5000;
|
|
29
|
+
|
|
30
|
+
/** The most waypoints a drag may pass through between its two ends. */
|
|
31
|
+
export const MAX_DRAG_WAYPOINTS = 8;
|
|
32
|
+
|
|
33
|
+
/** The furthest one scroll moves, in wheel pixels, on either axis. */
|
|
34
|
+
export const MAX_SCROLL_PX = 4000;
|
|
35
|
+
|
|
36
|
+
/** One wheel event carries this much; a longer scroll is several events. */
|
|
37
|
+
export const WHEEL_NOTCH_PX = 120;
|
|
38
|
+
|
|
39
|
+
/** The most inputs in one timed sequence, and the longest it may run. */
|
|
40
|
+
export const MAX_SEQUENCE_STEPS = 24;
|
|
41
|
+
export const MAX_SEQUENCE_MS = 6000;
|
|
42
|
+
|
|
43
|
+
/** A sequence step with no hold_ms is a tap this long. */
|
|
44
|
+
export const SEQUENCE_TAP_MS = 50;
|
|
45
|
+
|
|
46
|
+
export const MOUSE_BUTTONS = Object.freeze(['left', 'right', 'middle']);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Names a model writes for a key, lower case, mapped to the name the browser
|
|
50
|
+
* driver sends. A single character is never looked up here: "a", ",", "<" and
|
|
51
|
+
* "1" are sent as they are.
|
|
52
|
+
*/
|
|
53
|
+
const WEB_KEY_ALIASES = Object.freeze({
|
|
54
|
+
esc: 'Escape', escape: 'Escape',
|
|
55
|
+
return: 'Enter', enter: 'Enter',
|
|
56
|
+
space: 'Space', spacebar: 'Space',
|
|
57
|
+
tab: 'Tab', backspace: 'Backspace',
|
|
58
|
+
del: 'Delete', delete: 'Delete', ins: 'Insert', insert: 'Insert',
|
|
59
|
+
home: 'Home', end: 'End',
|
|
60
|
+
pgup: 'PageUp', pageup: 'PageUp', pgdn: 'PageDown', pgdown: 'PageDown', pagedown: 'PageDown',
|
|
61
|
+
up: 'ArrowUp', down: 'ArrowDown', left: 'ArrowLeft', right: 'ArrowRight',
|
|
62
|
+
arrowup: 'ArrowUp', arrowdown: 'ArrowDown', arrowleft: 'ArrowLeft', arrowright: 'ArrowRight',
|
|
63
|
+
shift: 'Shift', ctrl: 'Control', control: 'Control', alt: 'Alt', option: 'Alt', opt: 'Alt',
|
|
64
|
+
meta: 'Meta', cmd: 'Meta', command: 'Meta', super: 'Meta', win: 'Meta',
|
|
65
|
+
capslock: 'CapsLock', numlock: 'NumLock', scrolllock: 'ScrollLock',
|
|
66
|
+
printscreen: 'PrintScreen', pause: 'Pause', contextmenu: 'ContextMenu', menu: 'ContextMenu',
|
|
67
|
+
comma: ',', period: '.', dot: '.', slash: '/', backslash: '\\', semicolon: ';', quote: '\'', apostrophe: '\'',
|
|
68
|
+
backquote: '`', backtick: '`', grave: '`', tilde: '~', minus: '-', dash: '-', hyphen: '-', equal: '=', equals: '=', plus: '+',
|
|
69
|
+
bracketleft: '[', bracketright: ']', less: '<', lt: '<', greater: '>', gt: '>',
|
|
70
|
+
numpadenter: 'NumpadEnter', numpadadd: 'NumpadAdd', numpadsubtract: 'NumpadSubtract',
|
|
71
|
+
numpadmultiply: 'NumpadMultiply', numpaddivide: 'NumpadDivide', numpaddecimal: 'NumpadDecimal'
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* One key name as the browser driver takes it: "esc" is Escape, "f5" is F5,
|
|
76
|
+
* "numpad 4" is Numpad4, "up" is ArrowUp. A single character and a name that
|
|
77
|
+
* is already right pass through.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} name
|
|
80
|
+
* @returns {string}
|
|
81
|
+
*/
|
|
82
|
+
export function webKeyName(name) {
|
|
83
|
+
const raw = String(name == null ? '' : name);
|
|
84
|
+
// A single character is itself, the space bar included.
|
|
85
|
+
if ([...raw].length === 1) return raw === ' ' ? 'Space' : raw;
|
|
86
|
+
const value = raw.trim();
|
|
87
|
+
const lower = value.toLowerCase().replace(/[\s_-]+/g, '');
|
|
88
|
+
if (Object.hasOwn(WEB_KEY_ALIASES, lower)) return WEB_KEY_ALIASES[lower];
|
|
89
|
+
const fn = lower.match(/^f(\d{1,2})$/);
|
|
90
|
+
if (fn && Number(fn[1]) >= 1 && Number(fn[1]) <= 24) return `F${Number(fn[1])}`;
|
|
91
|
+
const pad = lower.match(/^(?:numpad|num|kp)(\d)$/);
|
|
92
|
+
if (pad) return `Numpad${pad[1]}`;
|
|
93
|
+
const digit = lower.match(/^digit(\d)$/);
|
|
94
|
+
if (digit) return `Digit${digit[1]}`;
|
|
95
|
+
const letter = lower.match(/^key([a-z])$/);
|
|
96
|
+
if (letter) return `Key${letter[1].toUpperCase()}`;
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {unknown} value
|
|
102
|
+
* @returns {'left'|'right'|'middle'}
|
|
103
|
+
*/
|
|
104
|
+
export function mouseButton(value) {
|
|
105
|
+
const name = String(value == null ? '' : value).trim().toLowerCase();
|
|
106
|
+
return MOUSE_BUTTONS.includes(name) ? name : 'left';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @param {unknown} value @param {number} max @returns {number} a whole number of milliseconds, 0 to max */
|
|
110
|
+
export function boundedMs(value, max) {
|
|
111
|
+
const ms = Math.round(Number(value) || 0);
|
|
112
|
+
return Math.min(Math.max(ms, 0), max);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The steps of a timed sequence, checked and in time order.
|
|
117
|
+
*
|
|
118
|
+
* Each step is one input at `atMs` after the start: a key (held for `holdMs`,
|
|
119
|
+
* a tap when there is none) or a mouse press at a point. The whole sequence
|
|
120
|
+
* is refused when one step is not readable, so half a combo is never played.
|
|
121
|
+
*
|
|
122
|
+
* @param {unknown} raw
|
|
123
|
+
* @returns {{steps: Array<Object>, durationMs: number}|{error: string}}
|
|
124
|
+
*/
|
|
125
|
+
export function sequenceSteps(raw) {
|
|
126
|
+
if (!Array.isArray(raw) || raw.length === 0) return { error: 'a sequence is a list of steps, each with at_ms and a key (or x and y)' };
|
|
127
|
+
if (raw.length > MAX_SEQUENCE_STEPS) return { error: `a sequence holds at most ${MAX_SEQUENCE_STEPS} steps` };
|
|
128
|
+
const steps = [];
|
|
129
|
+
for (const [index, entry] of raw.entries()) {
|
|
130
|
+
if (!entry || typeof entry !== 'object') return { error: `sequence step ${index + 1} is not an object` };
|
|
131
|
+
const atMs = Math.round(Number(entry.at_ms ?? entry.atMs));
|
|
132
|
+
if (!Number.isFinite(atMs) || atMs < 0) return { error: `sequence step ${index + 1} needs at_ms, the time it is pressed in milliseconds from the start` };
|
|
133
|
+
const holdMs = Math.max(boundedMs(entry.hold_ms ?? entry.holdMs, MAX_HOLD_MS), SEQUENCE_TAP_MS);
|
|
134
|
+
if (atMs + holdMs > MAX_SEQUENCE_MS) return { error: `a sequence runs at most ${MAX_SEQUENCE_MS} ms; step ${index + 1} ends at ${atMs + holdMs}` };
|
|
135
|
+
const hasPoint = Number.isFinite(Number(entry.x)) && Number.isFinite(Number(entry.y)) && entry.x !== null && entry.y !== null && entry.x !== '' && entry.y !== '';
|
|
136
|
+
const key = typeof entry.key === 'string' && entry.key !== '' ? entry.key : null;
|
|
137
|
+
if (!key && !hasPoint) return { error: `sequence step ${index + 1} needs a key, or x and y for a mouse press` };
|
|
138
|
+
steps.push(key
|
|
139
|
+
? { atMs, holdMs, key }
|
|
140
|
+
: { atMs, holdMs, x: Number(entry.x), y: Number(entry.y), button: mouseButton(entry.button) });
|
|
141
|
+
}
|
|
142
|
+
steps.sort((a, b) => a.atMs - b.atMs);
|
|
143
|
+
return { steps, durationMs: Math.max(...steps.map(step => step.atMs + step.holdMs)) };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* A sequence as the presses and releases it is made of, in time order. A
|
|
148
|
+
* release sorts before a press at the same instant, so a key tapped twice in
|
|
149
|
+
* a row comes up before it goes down again.
|
|
150
|
+
*
|
|
151
|
+
* @param {Array<Object>} steps from {@link sequenceSteps}
|
|
152
|
+
* @returns {Array<{atMs: number, down: boolean, step: Object}>}
|
|
153
|
+
*/
|
|
154
|
+
export function sequenceEvents(steps) {
|
|
155
|
+
const events = [];
|
|
156
|
+
for (const step of steps) {
|
|
157
|
+
events.push({ atMs: step.atMs, down: true, step });
|
|
158
|
+
events.push({ atMs: step.atMs + step.holdMs, down: false, step });
|
|
159
|
+
}
|
|
160
|
+
return events.sort((a, b) => a.atMs - b.atMs || Number(a.down) - Number(b.down));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The points a drag passes through after the press: straight lines from the
|
|
165
|
+
* start through any waypoints to the end, cut into `steps` moves. The last
|
|
166
|
+
* point is the end itself.
|
|
167
|
+
*
|
|
168
|
+
* @param {{x: number, y: number}} from
|
|
169
|
+
* @param {{x: number, y: number}} to
|
|
170
|
+
* @param {{steps?: number, via?: Array<{x: number, y: number}>}} [options]
|
|
171
|
+
* @returns {Array<{x: number, y: number}>}
|
|
172
|
+
*/
|
|
173
|
+
export function dragPath(from, to, { steps = DEFAULT_DRAG_STEPS, via = [] } = {}) {
|
|
174
|
+
const count = Math.min(Math.max(Math.round(Number(steps) || DEFAULT_DRAG_STEPS), MIN_DRAG_STEPS), MAX_DRAG_STEPS);
|
|
175
|
+
const corners = [from, ...via.slice(0, MAX_DRAG_WAYPOINTS), to];
|
|
176
|
+
const lengths = [];
|
|
177
|
+
let total = 0;
|
|
178
|
+
for (let i = 1; i < corners.length; i += 1) {
|
|
179
|
+
const length = Math.hypot(corners[i].x - corners[i - 1].x, corners[i].y - corners[i - 1].y);
|
|
180
|
+
lengths.push(length);
|
|
181
|
+
total += length;
|
|
182
|
+
}
|
|
183
|
+
if (total === 0) return Array.from({ length: count }, () => ({ x: to.x, y: to.y }));
|
|
184
|
+
const points = [];
|
|
185
|
+
for (let n = 1; n <= count; n += 1) {
|
|
186
|
+
let distance = (total * n) / count;
|
|
187
|
+
let leg = 0;
|
|
188
|
+
while (leg < lengths.length - 1 && distance > lengths[leg]) {
|
|
189
|
+
distance -= lengths[leg];
|
|
190
|
+
leg += 1;
|
|
191
|
+
}
|
|
192
|
+
const share = lengths[leg] === 0 ? 1 : Math.min(distance / lengths[leg], 1);
|
|
193
|
+
points.push({
|
|
194
|
+
x: Math.round(corners[leg].x + (corners[leg + 1].x - corners[leg].x) * share),
|
|
195
|
+
y: Math.round(corners[leg].y + (corners[leg + 1].y - corners[leg].y) * share)
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
points[points.length - 1] = { x: Math.round(to.x), y: Math.round(to.y) };
|
|
199
|
+
return points;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* A scroll as the wheel events it is sent as: notches of at most
|
|
204
|
+
* WHEEL_NOTCH_PX, the way a wheel turns.
|
|
205
|
+
*
|
|
206
|
+
* @param {number} dx
|
|
207
|
+
* @param {number} dy
|
|
208
|
+
* @returns {Array<{dx: number, dy: number}>}
|
|
209
|
+
*/
|
|
210
|
+
export function wheelNotches(dx, dy) {
|
|
211
|
+
const x = Math.min(Math.max(Math.round(Number(dx) || 0), -MAX_SCROLL_PX), MAX_SCROLL_PX);
|
|
212
|
+
const y = Math.min(Math.max(Math.round(Number(dy) || 0), -MAX_SCROLL_PX), MAX_SCROLL_PX);
|
|
213
|
+
const count = Math.max(Math.ceil(Math.abs(x) / WHEEL_NOTCH_PX), Math.ceil(Math.abs(y) / WHEEL_NOTCH_PX), 1);
|
|
214
|
+
const notches = [];
|
|
215
|
+
let sentX = 0;
|
|
216
|
+
let sentY = 0;
|
|
217
|
+
for (let n = 1; n <= count; n += 1) {
|
|
218
|
+
const nextX = Math.round((x * n) / count);
|
|
219
|
+
const nextY = Math.round((y * n) / count);
|
|
220
|
+
notches.push({ dx: nextX - sentX, dy: nextY - sentY });
|
|
221
|
+
sentX = nextX;
|
|
222
|
+
sentY = nextY;
|
|
223
|
+
}
|
|
224
|
+
return notches;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* What a run could not send, by driver, in the words a report uses under
|
|
229
|
+
* "What this run didn't cover". These are limits of the runner and never
|
|
230
|
+
* findings about the game.
|
|
231
|
+
*/
|
|
232
|
+
export const MISSING_INPUTS = Object.freeze({
|
|
233
|
+
playwright_web: Object.freeze([
|
|
234
|
+
'touch and two finger gestures, on a run played with the mouse (--phone plays with touch on a phone sized screen)',
|
|
235
|
+
'a gamepad or controller, in a game that never looks for one (a game that reads gamepads is offered a virtual one)',
|
|
236
|
+
'sound: the run cannot hear, so music, effects and spoken cues were not judged',
|
|
237
|
+
'mouse look with pointer lock (a headless browser refuses the lock)',
|
|
238
|
+
'a microphone, a camera, tilt or motion'
|
|
239
|
+
]),
|
|
240
|
+
godot_driver: Object.freeze([
|
|
241
|
+
'touch and multi-touch gestures',
|
|
242
|
+
'a gamepad or controller',
|
|
243
|
+
'sound: the run cannot hear, so music, effects and spoken cues were not judged',
|
|
244
|
+
'captured mouse look (relative mouse motion with the cursor captured)'
|
|
245
|
+
])
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @param {string} driverName
|
|
250
|
+
* @returns {Array<string>}
|
|
251
|
+
*/
|
|
252
|
+
export function missingInputs(driverName) {
|
|
253
|
+
return [...(MISSING_INPUTS[driverName] || [])];
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export default { webKeyName, mouseButton, sequenceSteps, sequenceEvents, dragPath, wheelNotches, missingInputs };
|
package/src/run/marks.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Named points on a canvas game's screen, so a persona can say where to click
|
|
2
|
+
// without guessing pixel coordinates: the shapes the screenshot shows
|
|
3
|
+
// (./regions.js) as marks m1 to m8, and a labelled grid of cells A1 to H5.
|
|
4
|
+
// Ported from the cloud runner. Only the model's copy of the screen has them
|
|
5
|
+
// drawn on it; the game page is never touched, so the video and the evidence
|
|
6
|
+
// screenshots show the game as a player saw it.
|
|
7
|
+
|
|
8
|
+
import { stripHtml, stripTokens } from './strip.js';
|
|
9
|
+
|
|
10
|
+
export const GRID = Object.freeze({ cols: 8, rows: 5 });
|
|
11
|
+
const COLS = 'ABCDEFGH';
|
|
12
|
+
const MARK = /^m([1-9])$/i;
|
|
13
|
+
const CELL = /^([A-H])([1-5])$/i;
|
|
14
|
+
|
|
15
|
+
/** True when a click target is a mark or a grid cell, by its shape alone. */
|
|
16
|
+
export function isMarkTarget(target) {
|
|
17
|
+
const value = String(target == null ? '' : target).trim();
|
|
18
|
+
return MARK.test(value) || CELL.test(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The centre of grid cell `C3` (column letter left to right, row number top to bottom), or null. */
|
|
22
|
+
export function cellPoint(id, { width, height, grid = GRID }) {
|
|
23
|
+
const match = CELL.exec(String(id || '').trim());
|
|
24
|
+
if (!match) return null;
|
|
25
|
+
const c = COLS.indexOf(match[1].toUpperCase());
|
|
26
|
+
const r = Number(match[2]) - 1;
|
|
27
|
+
if (c < 0 || c >= grid.cols || r < 0 || r >= grid.rows) return null;
|
|
28
|
+
return { x: Math.round(((c + 0.5) * width) / grid.cols), y: Math.round(((r + 0.5) * height) / grid.rows) };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Regions as marks: `[{ id: 'm1', x, y, width, height, label }]`. */
|
|
32
|
+
export function marksFor(regions) {
|
|
33
|
+
return (Array.isArray(regions) ? regions : []).slice(0, 9).map((region, i) => ({ id: `m${i + 1}`, x: region.x, y: region.y, width: region.width, height: region.height, label: region.label }));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A mark (`m2`) or a grid cell (`C3`) as a point on the screen, or null when
|
|
38
|
+
* this screen has no such mark.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} id
|
|
41
|
+
* @param {{marks: Array<Object>, width: number, height: number}} screen
|
|
42
|
+
* @returns {{x: number, y: number, mark: string}|null}
|
|
43
|
+
*/
|
|
44
|
+
export function resolveMark(id, screen) {
|
|
45
|
+
const value = String(id == null ? '' : id).trim();
|
|
46
|
+
if (!screen) return null;
|
|
47
|
+
if (MARK.test(value)) {
|
|
48
|
+
const mark = (screen.marks || []).find(candidate => candidate.id === value.toLowerCase());
|
|
49
|
+
return mark ? { x: mark.x, y: mark.y, mark: mark.id } : null;
|
|
50
|
+
}
|
|
51
|
+
const point = cellPoint(value, screen);
|
|
52
|
+
return point ? { ...point, mark: value.toUpperCase() } : null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The marks as text for the screen the persona reads. */
|
|
56
|
+
export function marksText(marks) {
|
|
57
|
+
const lines = [`The picture has a grid drawn on it: cells A1 to ${COLS[GRID.cols - 1]}${GRID.rows} (columns A to ${COLS[GRID.cols - 1]} left to right, rows 1 to ${GRID.rows} top to bottom). To click a cell's centre, pass the cell as the click target, such as C3. The grid and the marks are drawn by the runner on your copy only; they are not part of the game.`];
|
|
58
|
+
if (marks.length > 0) lines.push(`Marked shapes (measured from the picture, text not read), clicked the same way, such as m1: ${marks.map(mark => `${mark.id}: ${mark.label}`).join('; ')}.`);
|
|
59
|
+
return lines.join('\n');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const esc = value => String(value).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The overlay page: the frame as the background, the grid lines and labels,
|
|
66
|
+
* and each mark's box and tag.
|
|
67
|
+
*
|
|
68
|
+
* @param {{data: Buffer, mediaType: string, width: number, height: number}} frame
|
|
69
|
+
* @param {Array<Object>} marks
|
|
70
|
+
* @returns {string}
|
|
71
|
+
*/
|
|
72
|
+
export function overlayHtml(frame, marks, { grid = GRID } = {}) {
|
|
73
|
+
const { width, height } = frame;
|
|
74
|
+
const parts = [];
|
|
75
|
+
for (let c = 1; c < grid.cols; c += 1) parts.push(`<div style="position:absolute;left:${Math.round((c * width) / grid.cols)}px;top:0;width:1px;height:${height}px;background:rgba(0,170,255,.35)"></div>`);
|
|
76
|
+
for (let r = 1; r < grid.rows; r += 1) parts.push(`<div style="position:absolute;top:${Math.round((r * height) / grid.rows)}px;left:0;height:1px;width:${width}px;background:rgba(0,170,255,.35)"></div>`);
|
|
77
|
+
for (let c = 0; c < grid.cols; c += 1) {
|
|
78
|
+
for (let r = 0; r < grid.rows; r += 1) {
|
|
79
|
+
parts.push(`<div style="position:absolute;left:${Math.round((c * width) / grid.cols) + 2}px;top:${Math.round((r * height) / grid.rows) + 2}px;font:bold 11px sans-serif;color:#0af;background:rgba(255,255,255,.7);padding:0 2px">${COLS[c]}${r + 1}</div>`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
for (const mark of marks) {
|
|
83
|
+
const x0 = Math.round(mark.x - mark.width / 2);
|
|
84
|
+
const y0 = Math.round(mark.y - mark.height / 2);
|
|
85
|
+
parts.push(`<div style="position:absolute;left:${x0}px;top:${y0}px;width:${Math.round(mark.width)}px;height:${Math.round(mark.height)}px;border:2px solid #f0f;box-sizing:border-box"></div>`);
|
|
86
|
+
parts.push(`<div style="position:absolute;left:${x0}px;top:${Math.max(0, y0 - 15)}px;font:bold 12px sans-serif;color:#fff;background:#f0f;padding:0 3px">${esc(mark.id)}</div>`);
|
|
87
|
+
}
|
|
88
|
+
return `<!DOCTYPE html><html><body style="margin:0;width:${width}px;height:${height}px;position:relative;overflow:hidden"><img src="data:${frame.mediaType};base64,${frame.data.toString('base64')}" style="position:absolute;left:0;top:0;width:${width}px;height:${height}px">${parts.join('')}</body></html>`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A page of its own for drawing marks: a separate browser context with no
|
|
93
|
+
* script, no network and no recording, made once per run. `close()` it when
|
|
94
|
+
* the run ends.
|
|
95
|
+
*
|
|
96
|
+
* @param {Object} browser a Playwright browser
|
|
97
|
+
* @param {{type: string, quality: number, mediaType: string}} format the frame the model is sent
|
|
98
|
+
*/
|
|
99
|
+
export async function createMarker(browser, format) {
|
|
100
|
+
const context = await browser.newContext({ javaScriptEnabled: false, serviceWorkers: 'block', acceptDownloads: false });
|
|
101
|
+
await context.route('**/*', route => route.abort('blockedbyclient'));
|
|
102
|
+
const page = await context.newPage();
|
|
103
|
+
return {
|
|
104
|
+
/**
|
|
105
|
+
* The model's copy with the grid and the marks drawn on, or the plain
|
|
106
|
+
* frame when anything goes wrong.
|
|
107
|
+
*/
|
|
108
|
+
async draw(frame, marks) {
|
|
109
|
+
try {
|
|
110
|
+
await page.setViewportSize({ width: frame.width, height: frame.height });
|
|
111
|
+
await page.setContent(overlayHtml(frame, marks), { waitUntil: 'load', timeout: 5000 });
|
|
112
|
+
const data = await page.screenshot({ type: format.type, quality: format.quality, timeout: 5000 });
|
|
113
|
+
return { ...frame, data, mediaType: format.mediaType, marked: true };
|
|
114
|
+
} catch {
|
|
115
|
+
return frame;
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
/**
|
|
119
|
+
* The picture with a row of small frames under it (../strip.js), or null
|
|
120
|
+
* when it cannot be drawn.
|
|
121
|
+
*
|
|
122
|
+
* @param {{data: Buffer, mediaType: string, width: number, height: number}} frame
|
|
123
|
+
* @param {Array<{data: Buffer, mediaType?: string, atMs: number}>} frames
|
|
124
|
+
* @returns {Promise<Buffer|null>}
|
|
125
|
+
*/
|
|
126
|
+
async strip(frame, frames) {
|
|
127
|
+
try {
|
|
128
|
+
const size = stripTokens(frame.width, frame.height);
|
|
129
|
+
await page.setViewportSize({ width: size.width, height: size.height });
|
|
130
|
+
await page.setContent(stripHtml(frame, frames), { waitUntil: 'load', timeout: 5000 });
|
|
131
|
+
return await page.screenshot({ type: format.type, quality: format.quality, timeout: 5000 });
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
async close() {
|
|
137
|
+
await context.close().catch(() => {});
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export default { GRID, cellPoint, marksFor, resolveMark, marksText, overlayHtml, createMarker, isMarkTarget };
|