Using JavaScript to analyze Splinterlands Battle Data
I got most of the script from a splinterlands bot. I get 50 battles from history with API and get my previous opponents 50 battles and store them in a file.
Complete code can be found at battles-data
Get Battles from API
async function getBattleHistory(player = '', data = {}) {
const battleHistory = await fetch('https://api.steemmonsters.io/battle/history?player=' + player)
// Code //
return battleHistory.battles;
}
The above function will retrieve 50 battles history for a given player. 50 battles because that is the limit set by the API. Before I gather all the battles I obtain the list of all opponents of a player and get the battles for them along with the player. By taking the battles from the player would set the level of the players and tier information to be relevant. For a bronze tier player, a silver tier player's information is useless because the cards used will be different and of different levels.
First Promise
getBattleHistory(process.env.ACCOUNT)
.then(u => u.map(x => {
return [x.player_1, x.player_2]
}).flat().filter(distinct))
getBattleHistory(process.env.ACCOUNT) | of the player, stored in a .env file. |
| Get the players of the battle in an array. |
| Flatten and make the array consist of distinct values |
Promise each player
let promises = [];
-> start with empty promises.
For each player above we push a promise, to the above array, to getBattleHistory(player)
. We extract information from a battle using extractGeneralInfo
and extractMonster
from the battle and teams respectively. each monster and the summoner details will be as follows:
summoner: {
id: team.summoner.card_detail_id,
level: team.summoner.level,
gold: team.summoner.gold,
},
This will boil down to:
| Foreach user in the ul |
| Promise each user to getBattleHistory |
| extract information from each battle and add it to battlesList |
Keeping all promises
The final part is to save all the information to a JSON file, which can be used later or analyzed further.
| Wait for all the promises to getBattleHistory |
| Read the json to update it with fresh data |
| update the file by writing to it |
I found the concept of Promise in JavaScript quite puzzling at first, but when I understood it, it was quite simple and elegant. Adding the promise array to gather all the promises was the result of a lot of StackOverflow research.
Credits
- NeoVim for an editor
- WezTerm for terminal
- splinterlands-bot for most of this code like 80%
Yay! 🤗
Your content has been boosted with Ecency Points
Use Ecency daily to boost your growth on platform!
Support Ecency
Vote for Proposal
Delegate HP and earn more, by @azarmadr3.
This is a very good content. How about if we want the bot to use rented cards.
currently the bots in circulation depend on dataset. you need to generate reliable datasets for your rented cards. maybe grab the battles of a player that has used them