Facebook
From Gray Pintail, 4 Years ago, written in Plain Text.
This paste is a reply to Dodatek from Ponton - view diff
Embed
Download Paste or View Raw
Hits: 319
  1. // ==UserScript==
  2. // @name         Wykrywacz herosów aldous
  3. // @version      0.1
  4. // @description  Wykrywacz herosów aldous
  5. // @author       Tesu
  6. // @match        http://aldous.margonem.pl/
  7. // @grant        none
  8. // ==/UserScript==
  9.  
  10. new class {
  11.     constructor() {
  12.         this.storageName = 'wykrywacz-herosow-informacje';
  13.         this.webHookUrl = 'https://discordapp.com/api/webhooks/638407799231217695/LnT7ss23CE5iLh8nR7_v-h3di082Keoya7csCNhnsvkLoz4MzNrzK6SW0JHe6kAeWIil';
  14.         this.informations = JSON.parse(localStorage.getItem(this.storageName)) || {};
  15.         this.newInterface = window.Engine !== undefined;
  16.         this.world = window.location.host.split('.')[0];
  17.         this.worlName = this.world[0].toUpperCase() + this.world.substring(1);
  18.         this.initListener();
  19.     }
  20.  
  21.     get npcs() {
  22.         return this.newInterface ? this.npcsOnNewInterface : window.g.npc;
  23.     }
  24.  
  25.     get map() {
  26.         return this.newInterface ? window.Engine.map.d : window.map;
  27.     }
  28.  
  29.     get hero() {
  30.         return this.newInterface ? window.Engine.hero.d : window.hero;
  31.     }
  32.  
  33.     get ts() {
  34.         return Date.now() / 1000;
  35.     }
  36.  
  37.     get npcsOnNewInterface() {
  38.         const npcs = {};
  39.         Object.entries(window.Engine.npcs.check()).forEach(([id, value]) => {
  40.             npcs[id] = value.d;
  41.         });
  42.         return npcs;
  43.     }
  44.  
  45.     saveStorage() {
  46.         localStorage.setItem(this.storageName, JSON.stringify(this.informations));
  47.     }
  48.  
  49.     checkInInformations(id) {
  50.         if (this.informations[id] === undefined) {
  51.             this.informations[id] = this.ts;
  52.             this.saveStorage();
  53.             return true;
  54.         }
  55.         if (this.informations[id] + 600 < this.ts) {
  56.             this.informations[id] = this.ts;
  57.             this.saveStorage();
  58.             return true;
  59.         }
  60.         return false;
  61.     }
  62.  
  63.     checkNpcType(npc) {
  64.         return npc.wt > 79;
  65.     }
  66.  
  67.     checkNpc([id, value]) {
  68.         if (this.npcs[id] !== undefined) return;
  69.         if (!this.checkNpcType(value)) return;
  70.         if (this.checkInInformations(id)) {
  71.             this.sendInformationsToDiscord(value);
  72.         }
  73.     }
  74.  
  75.     sendInformationsToDiscord(npc) {
  76.         fetch(this.webHookUrl, {
  77.             method: 'POST',
  78.             headers: {
  79.                 'Content-Type': 'application/json'
  80.             },
  81.             body: JSON.stringify({
  82.                 embeds: [{
  83.                     'title': `${this.hero.nick} · ${this.hero.lvl}${this.hero.prof} znalazł herosa/tytana!`,
  84.                     'color': ((Math.floor(npc.lvl / 300 * 221) + 32) * 256 + (Math.floor(npc.lvl / 300 * (-112)) + 120)) * 256 + Math.floor(npc.lvl / 300 * (-204)) + 217,
  85.                     'description': `${npc.nick} (${npc.lvl}lvl)\n${this.map.name} (${npc.x}, ${npc.y})\n${new Date().toLocaleTimeString()}\n${this.worlName}`,
  86.                     'thumbnail': {
  87.                         'url': `http://berufs.margonem.pl/obrazki/npc/${npc.icon}`
  88.                     }
  89.                 }],
  90.                 content: `Znaleźli mnie na mapie ${this.map.name}`,
  91.                 username: npc.nick,
  92.                 avatar_url: `http://berufs.margonem.pl/obrazki/npc/${npc.icon}`
  93.             })
  94.         })
  95.     }
  96.  
  97.     parseInput(data) {
  98.         if (data.hasOwnProperty('npc') && data.npc !== undefined) {
  99.             Object.entries(data.npc).forEach(this.checkNpc.bind(this));
  100.         }
  101.     }
  102.  
  103.     initListener() {
  104.         const self = this;
  105.         const _ajax = window.$.ajax;
  106.  
  107.         window.$.ajax = (xhr, ...args) => {
  108.             if (xhr.url.includes('/engine?t=')) {
  109.                 const oldsucc = xhr.success;
  110.                 xhr.success = (data, ...args) => {
  111.                     const canEmit = typeof data === 'object' && data !== undefined && data.e === 'ok';
  112.                     if (canEmit) {
  113.                         self.parseInput(data);
  114.                     }
  115.                     return oldsucc.apply(this, [data, ...args]);
  116.                 }
  117.             }
  118.             return _ajax.apply(this, [xhr, ...args]);
  119.         }
  120.     }
  121. }