-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
executable file
·75 lines (67 loc) · 2.21 KB
/
runner.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
/*
* Copyright 2021 Yufan You
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
const { execSync } = require('child_process');
const { program } = require('commander');
const { version } = require('./package.json');
function exec(command, options) {
try {
return execSync(command, options);
} catch (err) {
console.log(options.input);
throw err;
}
}
program.version(version)
.requiredOption('-j, --judger <judger>', 'Executable judger program')
.requiredOption('-b, --bot <bots...>', 'Executable bot programs')
.option('-i, --initdata <initdata>', 'Stringified JSON initdata (optional)', "")
.parse();
const { judger, bot } = program.opts();
let initdata = "";
try {
initdata = JSON.parse(program.opts().initdata);
} catch { }
const log = [];
const requests = Array.from(Array(bot.length), () => []);
const responses = Array.from(Array(bot.length), () => []);
const data = Array(bot.length);
const globaldata = Array(bot.length);
for (let round = 0; ; ++round) {
const output = JSON.parse(exec(judger, { input: JSON.stringify({ log, initdata }) }));
const { command, content, display } = output;
if (round === 0 && output.initdata !== void (0)) initdata = output.initdata;
console.log(display);
log.push({ output });
if (command === 'finish') {
console.log('----- FINISHED -----');
console.log(content);
break;
} else if (command !== 'request') {
console.error(`Invalid command: ${command}`);
break;
}
const res = {};
for (const [player, request] of Object.entries(content)) {
requests[player].push(request);
const input = JSON.stringify({
requests: requests[player],
responses: responses[player],
data: data[player],
globaldata: globaldata[player]
});
const botOutput = JSON.parse(exec(bot[player], { input }));
data[player] = botOutput.data;
globaldata[player] = botOutput.globaldata;
res[player] = { verdict: "OK", response: botOutput.response };
responses[player].push(botOutput.response);
}
log.push(res);
}