-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
126 lines (110 loc) · 3.69 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
119
120
121
122
123
124
125
126
/**
* Created by Quake on 07.11.2017.
*/
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 teamSize;
var game;
var Move = require('./model/move.js');
goToSafeMode();
var MyStrategy = require(__dirname + '/' + (process.argv[5] || './my-strategy.js'));
var isCallbackedStrategy = false;
var _move;
function run() {
remoteProcessClient.writeTokenMessage(token);
remoteProcessClient.writeProtocolVersionMessage();
remoteProcessClient.readTeamSizeMessage(function f1(v) {
teamSize = v;
remoteProcessClient.readGameContextMessage(function f2(v) {
game = v;
strategy = MyStrategy.getInstance();
isCallbackedStrategy = strategy.length === 5; //http proxy strategy with callback
remoteProcessClient.readPlayerContextMessage(handleGameFrame);
});
});
}
function handleGameFrame(playerContext) {
if (!playerContext) {
remoteProcessClient.close();
process.exit(1);
}
var player = playerContext.player;
if (player == null) {
console.log('wrong player');
process.exit(1);
} else {
callBackCount = 0;
var move = Move.getInstance();
if (!isCallbackedStrategy) {
_move = move;
}
if (process.env.DEBUG) {
callStrategy(player, playerContext.world, game, move);
} else {
try {
callStrategy(player, playerContext.world, game, move);
} catch (e) {
console.log(e.stack);
process.exit(1);
}
}
}
if (!isCallbackedStrategy) {
afterAllStrategyProcessed();
}
}
function afterAllStrategyProcessed() {
remoteProcessClient.writeMoveMessage(_move);
remoteProcessClient.readPlayerContextMessage(handleGameFrame);
}
var callBackCount;
function callStrategy(player, world, game, move) {
if (isCallbackedStrategy) {
callBackCount++;
strategy(player, world, game, move, function (returnedMove) {
_move = returnedMove;
callBackCount--;
if (callBackCount === 0) {
afterAllStrategyProcessed();
}
});
} else {
strategy(player, world, game, move);
}
}