-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkcapp-bot.js
90 lines (80 loc) · 3.12 KB
/
kcapp-bot.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
const debug = require('debug')('kcapp-bot:main');
const decache = require('decache');
const sleep = require('./sleep');
class KcappBot {
constructor(botId, sioURL, sioPort = 3000, apiURL = 'http://localhost:8001', protocol = 'http') {
this.botId = botId;
this.sioURL = sioURL;
this.sioPort = sioPort;
this.apiURL = apiURL;
this.protocol = protocol;
this.firstThrow = true;
}
async doScore(socket, bot) {
const player = socket.currentPlayer;
if (player.player_id === bot.id) {
if (this.firstThrow) {
debug(`Waiting because of first throw`);
await sleep(5000); // wait for page to load
}
await sleep(500);
await bot.score(socket);
await sleep(1000);
socket.emitVisit();
}
this.firstThrow = false;
}
async handleScoreUpdate(socket, data, bot) {
const leg = data.leg;
if (leg.is_finished) {
return;
} else if (leg.current_player_id !== bot.id) {
debug(`[${leg.id}] Not our turn, waiting...`);
} else if (data.is_undo) {
debug(`[${leg.id}] Recevied undo visit, forwarding`);
bot.undoVisit();
socket.emit('undo_visit', {});
} else {
this.doScore(socket, bot);
}
}
playLeg(legId, botSkill) {
const SkillBot = require('./bot');
const bot = new SkillBot(this.botId, botSkill);
const kcapp = require('kcapp-sio-client/kcapp')(this.sioURL, this.sioPort, 'kcapp-bot', this.protocol);
// Make sure we get a separate instance for each leg we connect to...
decache('kcapp-sio-client/kcapp');
kcapp.connectLegNamespace(legId, (socket) => {
debug(`[${legId}] kcapp-bot connected to leg`);
this.firstThrow = true;
socket.on('score_update', (data) => {
this.handleScoreUpdate(socket, data, bot);
});
socket.on('leg_finished', (data) => {
debug(`[${legId}] Leg is finished`);
socket.disconnect();
});
this.doScore(socket, bot);
});
}
replayLeg(legId, playerId, startingScore = 301) {
const ReplayBot = require('./replay-bot')
const bot = new ReplayBot(this.botId, legId, playerId, this.apiURL, startingScore);
const kcapp = require('kcapp-sio-client/kcapp')(this.sioURL, this.sioPort, 'kcapp-bot', this.protocol);
// Make sure we get a separate instance for each leg we connect to...
decache('kcapp-sio-client/kcapp');
kcapp.connectLegNamespace(legId, (socket) => {
debug(`[${legId}] replay-bot connected to leg`);
this.firstThrow = true;
socket.on('score_update', (data) => {
this.handleScoreUpdate(socket, data, bot);
});
socket.on('leg_finished', (data) => {
debug(`[${legId}] Leg is finished`);
socket.disconnect();
});
this.doScore(socket, bot);
});
}
}
module.exports = KcappBot;