SHARE
TWEET

Untitled

a guest Dec 2nd, 2019 88 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("./keysS.json");
  5. const cardData = require("./cardData.json");
  6. var gamesPlayed = 0;
  7. var delay = 20000;
  8. var txId;
  9. var err = 0;
  10.  
  11. class Battle {
  12.   constructor(callback, appName = "smitopbot-public/1.0.0", matchType = "Ranked") {
  13.  
  14.     this.callback = callback;
  15.     this.status = {};
  16.     this.submittedTeam = false;
  17.    
  18.     console.log("Quest Name: ")
  19.     checkQuest()
  20.     setTimeout(function() {
  21.     console.log(KEYS.dolochanusername+"'s Rating: ");
  22.             checkRating();
  23.  
  24.           }, 1700);
  25.  
  26.     //broadcast sm_find_match
  27.     steem.broadcast.customJson(KEYS.dolochanposting, [], [KEYS.dolochanusername], "sm_find_match", JSON.stringify({
  28.       match_type: matchType,
  29.       app: appName
  30.     }), (err, result) => {
  31.       if (err) throw err;
  32.       console.log("Broadcasted sm_find_match");
  33.       this.findMatchId = result.id;
  34.     });
  35.     //start /battle/status check loop
  36.     this._checkInterval = setInterval(() => {
  37.       this._checkBattleStatus();
  38.     }, 2500);
  39.  
  40.   }
  41.   end() {
  42.     this.ended = true;
  43.     clearInterval(this._checkInterval);
  44.   }
  45.  
  46.   setTeam(team) {
  47.     this.team = team;
  48.   }
  49.   broadcastTeam(summoner, monsters, skipReveal = false) {
  50.     const secret = Battle.generatePassword();
  51.     const teamHash = md5(summoner + "," + monsters.join() + "," + secret)
  52.     const team = {
  53.       summoner,
  54.       monsters,
  55.       secret
  56.     };
  57.  
  58.     this.submittedTeam = true;
  59.     var data = {
  60.       trx_id: this.findMatchId,
  61.       app: this.appName,
  62.       team_hash: teamHash
  63.     };
  64.  
  65.          txId = data.trx_id
  66.  
  67.  
  68.     if (skipReveal) {
  69.       data.summoner = summoner;
  70.       data.monsters = monsters;
  71.     }
  72.     steem.broadcast.customJson(KEYS.dolochanposting, [], [KEYS.dolochanusername], "sm_submit_team", JSON.stringify(data), async (err, result) => {
  73.       if (err) throw err;
  74.       console.log("Broadcasted sm_submit_team");
  75.       this.findMatchId = result.id;
  76.       if (!skipReveal) {
  77.         await new Promise(resolve => setTimeout(resolve, 3300));
  78.         console.log("Revealing team...");
  79.  
  80.         steem.broadcast.customJson(KEYS.dolochanposting, [], [KEYS.dolochanusername], "sm_team_reveal", JSON.stringify({
  81.           ...data,
  82.           summoner: summoner,
  83.           monsters: monsters,
  84.           secret: secret,
  85.           app: "smitopbot-public/1.0.0"
  86.         }), (err, result) => {
  87.           console.log("Revealed team!");
  88.           gamesPlayed++;
  89.           console.log("Games played:", gamesPlayed);
  90.  
  91. var battleover;
  92.  
  93. var myVar = setInterval(function() {
  94.   battleover = checkBattle(txId, myVar);
  95.  
  96.   battleover.then(function(result) {
  97. if (result === true){
  98. clearInterval(myVar)
  99. err = 0
  100.     main();
  101.   }
  102. });
  103.          }, 5000);
  104.        
  105.  
  106.  
  107.         });
  108.       }
  109.  
  110.  
  111.  
  112.     });
  113.  
  114.  
  115.  
  116.   }
  117.  
  118.   _revealTeam() {
  119.  
  120.   }
  121.   async _checkBattleStatus() {
  122.     if (!this.findMatchId) return;
  123.     const rawResponse = await fetch("https://api.steemmonsters.io/battle/status?id=" + this.findMatchId);
  124.     const json = await rawResponse.json();
  125.     this.status.data = json;
  126.  
  127.     if ((typeof json) === "string") {
  128.       console.log(json);
  129.       err = err+1    
  130.     if(err>9){
  131.       err=0
  132.     this.ended = true;
  133.     clearInterval(this._checkInterval);
  134.     main()
  135.     }
  136.       this.status.statusName = "battleTxProcessing";
  137.       this.callback(this.status);
  138.       return;
  139.     }
  140.  
  141.     if (json.error) {
  142.       this.status.statusName = "error";
  143.       main();
  144.     } else if (json.status === 0) {
  145.       this.status.statusName = "searchingForEnemy";
  146.     } else if (json.status === 1) {
  147.       this.status.statusName = "enemyFound";
  148.     } else if (json.status === 3) {
  149.       this.status.statusName = "noEnemyFound";
  150.     }
  151.     this.callback(this.status);
  152.   }
  153.   _checkBattleTrxStatus() {
  154.  
  155.   }
  156.   static generatePassword(length = 10) {
  157.     var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  158.       retVal = "";
  159.     for (var i = 0, n = charset.length; i < length; ++i) {
  160.       retVal += charset.charAt(Math.floor(Math.random() * n));
  161.     }
  162.     return retVal;
  163.   }
  164. }
  165. async function getSMJson(url) {
  166.   const res = await (fetch("https://api.steemmonsters.io" + url));
  167.   const text = await res.text();
  168.   try {
  169.     return JSON.parse(text);
  170.   } catch (e) {
  171.     console.log("Invalid JSON; retrying", text)
  172.     await new Promise((resolve) => setTimeout(resolve, 1000));
  173.     return await getSMJson();
  174.   }
  175. }
  176.  
  177. async function checkQuest(){
  178. const quest = await (fetch("https://api.steemmonsters.io/players/quests?username="+KEYS.dolochanusername));
  179. const questAns = await quest.json()
  180. var questAnss = questAns[0]
  181. console.log(questAnss["name"])
  182. console.log("Wins in Splinter: ")
  183. console.log(questAnss["completed_items"])
  184. if (questAnss["completed_items"] > 4 && questAnss["claim_date"]=== null){
  185.   console.log('Rewards are ready to be collected on account "'+KEYS.dolochanusername+'"!')
  186.   claimQuest(questAnss["id"]);
  187.     }
  188.     var g = new Date();
  189.     g = g.getUTCDate();
  190.     var diff = g-questAnss["created_date"]
  191. if (diff.hours>=1 && diff.day>=1 && diff.minutes>=1&&questAnss["claim_date"] !== null){
  192. startQuest()
  193. }
  194. if(questAnss["refresh_trx_id"]===null&&questAnss["name"]==="Pirate Attacks"||questAnss["name"]==="Lyanna's Call"){
  195. refreshQuest(questAnss["id"])
  196. }
  197.  
  198.  
  199.   }
  200.  
  201. function refreshQuest(questID){
  202.   steem.broadcast.customJson(KEYS.dolochanposting, [], [KEYS.dolochanusername], "sm_refresh_quest", JSON.stringify({
  203.     "type": "daily",
  204.     "app":"smitopbot-public/1.0.0"
  205. }));
  206.  
  207. }
  208.  
  209. function startQuest(){
  210.   steem.broadcast.customJson(KEYS.dolochanposting, [], [KEYS.dolochanusername], "sm_start_quest", JSON.stringify({
  211.     "type": "daily",
  212.     "app":"smitopbot-public/1.0.0"
  213. }));
  214. }
  215.  
  216. function claimQuest(questID){
  217.     steem.broadcast.customJson(KEYS.dolochanposting, [], [KEYS.dolochanusername], "sm_claim_reward", JSON.stringify({
  218.     "type": "quest",
  219.     "quest_id":questID,
  220.     "app":"smitopbot-public/1.0.0"
  221. }));
  222.     console.log("Quest Claimed!")
  223.  
  224. }
  225.  
  226. function checkStarter(cardid){
  227. return cardData.filter(card => {
  228.   if (cardid === card) {
  229.     if(card.is_starter === true){
  230.       return true
  231.     }else {
  232.       return false
  233.     }
  234.   }
  235. });
  236. }
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252. async function checkRating(){
  253. const rating = await (fetch("https://api.steemmonsters.io/players/quests?username="+KEYS.dolochanusername));
  254. const ratingAns = await rating.json()
  255. var ratingAnss = ratingAns[0]
  256. console.log(ratingAnss["rating"])
  257. }
  258.  
  259. async function checkBattle(txId, myVar){
  260.  
  261. const battlem = await fetch("https://api.steemmonsters.io/battle/status?id=" + txId);
  262. const battlemm = await battlem.json()
  263. var battlemmm = await battlemm
  264.  
  265. if(battlemmm["status"] === 1){
  266. console.log("Battle is not over yet!")
  267. battleover = false
  268. return battleover
  269. }else if(battlemmm["status"] === 2){
  270.  
  271. console.log("Battle is over!")
  272.  
  273. console.log("Playing another one...")
  274. battleover = true
  275. return battleover
  276. }
  277.  
  278. }
  279.  
  280. function cardBcx(xp, rarity, edition, gold) {
  281.   if (edition === 3) edition = 1; //reward cards are same as beta
  282.   var cards = 0;
  283.   if (edition == 0 && gold == 0 && rarity == 4)
  284.     cards = (xp / 1000) + 1
  285.   if (edition == 0 && gold == 0 && rarity == 3)
  286.     cards = (xp / 250) + 1
  287.   if (edition == 0 && gold == 0 && rarity == 2)
  288.     cards = (xp / 100) + 1
  289.   if (edition == 0 && gold == 0 && rarity == 1)
  290.     cards = (xp / 20) + 1
  291.   if (edition == 0 && gold == 1 && rarity == 4)
  292.     cards = xp / 2500
  293.   if (edition == 0 && gold == 1 && rarity == 3)
  294.     cards = xp / 1000
  295.   if (edition == 0 && gold == 1 && rarity == 2)
  296.     cards = xp / 500
  297.   if (edition == 0 && gold == 1 && rarity == 1)
  298.     cards = xp / 250
  299.  
  300.   // Beta Edition Cards per XP
  301.   if (edition == 1 && gold == 0 && rarity == 4)
  302.     cards = (xp / 750) + 1
  303.   if (edition == 1 && gold == 0 && rarity == 3)
  304.     cards = (xp / 175) + 1
  305.   if (edition == 1 && gold == 0 && rarity == 2)
  306.     cards = (xp / 75) + 1
  307.   if (edition == 1 && gold == 0 && rarity == 1)
  308.     cards = (xp / 15) + 1
  309.   if (edition == 1 && gold == 1 && rarity == 4)
  310.     cards = xp / 2000
  311.   if (edition == 1 && gold == 1 && rarity == 3)
  312.     cards = xp / 800
  313.   if (edition == 1 && gold == 1 && rarity == 2)
  314.     cards = xp / 400
  315.   if (edition == 1 && gold == 1 && rarity == 1)
  316.     cards = xp / 200
  317.   // Promo Edition Cards per XP
  318.   if (edition == 2 && gold == 0 && rarity == 4)
  319.     cards = (xp / 1000) + 1
  320.   if (edition == 2 && gold == 0 && rarity == 3)
  321.     cards = (xp / 250) + 1
  322.   if (edition == 2 && gold == 0 && rarity == 2)
  323.     cards = (xp / 100) + 1
  324.   if (edition == 2 && gold == 0 && rarity == 1)
  325.     cards = (xp / 20) + 1
  326.   if (edition == 2 && gold == 1 && rarity == 4)
  327.     cards = xp / 2500
  328.   if (edition == 2 && gold == 1 && rarity == 3)
  329.     cards = xp / 1000
  330.   if (edition == 2 && gold == 1 && rarity == 2)
  331.     cards = xp / 500
  332.   if (edition == 2 && gold == 1 && rarity == 1)
  333.     cards = xp / 250
  334.   if (cards === 0) throw new Error("Unable to find card BCX");
  335.   return cards
  336. }
  337.  
  338. async function main() {
  339.   let curBlock = -1;
  340.   steem.api.streamBlockNumber((err, b) => curBlock = b);
  341.   let ourCollection = (await getSMJson("/cards/collection/" + KEYS.dolochanusername)).cards;
  342.   setInterval(async () => {
  343.     ourCollection = (await getSMJson("/cards/collection/" + KEYS.dolochanusername)).cards;
  344.     scanCollection();
  345.   }, 1200000);
  346.  
  347.   function scanCollection() {
  348.     ourCollection = ourCollection.filter(card => canPlayCard(card));
  349.   }
  350.   scanCollection();
  351.   setInterval(() => scanCollection(), 25000);
  352.  
  353.   function bestCard(id) {
  354.     return ourCollection.filter(card => (card.card_detail_id === id))
  355.       .map(card => ({
  356.         ...card,
  357.         rarity: cardData.filter(cardData => card.card_detail_id === cardData.id)[0].rarity
  358.       }))
  359.       .sort((a, b) => {
  360.         console.log(a, b);
  361.         return cardBcx(a.xp, a.rarity, a.edition, a.gold) - cardBcx(b.xp, b.rarity, b.edition, b.gold)
  362.       }).reverse()[0];
  363.   }
  364.  
  365.   function printTeamNames(id){
  366.     return ourCollection.filter(card => (card.id === id))
  367.     console.log (card.name)
  368.  
  369.   }
  370.  
  371.   function canPlayCard(card) {
  372.     if (card.market_id)
  373.       return false;
  374.     if (!card.last_transferred_block || !card.last_used_block)
  375.       return true;
  376.     if (card.delegated_to) return false;
  377.  
  378.     if (curBlock === -1) return true;
  379.     var last_block = curBlock;
  380.     return card.last_transferred_block <= last_block - 201600 || card.last_used_block <= last_block - 201600;
  381.   }
  382.   var submittedTeam = false;
  383.   const battle = new Battle(async status => {
  384.     console.log(status.statusName);
  385.     if (!submittedTeam && (status.statusName === "enemyFound")) {
  386.       var manaUsed = 3; //for summoner
  387.       var colorSummoners = {
  388.         Red: 5,
  389.         Blue: 16,
  390.         Green: 27,
  391.         White: 38,
  392.         Black: 49,
  393.         Gold: 56,
  394.         Daria: 88
  395.       };
  396.       function possibleCards(splinter, battle = {}) {
  397.         return cardData.filter(card => {
  398.           if (card.type !== "Monster") return;
  399.           if ((card.color !== "Blue") && (card.color !== "Gray")) return;
  400.           if (status.data.ruleset === "Lost Legendaries" && card.rarity === 4) return;
  401.           if (status.data.ruleset === "Rise of the Commons" && card.rarity > 2) return;
  402.           if (status.data.ruleset === "Taking Sides" && card.color === "Gray") return;
  403.           //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
  404.           if (status.data.ruleset === "Up Close & Personal" && card.stats.attack[0] === 0) return;
  405.           if (status.data.ruleset === "Broken Arrows" && card.stats.ranged[card.stats.ranged.length - 1] > 0) return;
  406.           if (status.data.ruleset === "Little League" && (card.stats.mana[0] > 4)) return;
  407.           return true;
  408.         });
  409.  
  410.       };
  411. function printCardName(id) {
  412.         return cardData.filter(card => {
  413.           if (card.id === id) {console.log(card.name)};
  414.  
  415.           return true;
  416.         });
  417.  
  418.       };
  419.       let team = [];
  420. if(status.data.mana_cap=== 15){
  421. var summoner = 49;
  422. team = [50,46,51];
  423. }
  424. if(status.data.mana_cap=== 16){
  425. var summoner = 49;
  426. team = [50,46,51];
  427. }
  428. if(status.data.mana_cap=== 17){
  429. var summoner = 49;
  430. team = [50,46,51,52];
  431. }
  432. if(status.data.mana_cap=== 18){
  433. var summoner = 49;
  434. team = [50,46,51,52];
  435. }
  436. if(status.data.mana_cap=== 19){
  437. var summoner = 5;
  438. team = [7,64,3,62,4];
  439. }
  440. if(status.data.mana_cap=== 20){
  441. var summoner = 5;
  442. team = [8,1,3,62,6];
  443. }
  444. if(status.data.mana_cap=== 21){
  445. var summoner = 38;
  446. team = [40,34,39,65];
  447. }
  448. if(status.data.mana_cap=== 22){
  449. var summoner = 5;
  450. team = [64,8,3,62,1,6];
  451. }
  452. if(status.data.mana_cap=== 23){
  453. var summoner = 49;
  454. team = [50,52,51,46,65];
  455. }
  456. if(status.data.mana_cap=== 24){
  457. var summoner = 49;
  458. team = [50,52,51,46,65];
  459. }
  460. if(status.data.mana_cap=== 25){
  461. var summoner = 38;
  462. team = [40,34,39,66,65];
  463. }
  464. if(status.data.mana_cap=== 26){
  465. var summoner = 49;
  466. team = [50,52,51,46,66,65];
  467. }
  468. if(status.data.mana_cap=== 27){
  469. var summoner = 38;
  470. team = [40,34,39,66,35,65];
  471. }
  472.         var teamNames = []
  473.         console.log("Ruleset: "+status.data.ruleset);
  474.         console.log("Mana Cap: "+status.data.mana_cap);
  475.         console.log("Summoner ID: "+summoner);
  476.         console.log("Summoner Name: ");
  477.         printCardName(summoner);
  478.         console.log("Team IDs: "+team);
  479.         console.log("Team Names: ")
  480.         for (i = 0; i<team.length; i++){
  481.           printCardName(team[i])
  482.         }
  483.         console.log("Inactive Splinters: "+status.data.inactive);
  484.         battle.broadcastTeam("starter-"+summoner+"-Th0tti", team.map(card => {
  485.           if(checkStarter === true){
  486.           return "starter-"+card+"-BgOne"
  487.         }else{
  488.           return bestCard(card)
  489.         }
  490.         }));
  491.  
  492.         submittedTeam = true;
  493.        
  494.                battle.end();
  495.       }
  496.     });
  497. }
  498.  
  499. 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