hwmTroopsReflector

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

  1. // ==UserScript==
  2. // @name hwmTroopsReflector
  3. // @namespace Tamozhnya1
  4. // @author Tamozhnya1
  5. // @description Кнопка отражения войск по вертикали при расстановке
  6. // @version 2.0
  7. // @include *heroeswm.ru/war.php*
  8. // @include *lordswm.com/war.php*
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. const playerIdMatch = document.cookie.match(/pl_id=(\d+)/);
  13. if(!playerIdMatch) {
  14. return;
  15. }
  16. const PlayerId = playerIdMatch[1];
  17. const lang = document.documentElement.lang || (location.hostname == "www.lordswm.com" ? "en" : "ru");
  18. const isEn = lang == "en";
  19. const win = window.wrappedJSObject || unsafeWindow;
  20. let battleLoadedTimerId;
  21. let battleStartedTimerId;
  22.  
  23. main();
  24. function main() {
  25. battleLoadedTimerId = setInterval(waitForBattleLoad, 200);
  26. }
  27. function waitForBattleLoad() {
  28. if(win.stage[win.war_scr].setted_atb) {
  29. clearInterval(battleLoadedTimerId);
  30. console.log(`btype: ${win.btype}`);
  31. if(win.warlog == 0) {
  32. document.getElementById('right_button').insertAdjacentHTML("beforeend", `
  33. <div id="reflectDeploymentButton" class="toolbars_img">
  34. <img src="https://dcdn.heroeswm.ru/i/combat/btn_autoalignment.png?v=6" alt="${isEn ? "Reflect deployment vertical" : "Отразить расстановку по вертикали"}" title="${isEn ? "Reflect deployment vertical" : "Отразить расстановку по вертикали"}" style="filter: sepia(100%) hue-rotate(190deg) saturate(900%);">
  35. </div>`);
  36. document.getElementById("reflectDeploymentButton").addEventListener("click", reflectDeployment);
  37. battleStartedTimerId = setInterval(waitForBattleStart, 200);
  38. document.getElementById("confirm_ins_img").addEventListener("click", removeReflectDeploymentButton, true);
  39. }
  40. }
  41. }
  42. function waitForBattleStart() {
  43. if (win.lastturn > -1) {
  44. clearInterval(battleStartedTimerId);
  45. removeReflectDeploymentButton();
  46. }
  47. }
  48. function removeReflectDeploymentButton() {
  49. const reflectDeploymentButton = document.getElementById("reflectDeploymentButton");
  50. if(reflectDeploymentButton) {
  51. reflectDeploymentButton.remove();
  52. }
  53. }
  54. function reflectDeployment() {
  55. const playerIndexOnYourSide = Math.floor((win.playero - 1) / 2);
  56. const deploymentFieldWidthBegin = Math.floor(playerIndexOnYourSide * win.defyn / win.yourside + 1);
  57. const deploymentFieldWidthEnd = Math.floor((playerIndexOnYourSide + 1) * win.defyn / win.yourside);
  58. console.log(`playero: ${win.playero}, yourside: ${win.yourside}, defxn: ${win.defxn}, defyn: ${win.defyn}, stackcount: ${win.stackcount}, camp_mirror: ${win.camp_mirror}, deploymentFieldWidthBegin: ${deploymentFieldWidthBegin}, deploymentFieldWidthEnd: ${deploymentFieldWidthEnd}`);
  59. const poleObj = win.stage[win.war_scr].obj; //console.log(poleObj);
  60. const playerUnits = Object.keys(poleObj).filter(k => !poleObj[k].hero && poleObj[k].owner == win.playero && !poleObj[k].building && !poleObj[k].ballista && poleObj[k].lname != "cannonani" && !poleObj[k].warmachine && poleObj[k].portal != 1).map(k => poleObj[k]);
  61. const deployment = playerUnits.map(u => [u.id, u.nownumber, u.x, deploymentFieldWidthBegin + deploymentFieldWidthEnd - u.y - u.big]); // big = 0 или 1, если большое существо
  62. //const deployment = playerUnits.map(x => [x.obj_index, x.nownumber, x.x, deploymentFieldWidthBegin + deploymentFieldWidthEnd - x.y - x.big]); // big = 0 или 1, если большое существо
  63. const insStr = deployment.map(x => x.join("#")).join("^") + "^"; console.log(insStr);
  64. const pole = win.stage[win.war_scr]; //stage[war_scr].obj Object.keys(stage[war_scr].obj).map(x => { let u = stage[war_scr].obj[x]; return `${u.id} ${u.nametxt} ${u.side} ${u.owner} x/y/big/n/building:${u.x}/${u.y}/${u.big}/${u.nownumber}/${u.building}`; })
  65. pole.useinsertion_cre(insStr); // Расстановка по данным из текстовой строки
  66. }