Download environments/games/web/engine/bridge.js from THUSI-Lab/GameDojo: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/datasets/THUSI-Lab/GameDojo/resolve/refs%2Fpr%2F4/environments/games/web/engine/bridge.js
- Command line
-
hf download hf://datasets/THUSI-Lab/GameDojo@refs/pr/4/environments/games/web/engine/bridge.js
-
curl -L -o bridge.js https://huggingface.co/datasets/THUSI-Lab/GameDojo/resolve/refs%2Fpr%2F4/environments/games/web/engine/bridge.js
3.87 kB
| // JSON command handler behind the Python gymnasium binding (prototype/bossbench/webenv.py). | |
| // The same function serves two transports: tools/gym_server.js (Node, stdin/stdout JSON lines, symbolic only) | |
| // and a headless browser page (Playwright) where BB.Renderer exists and 'rgb' frames come back as base64 RGB. | |
| // Time modes are handled on the Python side: the env here always runs virtual_rt and is told how many | |
| // no-op packets to run first (latency = lag * 0.2 s), so every engine shares one lag rule. | |
| (function (g) { | |
| var BB = g.BB, env = null, R = null, rgb = false; | |
| function frame() { | |
| var W = BB.VIEW.W, H = BB.VIEW.H; | |
| R.draw(env.case, env.case.t / BB.FPS, ''); | |
| var px = R.ctx.getImageData(0, 0, W, H).data, n = W * H, out = new Uint8Array(n * 3); | |
| for (var i = 0, j = 0; i < n * 4; i += 4) { out[j++] = px[i]; out[j++] = px[i + 1]; out[j++] = px[i + 2]; } | |
| var s = '', CH = 0x8000; | |
| for (var k = 0; k < out.length; k += CH) s += String.fromCharCode.apply(null, out.subarray(k, k + CH)); | |
| return { b64: g.btoa(s), shape: [H, W, 3] }; | |
| } | |
| function pack(r) { | |
| var c = env.case; | |
| r.info.player_max_hp = c.body.max_hp; | |
| var res = { state: r.obs, reward: r.reward, terminated: r.terminated, truncated: r.truncated, info: r.info, | |
| verifier: c.verifier() }; | |
| if (rgb) res.frame = frame(); | |
| return res; | |
| } | |
| BB.bridge = function (m) { | |
| switch (m.cmd) { | |
| case 'cases': | |
| return { cases: Object.keys(BB.CASES).sort().map(function (id) { return { id: id, title: BB.CASES[id].title }; }) }; | |
| case 'make': | |
| env = BB.make(m.id, { obs_mode: 'symbolic', time_mode: 'virtual_rt' }); | |
| rgb = !!m.rgb; | |
| if (rgb && !BB.Renderer) throw new Error('rgb needs the browser renderer (engine/renderer.js); Node has no canvas'); | |
| return { fps: BB.FPS, max_seconds: BB.MAX_SECONDS, packet_seconds: BB.PACKET_SECONDS, manual: env.case.manual() }; | |
| case 'reset': | |
| if (rgb) R = new BB.Renderer(g.document.createElement('canvas')); // fresh juice/art state per episode | |
| var options = Object.assign({}, m.options || {}); | |
| if (m.difficulty != null) options.difficulty = m.difficulty; // legacy top-level value takes precedence | |
| var r = env.reset(m.seed, options); | |
| return pack({ obs: r.obs, reward: 0, terminated: false, truncated: false, info: r.info }); | |
| case 'step': | |
| return pack(env.step(m.packet, (m.lag || 0) * BB.PACKET_SECONDS)); | |
| case 'render': | |
| return { frame: frame() }; | |
| case 'hash': | |
| return { hash: BB.hashState(env.case) }; | |
| case 'band': // optional: the game's own name for d's segment | |
| return { band: (BB.GAME && typeof BB.GAME.difficultyBand === 'function') | |
| ? (BB.GAME.difficultyBand(m.d) || null) : null }; | |
| case 'parse': // packet parity tests against the Python parser | |
| var p = BB.parsePacket(m.text); | |
| return { packet: p && { mouse: p.mouse, subs: p.subs.map(function (h) { return Object.keys(h).sort(); }) } }; | |
| case 'scripted': // trusted reference controller (bossbench/scripted.py) | |
| return { packet: BB.scriptedPacket(env.case) }; | |
| case 'meta': | |
| return { game: BB.GAME && BB.GAME.id || null, rules: BB.GAME && BB.GAME.rules || null, | |
| scripted: !!(BB.GAME && typeof BB.GAME.scripted === 'function'), | |
| tiers: BB.GAME && BB.GAME.tiers || null }; | |
| default: | |
| throw new Error('unknown cmd ' + m.cmd); | |
| } | |
| }; | |
| // Trusted harness access (video capture, scripted play); never exposed to a policy. | |
| BB.bridgeEnv = function () { return env; }; | |
| })(typeof window !== 'undefined' ? window : globalThis); | |