Recreate Setup on epicmafia.com

Add a recreate button to the epicmafia.com setup pages

ของเมื่อวันที่ 20-11-2017 ดู เวอร์ชันล่าสุด

// ==UserScript==
// @name Recreate Setup on epicmafia.com
// @namespace    https://greasyforks.org/en/users/159342-cleresd
// @description Add a recreate button to the epicmafia.com setup pages
// @version 1.02
// @match       https://epicmafia.com/setup/*
// @include     https://epicmafia.com/game/new
// @grant GM_setValue
// @grant GM_getValue
// @run-at  document-end
// ==/UserScript==


$(document).ready(function () {
    if ($('.setup_actions').length)
        $('.setup_actions').after('<div class="play_menu" onclick="my.reCreateSetup()" onmouseover="this.style.borderColor=\'#b11\';this.style.backgroundColor=\'#eee\';" onmouseout="this.style.borderColor=\'#999\';this.style.backgroundColor=\'#eee\';" style="width: 65px"><div class="play_menu_text" style="color: rgb(205, 136, 211);">Recreate</div></div></div>');
    else if ($('#subnav').find('li.sel > a.sel').text() === 'Create game') {
        console.log(GM_getValue("myRecreateSetupFunctionText"));
        let recreateSetupFunctionText = GM_getValue("myRecreateSetupFunctionText");
        if (recreateSetupFunctionText !== undefined && recreateSetupFunctionText !== '') {
            $(document.body).append('<script type="text/javascript">' + GM_getValue("myRecreateSetupFunctionText") + '</script>');
            GM_setValue("myRecreateSetupFunctionText", '');
        }
    }
});


// Примечание: Опция Multiple Mafias не работает в Main lobby
my = {};
my.reCreateSetup = function () {
    let getDataFromSetupObject = {
        result: '(function(){',
        gameOptionIds: [],
        roles: [],
        gameSetup: $('.gamesetup').children(),

        getDataFromFixedSetup: function () {
            // get game's options
            /*this.gameOptionIds = $(this.gameSetup)
                .filter((_, currEl) => currEl.classList.contains('false'))
                .map((_, currEl) => '#add_' + currEl.classList[0].split('-')[0]);
            // get roles
            $(this.gameSetup)
                .filter((_, el) => el.classList.contains('roleimg'))
                .forEach((_, currEl) => {
                    let roleName = currEl.firstElementChild.classList[1];
                    roleName += currEl.firstElementChild.classList.contains('mafia_red') ?
                        '.mafia_red' : ':not(.mafia_red)';
                    let roleCount = currEl.firstElementChild.hasChildNodes() ?
                        currEl.firstElementChild.firstElementChild.textContent : 1;
                    this.addRoles(roleName, roleCount);
                });*/

            for (let i = 0; i < this.gameSetup.length - 3; i++) {
                let currEl = this.gameSetup[i];

                if (currEl.classList.contains('false')) {
                    // get game's option
                    let optionId = '#add_' + currEl.classList[0].split('-')[0];
                    this.gameOptionIds.push(optionId);
                } else if (currEl.firstElementChild.classList.contains('roleimg')) {
                    // get role
                    // get role's count
                    let roleName = currEl.firstElementChild.classList[1];
                    roleName += currEl.firstElementChild.classList.contains('mafia_red') ?
                        '.mafia_red' : ':not(.mafia_red)';
                    let roleCount = currEl.firstElementChild.hasChildNodes() ?
                        currEl.firstElementChild.firstElementChild.textContent : 1;
                    this.addRoles(roleName, roleCount);
                }
            }
        },

        addRoles: function (roleName, roleCount) {
            for (let j = 0; j < roleCount; j++) {
                this.roles.push('\'.' + roleName + '\'');
            }
        },

        getDataFromRandomSetup: function () {
            for (let i = 0; i < this.gameSetup.length - 3; i++) {
                let currEl = this.gameSetup[i];

                if (currEl.classList.contains('false')) {
                    // get game option
                    let optionId = '#add_' + currEl.classList[0].split('-')[0];
                    this.gameOptionIds.push(optionId);
                } else {
                    // get roles
                    let $allRoles = $('.allroles_align').children();
                    for (let i = 0; i < $allRoles.length; i++) {
                        this.addRandomRole($allRoles[i]);
                    }
                }
            }
        },

        addRandomRole: function (randomRole) {
            let roleName = randomRole.classList[1];
            roleName += randomRole.classList.contains('mafia_red') ?
                '.mafia_red' : ':not(.mafia_red)';
            this.roles.push('\'.' + roleName + '\'');
        },

        getOptionsBeforeRoles: function () {
            let addBeforeRoles = '';
            // closed roles on the top to add all roles in correct way
            let closedRolesOptionIndex;
            if ((closedRolesOptionIndex = this.gameOptionIds.indexOf('#add_closedroles')) !== -1) {
                // if exist
                this.gameOptionIds.splice(closedRolesOptionIndex, 1);
                addBeforeRoles += '\n$(\'#add_closedroles\').click();\n';
            }
            return addBeforeRoles;
        },

        getOptionsAfterRoles: function () {
            let addAfterRoles = '';
            // make sure it would be all right with whisper game option
            let whisperOptionIndex;
            if ((whisperOptionIndex = this.gameOptionIds.indexOf('#add_whisper')) !== -1) {
                // if exist
                this.gameOptionIds.splice(whisperOptionIndex, 1);
            } else {
                // if doesn't exist
                this.gameOptionIds.push('#add_whisper');
            }

            // add all game options to the result line
            for (let i = 0; i < this.gameOptionIds.length; i++) {
                let optionId = this.gameOptionIds[i];
                addAfterRoles += '$(\'' + optionId + '\').click();\n';
            }

            if ($('#prob_village').find('> div > .uniquealign').length === 1) {
                addAfterRoles += '$(\'#add_unique_village\').click();\n';
            }
            if ($('#prob_mafia').find('> div > .uniquealign').length === 1) {
                addAfterRoles += '$(\'#add_unique_mafia\').click();\n';
            }
            if ($('#prob_third').find('> div > .uniquealign').length === 1) {
                addAfterRoles += '$(\'#add_unique_third\').click();\n';
            }

            return addAfterRoles;
        },

        getFinalResult: function(addBeforeRoles, addAfterRoles) {
            this.result += '\nlet roles = [' + this.roles + '];\n';

            this.result += `
let checkExist = setInterval(function() {
  if ($(roles[0]).length) {
    clearInterval(checkExist);\n` + addBeforeRoles + `
    for (let i = 0; i < roles.length; i++) {
      $(roles[i]).prev().children('.inc').click();
    }\n` + addAfterRoles + `
  }
}, 500);\n`;
        },

        getFinalResultFromMultisetup: function (addBeforeRoles, addAfterRoles) {
            let roleCountInOneSetup = Number($('.rolecont > .roleimg.role-unknown > .sup').text());
            let setupCount = this.roles.length / roleCountInOneSetup;

            function addEmptySetups() {
                let result = '';
                for (let i = 0; i < setupCount - 1; i++) {
                    result += "$('#addgroup').click();\n";
                }
                return result;
            }

            this.result += '\nlet roles = [' + this.roles + '];\n';

            this.result += `
function addSetups(currentSetup) {
    let result = '';
    let startId = currentSetup * roleCountInOneSetup;
    let endId = startId + roleCountInOneSetup;
    for (let i = startId; i < endId; i++) {
        $(roles[i]).prev().children('.inc').click();
    }
    return result;
}

let roleCountInOneSetup = ` + roleCountInOneSetup + `;
let setupCount = ` + setupCount + `;
let currentSetup = 0;
let checkExist = setInterval(function() {
  if ($(roles[0]).length) {
    clearInterval(checkExist);\n` +
    addBeforeRoles +
    addEmptySetups() + `
    for (let i = 0; i < setupCount; i++){
        $('#gameicons').children().eq(i).click();
        addSetups(i);
    }\n` +
    addAfterRoles +
` }
}, 500);\n`;
        },

        getResult: function () {
            if ($('.gamesetup > .probabilities').length === 1) {
                this.getDataFromRandomSetup();
            } else {
                this.getDataFromFixedSetup();
            }

            let addBeforeRoles = this.getOptionsBeforeRoles();
            let addAfterRoles = this.getOptionsAfterRoles();

            // add all roles to the result line
            if (this.gameSetup.parent().find('li.selsetup').length === 1) {
                this.getFinalResultFromMultisetup(addBeforeRoles, addAfterRoles);
            } else {
                this.getFinalResult(addBeforeRoles, addAfterRoles);
            }

            this.result += '\n})();';
        }
    };

    getDataFromSetupObject.getResult();
    console.log(getDataFromSetupObject.result);
    GM_setValue("myRecreateSetupFunctionText", getDataFromSetupObject.result);
    window.open('https://epicmafia.com/game/new', '_blank');
};
长期地址
遇到问题?请前往 GitHub 提 Issues。