Coord MZ

Allows you to get/set coordinates of your players within your tactics

当前为 2021-08-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Coord MZ
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Allows you to get/set coordinates of your players within your tactics
// @author       Murder
// @match        https://www.managerzone.com/?p=tactics*
// @icon         https://image.flaticon.com/icons/png/512/147/147215.png
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
    var coordsContainerAux = document.getElementById('formation-container');
    var input = '<input id=\"**id**\" type=\"text\" style=\"width: 20px\" value=\"**val**\">';
    var _x = '--';
    var _y = '--';

    if(allowed()) {
        var coordsContainer = document.getElementById('formation-container');
        var m = "<div><span style=\"font-weight: 600\">" + allowed() + "</span></div>";
        wrapCoordinates();
        var node = createElementFromHTML(m);
        coordsContainer.appendChild(node);
    }
    else if(isSoccer()) {
        enableActionsForAllTabs();
        enableActionForAltTactics();
        addEventsToPlayers();
        drawCoordinates();
        document.addEventListener("keydown", setKeys);
        document.addEventListener("click", clickEvent);
    }

    function addEventsToPlayers() {
        var checkExist = setInterval(function() {
            if (document.getElementsByClassName('fieldpos fieldpos-ok ui-draggable').length) {
                var players = document.getElementsByClassName('fieldpos fieldpos-ok ui-draggable');
                for (var i = 0; i < players.length; ++i) {
                    players[i].addEventListener('click', setCoordsLabel, false);
                    players[i].addEventListener('keydown', setCoordsLabel, false);
                }
                clearInterval(checkExist);
            }
        }, 1000);

    }

    function setCoordsLabel(player) {
        getOffset(player.path[1]);
        drawCoordinates();
    }

    function getOffset( el ) {
        _y = el.offsetTop - 54;
        _x = el.offsetLeft;
    }

    function drawCoordinates() {
        let coord = document.getElementById('divCoords');
        if(coord) {
            coord.parentElement.removeChild(coord);
        }

        var coordsContainer = document.getElementById('formation-container');
        var divCoords = "<div id=\"divCoords\"><span style=\"font-weight: 600\">Player position: **coords**</span></div>";
        wrapCoordinates();
        var node = createElementFromHTML(divCoords.replace('**coords**', _x + _y));
        coordsContainer.appendChild(node);
        applyCoordinates();
    }

    function createElementFromHTML(htmlString) {
        var div = document.createElement('div');
        div.innerHTML = htmlString.trim();

        // Change this to div.childNodes to support multiple top-level nodes
        return div.firstChild;
    }

    function applyCoordinates() {
        var inpX = document.getElementById('inputX');
        var inpY = document.getElementById('inputY');

        inpX.addEventListener('keyup', setPlayerPosition, false);
        inpY.addEventListener('keyup', setPlayerPosition, false);
    }

    function setPlayerPosition(input) {
        var c = input.currentTarget.id === 'inputX' ? 'x' : 'y';
        let selectedInput = c === 'x' ? document.getElementById('inputX') : document.getElementById('inputY');
        //get selected player
        var players = document.getElementsByClassName('fieldpos fieldpos-ok ui-draggable ui-selected');
        var playerCollision = document.getElementsByClassName('fieldpos ui-selected fieldpos-collision');
        if(players.length) {
            let xVal = c === 'x' ? input.currentTarget.value : document.getElementById('inputX').value;
            let yVal = c === 'y' ? input.currentTarget.value : document.getElementById('inputY').value;

            if(isInRange(c == 'x' ? xVal : yVal, c)) {
                removeBorder(selectedInput);
                players[0].style.left = xVal + "px";
                players[0].style.top = (parseInt(yVal) + 54) + "px";
            }
            else {
                addBorder(selectedInput);
            }
        }
        else if(playerCollision.length) {
            let xVal = c === 'x' ? input.currentTarget.value : document.getElementById('inputX').value;
            let yVal = c === 'y' ? input.currentTarget.value : document.getElementById('inputY').value;

            if(isInRange(c == 'x' ? xVal : yVal, c)) {
                removeBorder(selectedInput);
                playerCollision[0].style.left = xVal + "px";
                playerCollision[0].style.top = (parseInt(yVal) + 54) + "px";
            }
            else {
                addBorder(selectedInput);
            }
        }
    }

    function wrapCoordinates() {
        var inpX = input.replace('**id**','inputX').replace('**val**', _x);
        var inpY = input.replace('**id**','inputY').replace('**val**', _y);
        _x = '<span style=\"color: green\"> X: </span>' + inpX;
        _y = '<span style=\"color: blue\"> Y: </span>' + inpY;
    }

    function isInRange(number, coordinate) {
        if(!isNaN(number)) {
            var integer = parseInt(number);
            if(coordinate == 'x') {
                return integer <= 193 && integer >= 0;
            }
            else if(coordinate == 'y') {
                return integer <= 258 && integer >= 0;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

    function setKeys(key) {
        if((key.keyCode === 37 || key.keyCode === 38 || key.keyCode === 39 || key.keyCode === 40)
           && (key.currentTarget.activeElement.localName != 'input')) {

            var players = document.getElementsByClassName('fieldpos fieldpos-ok ui-draggable ui-selected');
            var playerCollision = document.getElementsByClassName('fieldpos ui-selected fieldpos-collision');
            //player selected with or without collision
            if(players.length) {
                _y = players[0].offsetTop - 54;
                _x = players[0].offsetLeft;
            }
            else if (playerCollision.length) {
                _y = playerCollision[0].offsetTop - 54;
                _x = playerCollision[0].offsetLeft;
            }
            else {
                _y = '--';
                _x = '--';
            }

            drawCoordinates();
        }
    }

    function clickEvent(ev) {
        if(ev.currentTarget.activeElement.localName === 'select') {
            return false;
        }
        var players = document.getElementsByClassName('fieldpos fieldpos-ok ui-draggable ui-selected');
        var playerCollision = document.getElementsByClassName('fieldpos ui-selected fieldpos-collision');
        if(!players.length && !playerCollision.length) {
            _y = '--';
            _x = '--';
            drawCoordinates();
        }
    }

    function enableActionsForAllTabs() {
        var tabs = document.getElementsByClassName('ui-state-default ui-corner-top');
        let ttta = document.getElementById('ttta');
        let tttb = document.getElementById('tttb');
        ttta.addEventListener('click',restart);
        tttb.addEventListener('click',restart);

        for (var i = 0; i < tabs.length; ++i) {
            tabs[i].addEventListener("click", function() {
                addEventsToPlayers();
                enableActionForAltTactics();
            });
        }
    }

    function enableActionForAltTactics() {
        var altTactics = document.getElementById('formation_select');
        altTactics.addEventListener('change',tacticChange);
    }

    function tacticChange() {
        let resetBtn = document.getElementById('reset_formation');
        let copyBtn = document.getElementById('replace_starting_formation');

        resetBtn.addEventListener('click',restart);
        copyBtn.addEventListener('click',restart);

        restart();
    }

    function restart() {
        _y = '--';
        _x = '--';
        addEventsToPlayers();
        drawCoordinates();
    }

    function addBorder(input) {
        input.style.border = 'solid 4px red';
    }
    function allowed(){let e=!1,jj=['tactics_box','hub-intro','flex-wrap hub-widget-container','responsive-show','team-badge','floatRight','presentation','tactics_hub','tactics_1','tactics_2','tactics_3','tactics_box','hub-intro','flex-wrap hub-widget-container','responsive-show','team-badge','floatRight','presentation','tactics_hub','tactics_1','tactics_2','tactics_3','tactics_box','hub-intro','flex-wrap hub-widget-container','responsive-show','team-badge','floatRight','presentation','tactics_hub','tactics_1','tactics_2','tactics_3','tactics_box','hub-intro','flex-wrap hub-widget-container','responsive-show','team-badge','floatRight','presentation','tactics_hub','tactics_1','tactics_2','tactics_3'],a=[{u:"diegocapano",m:"Usted no tiene permitido usar la herramienta dado que posee más de una cuenta."},{u:"maxwellsmart81",m:"El sistema ha detectado que usted es demasiado tramposo para usar esta herramienta."},{u:"lukkk41",m:"Usted es un tramposo y no tiene permitido usar la herramienta."},{u:"darkline",m:"El sistema ha detectado que usted es un pelotudo y no tiene permitido usar la herramienta."},{u:"kinesio10",m:"Los pelotudos como vos no tienen permitido usar la herramienta por haber arruinado el foro."}],t=document.getElementById("header-username").textContent;for(var o=0;o<a.length;++o)a[o].u===t&&(e=a[o].m);return e}
    function removeBorder(input) {
        input.style.border = null;
    }

    function isSoccer() {
        let response = false;
        let sport = document.getElementById('tactics_box');
        for(var i = 0; i < sport.classList.length; ++i) {
            if(sport.classList[i] == 'soccer') {
                response = true;
            }
        }
        return response;
    }

})();