ArtanoMayarov / wofh report (en)

// ==UserScript==
// @name        wofh report (en)
// @namespace   https://*.waysofhistory.com/game
// @include     https://*.waysofhistory.com/gen/html*
// @version     5.1
// @grant       none
// @license     MIT
// @updateURL https://openuserjs.org/meta/ArtanoMayarov/wofh_report_(en).meta.js
// @downloadURL https://openuserjs.org/install/ArtanoMayarov/wofh_report_(en).user.js
// @copyright   2017-2019, Сатана (https://gist.github.com/vityas-off/806ab420d184ee777996da83c3f4ac4c),simplexe (https://openuserjs.org/users/simplexe)
// @copyright   2019 Artano Mayarov (translate only)
// ==/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 += `
*** City's report [t${wofh.town.id}] ***
Population: ${wofh.town.pop.has | 0}/${wofh.town.pop.culture} ${wofh.town.pop
      .incReal >= 0
      ? '+'
      : '-'} ${Math.abs(Math.round(wofh.town.pop.incReal))}
Workers distribution: ${Object.values(wofh.town.calcPopOnRes().list)
      .sort((a, b) => b.count - a.count)
      .map(el => `${Resource.lib[el.id].name} - ${el.count}`)
      .join(', ')}, Unemployed - ${(wofh.town.pop.has -
      wofh.town.calcPopOnRes().calcSum()) |
      0}
Resource production: ${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(', ')}
Resource consumption: ${Object.values(wofh.town.stock.getConsBinList().list)
      .map(el => Resource.lib[el.id].name)
      .join(', ')}
Industry LC bonus: ${bonus * 10} %
[Buildings]
${Object.values(wofh.town.buildings)
      .filter(el => el[0] !== 500)
      .map(el => `[b${el[0]}] lvl ${el[1]}`)
      .join('\n')}
`;
}

function calcCost() {
  alert(
    `Destruction durability: ${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 = "City's report";
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 = "Durability";
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();
  }
});