-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.js
105 lines (91 loc) · 2.55 KB
/
run.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var spawn = require('child_process').spawn;
var fs = require('fs');
var args = [
//'--replay-directory',
//'"Build/replays/"',
'-vvv',
'--results-as-json',
'--no-compression',
'--width',
'32',
'--height',
'32',
'--no-logs',
'"cd Build/Release && MyBot.exe"',
'"cd Build/Release && MyBot.exe"'
];
var prc = spawn("halite.exe", args, {
cwd: __dirname
});
var match = {
prc: prc,
stdout: '',
stderr: '',
exit_code: -1
};
prc.stdout.on('data', (data) => {
match.stdout += data.toString();
});
prc.stderr.on('data', (data) => {
match.stderr += data.toString();
});
var global_replay = null;
function applyDebug(i, cb) {
fs.readFile(__dirname + '/Build/Release/bot-' + i + '.log', 'utf8', function (err, data) {
var s = data.split('\n');
for(var line of s) {
var token = "FLUORINEDEBUG ";
if(line.startsWith(token)) {
line = line.substr(token.length);
var debuginfo = JSON.parse(line);
if(debuginfo.meta.type == "ship") {
global_replay["full_frames"][debuginfo.turn]["entities"][i+""][debuginfo.meta.ship_id+""].data = debuginfo.data;
} else if(debuginfo.meta.type == "minCost") {
if(!global_replay["full_frames"][debuginfo.turn]["minCost"]) {
global_replay["full_frames"][debuginfo.turn]["minCost"] = {
"0": {},
"1": {},
"2": {},
"3": {}
};
}
global_replay["full_frames"][debuginfo.turn]["minCost"][i+""][debuginfo.meta.position_x+"_"+debuginfo.meta.position_y] = debuginfo.data;
} else if(debuginfo.meta.type == "priority") {
if(!global_replay["full_frames"][debuginfo.turn]["priority"]) {
global_replay["full_frames"][debuginfo.turn]["priority"] = {
"0": {},
"1": {},
"2": {},
"3": {}
};
}
global_replay["full_frames"][debuginfo.turn]["priority"][i+""][debuginfo.meta.position_x+"_"+debuginfo.meta.position_y] = debuginfo.data;
}
}
}
cb();
});
}
prc.on('exit', (code) => {
match.exit_code = code;
if(match.exit_code != 0) {
console.log("Match failed! exit_code: " + match.exit_code);
return;
} else {
console.log("Match finalized!");
}
console.log(match.stderr);
console.log(match.stdout);
var info = JSON.parse(match.stdout);
var replay_file = __dirname + '/' + info.replay;
fs.readFile(replay_file, function (err, data) {
if(err) throw err;
global_replay = JSON.parse(data.toString());
applyDebug(0, function() {
applyDebug(1, function() {
console.log("Apply and save again");
fs.writeFileSync(replay_file,JSON.stringify(global_replay),{encoding:'utf8',flag:'w'})
});
});
});
});