All files / js keylog.js

45.45% Statements 25/55
75% Branches 3/4
28.57% Functions 2/7
45.45% Lines 25/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 5673x 73x 73x 73x 73x 73x             73x 73x 13379x 13379x 73x 73x                         73x 73x                         73x 73x 73x 73x 73x 72x 72x 72x 72x 72x 72x  
import { game } from './gstate.js';
// keylog.js -- Keystroke recording for reproducible game sessions.
// Records every key processed by nhgetch() along with PRNG seed and datetime.
// The resulting JSON can be downloaded via the hamburger menu (dev mode).
 
export function startRecording(seed, datetime, nethackrc) {
    game.gameSeed = seed;
    game.gameDatetime = datetime || null;
    game.gameNethackrc = nethackrc || null;
    game.keylog_keys = [];
    game.keylog_recording = true;
}
 
export function recordKey(ch) {
    if (game.keylog_recording) game.keylog_keys.push(ch);
}
 
export function getKeylog() {
    return {
        version: 1,
        seed: game.gameSeed,
        datetime: game.gameDatetime,
        nethackrc: game.gameNethackrc,
        keys: [...keys],
        metadata: {
            date: new Date().toISOString(),
            source: 'teleport-js',
        },
    };
}
 
export function saveKeylog() {
    const keylog = getKeylog();
    const filename = `keylog_seed${keylog.seed}_${Date.now()}.json`;
    const json = JSON.stringify(keylog, null, 2);
    const blob = new Blob([json], { type: 'application/json' });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = filename;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    URL.revokeObjectURL(a.href);
}
 
export function isRecording() { return game.keylog_recording; }
export function getKeyCount() { return game.keylog_keys.length; }
 
export function init_keylog_globals() {
    game.keylog_recording = false;
    game.keylog_keys = [];
    game.gameSeed = 0;
    game.gameDatetime = null;
    game.gameNethackrc = null;
}