simplexe / wofh report

// ==UserScript==
// @name        wofh report
// @namespace   https://*.waysofhistory.com/game
// @include     https://*.waysofhistory.com/gen/html*
// @version     5.1
// @grant       none
// @license     MIT
// @updateURL https://openuserjs.org/meta/simplexe/wofh_report.meta.js
// @downloadURL https://openuserjs.org/install/simplexe/wofh_report.user.js
// @copyright   2017-2019, Сатана (https://gist.github.com/vityas-off/806ab420d184ee777996da83c3f4ac4c),simplexe (https://openuserjs.org/users/simplexe)
// ==/UserScript==

const changeBg = () => {
  const bg = localStorage.getItem('bg');
  if (bg) document.body.style.background = `url(${bg}) no-repeat`;
};

document.addEventListener('dragover', evt => {
  evt.preventDefault();
});

document.addEventListener('drop', evt => {
  evt.preventDefault();
  const file = evt.dataTransfer.files[0];
  const reader = new FileReader();
  reader.addEventListener('load', () => {
    localStorage.setItem('bg', reader.result);
    changeBg();
  });
  if (file) reader.readAsDataURL(file);
});

changeBg();

function createReport() {
  var bonus = wofh.town.getLuckBonus(3).level;
  document.querySelector('.messages-write-text').value += `
*** Отчет по городу [t${wofh.town.id}] ***
Население: ${wofh.town.pop.has | 0}/${wofh.town.pop.culture} ${wofh.town.pop
      .incReal >= 0
      ? '+'
      : '-'} ${Math.abs(Math.round(wofh.town.pop.incReal))}
Распределение рабочих: ${Object.values(wofh.town.calcPopOnRes().list)
      .sort((a, b) => b.count - a.count)
      .map(el => `${Resource.lib[el.id].name} - ${el.count}`)
      .join(', ')}, Безработных - ${(wofh.town.pop.has -
      wofh.town.calcPopOnRes().calcSum()) |
      0}
Добыча ресурсов: ${Object.values(wofh.town.stock.list)
      .filter(el => el.incom > 0 && el.id !== 2)
      .sort((a, b) => b.incom - a.incom)
      .map(el => `${Resource.lib[el.id].name} - ${el.incom | 0}`)
      .join(', ')}
Потребляемые ресурсы: ${Object.values(wofh.town.stock.getConsBinList().list)
      .map(el => Resource.lib[el.id].name)
      .join(', ')}
Бонус промки: ${bonus * 10} %
[Застройка]
${Object.values(wofh.town.buildings)
      .filter(el => el[0] !== 500)
      .map(el => `[b${el[0]}] ур. ${el[1]}`)
      .join('\n')}
`;
}

function calcCost() {
  alert(
    `Прочность города: ${Object.values(wofh.town.slots.list)
        .filter(el => el.build.cost && !Build.isWonder(el.id))
        .map(el => el.calcTotalCost().list)
        .reduce(
          (acc, el) =>
            acc +
            Object.values(el)
              .map(el => el.count)
              .reduce((total, num) => num + total, 0),
          0
        )
        .toLocaleString('ru-RU')}`
  );
}

var bReport = document.createElement('button');
bReport.className = "button1 btn-Report";
bReport.setAttribute('style', 'position: absolute; top: 0; right: 110px');
bReport.textContent = "Отчет";
bReport.addEventListener('click', createReport);

var bCalc = document.createElement('button');
bCalc.className = "button1 btn-Calc";
bCalc.setAttribute('style', 'position: absolute; top: 0; right: 220px');
bCalc.textContent = "Прочность";
bCalc.addEventListener('click', calcCost);

document.addEventListener('click', evt => {
  if (document.querySelector('.messages-write-form') !== null && document.querySelector('.messages-write-form').length > 0) {
    if (document.querySelector('.btn-Report') === null) {
      document.querySelector('.messages-write-footer').insertBefore(bReport, document.querySelector('.messages-write-chrCount'));
    }
    if (document.querySelector('.btn-Calc') === null) {
      document.querySelector('.messages-write-footer').insertBefore(bCalc, document.querySelector('.messages-write-chrCount'));
    }
  }
});

document.addEventListener('keydown', evt => {
  if (evt.keyCode === 49 && evt.ctrlKey && evt.altKey) {
    createReport();
  }
  if (evt.keyCode === 48 && evt.ctrlKey && evt.altKey) {
    calcCost();
  }
});