This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
96 lines (73 loc) · 2.71 KB
/
main.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
const { getBoardAsString } = require('./src/utils');
const { getNextSnakeMove } = require('./src/bot');
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.on('connectFailed', function (error) {
console.log('Connect Error: ' + error.toString());
});
var forceGC = function () { }
if (typeof gc === 'function') {
forceGC = gc;
console.log('Clean gc usage');
} else {
console.log('Clean gc usage is not available');
}
client.on('connect', function (connection) {
console.log('WebSocket Client Connected');
connection.on('error', function (error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function () {
console.log('echo-protocol Connection Closed');
});
function processBoard(board) {
var programLogs = "";
function logger(message) {
programLogs += message + "\n"
}
try {
var answer = getNextSnakeMove(board, logger);
var boardString = getBoardAsString(board);
var logMessage = '';
if (programLogs) {
logMessage += "-----------------------------------\n";
logMessage += programLogs;
}
logMessage += "-----------------------------------\n";
logMessage += "Answer: " + answer + "\n";
console.info(boardString);
console.debug(logMessage);
return answer;
} catch (e) {
logger(`!!!!!!!!!!!!!!!!!!!!!!!!`);
logger(JSON.stringify(e));
logger(`!!!!!!!!!!!!!!!!!!!!!!!!`);
console.error(boardString);
//logger(`next: ${q[1]} ${q[2]}`);
return 'ACT';
}
//printBoard(boardString);
//printLog(logMessage + '\n\n' + boardString);
}
connection.on('message', function (event) {
var pattern = new RegExp(/^board=(.*)$/);
var message = event.utf8Data;
var parameters = message.match(pattern);
var board = parameters[1];
var answer = processBoard(board);
connection.send(answer);
setTimeout(function () {
forceGC();
})
});
});
var URL = 'https://game1.epam-bot-challenge.com.ua/codenjoy-contest/board/player/tolik@sqrtt.pro?code=1950246074193093654';
//var URL = 'https://game3.epam-bot-challenge.com.ua/codenjoy-contest/board/player/tolik@sqrtt.pro?code=1950246074193093654';
process.argv.forEach(function (val, index, array) {
if (val.includes("URL=")) {
URL = val.split('URL=')[1];
}
});
var url = URL.replace("http", "ws").replace("board/player/", "ws?user=").replace("?code=", "&code=");
console.log(url);
client.connect(url);