SHARE
TWEET

Untitled

a guest Apr 21st, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const steem = require("steem");
  2. const fetch = require("node-fetch");
  3. const md5 = require("md5");
  4. const KEYS = require("./keys.json");
  5. const cardData = require("./cardData.json");
  6.  
  7. class Battle {
  8.  
  9.   constructor(callback, appName = "steemmonsters", matchType = "Ranked") {
  10.     this.callback = callback;
  11.     this.status = {};
  12.     this.submittedTeam = false;
  13.     //broadcast sm_find_match
  14.     steem.broadcast.customJson(KEYS.posting, [], [KEYS.username], "sm_find_match", JSON.stringify({
  15.       match_type: matchType,
  16.       app: appName
  17.     }), (err, result) => {
  18.       if (err) throw err;
  19.       console.log("Broadcasted sm_find_match");
  20.       this.findMatchId = result.id;
  21.     });
  22.     //start /battle/status check loop
  23.     this._checkInterval = setInterval(() => {
  24.       this._checkBattleStatus();
  25.     }, 2500);
  26.     //
  27.   }
  28.   end() {
  29.     this.ended = true;
  30.     clearInterval(this._checkInterval);
  31.     delete this;
  32.   }
  33.  
  34.   setTeam(team) {
  35.     this.team = team;
  36.   }
  37.   broadcastTeam(summoner, monsters, skipReveal = false) {
  38.     const secret = Battle.generatePassword();
  39.     const teamHash = md5(summoner + "," + monsters.join() + "," + secret)
  40.     const team = {summoner, monsters, secret};
  41.  
  42.     this.submittedTeam = true;
  43.     var data = {
  44.       trx_id: this.findMatchId,
  45.       team_hash: teamHash,
  46.       app: this.appName
  47.     };
  48.     if (skipReveal) {
  49.       data.summoner = summoner;
  50.       data.monsters = monsters;
  51.       data.secret = secret;
  52.     }
  53.     steem.broadcast.customJson(KEYS.posting, [], [KEYS.username], "sm_submit_team", JSON.stringify(data), async (err, result) => {
  54.       if (err) throw err;
  55.       console.log("Broadcasted sm_submit_team");
  56.       this.findMatchId = result.id;
  57.       if (!skipReveal) {
  58.         await new Promise(resolve => setTimeout(resolve, 3300));
  59.         console.log("Revealing team...");
  60.         steem.broadcast.customJson(KEYS.posting, [], [KEYS.username], "sm_team_reveal", JSON.stringify({
  61.           ...data,
  62.           summoner: summoner,
  63.           monsters: monsters,
  64.           secret: secret
  65.         }), (err, result) => {
  66.           console.log("Revealed team!");
  67.         });
  68.       }
  69.     });
  70.   }
  71.  
  72.   _revealTeam() {
  73.  
  74.   }
  75.   async _checkBattleStatus() {
  76.     if (!this.findMatchId) return;
  77.     const rawResponse = await fetch("https://api.steemmonsters.io/battle/status?id=" + this.findMatchId);
  78.     const json = await rawResponse.json();
  79.     this.status.data = json;
  80.  
  81.     if ((typeof json) === "string") {
  82.       console.log(json);
  83.       this.status.statusName = "battleTxProcessing";
  84.       this.callback(this.status);
  85.       return;
  86.     }
  87.  
  88.     if (json.error) {
  89.       this.status.statusName = "error";
  90.     } else if (json.status === 0) {
  91.       this.status.statusName = "searchingForEnemy";
  92.     } else if (json.status === 1) {
  93.       this.status.statusName = "enemyFound";
  94.     } else if (json.status === 3) {
  95.       this.status.statusName = "noEnemyFound";
  96.     }
  97.     this.callback(this.status);
  98.   }
  99.   _checkBattleTrxStatus() {
  100.  
  101.   }
  102.   static generatePassword(length = 10) {
  103.     var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  104.     retVal = "";
  105.     for (var i = 0, n = charset.length; i < length; ++i) {
  106.       retVal += charset.charAt(Math.floor(Math.random() * n));
  107.     }
  108.     return retVal;
  109.   }
  110. }
  111. async function getSMJson(url) {
  112.   const res = await (fetch("https://api.steemmonsters.io" + url));
  113.   const text = await res.text();
  114.   try {
  115.     return JSON.parse(text);
  116.   } catch (e) {
  117.     console.log("Invalid JSON; retrying", text)
  118.     await new Promise((resolve) => setTimeout(resolve, 1000));
  119.     return await getSMJson();
  120.   }
  121. }
  122.  
  123. function cardBcx(xp, rarity, edition, gold) {
  124.         if (edition === 3) edition = 1; //reward cards are same as beta
  125.         var cards = 0;
  126.         if (edition == 0 && gold == 0 && rarity == 4)
  127.             cards = (xp / 1000) +1
  128.         if (edition == 0 && gold == 0 && rarity == 3)
  129.             cards = (xp / 250) +1
  130.         if (edition == 0 && gold == 0 && rarity == 2)
  131.             cards = (xp / 100) +1
  132.         if (edition == 0 && gold == 0 && rarity == 1)
  133.             cards = (xp / 20) +1
  134.         if (edition == 0 && gold == 1 && rarity == 4)
  135.             cards = xp / 2500
  136.         if (edition == 0 && gold == 1 && rarity == 3)
  137.             cards = xp / 1000
  138.         if (edition == 0 && gold == 1 && rarity == 2)
  139.             cards = xp / 500
  140.         if (edition == 0 && gold == 1 && rarity == 1)
  141.             cards = xp / 250
  142.  
  143.         // Beta Edition Cards per XP
  144.         if (edition == 1 && gold == 0 && rarity == 4)
  145.             cards = (xp / 750) +1
  146.         if (edition == 1 && gold == 0 && rarity == 3)
  147.             cards = (xp / 175) +1
  148.         if (edition == 1 && gold == 0 && rarity == 2)
  149.             cards = (xp / 75) +1
  150.         if (edition == 1 && gold == 0 && rarity == 1)
  151.             cards = (xp / 15) +1
  152.         if (edition == 1 && gold == 1 && rarity == 4)
  153.             cards = xp / 2000
  154.         if (edition == 1 && gold == 1 && rarity == 3)
  155.             cards = xp / 800
  156.         if (edition == 1 && gold == 1 && rarity == 2)
  157.             cards = xp / 400
  158.         if (edition == 1 && gold == 1 && rarity == 1)
  159.             cards = xp / 200
  160.         // Promo Edition Cards per XP
  161.         if (edition == 2 && gold == 0 && rarity == 4)
  162.             cards = (xp / 1000) +1
  163.         if (edition == 2 && gold == 0 && rarity == 3)
  164.             cards = (xp / 250) +1
  165.         if (edition == 2 && gold == 0 && rarity == 2)
  166.             cards = (xp / 100) +1
  167.         if (edition == 2 && gold == 0 && rarity == 1)
  168.             cards = (xp / 20) +1
  169.         if (edition == 2 && gold == 1 && rarity == 4)
  170.             cards = xp / 2500
  171.         if (edition == 2 && gold == 1 && rarity == 3)
  172.             cards = xp / 1000
  173.         if (edition == 2 && gold == 1 && rarity == 2)
  174.             cards = xp / 500
  175.         if (edition == 2 && gold == 1 && rarity == 1)
  176.             cards = xp / 250
  177.         if (cards === 0) throw new Error("Unable to find card BCX");
  178.         return cards
  179.       }
  180.  
  181. async function main() {
  182.   let curBlock = -1;
  183.   steem.api.streamBlockNumber((err, b) => curBlock = b);
  184.   let ourCollection = (await getSMJson("/cards/collection/" + KEYS.username)).cards;
  185.   setInterval(async () => { ourCollection = (await getSMJson("/cards/collection/" + KEYS.username)).cards; scanCollection(); }, 1200000);
  186.   function scanCollection() {
  187.     ourCollection = ourCollection.filter(card => canPlayCard(card));
  188.   }
  189.   scanCollection();
  190.   setInterval(() => scanCollection(), 25000);
  191.   function bestCard(id) {
  192.     return ourCollection.filter(card => (card.card_detail_id === id))
  193.       .map(card => ({...card, rarity: cardData.filter(cardD => card.card_detail_id === cardD.id)[0].rarity}))
  194.       .sort((a, b) => { console.log(a,b); return cardBcx(a.xp, a.rarity, a.edition, a.gold) - cardBcx(b.xp, b.rarity, b.edition, b.gold) }).reverse()[0];
  195.   }
  196.   function canPlayCard(card) {
  197.     if (card.market_id)
  198.       return false;
  199.     if (!card.last_transferred_block || !card.last_used_block)
  200.       return true;
  201.  
  202.     if (curBlock === -1) return true;
  203.     var last_block = curBlock;
  204.     return card.last_transferred_block <= last_block - 201600 || card.last_used_block <= last_block - 201600;
  205.   }
  206.   var submittedTeam = false;
  207.   const battle = new Battle(async status => {
  208.     console.log(status.statusName);
  209.     if (!submittedTeam && (status.statusName === "enemyFound")) {
  210.          var manaUsed = 3; //for summoner
  211.     var colorSummoners = {
  212.       Red: 5,
  213.       Blue: 16,
  214.       Green: 27,
  215.       White: 38,
  216.       Black: 49
  217.     };
  218.      let cardsToScore = possibleCards("Blue", status.data)
  219.       function possibleCards(Blue, battle = {}) {
  220.   return cards.filter(card => {
  221.     if (card.type !== "Monster") return;
  222.     if ((card.color !== "Blue") && (card.color !== "Gray")) return;
  223.     if (battle.ruleset === "Lost Legendaries" && card.rarity === 4) return;
  224.     if (battle.ruleset === "Rise of the Commons" && card.rarity > 2) return;
  225.     if (battle.ruleset === "Taking Sides" && card.color === "Gray") return;
  226.     //these should actually take in to account the level of the card. creeping ooze becomes a melee monster @ level 4, but this never allows it
  227.     if (battle.ruleset === "Up Close & Personal" && card.stats.attack[0] === 0) return;
  228.     if (battle.ruleset === "Broken Arrows" && card.stats.ranged[card.stats.ranged.length - 1] > 0) return;
  229.     if (battle.ruleset === "Little League" && (card.stats.mana[0] > 4)) return;
  230.     return true;
  231.   });
  232.  
  233. };
  234. var summoner = cardData.filter(card => card.id === colorSummoners[16])[0];
  235.     for (let i = 0; i < cardsToScore.length; i++) {
  236.       const card = cardsToScore[i];
  237.       const cardMana = card.stats.mana[0] ? card.stats.mana[0] : card.stats.mana;
  238.       //verify it's in our collection
  239.       if (ourCollection.filter(colCard => card.id === colCard.card_detail_id)[0] === undefined) continue;
  240.       if ((manaUsed + cardMana) > status.data.mana_cap) {
  241.         if (team.length < 6) continue;
  242.         let lastCardMana = team[team.length - 1].stats.mana[0] ? team[team.length - 1].stats.mana[0] : team[team.length - 1].stats.mana;
  243.         if (((manaUsed - lastCardMana + cardMana) <= status.data.mana_cap) && (cardMana > lastCardMana)) {
  244.           team[team.length - 1] = card;
  245.         } else {
  246.           continue;
  247.         }
  248.       } else if (team.length < 6) {
  249.         if (card.stats.ranged[0] === 0) {
  250.          //not a ranged monster
  251.           if (card.id !== 91) nonRangedUsed++; //ignore ooze
  252.         } else if ((nonRangedUsed < 2) && (team.length > 4)) {
  253.           //Ranged monster, and we have more than 4 monsters already. We need more melee monsters now, as we have either only 0 or 1, so skip this
  254.           continue;
  255.         }
  256.         team.push(card);
  257.       }
  258.       manaUsed += cardMana;
  259.     }
  260.     battle.broadcastTeam(bestCard(16).uid, team.map(card => {
  261.   return bestCard(card.id).uid
  262. }));
  263.       submittedTeam = true;
  264.     }
  265.   });
  266.  
  267. }
  268. main();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top