-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
645 lines (589 loc) · 28 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//'use strict';
require('dotenv').config()
const puppeteer = require('puppeteer');
const fetch = require("node-fetch");
const chalk = require('chalk');
const splinterlandsPage = require('./splinterlandsPage');
const user = require('./user');
const card = require('./cards');
const helper = require('./helper');
const quests = require('./quests');
const ask = require('./possibleTeams');
const api = require('./api');
const misc = require('./misc');
const version = 0.43;
let resultAll = [];
let captureRateAll = [];
let questRewardAll = [];
let finalRateAll = [];
async function checkForUpdate() {
await misc.writeToLogNoUsername('------------------------------------------------------------------------------------------------');
await fetch('http://jofri.pf-control.de/prgrms/splnterlnds/version.txt')
.then(response => response.json())
.then(newestVersion => {
if (newestVersion > version) {
misc.writeToLogNoUsername('New Update! Please download on https://github.com/PCJones/ultimate-splinterlands-bot');
misc.writeToLogNoUsername('New Update! Please download on https://github.com/PCJones/ultimate-splinterlands-bot');
misc.writeToLogNoUsername('New Update! Please download on https://github.com/PCJones/ultimate-splinterlands-bot');
} else {
misc.writeToLogNoUsername('No update available');
}
})
misc.writeToLogNoUsername('------------------------------------------------------------------------------------------------');
}
async function checkForMissingConfigs() {
if (!process.env.LOGIN_VIA_EMAIL) {
misc.writeToLogNoUsername("Missing LOGIN_VIA_EMAIL parameter in .env - see updated .env-example!");
await sleep(60000);
}
if (!process.env.HEADLESS) {
misc.writeToLogNoUsername("Missing HEADLESS parameter in .env - see updated .env-example!");
await sleep(60000);
}
if (!process.env.KEEP_BROWSER_OPEN) {
misc.writeToLogNoUsername("Missing KEEP_BROWSER_OPEN parameter in .env - see updated .env-example!");
await sleep(60000);
}
if (!process.env.CLAIM_QUEST_REWARD) {
misc.writeToLogNoUsername("Missing CLAIM_QUEST_REWARD parameter in .env - see updated .env-example!");
await sleep(60000);
}
if (!process.env.USE_CLASSIC_BOT_PRIVATE_API) {
misc.writeToLogNoUsername("Missing USE_CLASSIC_BOT_PRIVATE_API parameter in .env - see updated .env-example!");
await sleep(60000);
}
if (!process.env.USE_API) {
misc.writeToLogNoUsername("Missing USE_API parameter in .env - see updated .env-example!");
await sleep(60000);
}
if (!process.env.API_URL || (process.env.USE_API === 'true' && !process.env.API_URL.includes('http'))) {
misc.writeToLogNoUsername("Missing API_URL parameter in .env - see updated .env-example!");
await sleep(60000);
}
if (process.env.USE_API === 'true' && process.env.USE_CLASSIC_BOT_PRIVATE_API === 'true') {
misc.writeToLogNoUsername('Please only set USE_API or USE_CLASSIC_BOT_PRIVATE_API to true');
await sleep(60000);
}
if (!process.env.ERC_THRESHOLD) {
misc.writeToLogNoUsername("Missing ERC_THRESHOLD parameter in .env - see updated .env-example!");
await sleep(60000);
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// Close popups by Jones
async function closePopups(page) {
if (await clickOnElement(page, '.close', 4000))
return;
await clickOnElement(page, '.modal-close-new', 1000);
}
// await loading circle by Jones
async function waitUntilLoaded(page) {
try {
await page.waitForSelector('.loading', {
timeout: 6000
})
.then(() => {
misc.writeToLog('Waiting until page is loaded...');
});
} catch (e) {
misc.writeToLog('No loading circle...')
return;
}
await page.waitForFunction(() => !document.querySelector('.loading'), {
timeout: 120000
});
}
async function clickMenuFightButton(page) {
try {
await page.waitForSelector('#menu_item_battle', {
timeout: 6000
})
.then(button => button.click());
} catch (e) {
misc.writeToLog('fight button not found')
}
}
// LOAD MY CARDS
async function getCards() {
const myCards = await user.getPlayerCards(process.env.ACCUSERNAME, new Date(Date.now() - 86400000)) // 86400000 = 1 day in milliseconds
return myCards;
}
async function getQuest() {
return quests.getPlayerQuest(process.env.ACCUSERNAME.split('@')[0])
.then(x => x)
.catch(e => misc.writeToLog('No quest data, splinterlands API didnt respond, or you are wrongly using the email and password instead of username and posting key'))
}
async function createBrowsers(count, headless) {
let browsers = [];
for (let i = 0; i < count; i++) {
const browser = await puppeteer.launch({
headless: headless,
args: process.env.CHROME_NO_SANDBOX === 'true' ? ["--no-sandbox"] : ['--disable-web-security',
'--disable-features=IsolateOrigins',
' --disable-site-isolation-trials'],
});
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(500000);
await page.on('dialog', async dialog => {
await dialog.accept();
});
browsers[i] = browser;
}
return browsers;
}
async function getElementText(page, selector, timeout = 20000) {
const element = await page.waitForSelector(selector, {
timeout: timeout
});
const text = await element.evaluate(el => el.textContent);
return text;
}
async function getElementTextByXpath(page, selector, timeout = 20000) {
const element = await page.waitForXPath(selector, {
timeout: timeout
});
const text = await element.evaluate(el => el.textContent);
return text;
}
async function clickOnElement(page, selector, timeout = 20000, delayBeforeClicking = 0) {
try {
const elem = await page.waitForSelector(selector, {
timeout: timeout
});
if (elem) {
await sleep(delayBeforeClicking);
misc.writeToLog('Clicking element ' + selector);
await elem.click();
return true;
}
} catch (e) {}
misc.writeToLog('Error: Could not find element ' + selector);
return false;
}
async function selectCorrectBattleType(page) {
try {
await page.waitForSelector("#battle_category_type", {
timeout: 20000
})
let battleType = (await page.$eval('#battle_category_type', el => el.innerText)).trim();
while (battleType !== "RANKED") {
misc.writeToLog("Wrong battleType! battleType is " + battleType + " - Trying to change it");
try {
await page.waitForSelector('#right_slider_btn', {
timeout: 500
})
.then(button => button.click());
} catch (e) {
misc.writeToLog('Slider button not found ', e)
}
await page.waitForTimeout(1000);
battleType = (await page.$eval('#battle_category_type', el => el.innerText)).trim();
}
} catch (error) {
misc.writeToLog("Error: couldn't find battle category type ", error);
}
}
async function startBotPlayMatch(page, myCards, quest, claimQuestReward, prioritizeQuest, useAPI) {
const ercThreshold = process.env.ERC_THRESHOLD;
if (myCards) {
misc.writeToLog('Deck size: ' + myCards.length)
} else {
misc.writeToLog('Playing only basic cards')
}
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
await page.setViewport({
width: 1800,
height: 1500,
deviceScaleFactor: 1,
});
await page.goto('https://splinterlands.com/?p=battle_history');
await page.waitForTimeout(4000);
let username = await getElementText(page, '.dropdown-toggle .bio__name__display', 10000);
if (username == process.env.ACCUSERNAME) {
misc.writeToLog('Already logged in!');
} else {
misc.writeToLog('Login')
await splinterlandsPage.login(page).catch(e => {
misc.writeToLog(e);
throw new Error('Login Error');
});
}
await waitUntilLoaded(page);
const erc = parseInt((await getElementTextByXpath(page, "//div[@class='dec-options'][1]/div[@class='value'][2]/div", 100)).split('.')[0]);
if (erc >= 50) {
misc.writeToLog('Current Energy Capture Rate is ' + chalk.green(erc + "%"));
captureRateAll.push(process.env.ACCUSERNAME + chalk.green(' ERC: ' + erc + "%"));
} else {
misc.writeToLog('Current Energy Capture Rate is ' + chalk.red(erc + "%"));
captureRateAll.push(process.env.ACCUSERNAME + chalk.red(' ERC: ' + erc + "%"));
}
if (erc < ercThreshold) {
misc.writeToLog('ERC is below threshold of ' + ercThreshold + '% - skipping this account');
return;
}
await page.waitForTimeout(1000);
await closePopups(page);
await page.waitForTimeout(2000);
if (!page.url().includes("battle_history")) {
await clickMenuFightButton(page);
await page.waitForTimeout(3000);
}
//check if season reward is available
if (process.env.CLAIM_SEASON_REWARD === 'true') {
try {
misc.writeToLog('Season reward check: ');
await page.waitForSelector('#claim-btn', {
visible: true,
timeout: 3000
})
.then(async(button) => {
button.click();
misc.writeToLog(`claiming the season reward. you can check them here https://peakmonsters.com/@${process.env.ACCUSERNAME}/explorer`);
await page.waitForTimeout(20000);
})
.catch(() => misc.writeToLog('no season reward to be claimed, but you can still check your data here https://peakmonsters.com/@${process.env.ACCUSERNAME}/explorer'));
} catch (e) {
misc.writeToLog('no season reward to be claimed');
}
}
let curRating = await getElementText(page, 'span.number_text', 2000);
await misc.writeToLog('Current Rating is ' + chalk.yellow(curRating));
//if quest done claim reward
misc.writeToLog('Quest details: ' + chalk.yellow(JSON.stringify(quest)));
try {
const claimButton = await page.waitForSelector('#quest_claim_btn', {
timeout: 2500,
visible: true
});
if (claimButton) {
misc.writeToLog(chalk.green('Quest reward can be claimed!'));
questRewardAll.push(process.env.ACCUSERNAME + " Quest: " + chalk.yellow(Object.values(quest)[3].toString() + "/" + Object.values(quest)[2].toString()) + chalk.yellow(' Quest reward can be claimed!'))
if (claimQuestReward) {
await claimButton.click();
await page.waitForTimeout(60000);
await page.reload();
await page.waitForTimeout(10000);
}
}
} catch (e) {
misc.writeToLog('No quest reward to be claimed waiting for the battle...')
questRewardAll.push(process.env.ACCUSERNAME + " Quest: " + chalk.yellow(Object.values(quest)[3].toString() + "/" + Object.values(quest)[2].toString()) + chalk.red(' No quest reward...'));
}
if (!page.url().includes("battle_history")) {
misc.writeToLog("Seems like battle button menu didn't get clicked correctly - try again");
misc.writeToLog('Clicking fight menu button again');
await clickMenuFightButton(page);
await page.waitForTimeout(5000);
}
// LAUNCH the battle
try {
misc.writeToLog('waiting for battle button...')
await selectCorrectBattleType(page);
await page.waitForXPath("//button[contains(., 'BATTLE')]", {
timeout: 3000
})
.then(button => {
misc.writeToLog('Battle button clicked');
button.click()
})
.catch(e => misc.writeErrorToLog('[ERROR] waiting for Battle button. is Splinterlands in maintenance?'));
await page.waitForTimeout(5000);
misc.writeToLog('waiting for an opponent...')
await page.waitForSelector('.btn--create-team', {
timeout: 25000
})
.then(() => misc.writeToLog('start the match'))
.catch(async(e) => {
misc.writeErrorToLog('[Error while waiting for battle]');
misc.writeToLog('Clicking fight menu button again');
await clickMenuFightButton(page);
misc.writeToLog('Clicking battle button again');
await page.waitForXPath("//button[contains(., 'BATTLE')]", {
timeout: 3000
})
.then(button => {
misc.writeToLog('Battle button clicked');
button.click()
})
.catch(e => misc.writeErrorToLog('[ERROR] waiting for Battle button. is Splinterlands in maintenance?'));
misc.writeErrorToLog('Refreshing the page and retrying to retrieve a battle');
await page.waitForTimeout(5000);
await page.reload();
await page.waitForTimeout(5000);
await page.waitForSelector('.btn--create-team', {
timeout: 50000
})
.then(() => misc.writeToLog('start the match'))
.catch(async() => {
misc.writeToLog('second attempt failed reloading from homepage...');
await page.goto('https://splinterlands.io/');
await page.waitForTimeout(5000);
await page.waitForXPath("//button[contains(., 'BATTLE')]", {
timeout: 20000
})
.then(button => button.click())
.catch(e => misc.writeErrorToLog('[ERROR] waiting for Battle button second time'));
await page.waitForTimeout(5000);
await page.waitForSelector('.btn--create-team', {
timeout: 25000
})
.then(() => misc.writeToLog('start the match'))
.catch((e) => {
misc.writeToLog('third attempt failed');
throw new Error(e);
})
})
})
} catch (e) {
misc.writeErrorToLog('[Battle cannot start]:', e)
throw new Error('The Battle cannot start');
}
await page.waitForTimeout(10000);
let[mana, rules, splinters] = await Promise.all([
splinterlandsPage.checkMatchMana(page).then((mana) => mana).catch(() => 'no mana'),
splinterlandsPage.checkMatchRules(page).then((rulesArray) => rulesArray).catch(() => 'no rules'),
splinterlandsPage.checkMatchActiveSplinters(page).then((splinters) => splinters).catch(() => 'no splinters')
]);
const matchDetails = {
mana: mana,
rules: rules,
splinters: splinters,
myCards: myCards,
quest: (prioritizeQuest && quest && (quest.total != quest.completed)) ? quest : '',
}
await page.waitForTimeout(1000);
//TEAM SELECTION
let teamToPlay;
misc.writeToLog(chalk.green('starting team selection'));
if (useAPI) {
const apiResponse = await api.getPossibleTeams(matchDetails);
let apiResponseCounter = 0;
while (JSON.stringify(apiResponse).includes('hash')) {
misc.writeToLog(chalk.magenta('API Response: ' + JSON.stringify(apiResponse)));
misc.writeToLog('Waiting 30 seconds for API to calculate team...');
misc.writeToLog('Update to V2 of this bot to make this faster: https://github.com/PCJones/Ultimate-Splinterlands-Bot-V2');
await sleep(30000);
apiResponse = await api.getPossibleTeams(matchDetails);
if (++apiResponseCounter >= 4) break;
}
if (apiResponse && !JSON.stringify(apiResponse).includes('api limit reached')) {
misc.writeToLog(chalk.magenta('API Response: ' + JSON.stringify(apiResponse)));
teamToPlay = {
summoner: Object.values(apiResponse)[1],
cards: [Object.values(apiResponse)[1], Object.values(apiResponse)[3], Object.values(apiResponse)[5], Object.values(apiResponse)[7], Object.values(apiResponse)[9],
Object.values(apiResponse)[11], Object.values(apiResponse)[13], Object.values(apiResponse)[15]]
};
misc.writeToLog(chalk.cyan('Team picked by API: ' + JSON.stringify(teamToPlay)));
// TEMP, testing
if (Object.values(apiResponse)[1] == '') {
misc.writeToLog('Seems like the API found no possible team - using local history');
const possibleTeams = await ask.possibleTeams(matchDetails).catch(e => misc.writeToLog('Error from possible team API call: ', e));
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
}
} else {
if (apiResponse && JSON.stringify(apiResponse).includes('api limit reached')) {
misc.writeToLog('API limit per hour reached, using local backup!');
misc.writeToLog('Visit discord or telegram group to learn more about API limits: https://t.me/ultimatesplinterlandsbot and https://discord.gg/hwSr7KNGs9');
} else {
misc.writeToLog('API failed, using local history with most cards used tactic');
}
const possibleTeams = await ask.possibleTeams(matchDetails).catch(e => misc.writeToLog('Error from possible team API call: ', e));
if (possibleTeams && possibleTeams.length) {
//misc.writeToLog('Possible Teams based on your cards: ', possibleTeams.length, '\n', possibleTeams);
misc.writeToLog('Possible Teams based on your cards: ' + possibleTeams.length);
} else {
misc.writeToLog('Error: ', JSON.stringify(matchDetails), JSON.stringify(possibleTeams))
throw new Error('NO TEAMS available to be played');
}
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
useAPI = false;
}
} else {
const possibleTeams = await ask.possibleTeams(matchDetails).catch(e => misc.writeToLog('Error from possible team API call: ', e));
if (possibleTeams && possibleTeams.length) {
//misc.writeToLog('Possible Teams based on your cards: ', possibleTeams.length, '\n', possibleTeams);
misc.writeToLog('Possible Teams based on your cards: ', possibleTeams.length);
} else {
misc.writeToLog('Error: ', JSON.stringify(matchDetails), JSON.stringify(possibleTeams))
throw new Error('NO TEAMS available to be played');
}
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
useAPI = false;
}
if (teamToPlay) {
page.click('.btn--create-team')[0];
} else {
throw new Error('Team Selection error');
}
await page.waitForTimeout(5000);
try {
await page.waitForXPath(`//div[@card_detail_id="${teamToPlay.summoner}"]`, {
timeout: 15000
}).then(summonerButton => summonerButton.click());
if (card.color(teamToPlay.cards[0]) === 'Gold') {
misc.writeToLog('Dragon play TEAMCOLOR ' + helper.teamActualSplinterToPlay(teamToPlay.cards.slice(0, 6)))
await page.waitForXPath(`//div[@data-original-title="${helper.teamActualSplinterToPlay(teamToPlay.cards.slice(0, 6))}"]`, {
timeout: 8000
})
.then(selector => selector.click())
}
await page.waitForTimeout(5000);
for (i = 1; i <= 6; i++) {
misc.writeToLog('play: ' + teamToPlay.cards[i].toString())
await teamToPlay.cards[i] ? page.waitForXPath(`//div[@card_detail_id="${teamToPlay.cards[i].toString()}"]`, {
timeout: 10000
})
.then(selector => selector.click()) : misc.writeToLog('nocard ' + i);
await page.waitForTimeout(1000);
}
await page.waitForTimeout(5000);
try {
await page.click('.btn-green')[0]; //start fight
} catch {
misc.writeToLog('Start Fight didnt work, waiting 5 sec and retry');
await page.waitForTimeout(5000);
await page.click('.btn-green')[0]; //start fight
}
await page.waitForTimeout(5000);
await page.waitForSelector('#btnRumble', {
timeout: 160000
}).then(() => misc.writeToLog('btnRumble visible')).catch(() => misc.writeToLog('btnRumble not visible'));
await page.waitForTimeout(5000);
await page.$eval('#btnRumble', elem => elem.click()).then(() => misc.writeToLog('btnRumble clicked')).catch(() => misc.writeToLog('btnRumble didnt click')); //start rumble
await page.waitForSelector('#btnSkip', {
timeout: 10000
}).then(() => misc.writeToLog('btnSkip visible')).catch(() => misc.writeToLog('btnSkip not visible'));
await page.$eval('#btnSkip', elem => elem.click()).then(() => misc.writeToLog('btnSkip clicked')).catch(() => misc.writeToLog('btnSkip not visible')); //skip rumble
try {
const winner = await getElementText(page, 'section.player.winner .bio__name__display', 15000);
if (winner.trim() == process.env.ACCUSERNAME.trim()) {
const decWon = await getElementText(page, '.player.winner span.dec-reward span', 100);
resultAll.push(process.env.ACCUSERNAME + chalk.green(' You won! Reward: ' + decWon + ' DEC'));
} else {
resultAll.push(process.env.ACCUSERNAME + chalk.red(' You lost :('));
if (useAPI) {
api.reportLoss(winner);
}
}
} catch (e) {
misc.writeToLog(e);
misc.writeToLog(chalk.blueBright('Could not find winner - draw?'));
resultAll.push(process.env.ACCUSERNAME + chalk.blueBright('Could not find winner - draw?'));
}
await clickOnElement(page, '.btn--done', 1000, 2500);
try {
let curRating = await getElementText(page, 'span.number_text', 2000);
misc.writeToLog('Updated Rating after battle is ' + chalk.yellow(curRating));
finalRateAll.push(process.env.ACCUSERNAME + (' New rating is ' + chalk.yellow(curRating)));
} catch (e) {
misc.writeToLog(e);
misc.writeToLog(chalk.blueBright('Unable to get new rating'));
finalRateAll.push(process.env.ACCUSERNAME + chalk.red(' Unable to get new rating'));
}
} catch (e) {
throw new Error(e);
}
}
// 30 MINUTES INTERVAL BETWEEN EACH MATCH (if not specified in the .env file)
const sleepingTimeInMinutes = process.env.MINUTES_BATTLES_INTERVAL || 30;
const sleepingTime = sleepingTimeInMinutes * 60000;
(async() => {
try {
await checkForUpdate();
await checkForMissingConfigs();
const loginViaEmail = JSON.parse(process.env.LOGIN_VIA_EMAIL.toLowerCase());
const accountusers = process.env.ACCUSERNAME.split(',');
const accounts = loginViaEmail ? process.env.EMAIL.split(',') : accountusers;
const passwords = process.env.PASSWORD.split(',');
const headless = JSON.parse(process.env.HEADLESS.toLowerCase());
const useAPI = JSON.parse(process.env.USE_API.toLowerCase());
const keepBrowserOpen = JSON.parse(process.env.KEEP_BROWSER_OPEN.toLowerCase());
const claimQuestReward = JSON.parse(process.env.CLAIM_QUEST_REWARD.toLowerCase());
const prioritizeQuest = JSON.parse(process.env.QUEST_PRIORITY.toLowerCase());
let browsers = [];
misc.writeToLogNoUsername('Headless: ' + headless);
misc.writeToLogNoUsername('Keep Browser Open: ' + keepBrowserOpen);
misc.writeToLogNoUsername('Login via Email: ' + loginViaEmail);
misc.writeToLogNoUsername('Claim Quest Reward: ' + claimQuestReward);
misc.writeToLogNoUsername('Prioritize Quests: ' + prioritizeQuest);
misc.writeToLogNoUsername('Use API: ' + useAPI);
misc.writeToLogNoUsername('Loaded ' + chalk.yellow(accounts.length) + ' Accounts')
misc.writeToLogNoUsername('Accounts: ' + chalk.greenBright(accounts))
while (true) {
for (let i = 0; i < accounts.length; i++) {
process.env['EMAIL'] = accounts[i];
process.env['PASSWORD'] = passwords[i];
process.env['ACCUSERNAME'] = accountusers[i];
if (keepBrowserOpen && browsers.length == 0) {
misc.writeToLog('Opening browsers');
browsers = await createBrowsers(accounts.length, headless);
} else if (!keepBrowserOpen && browsers.length == 0) { // close browser, only have 1 instance at a time
misc.writeToLog('Opening browser');
browsers = await createBrowsers(1, headless);
}
const page = (await(keepBrowserOpen ? browsers[i] : browsers[0]).pages())[1];
//page.goto('https://splinterlands.io/');
misc.writeToLog('getting user cards collection from splinterlands API...')
const myCards = await getCards()
.then((x) => {
misc.writeToLog('cards retrieved');
return x
})
.catch(() => misc.writeToLog('cards collection api didnt respond. Did you use username? avoid email!'));
misc.writeToLog('getting user quest info from splinterlands API...');
const quest = await getQuest();
if (!quest) {
misc.writeToLog('Error for quest details. Splinterlands API didnt work or you used incorrect username');
}
await startBotPlayMatch(page, myCards, quest, claimQuestReward, prioritizeQuest, useAPI)
.then(() => {
misc.writeToLog('Closing battle');
})
.catch((e) => {
misc.writeToLog(e)
})
await page.waitForTimeout(5000);
if (keepBrowserOpen) {
await page.goto('about:blank');
} else {
await page.evaluate(function () {
SM.Logout();
});
//let pages = await browsers[0].pages();
//await Promise.all(pages.map(page =>page.close()));
//await browsers[0].close();
//browsers[0].process().kill('SIGKILL');
}
}
console.log('--------------------------Battle Result Summary:----------------------');
for (let i = 0; i < resultAll.length; i++) {
console.log(resultAll[i]);
}
for (let i = 0; i < finalRateAll.length; i++) {
console.log(finalRateAll[i]);
}
for (let i = 0; i < captureRateAll.length; i++) {
console.log(captureRateAll[i]);
}
for (let i = 0; i < questRewardAll.length; i++) {
console.log(questRewardAll[i]);
}
console.log('----------------------------------------------------------------------');
console.log('Waiting for the next battle in', sleepingTime / 1000 / 60, ' minutes at ', new Date(Date.now() + sleepingTime).toLocaleString());
console.log(chalk.green('Interested in a bot that transfers all cards, dec and sps to your main account? Visit the discord or telegram!'));
console.log(chalk.green('Join the telegram group https://t.me/ultimatesplinterlandsbot and discord https://discord.gg/hwSr7KNGs9'));
console.log('--------------------------End of Battle--------------------------------');
await new Promise(r => setTimeout(r, sleepingTime));
resultAll = [];
captureRateAll = [];
questRewardAll = [];
finalRateAll = [];
}
} catch (e) {
console.log('Routine error at: ', new Date().toLocaleString(), e)
}
})();