-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
55 lines (51 loc) · 1.17 KB
/
game.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
'use strict';
const request = require('request-promise-native');
module.exports = {
'play': play,
};
function play(gameUrl, debug, proxy) {
const options = {
'json': true,
'forever': true,
'proxy': proxy,
};
debug && console.log(`start playing ${gameUrl}`);
return request(gameUrl, options)
.then(response => {
playTurn(response, gameUrl, debug, proxy)
});
}
function playTurn(response, gameUrl, debug, proxy) {
const move = getMove(response);
const options = {
'json': true,
'forever': true,
'body': move,
'proxy': proxy,
};
if (debug) {
console.log(JSON.stringify(response));
console.log('\nsending request ', move);
}
return request.post(gameUrl, options)
.then(response => {
if (debug) {
setTimeout(function () {
playTurn(response, gameUrl, true, proxy);
}, 3000);
} else {
playTurn(response, gameUrl, false, proxy);
}
})
}
/**
* Get coordinates (start point) of next move.
* @param data
* @returns {{x: number, y: number}}
*/
function getMove(data) {
return {
'x': Math.floor(Math.random() * 5),
'y': Math.floor(Math.random() * 5),
}
}