Facebook
From Gray Pintail, 4 Years ago, written in Plain Text.
This paste is a reply to Dodatek from Ponton - go back
Embed
Viewing differences between Dodatek and Re: Dodatek
// ==UserScript==
// @name         Wykrywacz herosów aldous
// @version      0.1
// @description  Wykrywacz herosów aldous
// @author       Tesu
// @match        http://aldous.margonem.pl/
// @grant        none
// ==/UserScript==

new class {
    constructor() {
        this.storageName = 'wykrywacz-herosow-informacje';
        this.webHookUrl = 'https://discordapp.com/api/webhooks/638407799231217695/LnT7ss23CE5iLh8nR7_v-h3di082Keoya7csCNhnsvkLoz4MzNrzK6SW0JHe6kAeWIil';
        this.informations = JSON.parse(localStorage.getItem(this.storageName)) || {};
        this.newInterface = window.Engine !== undefined;
        this.world = window.location.host.split('.')[0];
        this.worlName = this.world[0].toUpperCase() + this.world.substring(1);
        this.initListener();
    }

    get npcs() {
        return this.newInterface ? this.npcsOnNewInterface : window.g.npc;
    }

    get map() {
        return this.newInterface ? window.Engine.map.d : window.map;
    }

    get hero() {
        return this.newInterface ? window.Engine.hero.d : window.hero;
    }

    get ts() {
        return Date.now() / 1000;
    }

    get npcsOnNewInterface() {
        const npcs = {};
        Object.entries(window.Engine.npcs.check()).forEach(([id, value]) => {
            npcs[id] = value.d;
        });
        return npcs;
    }

    saveStorage() {
        localStorage.setItem(this.storageName, JSON.stringify(this.informations));
    }

    checkInInformations(id) {
        if (this.informations[id] === undefined) {
            this.informations[id] = this.ts;
            this.saveStorage();
            return true;
        }
        if (this.informations[id] + 600 < this.ts) {
            this.informations[id] = this.ts;
            this.saveStorage();
            return true;
        }
        return false;
    }

    checkNpcType(npc) {
        return npc.wt > 79;
    }

    checkNpc([id, value]) {
        if (this.npcs[id] !== undefined) return;
        if (!this.checkNpcType(value)) return;
        if (this.checkInInformations(id)) {
            this.sendInformationsToDiscord(value);
        }
    }

    sendInformationsToDiscord(npc) {
        fetch(this.webHookUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                embeds: [{
                    'title': `${this.hero.nick} · ${this.hero.lvl}${this.hero.prof} znalazł herosa/tytana!`,
                    'color': ((Math.floor(npc.lvl / 300 * 221) + 32) * 256 + (Math.floor(npc.lvl / 300 * (-112)) + 120)) * 256 + Math.floor(npc.lvl / 300 * (-204)) + 217,
                    'description': `${npc.nick} (${npc.lvl}lvl)\n${this.map.name} (${npc.x}, ${npc.y})\n${new Date().toLocaleTimeString()}\n${this.worlName}`,
                    'thumbnail': {
                        'url': `http://berufs.margonem.pl/obrazki/npc/${npc.icon}`
                    }
                }],
                content: `Znaleźli mnie na mapie ${this.map.name}`,
                username: npc.nick,
                avatar_url: `http://berufs.margonem.pl/obrazki/npc/${npc.icon}`
            })
        })
    }

    parseInput(data) {
        if (data.hasOwnProperty('npc') && data.npc !== undefined) {
            Object.entries(data.npc).forEach(this.checkNpc.bind(this));
        }
    }

    initListener() {
        const self = this;
        const _ajax = window.$.ajax;

        window.$.ajax = (xhr, ...args) => {
            if (xhr.url.includes('/engine?t=')) {
                const oldsucc = xhr.success;
                xhr.success = (data, ...args) => {
                    const canEmit = typeof data === 'object' && data !== undefined && data.e === 'ok';
                    if (canEmit) {
                        self.parseInput(data);
                    }
                    return oldsucc.apply(this, [data, ...args]);
                }
            }
            return _ajax.apply(this, [xhr, ...args]);
        }
    }
}