| 1 | // Mide si un juego generado está VIVO, no cuánto pinta. |
| 2 | // |
| 3 | // Contar píxeles premiaba una pantalla de GAME OVER a toda página por encima |
| 4 | // de un juego jugable (lo vimos: el breakout+jurado marcaba 480.000 px porque |
| 5 | // arrancaba ya terminado). Aquí se comprueban cinco cosas independientes: |
| 6 | // |
| 7 | // pinta · dibuja algo en el canvas |
| 8 | // vivo · lo que dibuja CAMBIA solo entre dos instantes (hay bucle) |
| 9 | // responde · cambia también al recibir teclas y ratón |
| 10 | // arranca · no muestra «game over» nada más abrir |
| 11 | // sinErrores · no lanza excepciones de JS |
| 12 | // |
| 13 | // Puntuación = cuántas cumple (0-5). |
| 14 | const crypto = require('crypto'); |
| 15 | |
| 16 | const HUELLA = `(() => { |
| 17 | const c = document.querySelector('canvas'); |
| 18 | if (!c) return null; |
| 19 | const x = c.getContext('2d'); |
| 20 | if (!x) return null; |
| 21 | const d = x.getImageData(0, 0, c.width, c.height).data; |
| 22 | let nb = 0, suma = 0; |
| 23 | for (let i = 0; i < d.length; i += 16) { // muestreo: basta para detectar cambio |
| 24 | if (d[i] || d[i+1] || d[i+2]) nb++; |
| 25 | suma = (suma * 31 + d[i] + d[i+1] * 3 + d[i+2] * 7) >>> 0; |
| 26 | } |
| 27 | return { nb, huella: suma, w: c.width, h: c.height }; |
| 28 | })()`; |
| 29 | |
| 30 | async function medir(page, url) { |
| 31 | const errores = []; |
| 32 | page.on('pageerror', e => errores.push(e.message.slice(0, 80))); |
| 33 | await page.goto(url, { waitUntil: 'domcontentloaded' }).catch(() => {}); |
| 34 | await page.waitForTimeout(1500); |
| 35 | |
| 36 | const a = await page.evaluate(HUELLA).catch(() => null); |
| 37 | const txtInicio = await page.evaluate(() => (document.body.innerText || '').toLowerCase()).catch(() => ''); |
| 38 | await page.waitForTimeout(2200); |
| 39 | const b = await page.evaluate(HUELLA).catch(() => null); |
| 40 | |
| 41 | // ¿reacciona a la entrada? teclas + ratón, que es lo que usan estos juegos |
| 42 | await page.evaluate(() => { |
| 43 | const c = document.querySelector('canvas'); if (!c) return; |
| 44 | const r = c.getBoundingClientRect(); |
| 45 | for (const k of ['ArrowLeft', 'ArrowRight', 'ArrowUp', ' ', 'w', 's']) { |
| 46 | document.dispatchEvent(new KeyboardEvent('keydown', { key: k, code: k === ' ' ? 'Space' : k, bubbles: true })); |
| 47 | window.dispatchEvent(new KeyboardEvent('keydown', { key: k, code: k === ' ' ? 'Space' : k, bubbles: true })); |
| 48 | } |
| 49 | for (let i = 0; i < 6; i++) { |
| 50 | const ev = new MouseEvent('mousemove', { clientX: r.left + 40 + i * 40, clientY: r.top + r.height / 2, bubbles: true }); |
| 51 | document.dispatchEvent(ev); c.dispatchEvent(ev); |
| 52 | } |
| 53 | c.dispatchEvent(new MouseEvent('click', { bubbles: true })); |
| 54 | }).catch(() => {}); |
| 55 | await page.waitForTimeout(1600); |
| 56 | const c = await page.evaluate(HUELLA).catch(() => null); |
| 57 | |
| 58 | // cuarta lectura sin mandar nada: un juego que espera a la primera tecla |
| 59 | // (que es lo correcto) no puede contarse como muerto por esperar |
| 60 | await page.waitForTimeout(1400); |
| 61 | const d = await page.evaluate(HUELLA).catch(() => null); |
| 62 | const texto = await page.evaluate(() => (document.body.innerText || '').toLowerCase()).catch(() => ''); |
| 63 | // «sobrevive»: la partida no puede acabarse sola sin que el jugador toque nada |
| 64 | const FIN = /game\s*over|has perdido|fin del juego|puntuaci[óo]n final/; |
| 65 | const finSolo = FIN.test(texto); // al final: ¿se acabó sola la partida? |
| 66 | // el «game over» pintado EN el canvas no está en el DOM: se detecta porque el |
| 67 | // juego no cambia nada y llena mucho, así que se mira también el texto del DOM |
| 68 | const gameOverDOM = FIN.test(txtInicio); // al principio: ¿arrancó ya terminado? |
| 69 | |
| 70 | const pinta = !!(a && a.nb > 0); |
| 71 | const vivo = !!((a && b && a.huella !== b.huella) || (c && d && c.huella !== d.huella)); |
| 72 | const responde = !!(b && c && b.huella !== c.huella); |
| 73 | const arranca = !gameOverDOM; |
| 74 | const sobrevive = !finSolo; |
| 75 | const sinErrores = errores.length === 0; |
| 76 | |
| 77 | // «pinta» es puerta de entrada: sin dibujar nada, no hay juego que puntuar. |
| 78 | // Si no, un HTML en blanco se llevaba 2 puntos gratis por «no muestra game |
| 79 | // over» y «sin errores», que se cumplen trivialmente cuando no hay nada. |
| 80 | const puntos = pinta ? [pinta, vivo, responde, arranca, sobrevive, sinErrores].filter(Boolean).length : 0; |
| 81 | return { |
| 82 | pinta, vivo, responde, arranca, sobrevive, sinErrores, puntos, |
| 83 | px: a ? a.nb : 0, tam: a ? a.w + 'x' + a.h : '—', error: errores[0] || null, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | module.exports = { medir }; |