Advertisement
Guest User

Untitled

a guest
May 9th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const request = require('async-request');
  2. const moment = require('moment');
  3. const tmi = require('tmi.js');
  4.  
  5. //const token = 'oauth:5bugc3awqv8t2t5kw2sdwze4f3ly54'
  6. //const username = 'expectedbot64'
  7.  
  8. //const channel = 'expectedint64'
  9.  
  10. const options = {
  11.   options: {
  12.     debug: true,
  13.   },
  14.   connection: {
  15.     cluster: 'aws',
  16.     reconnect: true,
  17.   },
  18.   identity: {
  19.     username: 'expectedbot64',
  20.     password: 'oauth:5bugc3awqv8t2t5kw2sdwze4f3ly54',
  21.   },
  22.   channels: ['expectedint64'],
  23. };
  24.  
  25. const client = new tmi.client(options);
  26.  
  27. function playerBet(name, min, sec) {
  28.   this.userName = name;
  29.   this.guessMin = min;
  30.   this.guessSec = sec;
  31. };
  32.  
  33. var playerBets = [];
  34.  
  35. client.connect();
  36. client.on('connected', (address, port) => {
  37.   client.action('expectedint64', 'Guess bot is now active, guess when the first death is using: !fd xx:xx');
  38. });
  39.  
  40. const initialTime = Date.now();
  41. let lastRequest = initialTime;
  42. async function pull() {
  43.   const startTime = lastRequest - initialTime;
  44.   const currentTime = Date.now();
  45.   const endTime = currentTime - initialTime;
  46.   lastRequest = currentTime;
  47.  
  48.   const code = "jg7MaLWQNxfmPZ34";
  49.   const api_key = "03ab0b4c402ab1296768abc6737133f4";
  50.   const response = await request(`https://www.warcraftlogs.com/v1/report/tables/deaths/${code}?end=${endTime}&api_key=${api_key}&start=${startTime}`);
  51.   const json = JSON.parse(response.body);
  52.  
  53.   if (json.entries.length === 0) {
  54.     console.log("No one died :(");
  55.   } else {
  56.     console.log(
  57.       json.entries
  58.         .map(entry => entry.name + " died at " + moment(initialTime + entry.timestamp).format("dddd, MMMM Do YYYY, h:mm:ss a"))
  59.         .join("\n")
  60.     );
  61.   }
  62.  
  63.   setTimeout(pull, 10000);
  64. }
  65. //pull(); // uncomment for live death feedback
  66.  
  67. //Get fights
  68. const allFights = [];
  69. async function getNewFights() {
  70.   //rJ9AxNF6VqcnzCHX
  71.   //2FD6G3QA1j9cHB8n
  72.   const code = "TnmqJrx8CZ6pvaW7";
  73.   const api_key = "03ab0b4c402ab1296768abc6737133f4";
  74.   const response = await request(`https://www.warcraftlogs.com/v1/report/fights/${code}?&api_key=${api_key}`);
  75.   const json = JSON.parse(response.body);
  76.   const fights = json.fights;
  77.  
  78.   const newFights = fights.slice(allFights.length);
  79.   allFights.push(...newFights);
  80.  
  81.   return newFights;
  82. }
  83.  
  84. //Get 1st death and print
  85. async function getDeaths() {
  86.   const newFights = await getNewFights();
  87.   if (newFights.length === 0) {
  88.     console.log("No new fights");
  89.     //setTimeout(getDeaths, 50000);
  90.   }
  91.   else {
  92.     const startTime = newFights.reduce((time, fight) => Math.min(fight.start_time, time), newFights[0].start_time);
  93.     const endTime = newFights.reduce((time, fight) => Math.max(fight.end_time, time), newFights[0].end_time);
  94.  
  95.     //rJ9AxNF6VqcnzCHX
  96.     //2FD6G3QA1j9cHB8n
  97.     const code = "TnmqJrx8CZ6pvaW7";
  98.     const api_key = "03ab0b4c402ab1296768abc6737133f4";
  99.  
  100.     const response = await request(`https://www.warcraftlogs.com/v1/report/tables/deaths/${code}?end=${endTime}&api_key=${api_key}&start=${startTime}`);
  101.     const json = JSON.parse(response.body);
  102.     if (json.entries.length === 0) {
  103.       console.log("No one died :(");
  104.       client.action('expectedint64', 'No one died, guess again!');
  105.       playerBets = [];
  106.     } else {
  107.       //const deathTime = Math.floor(((entry.timestamp - newFights[0].start_time)/1000));
  108.       //var deathMinutes = Math.floor(Math.floor(((entry.timestamp - newFights[0].start_time)/1000)) / 60);
  109.       // var deathSeconds = Math.floor(((entry.timestamp - newFights[0].start_time)/1000)) - Math.floor(Math.floor(((entry.timestamp - newFights[0].start_time)/1000)) / 60) * 60;
  110.       if (json.entries.length > 0) {
  111.         const entry = json.entries[0];
  112.         const deathTimeMinutes = Math.floor(Math.floor(((entry.timestamp - newFights[0].start_time) / 1000)) / 60);
  113.         const deathTimeSeconds = Math.floor(((entry.timestamp - newFights[0].start_time) / 1000)) - deathTimeMinutes * 60;
  114.         const killer = entry.killingBlow ? entry.killingBlow.name : "unknown";
  115.         client.action('expectedint64', entry.name + " died at: " + deathTimeMinutes + " minutes and " + deathTimeSeconds + " seconds, fighting: " + newFights[0].name + ", from: " + killer + "! What a pleb :)");
  116.         for(i = 0; i<playerBets.length; i++)
  117.         {
  118.           console.log(playerBets[i].guessMin + '=' + deathTimeMinutes + '|' +playerBets[i].guessSec + '='+deathTimeSeconds + typeof(deathTimeMinutes) + typeof(playerBets[i].guessMin));
  119.           if(parseInt(playerBets[i].guessMin, 10) === parseInt(deathTimeMinutes, 10) && parseInt(playerBets[i].guessSec, 10) === parseInt(deathTimeSeconds, 10))
  120.           {
  121.             console.log("True");
  122.             client.action('expectedint64', playerBets[i].userName + ' guessed the first death!');
  123.           }
  124.         }
  125.         playerBets = [];
  126.       }
  127.       /*client.action('expectedint64', json.entries
  128.       .map(entry => entry.name + " died at: " + (Math.floor(Math.floor(((entry.timestamp - newFights[0].start_time)/1000)) / 60)) + " minutes and "+ (Math.floor(((entry.timestamp - newFights[0].start_time)/1000)) - Math.floor(Math.floor(((entry.timestamp - newFights[0].start_time)/1000)) / 60) * 60) + " seconds, fighting: " + newFights[0].name + ", from: " + (entry.killingBlow ? entry.killingBlow.name : "unknown") + " what a pleb")
  129.       .join("\n"));*/
  130.       /* console.log(
  131.          json.entries
  132.            .map(entry => entry.name + " died from: " + (entry.killingBlow ? entry.killingBlow.name : "unknown") + " at " + moment(initialTime + entry.timestamp).format("dddd, MMMM Do YYYY, h:mm:ss a"))
  133.            .join("\n")
  134.        );*/
  135.       //console.log(json.entry.name);
  136.     }
  137.   }
  138.   setTimeout(getDeaths, 60000);
  139. }
  140. getDeaths();
  141.  
  142. //Betting in Chat
  143. client.on('chat', (channel, user, message, self) => {
  144.   if (message.includes("!fd") && message.charAt(0) === '!') {
  145.     var timeChatMin = 0;
  146.     var timeChatSec = 0;
  147.     var minDone = false;
  148.     var firstPassMin = false;
  149.     var firstPassSec = false;
  150.     var timeDone = false;
  151.     //var firstTimeDone = false;
  152.     //var betAmount = 0;
  153.     for (i = 0; i < message.length; i++) {
  154.       console.log(message.charAt(i));
  155.       //if (message.charAt(i) === " ") { timeDone = true; }
  156.  
  157.       /*f (!isNaN(message.charAt(i)) && timeDone) {
  158.         if (!firstTimeDone) {
  159.           betAmount = message.charAt(i);
  160.           firstTimeDone = true;
  161.         }
  162.         else {
  163.           betAmount = betAmount + message.charAt(i);
  164.         }
  165.         console.log('Bet has passed' + message.charAt(i));
  166.       }*/
  167.       if (message.charAt(i) === ':') { minDone = true; }
  168.       if (!isNaN(message.charAt(i)) && !minDone) {
  169.         if (!firstPassMin) {
  170.           timeChatMin = message.charAt(i);
  171.           firstPassMin = true;
  172.         }
  173.         else {
  174.           timeChatMin = timeChatMin + message.charAt(i);
  175.         }
  176.       }
  177.       if (!isNaN(message.charAt(i)) && minDone) {
  178.         if (!firstPassSec) {
  179.           timeChatSec = message.charAt(i);
  180.           firstPassSec = true;
  181.         }
  182.         else {
  183.           timeChatSec = timeChatSec + message.charAt(i);
  184.         }
  185.       }
  186.     }
  187.     playerBets.push(new playerBet(`${user['display-name']}`, timeChatMin, timeChatSec));
  188.     client.action('expectedint64', `${user['display-name']}` + ' Thinks first death is at: ' + timeChatMin + ':' + timeChatSec);
  189.   }
  190.  
  191.   if (message.includes('!done')) {
  192.     for (i = 0; i < playerBets.length; i++) {
  193.       console.log(playerBets[i].userName + ' guessed first death at: ' + playerBets[i].guessMin + ':' + playerBets[i].guessSec);
  194.     }
  195.     playerBets = [];
  196.   }
  197. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement