Skip to content

Commit

Permalink
Merge pull request #100 from alfficcadenti/delegatedCardsPreference
Browse files Browse the repository at this point in the history
added preferred cards option to prioritize delegated cards
and fixed bug on opening daily quest box
  • Loading branch information
alfficcadenti authored Jan 8, 2022
2 parents 7d68bf4 + a3be8ee commit e57632e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ CLAIM_DAILY_QUEST_REWARD=true
#FAVOURITE_DECK=dragon #choose only one splinter among: fire, life, earth, water, death, dragon
#MULTI_ACCOUNT=false
ACCOUNT=lowercase_username
PASSWORD=postingkey
PASSWORD=postingkey
#DELEGATED_CARDS_PRIORITY=true
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ By default, the BOT will run as headless. Set `HEADLESS` to false to see your br
By default, the BOT will run no matter the ECR level. Set `ECR_STOP_LIMIT` to a specific value you want the bot to rest and recover the ECR. The bot will recover until the `ECR_RECOVER_TO` is reached or until 100% ECR.
If you want the bot to play only one color (when it's possible), use the variable `FAVOURITE_DECK` and specify the splinter by choosing only one among: fire, life, earth, water, death, dragon.
If you want the bot to try to skip specific quest types you can include multiple quest in the variable `SKIP_QUEST` separated by the comma (`SKIP_QUEST=life,snipe,neutral`). whenever it's possible, the bot will click to ask for a new one. Remember you can only ask for a new one once based on the game rules.
If you want the bot to prioritize teams that uses delegated cards, set the variable `DELEGATED_CARDS_PRIORITY` equal to `true`.

Example:

Expand All @@ -71,6 +72,9 @@ Example:

- `SKIP_QUEST=life,snipe,neutral`

- `DELEGATED_CARDS_PRIORITY=true`


### Running bot with multiaccount setting

in order to run multple accounts launching the script only once, you can simply add the list of usernames and posting keys in the .env file and set the variable `MULTI_ACCOUNT` as true:
Expand Down
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ async function getCards() {
return myCards;
}

// LOAD RENTED CARDS AS PREFERRED
async function getPreferredCards() {
const myPreferredCards = await user.getRentedCards(account)
return myPreferredCards;
}

async function getQuest() {
return quests.getPlayerQuest(account)
.then(x=>x)
Expand Down Expand Up @@ -379,7 +385,11 @@ async function startBotPlayMatch(page, browser) {

console.log('getting user cards collection from splinterlands API...')
const myCards = await getCards()
.then((x)=>{console.log('cards retrieved'); return x})
.then((x)=>{console.log('cards retrieved:',x?.length); return x})
.catch(()=>console.log('cards collection api didnt respond. Did you use username? avoid email!'));

const myPreferredCards = await getPreferredCards()
.then((x)=>{console.log('delegated cards size:', x?.length, x); return x})
.catch(()=>console.log('cards collection api didnt respond. Did you use username? avoid email!'));

if(myCards) {
Expand Down Expand Up @@ -415,7 +425,8 @@ async function startBotPlayMatch(page, browser) {
if (isClaimDailyQuestMode === true) {
try {
await page.waitForSelector('#quest_claim_btn', { timeout: 5000 })
.then(button => button.click());
.then(button => button.click())
.then(()=>page.goto('https://splinterlands.com/?p=battle_history'));
} catch (e) {
console.info('no quest reward to be claimed waiting for the battle...')
}
Expand All @@ -437,7 +448,8 @@ async function startBotPlayMatch(page, browser) {
mana: mana,
rules: rules,
splinters: splinters,
myCards: myCards
myCards: myCards,
preferredCards: myPreferredCards
}
await page.waitForTimeout(2000);
const possibleTeams = await ask.possibleTeams(matchDetails, account).catch(e=>console.log('Error from possible team API call: ',e));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "splinterlands-bot",
"version": "1.8.4",
"version": "1.10.0",
"description": "",
"main": "main.js",
"engines": {
Expand Down
35 changes: 32 additions & 3 deletions possibleTeams.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,31 @@ const filterOutUnplayableDragonsAnfUnplayableSplinters = (teams = [], matchDetai
return filteredTeamsForAvailableSplinters || teams;
}

const filterPreferredCardsTeams = (teams = [], preferredCards = []) => teams.filter(team => team.slice(0,6).some(card => preferredCards.includes(card)))

const teamSelection = async (possibleTeams, matchDetails, quest, favouriteDeck) => {
let priorityToTheQuest = process.env.QUEST_PRIORITY === 'false' ? false : true;
console.log('quest custom option set as:', priorityToTheQuest)
const availableTeamsToPlay = await filterOutUnplayableDragonsAnfUnplayableSplinters(possibleTeams ,matchDetails);
let filterPreferredTeams = [];
if(process.env.DELEGATED_CARDS_PRIORITY === 'true') filterPreferredTeams = await filterPreferredCardsTeams(availableTeamsToPlay,matchDetails?.preferredCards)

//CHECK FOR QUEST:
if(priorityToTheQuest && availableTeamsToPlay.length > 10 && quest && quest.total) {
const left = quest.total - quest.completed;
const questCheck = matchDetails.splinters.includes(quest.splinter) && left > 0;
const filteredTeamsForQuest = availableTeamsToPlay.filter(team=>team[7]===quest.splinter)
const filteredTeamsPreferredCardsForQuest = filterPreferredTeams.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)

//QUEST FOR V2
if (process.env.API_VERSION == 2 && availableTeamsToPlay[0][8]) {
console.log('V2 try to play for the quest?')
if(left > 0 && filteredTeamsForQuest?.length >= 1 && questCheck && filteredTeamsForQuest[0][8]) {
if(left > 0 && filteredTeamsPreferredCardsForQuest?.length >= 1 && questCheck && filteredTeamsPreferredCardsForQuest[0][8]) {
console.log('PLAY for the quest with Teams choice of size (V2): ',filteredTeamsPreferredCardsForQuest.length, 'PLAY this with preferred cards: ', filteredTeamsPreferredCardsForQuest[0])
return { summoner: filteredTeamsPreferredCardsForQuest[0][0], cards: filteredTeamsPreferredCardsForQuest[0] };
} else if(left > 0 && filteredTeamsForQuest?.length >= 1 && questCheck && filteredTeamsForQuest[0][8]) {
console.log('PLAY for the quest with Teams choice of size (V2): ',filteredTeamsForQuest.length, 'PLAY this: ', filteredTeamsForQuest[0])
return { summoner: filteredTeamsForQuest[0][0], cards: filteredTeamsForQuest[0] };
} else {
Expand All @@ -251,7 +259,17 @@ const teamSelection = async (possibleTeams, matchDetails, quest, favouriteDeck)
} else if (process.env.API_VERSION!=2 && availableTeamsToPlay[0][0]) {
// QUEST FOR V1
console.log('play quest for V1')
if(left > 0 && filteredTeamsForQuest && filteredTeamsForQuest?.length > 3 && splinters.includes(quest.splinter)) {
if(left > 0 && filteredTeamsPreferredCardsForQuest?.length) {
console.log('Try to play for the quest with Teams size (V1): ',filteredTeamsPreferredCardsForQuest.length)
console.log("TEAMS:", filteredTeamsPreferredCardsForQuest)
const res = await mostWinningSummonerTankCombo(filteredTeamsPreferredCardsForQuest, matchDetails);
if (res[0] && res[1]) {
console.log('Play this with preferred cards for the quest:', res)
return { summoner: res[0], cards: res[1] };
}
}

if(left > 0 && filteredTeamsForQuest?.length > 3 && splinters.includes(quest.splinter)) {
console.log('Try to play for the quest with Teams size (V1): ',filteredTeamsForQuest.length)
const res = await mostWinningSummonerTankCombo(filteredTeamsForQuest, matchDetails);
if (res[0] && res[1]) {
Expand Down Expand Up @@ -295,7 +313,11 @@ const teamSelection = async (possibleTeams, matchDetails, quest, favouriteDeck)

//V2 Strategy ONLY FOR PRIVATE API
if (process.env.API_VERSION == 2 && availableTeamsToPlay?.[0]?.[8]) {
if(availableTeamsToPlay?.length) {
if(filterPreferredTeams?.length) {
console.log('play the highest winning rate team with preferred cards: ', filterPreferredTeams[0])
return { summoner: filterPreferredTeams[0][0], cards: filterPreferredTeams[0] };
}
else if(availableTeamsToPlay?.length) {
console.log('play the highest winning rate team: ', availableTeamsToPlay[0])
return { summoner: availableTeamsToPlay[0][0], cards: availableTeamsToPlay[0] };
}
Expand All @@ -306,6 +328,13 @@ const teamSelection = async (possibleTeams, matchDetails, quest, favouriteDeck)
} else if (process.env.API_VERSION!=2 && availableTeamsToPlay[0][0]) {
//V1 Strategy
//find best combination (most used)
if(filterPreferredTeams?.length) {
const res = await mostWinningSummonerTankCombo(filterPreferredTeams, matchDetails);
if (res[0] && res[1]) {
console.log('Dont play for the quest, and play this with preferred cards:', res)
return { summoner: res[0], cards: res[1] };
}
}
const res = await mostWinningSummonerTankCombo(availableTeamsToPlay, matchDetails);
if (res[0] && res[1]) {
console.log('Dont play for the quest, and play this:', res)
Expand Down
21 changes: 20 additions & 1 deletion user.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,23 @@ getPlayerCards = (username) => (fetch(`https://api2.splinterlands.com/cards/coll
})
)

module.exports.getPlayerCards = getPlayerCards;
getRentedCards = (username) => (fetch(`https://api2.splinterlands.com/cards/collection/${username}`,
{ "credentials": "omit", "headers": { "accept": "application/json, text/javascript, */*; q=0.01" }, "referrer": `https://splinterlands.com/?p=collection&a=${username}`, "referrerPolicy": "no-referrer-when-downgrade", "body": null, "method": "GET", "mode": "cors" })
.then(x => x && x.json())
.then(x => x['cards'] ? x['cards'].filter(x=>x.player !== username && x.delegated_to === username).map(card => card.card_detail_id) : '')
.catch(e=> {
console.log('Error: game-api.splinterlands did not respond trying api.slinterlands... ');
fetch(`https://api.splinterlands.io/cards/collection/${username}`,
{ "credentials": "omit", "headers": { "accept": "application/json, text/javascript, */*; q=0.01" }, "referrer": `https://splinterlands.com/?p=collection&a=${username}`, "referrerPolicy": "no-referrer-when-downgrade", "body": null, "method": "GET", "mode": "cors" })
.then(x => x && x.json())
.then(x => x['cards'] ? x['cards'].filter(x=>x.player !== username && x.delegated_to === username).map(card => card.card_detail_id) : '')
.then(advanced => basicCards.concat(advanced))
.catch(e => {
console.log('Using only basic cards due to error when getting user collection from splinterlands: ',e);
return basicCards
})
})
)

module.exports.getPlayerCards = getPlayerCards;
module.exports.getRentedCards = getRentedCards;

0 comments on commit e57632e

Please sign in to comment.