forked from PCJones/ultimate-splinterlands-bot
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
970 lines (911 loc) · 46 KB
/
index.js
File metadata and controls
970 lines (911 loc) · 46 KB
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
//'use strict';
require('dotenv').config()
const puppeteer = require('puppeteer');
const fetch = require("cross-fetch");
const chalk = require('chalk');
const fs = require('fs');
const readline = require('readline');
const figlet = require('figlet');
const { table } = require('table');
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 tn = require('./telnotif');
const nq = require('./newquests');
const fnAllCardsDetails = ('./data/cardsDetails.json');
const basicCards = require('./data/basicCards');
const battles = require('./auto-gather');
const version = 11.25;
const unitVersion = 'desktop'
async function readJSONFile(fn){
const jsonString = fs.readFileSync(fn);
const ret = JSON.parse(jsonString);
return ret;
}
async function checkForUpdate(teleNotif) {
console.log(figlet.textSync('USBpc', {
horizontalLayout: 'default',
verticalLayout: 'default',
width: 80,
whitespaceBreak: true
}));
let config = {columns: [ { width: 25 , alignment: 'center'}]};
let updateNews = [[chalk.green('USBpc Version ' + version)]];
console.log(table(updateNews, config));
console.log('---------------------------------------------------------------------------------')
await fetch('https://raw.githubusercontent.com/virgaux/USBpc/master/USBpc-Version.json')
.then(newestVersion => newestVersion.json())
.then(newestVersion => {
if (newestVersion.Version > version) {
if (teleNotif == true){tn.sender('New Update! Please download on https://github.com/virgaux/USBpc')}
console.log(chalk.green(' New Update! Please download on https://github.com/virgaux/USBpc'))
console.table(newestVersion)
} else {
console.log(chalk.yellow(' No update available'))
}
})
console.log('--------------------------------------------------------------------------------- \n')
}
async function checkForMissingConfigs() {
if (!process.env.TELEGRAM_NOTIF) {
misc.writeToLogNoUsername(chalk.red("Missing TELEGRAM_NOTIF parameter in .env. Reverting to default setup."));
process.env.TELEGRAM_NOTIF=false
}
if (!process.env.HEADLESS) {
misc.writeToLogNoUsername(chalk.red("Missing HEADLESS parameter in .env. Reverting to default setup."));
process.env.HEADLESS=true
}
if (!process.env.CLAIM_QUEST_REWARD) {
misc.writeToLogNoUsername(chalk.red("Missing CLAIM_QUEST_REWARD parameter in .env. Reverting to default setup."));
process.env.CLAIM_QUEST_REWARD=false
}
if (!process.env.USE_API) {
misc.writeToLogNoUsername(chalk.red("Missing USE_API parameter in .env. Reverting to to use local history."));
process.env.USE_API=false
}
if (!process.env.API_URL || (process.env.USE_API === 'true' && !process.env.API_URL.includes('http'))) {
misc.writeToLogNoUsername(chalk.red("Missing API_URL parameter in .env. Reverting to to use local history."));
process.env.USE_API=false
}
if (!process.env.ERC_THRESHOLD) {
misc.writeToLogNoUsername(chalk.red("Missing ERC_THRESHOLD parameter in .env. Reverting to default setup."));
process.env.ERC_THRESHOLD=50
}
if (!process.env.GET_DATA_FOR_LOCAL) {
misc.writeToLogNoUsername(chalk.red("process.env.GET_DATA_FOR_LOCAL parameter in .env. Reverting to default setup."));
process.env.GET_DATA_FOR_LOCAL=false
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function checkURl(path) {
return await fetch(path).then(response=>response.status)
}
const withTimeout = (millis, promise) => {
const timeout = new Promise((resolve, reject) =>
setTimeout(
() => reject(`Timed out after ${millis} ms.`),
millis));
return Promise.race([
promise,
timeout
]);
};
// 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')
}
}
// file size checker
async function getFilesizeInBytes(filename) {
var stats = fs.statSync(filename);
var fileSizeInBytes = stats.size;
return fileSizeInBytes;
}
// LOAD MY CARDS
async function getCards() {
const APIs = ['https://api.splinterlands.io/cards/collection/','https://game-api.splinterlands.io/cards/collection/','https://api.steemmonsters.io/cards/collection/','https://api2.splinterlands.com/cards/collection/']
let i =0
while(i<4){
var myCards = await user.getPlayerCards(process.env.ACCUSERNAME, new Date(Date.now() - 86400000), APIs[i]) // 86400000 = 1 day in milliseconds
if (myCards != undefined){
break;
}
i++;}
if (myCards != undefined){
return myCards
}else {
misc.writeToLog('Using Basic Cards')
return basicCards
}
}
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({
product: 'chrome',
headless: headless,
args: headless === true ? [
"--no-sandbox",
'--disable-features=IsolateOrigins',
'--disable-site-isolation-trials',
'--disable-setuid-sandbox',
'--disable-accelerated-2d-canvas',
'--disable-canvas-aa', // Disable antialiasing on 2d canvas
'--disable-2d-canvas-clip-aa', // Disable antialiasing on 2d canvas clips
'--disable-gl-drawing-for-tests', // BEST OPTION EVER! Disables GL drawing operations which produce pixel output. With this the GL output will not be correct but tests will run faster.
'--no-first-run',
'--no-zygote', // wtf does that mean ?
'--disable-dev-shm-usage', // ???
'--use-gl=desktop', // better cpu usage with --use-gl=desktop rather than --use-gl=swiftshader, still needs more testing.
'--disable-gpu',
'--hide-scrollbars',
'--mute-audio',
'--disable-infobars',
'--disable-breakpad',
'--enable-low-end-device-mode',
'--disable-web-security'
] : ["--no-sandbox",
'--disable-accelerated-2d-canvas',
'--disable-canvas-aa', // Disable antialiasing on 2d canvas
'--disable-2d-canvas-clip-aa',
'--hide-scrollbars',
'--mute-audio',
'--disable-web-security']
});
const page = await browser.newPage();
page.setDefaultNavigationTimeout(500000);
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, logSummary, getDataLocal, logSummary1, seasonRewards) {
try{
let newlogvisual = {};
const ercThreshold = process.env.ERC_THRESHOLD;
const allCardDetails = await readJSONFile(fnAllCardsDetails);
logSummary.push(' \n -----' + process.env.ACCUSERNAME + '-----')
logSummary1[process.env.ACCUSERNAME] = newlogvisual
misc.writeToLog(!myCards?'Playing only basic cards':`Deck size: ${myCards.length}`)
await page.waitForNavigation({timeout:10000})
await page.waitForSelector('#play_now_btn',{visible: true, timeout: 2000}).catch(()=> misc.writeToLog('Took 2 sec to load.'));
//check if maintenance
const maintenance = page.url().includes('p=maintenance')
if (page.url().includes('/?p=maintenance')) {
await page.reload()
await page.waitForNavigation({timeout:10000})
if (page.url().includes('/?p=maintenance')){
return misc.writeToLog('Game is currently on maintenance.')
}
} else {
const username = await getElementText(page, '.dropdown-toggle .bio__name__display', 5000).catch(async () => {
await page.goto(`https://${gameUrl}`);
await getElementText(page, '.dropdown-toggle .bio__name__display', 5000)
});
if (username == process.env.ACCUSERNAME) {
misc.writeToLog('Already logged in!');
} else {
misc.writeToLog('Login')
await splinterlandsPage.login(page).catch(async () => {
misc.writeToLog('Unable to login. Reloading page.');
await page.goto(`https://splinterlands.com/?p=battle_history`);
await page.waitForTimeout(4000);
const userlogin = await getElementText(page, '.dropdown-toggle .bio__name__display', 10000);
if (userlogin!= process.env.ACCUSERNAME) await splinterlandsPage.login(page).catch(e => {
misc.writeToLog(e);
logSummary.push(chalk.red(' No records due to login error'));
throw new Error ('Skipping this account due to to login error.');
});
});
}
}
await page.goto('https://splinterlands.com/?p=battle_history');
await page.reload();
await closePopups(page);
await waitUntilLoaded(page);
const ercCurrentraw = await getElementTextByXpath(page, "//div[@class='dec-options'][1]/div[@class='value'][2]/div", 1000).catch(async () =>{
misc.writeToLog('unable to get ECR via browser. Will get info via API call.')
await page.evaluate(()=>SM.Player.balances.find(x=>x.token=='ECR').balance)
.then(ecrApi1 => {
ecrApi1 = (ecrApi1.toString()).slice(0, 2)+ "." + (ecrApi1.toString()).slice(2);
return ecrApi1;
});
});
let ecrInitial = ercCurrentraw.includes('%')? ercCurrentraw.split('%')[0] : ercCurrentraw
if ( parseFloat(ecrInitial) >= 50) {
misc.writeToLog('Current ECR is ' + chalk.green(ecrInitial + "%"));
} else {
misc.writeToLog('Current ECR is ' + chalk.red(ecrInitial + "%"));
}
if (parseFloat(ecrInitial) < parseFloat(ercThreshold)) {
misc.writeToLog('ECR is below threshold of ' + chalk.red(ercThreshold + '% ') + '- Skipping this account');
logSummary.push(' Account skipped: ' + chalk.red('ECR is below threshold of ' + ercThreshold))
return;
}
//if quest done claim reward
let quester = {}
quester['Quest:'] = quest;
console.table(quester);
// boart2k added
const powerThreshold = process.env.POWER_THRESHOLD;
const powerRaw = await page.evaluate(()=>SM.Player.collection_power);
if(powerRaw < powerThreshold){
misc.writeToLog('Collection Power: ' + chalk.red(powerRaw) + ' is lower than the ' + chalk.red(powerThreshold) + ' you have set.');
logSummary.push(' Collection Power: ' + chalk.red(powerRaw) + ' is lower than the ' + chalk.red(powerThreshold) + ' you have set.');
newlogvisual['Power'] = powerRaw
} else {
misc.writeToLog('Collection Power: ' + chalk.green(powerRaw));
logSummary.push(' Collection Power: ' + chalk.green(powerRaw));
newlogvisual['Power'] = powerRaw
}
// boart2k end
//check if season reward is available
await page.waitForTimeout(1000);
await closePopups(page).catch(()=>misc.writeToLog('No pop up to be closed.'));
await page.waitForTimeout(2000);
await nq.seasonQuest(page, logSummary, allCardDetails, seasonRewards);
if (!page.url().includes("battle_history")) {
await clickMenuFightButton(page);
await page.waitForTimeout(3000);
}
const curRating = await page.evaluate(()=>SM.Player.rating).catch(() => {misc.writeToLog('Unable to get current Rating')} );
misc.writeToLog('Current Rating is ' + chalk.yellow(curRating));
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/?p=battle_history');
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');
misc.writeToLog('Skipping account due to error')
logSummary.push(' Skipping account due to error')
throw new Error;
})
})
})
} catch (e) {
misc.writeErrorToLog('[Battle cannot start]:', e)
logSummary.push(chalk.red(' No records due to battle error'));
return;
}
await page.waitForTimeout(5000);
const matchsetup = await splinterlandsPage.checkMatchSetup(page)
var [mana, rules, splinters] = [matchsetup.mana, matchsetup.rules, matchsetup.splinters]
const matchDetails = {
player: process.env.ACCUSERNAME.trim(),
mana: mana,
rules: rules,
splinters: splinters,
myCards: myCards,
quest: (prioritizeQuest && quest && (quest.total != quest.completed)) ? quest : '',
}
console.log(matchDetails)
await page.waitForTimeout(1000);
//TEAM SELECTION
let teamToPlay;
if (mana == 'no mana') {
logSummary.push(' No Mana error. Skipping account.')
throw new Error(" No mana. Game server error.");
}
var priorityCards = process.env.PRIORITY_CARD;
if (priorityCards) {
priorityCards = priorityCards.split(',')
priorityCards = priorityCards.filter(x => myCards.includes(parseInt(x.trim())))
} else {
priorityCards = 0
}
misc.writeToLog(chalk.green('Battle details:'));
misc.writeToLog('Mana:'+ chalk.yellow(mana) + ' Rules:' + chalk.yellow(rules) + ' Splinters:' + chalk.yellow(splinters))
misc.writeToLog(chalk.green('starting team selection'));
var twirlTimer = (function() {
var P = ["Please wait |", "Please wait /", "Please wait -", "Please wait \\"];
var x = 0;
return setInterval(function() {
process.stdout.write("\r" + P[x++]);
x &= 3;
}, 250); })();
if (useAPI) {
try {
const apiResponse = await withTimeout(100000, api.getPossibleTeams(matchDetails,process.env.API_URL)
.then(async data =>{
if (JSON.stringify(data).includes('api limit reached')||JSON.stringify(data).includes("Rate limit exceeded")){
return api.getPossibleTeams(matchDetails,process.env.API_URL_FALLBACK)
} else {
return data
}
})
.catch(async err=>{
console.log(err)
if (err.includes('api limit reached')|| err.includes("Rate limit exceeded")){
return api.getPossibleTeams(matchDetails,process.env.API_URL_FALLBACK)
} else {
return 'api limit reached'
}
}));
if (apiResponse && !JSON.stringify(apiResponse).includes('api limit reached')) {
readline.cursorTo(process.stdout, 0);
misc.writeToLog(chalk.magenta('API Response Result: '));
console.log(chalk.cyan(' Team picked by API: '));
console.table({
'Play for quest': Object.values(apiResponse)[0],
'Team Rank': Object.values(apiResponse)[16],
'Win Percentage' : (Object.values(apiResponse)[2].replace(',','.')* 100).toFixed(2) + '%',
'Element' : Object.values(apiResponse)[15],
'Summoner': Object.values(apiResponse)[1],
'Cards 1': Object.values(apiResponse)[3],
'Cards 2': Object.values(apiResponse)[5],
'Cards 3': Object.values(apiResponse)[7],
'Cards 4': Object.values(apiResponse)[9],
'Cards 5': Object.values(apiResponse)[11],
'Cards 6': Object.values(apiResponse)[13]
});
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]]
};
subElement = helper.teamActualSplinterToPlay(splinters,teamToPlay.cards.slice(0, 6)).toLowerCase()
if (Object.values(apiResponse)[15] === 'dragon' && splinters.includes(subElement) == false ) {
misc.writeToLog('Sub-element is ' + subElement + ' but not included on available splinters.')
misc.writeToLog('API choose inappropriate splinter sub-element. Reverting to local history.');
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))
logSummary.push(' NO TEAMS available to be played')
throw new Error ('NO TEAMS available to be played');
}
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
useAPI = false;
} else {
const winPercent = (Object.values(apiResponse)[2].replace(',','.')* 100).toFixed(2)
if (winPercent < process.env.SWITCH_THRESHOLD && JSON.parse(process.env.AUTO_SWITCH.toLowerCase()) == true) { // auto-select to local if win percentage is below 50%
readline.cursorTo(process.stdout, 0);
misc.writeToLog('API choose low winning percentage splinter . Reverting to local history.');
const possibleTeams = await ask.possibleTeams(matchDetails, priorityCards).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 {
readline.cursorTo(process.stdout, 0);
misc.writeToLog('Error: ', JSON.stringify(matchDetails), JSON.stringify(possibleTeams))
logSummary.push(' NO TEAMS available to be played')
throw new Error (' NO TEAMS available to be played');
}
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
useAPI = false;
} else {
apiSelect = true;
// TEMP, testing
if (Object.values(apiResponse)[1] == '') {
readline.cursorTo(process.stdout, 0);
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')) {
readline.cursorTo(process.stdout, 0);
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');
apiSelect = 'false'
} else {
misc.writeToLog('API failed, using local history with most cards used tactic');
}
const possibleTeams = await ask.possibleTeams(matchDetails, priorityCards).catch(e => misc.writeToLog('Error from possible team API call: ', e));
if (possibleTeams && possibleTeams.length) {
readline.cursorTo(process.stdout, 0);
misc.writeToLog('Possible Teams based on your cards: ' + possibleTeams.length);
} else {
readline.cursorTo(process.stdout, 0);
misc.writeToLog('Error: ', JSON.stringify(matchDetails), JSON.stringify(possibleTeams))
logSummary.push(' NO TEAMS available to be played')
throw new Error;
}
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
useAPI = false;
}
} catch (e){
misc.writeToLog('API taking too long. Reverting to use local history' + e);
const possibleTeams = await ask.possibleTeams(matchDetails, priorityCards).catch(e => misc.writeToLog('Error from possible team API call: ', e));
if (possibleTeams && possibleTeams.length) {
readline.cursorTo(process.stdout, 0);
misc.writeToLog('Possible Teams based on your cards: ', possibleTeams.length);
} else {
misc.writeToLog('Error: ', JSON.stringify(matchDetails), JSON.stringify(possibleTeams))
logSummary.push(' NO TEAMS available to be played');
throw new Error;
}
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
useAPI = false;
}
} else {
const possibleTeams = await ask.possibleTeams(matchDetails, priorityCards).catch(e => misc.writeToLog('Error from possible team API call: ', e));
if (possibleTeams && possibleTeams.length) {
readline.cursorTo(process.stdout, 0);
misc.writeToLog('Possible Teams based on your cards: ', possibleTeams.length);
} else {
readline.cursorTo(process.stdout, 0);
misc.writeToLog('Error: ', JSON.stringify(matchDetails), JSON.stringify(possibleTeams))
logSummary.push(' NO TEAMS available to be played')
throw new Error (' NO TEAMS available to be played');
}
teamToPlay = await ask.teamSelection(possibleTeams, matchDetails, quest);
useAPI = false;
}
if (teamToPlay) {
try{
await page.click('.btn--create-team')[0];
} catch {
await page.reload().then(async () =>{
await page.waitForTimeout(5000);
await page.click('.btn--create-team')[0];
}).catch((e) => {
logSummary.push('Team Selection error')
return ('Team Selection error');
})
}
} else {
logSummary.push('Team Selection error')
}
await page.waitForTimeout(5000);
try {
await page.waitForXPath(`//div[@card_detail_id="${teamToPlay.summoner}"]`, {
timeout: 15000
}).then(summonerButton => summonerButton.click()).catch( async (error) =>{
await page.reload()
await page.waitForTimeout(5000);
page.click('.btn--create-team')[0];
await page.waitForTimeout(5000);
await page.waitForXPath(`//div[@card_detail_id="${teamToPlay.summoner}"]`, {
timeout: 30000
}).then(summonerButton => summonerButton.click())
});
if (card.color(teamToPlay.cards[0]) === 'Gold') {
readline.cursorTo(process.stdout, 0);
misc.writeToLog(' Dragon play TEAMCOLOR ' + helper.teamActualSplinterToPlay(splinters,teamToPlay.cards.slice(0, 6)))
await page.waitForXPath(`//div[@data-original-title="${helper.teamActualSplinterToPlay(splinters,teamToPlay.cards.slice(0, 6))}"]`, {
timeout: 8000
})
.then(selector => selector.click()).catch( async (error) =>{
await page.reload()
await page.waitForTimeout(5000);
page.click('.btn--create-team')[0];
await page.waitForTimeout(5000);
await page.waitForXPath(`//div[@data-original-title="${helper.teamActualSplinterToPlay(splinters,teamToPlay.cards.slice(0, 6))}"]`, {
timeout: 30000
})
.then(selector => selector.click())
});
}
await page.waitForTimeout(10000);
clearInterval(twirlTimer);
readline.cursorTo(process.stdout, 0);
misc.writeToLog('Summoner: ' + chalk.yellow(teamToPlay.summoner.toString().padStart(3)) + ' Name: ' + chalk.green(allCardDetails[(parseInt(teamToPlay.summoner))-1].name.toString()));
for (i = 1; i <= 6; i++) {
await sleep(300);
let strCard = 'nocard';
if(teamToPlay.cards[i] != ''){ strCard = allCardDetails[(parseInt(teamToPlay.cards[i]))-1].name.toString(); }
if(strCard !== 'nocard'){
misc.writeToLog('Play: ' + chalk.yellow(teamToPlay.cards[i].toString().padStart(3)) + ' Name: ' + chalk.green(strCard));
} else {
misc.writeToLog(' ' + strCard);
}
if (teamToPlay.cards[i]){
await page.waitForXPath(`//div[@card_detail_id="${teamToPlay.cards[i].toString()}"]`, {timeout: 20000})
.then(selector => selector.click())}
await page.waitForTimeout(1000);
}
await page.waitForTimeout(2000);
try {
misc.writeToLog('Team submit. Please wait for the result.');
await page.click('.btn-green')[0]; //start fight
} catch {
misc.writeToLog('Start Fight didnt work, waiting 5 sec and retry');
await page.waitForTimeout(2000);
await page.click('.btn-green')[0]; //start fight
}
var twirlTimer = (function() {
var P = ["Please wait |", "Please wait /", "Please wait -", "Please wait \\"];
var x = 0;
return setInterval(function() {
process.stdout.write("\r" + P[x++]);
x &= 3;
}, 250); })();
await page.waitForTimeout(2000);
await page.waitForSelector('#btnRumble', {
timeout: 160000
}).then(() => {
clearInterval(twirlTimer);
readline.cursorTo(process.stdout, 0);
misc.writeToLog('btnRumble visible')
}).catch(() => {
clearInterval(twirlTimer);
readline.cursorTo(process.stdout, 0);
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
//getting battle result
misc.writeToLog('Getting battle result...');
await page.goto('https://splinterlands.io/?p=battle_history');
const battleResult = await fetch(`https://api2.splinterlands.com/battle/history?player=${process.env.ACCUSERNAME}`).then(response => response.json())
.catch(async () => await fetch(`https://api.splinterlands.io/battle/history?player=${process.env.ACCUSERNAME}`).then(response => response.json()))
.catch(async () => await fetch(`https://api.steemmonsters.io/battle/history?player=${process.env.ACCUSERNAME}`).then(response => response.json()))
.catch(async () => await fetch(`https://game-api.splinterlands.io/battle/history?player=${process.env.ACCUSERNAME}`).then(response => response.json()))
.catch((e) => {
misc.writeToLog(e);
misc.writeToLog(chalk.blueBright('Could not find winner'));
logSummary.push(chalk.blueBright(' Could not find winner'));
newlogvisual['Battle Result'] = 'Could not find winner'
})
if (battleResult) {
const winner = battleResult.battles[0].winner
const decWon = battleResult.battles[0].reward_dec
const SPSwon = battleResult.battles[0].reward_sps
await waitUntilLoaded(page);
if (winner.trim() == process.env.ACCUSERNAME.trim()) {
misc.writeToLog(chalk.green('You won!'));
let battleRewards = []
battleRewards.reward = {DEC : decWon, SPS : SPSwon}
console.table(battleRewards)
logSummary.push(' Battle result:' + chalk.green(' Win Reward: DEC ' + decWon + ' SPS ' + SPSwon));
newlogvisual['Battle Result'] = 'Win ' + decWon
} else if (winner.trim() === "DRAW") {
misc.writeToLog(chalk.yellow("It's a draw"));
logSummary.push(' Battle result:' + chalk.blueBright(' Draw'));
newlogvisual['Battle Result'] = 'Draw'
} else {
misc.writeToLog(chalk.red('You lost :('));
logSummary.push(' Battle result:' + chalk.red(' Lose'));
newlogvisual['Battle Result'] = 'Lose'
if (useAPI) {
api.reportLoss(winner);
}
}
if (getDataLocal == true) {
let fileSizeInMegabytes = (await getFilesizeInBytes("data/newHistory.json") / 1024) // *1024)
if (fileSizeInMegabytes.toString().split('.')[0] >= 490000) {
misc.writeToLog("Unable to gather data as newHistory file is now 500MB")
} else {
misc.writeToLog("Gathering winner's battle data for local history backup")
await battles.battlesList(winner).then(x=>x).catch((e) => misc.writeToLog('Unable to gather data for local.' + e));
}
}
}
try {
await closePopups(page).catch(()=>misc.writeToLog('No pop up to be closed.'));
const UpDateDec = (await page.evaluate(()=>SM.Player.balances.find(x=>x.token=='DEC').balance)).toFixed(2);
const newERCRaw = await page.evaluate(()=>SM.Player.balances.find(x=>x.token=='ECR').balance)
.then(ecrApi2 => {
ecrApi2 = (ecrApi2.toString()).slice(0, 2)+ "." + (ecrApi2.toString()).slice(2)
return ecrApi2 });
const newRating = await page.evaluate(()=>SM.Player.rating);
misc.writeToLog('Updated Rating after battle is ' + chalk.yellow(newRating));
logSummary.push(' New rating: ' + chalk.yellow(newRating));
logSummary.push(' New DEC Balance: ' + chalk.cyan(UpDateDec + ' DEC'));
newlogvisual['Rating'] = newRating
newlogvisual['DEC Balance'] = UpDateDec + ' dec'
let e = parseFloat(newERCRaw);
if (e >= 50) {
newERC = chalk.green(e + '%')
}
else {
newERC = chalk.red(e + '%')
}
logSummary.push(' Remaining ERC: ' + newERC);
misc.writeToLog('Remaining ERC: ' + newERC);
newlogvisual['ERC'] = newERC.replace(/\u001b[^m]*?m/g,"")
} catch (e) {
misc.writeToLog(e);
misc.writeToLog(chalk.blueBright(' Unable to get new rating'));
misc.writeToLog(chalk.blueBright(' Unable to get remaining ERC'));
logSummary.push(chalk.blueBright(' Unable to get new rating'));
logSummary.push(chalk.blueBright(' Unable to get remaining ERC '));
newlogvisual['Rating'] = 'n/a'
newlogvisual['DEC Balance'] = 'n/a'
newlogvisual['ECR']= 'n/a'
}
let Newquest = await getQuest();
await nq.newquestUpdate(Newquest, claimQuestReward, page, logSummary, allCardDetails, newlogvisual, powerThreshold, powerRaw);
teamToPlay = '';
erc='';
useAPI ='';
winPercent ='';
newERC = '';
} catch (e) {
clearInterval(twirlTimer);
readline.cursorTo(process.stdout, 0);
misc.writeToLog(' Unable to proceed due to error.' + e)
logSummary.push(chalk.red(' Unable to proceed due to error. Please see logs'));
return;
}
} catch (e) {
clearInterval(twirlTimer);
readline.cursorTo(process.stdout, 0);
misc.writeToLog(' Unable to proceed due to error.' + e)
return;
}
}
// 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 {
if (process.env.TELEGRAM_NOTIF === 'true') { tn.startTG()}
const accountusers = process.env.ACCUSERNAME.split(',');
const accounts = 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 claimQuestReward = JSON.parse(process.env.CLAIM_QUEST_REWARD.toLowerCase());
const prioritizeQuest = JSON.parse(process.env.QUEST_PRIORITY.toLowerCase());
const teleNotif = JSON.parse(process.env.TELEGRAM_NOTIF.toLowerCase());
const getDataLocal = JSON.parse(process.env.GET_DATA_FOR_LOCAL.toLowerCase());
const autoSwitch = JSON.parse(process.env.AUTO_SWITCH.toLowerCase());
await checkForUpdate(teleNotif);
await checkForMissingConfigs(teleNotif);
let browsers = [];
misc.writeToLogNoUsername('Headless: ' + headless);
misc.writeToLogNoUsername('Get data for local history: ' + getDataLocal);
misc.writeToLogNoUsername('Claim Quest Reward: ' + claimQuestReward);
misc.writeToLogNoUsername('Prioritize Quests: ' + prioritizeQuest);
misc.writeToLogNoUsername('Auto Switch to Local: ' + autoSwitch);
misc.writeToLogNoUsername('Telegram Notification: ' + teleNotif);
misc.writeToLogNoUsername('Use API: ' + useAPI);
misc.writeToLogNoUsername('Loaded ' + chalk.yellow(accounts.length) + ' Accounts');
misc.writeToLogNoUsername('Accounts: ' + chalk.greenBright(accounts));
while (true) {
let logSummary = [];
let logSummary1 = [];
let seasonRewards = [];
let startTimer = new Date().getTime();
if (process.env.TELEGRAM_NOTIF === 'true'){tn.sender(' Bot Initiated: Battle now starting.' + ' \n' + ' Please wait for the battle results.')};
for (let i = 0; i < accounts.length; i++) {
process.env['EMAIL'] = accounts[i];
process.env['PASSWORD'] = passwords[i];
process.env['ACCUSERNAME'] = accountusers[i];
if (browsers.length == 0){
misc.writeToLog('Opening browser');
browsers = await createBrowsers(1, headless);
}
const page = (await browsers[0].pages())[1];
page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36');
page.setViewport({ width: 1800,height: 1500,deviceScaleFactor: 1,}).then(()=> page.goto(`https://splinterlands.io`))
//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, logSummary, getDataLocal , logSummary1, seasonRewards)
.then(() => {
misc.writeToLog('Closing battle \n');
})
.catch((e) => {
misc.writeToLog(e)
})
await page.waitForTimeout(5000);
if (accounts.indexOf(process.env.ACCUSERNAME) + 1 === accounts.length) {
await browsers[0].close();
browsers[0].process().kill('SIGKILL');
browsers = [];
} else {
await page.evaluate(async function () {
await SM.Logout(); // this makes puppeteer faster.
}).catch(async function(){
await browsers[0].close(); // in case game maintenance
await browsers[0].process().kill('SIGKILL');
browsers = [];
});
}
}
let endTimer = new Date().getTime();
let totalTime = endTimer - startTimer;
let tet = ' Total execution time: ' + chalk.green((totalTime / 1000 / 60).toFixed(2) + ' mins')
console.log('--------------------------Battle Result Summary:----------------------');
console.log(tet);
if (unitVersion == 'default'){
if (accounts.length > 1) {
logSummary.forEach(x => console.log(x));
}
} else if (unitVersion == 'desktop') {
console.table(logSummary1)
if (seasonRewards.length > 0){
console.table(seasonRewards)
}
} else if (unitVersion == 'mobile') {
console.table(logSummary1,["Power",'Battle Result','Rating','DEC Balance'])
console.table(logSummary1,['ERC', 'Quest','Reward'])
if (seasonRewards.length > 0){
console.table(seasonRewards)
}
}
// telegram notification
if (process.env.TELEGRAM_NOTIF === 'true') {
tn.battlesummary(logSummary,tet,sleepingTime, sleep)
}
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--------------------------------');
browsers = [];
seasonRewards = [];
startTimer = '';
logSummary1= [];
await new Promise(r => setTimeout(r, sleepingTime));
}
} catch (e) {
let browsers = await puppeteer.launch({product: 'chrome'})
await browsers.close().then(()=> console.log('Chromium is a bitch, have to kill it to save CPU memory.'));
await browsers.process().kill('SIGKILL');
if (process.env.TELEGRAM_NOTIF === 'true'){tn.sender("Bot stops due to error. Please see logs for details.")};
console.log('Routine error at: ', new Date().toLocaleString(), e)
console.log(e.lineNumber)
}
})();