GameDojo / environments /games /web /tools /gym_server.js
iTheresaApocalypse's picture hanoiy's picture
Add GameDojo game environments (#2)
c04dd6a
Raw History Blame Contribute Delete
1.03 kB
// Node transport for the Python gymnasium binding: one env per process, one JSON command per stdin line,
// one JSON reply per stdout line ({error} on failure). Symbolic only; pixels go through the browser transport.
// usage: node tools/gym_server.js [game = hollowknight_remake] (a name under web/games/ or a path to a game folder)
// One game per process (games share the BB global); prototype/bossbench/webenv.py starts one per game.
var path = require('path'), fs = require('fs');
var arg = process.argv[2] || 'hollowknight_remake', dir = fs.existsSync(path.join(__dirname, '..', 'games', arg)) ? path.join(__dirname, '..', 'games', arg) : path.resolve(arg);
var BB = require('../engine/load.js')(dir);
require('../engine/bridge.js');
var rl = require('readline').createInterface({ input: process.stdin, terminal: false });
rl.on('line', function (line) {
var res;
try { res = BB.bridge(JSON.parse(line)); } catch (e) { res = { error: String(e && e.stack || e) }; }
process.stdout.write(JSON.stringify(res) + '\n');
});