Hello again, a little update to my previous post on claiming stash on TerraCore.
Rather than claim every 4 hours I thought I'd try pulling the leader board statistics and claiming when I reach the 'danger zone' of being attacked, this gives me flexibility to increase or decrease my mining rate (engineering) by switching gear.
Of course I'm using ChatGPT again, other than fiddling with a few values and variables it's all AI coded! Boy I wish I'd explored this concept sooner! 🤣
Basically the script pulls data using HTTP API in JSON format, parses it, displays some info on screen and triggers the previous function to claim.
One other change, rather than set active key using 'set' from command line I've used Windows environment variables as for some reason the data would get lost, causing errors and exiting the script! ðŸ˜
Code as follows:
const axios = require('axios');
const { Client, PrivateKey } = require('@hiveio/dhive');
const { exec } = require('child_process');
const myusername = 'hive_username';
const amountToClaim = 100;
const client = new Client('https://api.hive.blog');
function clearConsole() {
// Clear console
console.clear();
// Print current timestamp
console.log(Timestamp: ${new Date().toLocaleString()}\n);
}
async function claim(player, amount) {
const privateKey = PrivateKey.fromString(process.env.ACTIVE_KEY);
const publicKey = privateKey.createPublic();
const op = [
'custom_json',
{
required_auths: [],
required_posting_auths: [player.username],
id: 'terracore_claim',
json: JSON.stringify({ amount, 'tx-hash': generateHash() })
}
];
const result = await client.broadcast.sendOperations([op], privateKey, [publicKey]);
console.log('Transaction broadcasted:', result);
}
function generateHash() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
function displayLeaderboard() {
clearConsole();
axios.get('http://terracore.herokuapp.com/leaderboard')
.then(response => {
const players = response.data;
const myusernamePlayer = players.find(player => player.username === myusername);
if (!myusernamePlayer) {
console.log(`Could not find ${myusername} in the leaderboard.`);
return;
}
const filteredPlayers = players.filter(player => {
return player.stats.defense <= myusernamePlayer.stats.defense && player.scrap > myusernamePlayer.scrap;
});
const sortedPlayers = filteredPlayers.sort((a, b) => b.scrap - a.scrap);
const top10Players = sortedPlayers.slice(0, 10);
console.log(`${myusername}:`);
console.table({
Player: myusernamePlayer.username,
'Stats Engineering': myusernamePlayer.stats.engineering,
'Stats Defense': myusernamePlayer.stats.defense,
Scrap: myusernamePlayer.scrap,
Claims: myusernamePlayer.claims,
Attacks: myusernamePlayer.attacks
});
console.log('');
console.log('Top 10 players with defense equal to or lower than ' + myusername + ' and higher scrap:');
console.table(top10Players.map(player => ({
Player: player.username,
'Stats Engineering': player.stats.engineering,
'Stats Defense': player.stats.defense,
Scrap: player.scrap
})));
console.log('\nTotal players matching the criteria: ' + filteredPlayers.length);
// Run claim function if claims is greater than 0 and total count is less than 8
if (myusernamePlayer.claims > 0 && filteredPlayers.length < 8) {
claim(myusernamePlayer, amountToClaim)
.then(() => {
console.log(`Successfully claimed ${amountToClaim} rewards.`);
})
.catch(error => {
console.error('Error claiming rewards:', error);
});
}
})
.catch(error => {
console.error('Error retrieving leaderboard:', error);
});
}
// Display leaderboard initially
displayLeaderboard();
// Set interval to refresh leaderboard every 30 seconds
setInterval(displayLeaderboard, 30000);
It's been great fun working with ChatGPT on this, highly recommend people give it a try, not just coding, ask it anything!
I really want to try building my own AI, there are plenty of open source resources but it looks like I would need a decent graphics card which is expensive and there are too many things I want to spend money on 🤑🤣🤓
Again, I hope this helps someone.
Next up I want to script Boss fights which should be fun too as there are no examples of code! (not that I've looked that hard yet)
Till next time,
Stay Frugal