forked from phpspd/js-cgdk-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
118 lines (98 loc) · 3.25 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Created by Quake on 18.12.2018
*/
'use strict';
function goToSafeMode() {
if (process.argv[6] === 'disable-fs') {
process.kill = null;
var path = require('path');
var Module = require('module');
var originalRequire = Module.prototype.require;
Module.prototype.require = function (moduleName) {
if (moduleName.indexOf(__dirname) !== 0) {
moduleName = path.resolve(__dirname, moduleName);
} else {
moduleName = path.resolve(moduleName);
}
if (moduleName.indexOf(__dirname) !== 0) {
throw moduleName + '; Modules import is restricted. Use require(__dirname+"/module.js") to import any modules. Ипорт модулей ограничен. Используйте (__dirname+"/module.js") для импорта модулей.';
}
return originalRequire(moduleName);
};
console.log('"fs" module disabled');
}
}
var token = process.argv[4] || "0000000000000000";
var RemoteProcessClient = require(__dirname + '/remote-process-client.js');
var remoteProcessClient = new RemoteProcessClient.connect(process.argv[2] || '127.0.0.1', process.argv[3] || 31001, function onServerConnect() {
if (MyStrategy.onLocalRunnerConnected) {
MyStrategy.onLocalRunnerConnected();
}
if (process.env.DEBUG) {
run();
} else {
try {
run();
} catch (e) {
console.log('INITIALIZATION ERROR: ' + e.message);
process.exit(1);
}
}
});
var strategy = null;
var rules;
var game;
var Action = require('./model/Action.js');
goToSafeMode();
var MyStrategy = require(__dirname + '/' + (process.argv[5] || './my-strategy.js'));
var isCallbackedStrategy = false;
var actions;
var callBackCount;
var _action;
function run() {
remoteProcessClient.writeTokenMessage(token, function() {
remoteProcessClient.readRules(function (v) {
rules = v;
remoteProcessClient.readGame(handleGameFrame)
});
});
//isCallbackedStrategy = strategy.length === 5; //http proxy strategy with callback
}
function handleGameFrame(v) {
game = v;
strategy = MyStrategy.getInstance();
isCallbackedStrategy = strategy.length === 5; //http proxy strategy with callback
if (!game) {
remoteProcessClient.close();
process.exit(1);
}
callBackCount = 0;
actions = [];
for (let robot of game.robots) {
if (robot.is_teammate) {
actions[robot.id] = new Action();
callStrategy(robot, rules, game, actions[robot.id]);
}
}
if (!isCallbackedStrategy) {
afterAllStrategyProcessed();
}
}
function callStrategy(robot, rules, game, action) {
if (isCallbackedStrategy) {
callBackCount++;
strategy(robot, rules, game, action, function (returnedAction) {
_action = returnedAction;
callBackCount--;
if (callBackCount === 0) {
afterAllStrategyProcessed();
}
});
} else {
strategy(robot, rules, game, action);
}
}
function afterAllStrategyProcessed() {
remoteProcessClient.write(actions);
remoteProcessClient.readGame(handleGameFrame);
}