Tagpro map stats

Tagpro map stats duh

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Tagpro map stats
// @namespace    https://tagpro.koalabeast.com/
// @version      0.4
// @description  Tagpro map stats duh
// @author       You
// @match        https://tagpro.koalabeast.com/game
// @icon         https://www.google.com/s2/favicons?domain=koalabeast.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function waitForMapname () {
        var waitForLoaded = new MutationObserver(function (mutations) {
            var elem = document.querySelector('#mapInfo');
            if (elem && elem.textContent) {
                waitForLoaded.disconnect();
                var mapname = elem.textContent.match(/Map:\s(.+)\sby/)[1];
                getMapStat(mapname);
            }
        });
        var config = {attributes: true, childList: true, subtree: true};
        waitForLoaded.observe(document.body, config)
    }

    function ucfirst(string) {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }

    function getMapStat(mapname){
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                var allmaps = JSON.parse(xhttp.responseText);
                var i, maptype;
                for(maptype in allmaps){
                    var maps = allmaps[maptype];
                    for(i in maps){
                        var map = maps[i];
                        if(map.name == mapname){
                            var msg = mapname + " (" + ucfirst(maptype) + " map) / rating: " + map.averageRating + "% / total plays: " + map.totalPlays;
                            // + (i + 1) + ". place in rotation";
                            console.log(msg);
                            setTimeout(function(){
                                tagpro.socket.emit("chat", {
                                    message: msg,
                                    toAll: 1,
                                });
                            }, 8000);
                            return;
                        }
                    }
                }

            }
        };
        xhttp.open("GET", "https://tagpro.koalabeast.com/maps.json", true);
        xhttp.send();
    }

    waitForMapname();

})();