forked from alfficcadenti/splinterlands-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpossibleTeams.js
228 lines (205 loc) · 9.55 KB
/
possibleTeams.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
const cards = require('./getCards.js');
const card = require('./cards');
const helper = require('./helper');
const battles = require('./battles');
const fetch = require("node-fetch");
const summoners = [{ 224: 'dragon' },
{ 27: 'earth' },
{ 16: 'water' },
{ 156: 'life' },
{ 189: 'earth' },
{ 167: 'fire' },
{ 145: 'death' },
{ 5: 'fire' },
{ 71: 'water' },
{ 114: 'dragon' },
{ 178: 'water' },
{ 110: 'fire' },
{ 49: 'death' },
{ 88: 'dragon' },
{ 38: 'life' },
{ 239: 'life' },
{ 74: 'death' },
{ 78: 'dragon' },
{ 260: 'fire' },
{ 70: 'fire' },
{ 109: 'death' },
{ 111: 'water' },
{ 112: 'earth' },
{ 130: 'dragon' },
{ 72: 'earth' },
{ 235: 'dragon' },
{ 56: 'dragon' },
{ 113: 'life' },
{ 200: 'dragon' },
{ 236: 'fire' },
{ 240: 'dragon' },
{ 254: 'water' },
{ 257: 'water' },
{ 258: 'dragon' },
{ 259: 'earth' },
{ 261: 'life' },
{ 262: 'dragon' },
{ 278: 'earth' },
{ 73: 'life' }]
const splinters = ['fire', 'life', 'earth', 'water', 'death', 'dragon']
const summonerColor = (id) => {
const summonerDetails = summoners.find(x => x[id]);
return summonerDetails ? summonerDetails[id] : '';
}
const historyBackup = require("./data/newHistory.json");
const basicCards = require('./data/basicCards.js');
const { filter } = require('./data/basicCards.js');
let availabilityCheck = (base, toCheck) => toCheck.slice(0, 7).every(v => base.includes(v));
const getBattlesWithRuleset = (ruleset, mana) => {
const rulesetEncoded = encodeURIComponent(ruleset);
const host = 'https://splinterlands-data-service.herokuapp.com/'
//const host = 'http://localhost:4000/'
const url = `battlesruleset?ruleset=${rulesetEncoded}&mana=${mana}&player=${process.env.ACCOUNT}`;
console.log('API call: ', host+url)
return fetch(host+url)
.then(x => x && x.json())
.then(data => data)
.catch((e) => console.log('fetch ', e))
}
const battlesFilterByManacap = async (mana, ruleset) => {
const history = await getBattlesWithRuleset(ruleset, mana);
if (history) {
console.log('API battles returned ', history.length)
return history.filter(
battle =>
battle.mana_cap == mana &&
(ruleset ? battle.ruleset === ruleset : true)
)
}
const backupLength = historyBackup && historyBackup.length
console.log('API battles did not return ', history)
console.log('Using Backup ', backupLength)
return historyBackup.filter(
battle =>
battle.mana_cap == mana &&
(ruleset ? battle.ruleset === ruleset : true)
)
}
const cardsIdsforSelectedBattles = (mana, ruleset, splinters) => battlesFilterByManacap(mana, ruleset, splinters)
.then(x => {
return x.map(
(x) => {
return [
x.summoner_id ? parseInt(x.summoner_id) : '',
x.monster_1_id ? parseInt(x.monster_1_id) : '',
x.monster_2_id ? parseInt(x.monster_2_id) : '',
x.monster_3_id ? parseInt(x.monster_3_id) : '',
x.monster_4_id ? parseInt(x.monster_4_id) : '',
x.monster_5_id ? parseInt(x.monster_5_id) : '',
x.monster_6_id ? parseInt(x.monster_6_id) : '',
summonerColor(x.summoner_id) ? summonerColor(x.summoner_id) : ''
]
}
).filter(
team => splinters.includes(team[7])
)
})
const askFormation = function (matchDetails) {
const cards = matchDetails.myCards || basicCards;
console.log('INPUT: ', matchDetails.mana, matchDetails.rules, matchDetails.splinters, cards.length)
return cardsIdsforSelectedBattles(matchDetails.mana, matchDetails.rules, matchDetails.splinters)
.then(x => x.filter(
x => availabilityCheck(cards, x))
.map(element => element)//cards.cardByIds(element)
)
}
const possibleTeams = async (matchDetails) => {
let possibleTeams = [];
while (matchDetails.mana > 0) {
console.log('check battles based on mana: '+matchDetails.mana)
possibleTeams = await askFormation(matchDetails)
if (possibleTeams.length > 0) {
return possibleTeams;
}
matchDetails.mana--;
}
return possibleTeams;
}
const mostWinningSummonerTankCombo = async (possibleTeams, matchDetails) => {
const bestCombination = await battles.mostWinningSummonerTank(possibleTeams)
console.log('BEST SUMMONER and TANK', bestCombination)
if (bestCombination.summonerWins >= 1 && bestCombination.tankWins > 1 && bestCombination.backlineWins > 1 && bestCombination.secondBacklineWins > 1 && bestCombination.thirdBacklineWins > 1 && bestCombination.forthBacklineWins > 1) {
const bestTeam = await possibleTeams.find(x => x[0] == bestCombination.bestSummoner && x[1] == bestCombination.bestTank && x[2] == bestCombination.bestBackline && x[3] == bestCombination.bestSecondBackline && x[4] == bestCombination.bestThirdBackline && x[5] == bestCombination.bestForthBackline)
console.log('BEST TEAM', bestTeam)
const summoner = bestTeam[0].toString();
return [summoner, bestTeam];
}
if (bestCombination.summonerWins >= 1 && bestCombination.tankWins > 1 && bestCombination.backlineWins > 1 && bestCombination.secondBacklineWins > 1 && bestCombination.thirdBacklineWins > 1) {
const bestTeam = await possibleTeams.find(x => x[0] == bestCombination.bestSummoner && x[1] == bestCombination.bestTank && x[2] == bestCombination.bestBackline && x[3] == bestCombination.bestSecondBackline && x[4] == bestCombination.bestThirdBackline)
console.log('BEST TEAM', bestTeam)
const summoner = bestTeam[0].toString();
return [summoner, bestTeam];
}
if (bestCombination.summonerWins >= 1 && bestCombination.tankWins > 1 && bestCombination.backlineWins > 1 && bestCombination.secondBacklineWins > 1) {
const bestTeam = await possibleTeams.find(x => x[0] == bestCombination.bestSummoner && x[1] == bestCombination.bestTank && x[2] == bestCombination.bestBackline && x[3] == bestCombination.bestSecondBackline)
console.log('BEST TEAM', bestTeam)
const summoner = bestTeam[0].toString();
return [summoner, bestTeam];
}
if (bestCombination.summonerWins >= 1 && bestCombination.tankWins > 1 && bestCombination.backlineWins > 1) {
const bestTeam = await possibleTeams.find(x => x[0] == bestCombination.bestSummoner && x[1] == bestCombination.bestTank && x[2] == bestCombination.bestBackline)
console.log('BEST TEAM', bestTeam)
const summoner = bestTeam[0].toString();
return [summoner, bestTeam];
}
if (bestCombination.summonerWins >= 1 && bestCombination.tankWins > 1) {
const bestTeam = await possibleTeams.find(x => x[0] == bestCombination.bestSummoner && x[1] == bestCombination.bestTank)
console.log('BEST TEAM', bestTeam)
const summoner = bestTeam[0].toString();
return [summoner, bestTeam];
}
if (bestCombination.summonerWins >= 1) {
const bestTeam = await possibleTeams.find(x => x[0] == bestCombination.bestSummoner)
console.log('BEST TEAM', bestTeam)
const summoner = bestTeam[0].toString();
return [summoner, bestTeam];
}
}
const teamSelection = async (possibleTeams, matchDetails, quest) => {
//check if daily quest is not completed
if(possibleTeams.length > 25 && quest && quest.total) {
const left = quest.total - quest.completed;
const questCheck = matchDetails.splinters.includes(quest.splinter) && left > 0;
const filteredTeams = possibleTeams.filter(team=>team[7]===quest.splinter)
console.log(left + ' battles left for the '+quest.splinter+' quest')
console.log('play for the quest ',quest.splinter,'? ',questCheck)
if(left > 0 && filteredTeams && filteredTeams.length > 10 && splinters.includes(quest.splinter)) {
console.log('PLAY for the quest with Teams: ',filteredTeams.length , filteredTeams)
const res = await mostWinningSummonerTankCombo(filteredTeams, matchDetails);
console.log('Play this for the quest:', res)
if (res[0] && res[1]) {
return { summoner: res[0], cards: res[1] };
}
}
//find best combination (most used)
const res = await mostWinningSummonerTankCombo(possibleTeams, matchDetails);
console.log('Dont play for the quest, and play this:', res)
if (res[0] && res[1]) {
return { summoner: res[0], cards: res[1] };
}
}
let i = 0;
for (i = 0; i <= possibleTeams.length - 1; i++) {
if (matchDetails.splinters.includes(possibleTeams[i][7]) && helper.teamActualSplinterToPlay(possibleTeams[i]) !== '' && matchDetails.splinters.includes(helper.teamActualSplinterToPlay(possibleTeams[i]).toLowerCase())) {
console.log('Less than 25 teams available. SELECTED: ', possibleTeams[i]);
const summoner = card.makeCardId(possibleTeams[i][0].toString());
return { summoner: summoner, cards: possibleTeams[i] };
}
console.log('DISCARDED: ', possibleTeams[i])
}
throw new Error('NO TEAM available to be played.');
}
module.exports.possibleTeams = possibleTeams;
module.exports.teamSelection = teamSelection;
// const summoners = history.map(x => x.summoner_id);
// // console.log([...new Set(summoners)])
// console.log(summonerColor(27))
// // TO TEST uncomment below:
// const matchDetails = { mana: 30, rules: '', splinters: ['fire','water','life','earth','death'], myCards: myCards}
// console.log(possibleTeams(matchDetails))