Skip to content

Instantly share code, notes, and snippets.

@reazuliqbal
Created October 18, 2019 14:22
Show Gist options
  • Save reazuliqbal/750288082336c1c7039d32f8205f8b39 to your computer and use it in GitHub Desktop.
Save reazuliqbal/750288082336c1c7039d32f8205f8b39 to your computer and use it in GitHub Desktop.
Calculate how much it costs for a certain level of Splinterlands deck
const tablemark = require('tablemark');
const axios = require('axios');
const editions = [1, 3]; // 0 = Alpha, 1 = Beta, 2 = Promo, 3 = Reward
// Remove splinters you don't want
const splinters = [
'Red', // Fire
'Green', // Earth
'Blue', // Water
'White', // Life
'Black', // Death
'Gray', // Neutral
'Gold', // Dragon
];
const targetLevel = 'silver'; // novice, bronze, silver, gold, diamond, champion
// You might not need to edit below this
const cards = [];
const cardPrices = [];
let settings = null;
let details = null;
let forSell = null;
const levels = {
// [common, rare, epic, legendary]
novice: [1, 1, 1, 1],
bronze: [3, 2, 2, 1],
silver: [5, 4, 3, 2],
gold: [8, 6, 5, 3],
diamond: [10, 8, 6, 4],
champion: [10, 8, 6, 4],
};
const getSplinter = (color) => {
const map = {
Red: 'Fire',
Green: 'Earth',
Blue: 'Water',
White: 'Life',
Black: 'Death',
Gray: 'Neutral',
Gold: 'Dragon',
};
return map[color];
};
const getEdition = (edition) => {
const map = ['Alpha', 'Beta', 'Promo', 'Reward', 'Untamed'];
return map[edition];
};
const callApi = async (endpoint) => {
let result = [];
try {
const { data } = await axios.get(`https://steemmonsters.com/${endpoint}`);
result = data;
} catch (e) {
console.log(e);
}
return result;
};
const calculatePrice = (level, card, gold, edition) => {
let xp = settings.xp_levels[card.rarity - 1][levels[level][card.rarity - 1] - 2];
if (!xp) xp = 0;
const cardOnMarket = forSell.find((c) => c.card_detail_id === card.id
&& c.gold === gold
&& c.edition === edition);
let xpProperty = gold ? 'beta_gold_xp' : 'beta_xp';
if ((card.edition === 0 || (card.edition === 2 && card.card_detail_id < 100))) {
xpProperty = (gold) ? 'gold_xp' : 'alpha_xp';
}
const bcxXp = settings[xpProperty][card.rarity - 1];
const bcx = Math.ceil(gold ? xp / bcxXp : (xp + bcxXp) / bcxXp, 1);
cardPrices.push({
id: card.id,
name: card.name,
splinter: card.splinter,
edition: getEdition(card.edition),
bcx,
price: (bcx * cardOnMarket.low_price).toFixed(3),
});
return bcx * cardOnMarket.low_price;
};
(async () => {
details = await callApi('cards/get_details');
forSell = await callApi('market/for_sale_grouped');
settings = await callApi('settings');
editions.forEach((e) => {
cards.push(...details.filter((c) => c.editions.includes(e))
.filter((c) => splinters.includes(c.color))
.map((c) => ({
id: c.id, splinter: getSplinter(c.color), name: c.name, rarity: c.rarity, edition: e,
})));
});
let totalPrice = 0;
cards.forEach((c) => {
totalPrice += calculatePrice(targetLevel, c, false, c.edition);
});
console.log(
tablemark(
cardPrices.sort((a, b) => Number(a.id) - Number(b.id)), {
columns: ['ID', 'Name', 'Splinter', 'Edition', 'BCX', 'Price'],
},
),
);
console.log(`Estimated cost of ${targetLevel.toLocaleUpperCase()} league deck is:`);
console.log('\tUSD:', totalPrice.toFixed(3));
console.log('\tSTEEM:', (totalPrice * settings.steem_price).toFixed(3));
console.log('\tSBD:', (totalPrice * settings.sbd_price).toFixed(3));
console.log('\tDEC:', (totalPrice * settings.dec_price).toFixed(3));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment