hwmTroopsReflector

Кнопка отражения войск по вертикали при расстоновке

目前為 2024-03-13 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           hwmTroopsReflector
// @namespace      Tamozhnya1
// @author         Tamozhnya1
// @description    Кнопка отражения войск по вертикали при расстоновке
// @version        1.5
// @include        *heroeswm.ru/war.php*
// @include        *lordswm.com/war.php*
// @license        MIT
// ==/UserScript==

const playerIdMatch = document.cookie.match(/pl_id=(\d+)/);
if(!playerIdMatch) {
    return;
}
const PlayerId = playerIdMatch[1];
const lang = document.documentElement.lang || (location.hostname == "www.lordswm.com" ? "en" : "ru");
const isEn = lang == "en";
const win = window.wrappedJSObject || unsafeWindow;
var loadId;
var startId;

main();
function main() {
    loadId = setInterval(waitForBattleLoad, 200);
}
function waitForBattleLoad() {
    if(win.stage[win.war_scr].setted_atb) {
        clearInterval(loadId);
        console.log(`btype: ${win.btype}`);
        if(win.warlog == 0) {
            createMirrorDeploymentButton();
            startId = setInterval(waitForBattleStart, 200);
            document.getElementById("confirm_ins_img").addEventListener("click", removeMirrorDeploymentButton, true);
        }
    }
}
function waitForBattleStart() {
    if (win.lastturn > -1) {
        clearInterval(startId);
        removeMirrorDeploymentButton();
    }
}
function removeMirrorDeploymentButton() {
    const mirrorDeploymentButton = document.getElementById("mirrorDeploymentButton");
    if(mirrorDeploymentButton) {
        mirrorDeploymentButton.remove();
    }
}
// TODO Подобрать картинку
function createMirrorDeploymentButton() {
    document.getElementById('right_button').insertAdjacentHTML("beforeend", `
<div id="mirrorDeploymentButton" class="toolbars_img">
    <img src="https://dcdn.heroeswm.ru/i/combat/btn_autoalignment.png?v=6" alt="${isEn ? "Deployment mirror vertical" : "Отразить расстановку по вертикали"}" title="${isEn ? "Deployment mirror vertical" : "Отразить расстановку по вертикали"}" style="filter: sepia(100%) hue-rotate(190deg) saturate(900%);">
</div>`);
    document.getElementById("mirrorDeploymentButton").addEventListener("click", mirrorDeployment);
}
function mirrorDeployment() {
    const playerNumber = Math.floor((win.playero - 1) / 2);
    const deploymentFieldWidthBegin = Math.floor(playerNumber * win.defyn / win.yourside + 1);
    const deploymentFieldWidthEnd = Math.floor((playerNumber + 1) * win.defyn / win.yourside);
    console.log(`playero: ${win.playero}, yourside: ${win.yourside}, defxn: ${win.defxn}, defyn: ${win.defyn}, deploymentFieldWidthBegin: ${deploymentFieldWidthBegin}, deploymentFieldWidthEnd: ${deploymentFieldWidthEnd}`);
    const poleObj = win.stage[win.war_scr].obj; //console.log(poleObj);
    const playerUnits = Object.keys(poleObj).filter(k => !poleObj[k].hero && poleObj[k].owner == win.playero).map(k => poleObj[k]);
    const deployment = playerUnits.map(x => [x.id, x.nownumber, x.x, deploymentFieldWidthBegin + deploymentFieldWidthEnd - x.y - x.big]); // big = 0 или 1, если большое существо
    const insStr = deployment.map(x => x.join("#")).join("^") + "^"; console.log(insStr);
    const pole = win.stage[win.war_scr]; //stage[war_scr].obj  Object.keys(stage[war_scr].obj).map(x => `${stage[war_scr].obj[x].nametxt} ${stage[war_scr].obj[x].side} ${stage[war_scr].obj[x].owner}`)
    pole.useinsertion_cre(insStr); // Расстановка по данным из текстовой строки
}