Greasy Fork is available in English.

GN_RobberEventCalc

Расчет прибыли разбойных нападений

// ==UserScript==
// @name        GN_RobberEventCalc
// @namespace   Gradient
// @description Расчет прибыли разбойных нападений
// @include     /^https{0,1}:\/\/(www\.heroeswm\.ru|178\.248\.235\.15)\/naym_event_set\.php/
// @version     1.0.6
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_deleteValue
// ==/UserScript==

//----------------------------------------------------------------------------//

"use strict";

//----------------------------------------------------------------------------//

(function(){  // wrapper start

//----------------------------------------------------------------------------//

var old_creatures = [];

start_work();

//----------------------------------------------------------------------------//

function start_work(){
  old_creatures = JSON.parse(GM_getValue('GN_RobberEventCalcSettings', '[]'));
  if(!old_creatures.length){
    // defaults, by forum
    old_creatures.push({ name: 'Рубаки',               min_price: 41,   max_price: 41,   last_buy_price: 0 }); // !
    old_creatures.push({ name: 'Дозорные',             min_price: 59,   max_price: 62,   last_buy_price: 0 }); // ?
    old_creatures.push({ name: 'Лесные хоббиты',       min_price: 75,   max_price: 81,   last_buy_price: 0 });
    old_creatures.push({ name: 'Лазутчицы',            min_price: 112,  max_price: 118,  last_buy_price: 0 });
    old_creatures.push({ name: 'Шпионки',              min_price: 125,  max_price: 125,  last_buy_price: 0 }); // !
    old_creatures.push({ name: 'Колдуны-ренегаты',     min_price: 71,   max_price: 71,   last_buy_price: 0 }); // !
    old_creatures.push({ name: 'Чародеи-ренегаты',     min_price: 83,   max_price: 89,   last_buy_price: 0 });
    old_creatures.push({ name: 'Молотобойцы',          min_price: 96,   max_price: 98,   last_buy_price: 0 });
    old_creatures.push({ name: 'Вышибалы',             min_price: 151,  max_price: 156,  last_buy_price: 0 });
    old_creatures.push({ name: 'Кочевники',            min_price: 259,  max_price: 259,  last_buy_price: 0 }); // !
    old_creatures.push({ name: 'Гунны',                min_price: 292,  max_price: 309,  last_buy_price: 0 });
    old_creatures.push({ name: 'Пустынные налетчики',  min_price: 421,  max_price: 447,  last_buy_price: 0 });
    old_creatures.push({ name: 'Мобильные баллисты',   min_price: 698,  max_price: 719,  last_buy_price: 0 });
    old_creatures.push({ name: 'Тэнгу',                min_price: 887,  max_price: 887,  last_buy_price: 0 }); // !
    old_creatures.push({ name: 'Ямабуси Тэнгу',        min_price: 1184, max_price: 1219, last_buy_price: 0 });
    old_creatures.push({ name: 'Великаны',             min_price: 1950, max_price: 2009, last_buy_price: 0 });
    old_creatures.push({ name: 'Великаны-лучники',     min_price: 2124, max_price: 2254, last_buy_price: 0 });
    old_creatures.push({ name: 'Гноллы',               min_price: 62,   max_price: 65,   last_buy_price: 0 });
    old_creatures.push({ name: 'Гноллы-шаманы',        min_price: 114,  max_price: 114,  last_buy_price: 0 }); // !
    old_creatures.push({ name: 'Загонщики на варанах', min_price: 450,  max_price: 477,  last_buy_price: 0 });
    old_creatures.push({ name: 'Служители оазисов',    min_price: 555,  max_price: 555,  last_buy_price: 0 }); // !
    old_creatures.push({ name: 'Стражи оазисов',       min_price: 711,  max_price: 711,  last_buy_price: 0 }); // !
  }

  var creatures_table = document.querySelectorAll('table.wbwhite[cellpadding="4"]')[1];
  if(!creatures_table)
    show_error('creatures_table not found');

  var clear_prices_btn = document.createElement('button');
  clear_prices_btn.appendChild(document.createTextNode('Сбросить цены с прошлого ивента'));
  creatures_table.parentNode.insertBefore(clear_prices_btn, creatures_table);

  clear_prices_btn.addEventListener('click', function(e){
    old_creatures = [];
    GM_setValue('GN_RobberEventCalcSettings', JSON.stringify(old_creatures));
    document.location.reload();
  });

  var creatures_trs = creatures_table.firstChild.childNodes;

  if(!creatures_trs.length)
    show_error('creatures_trs not found');

  var current_silver_el = document.querySelector('td.wbwhite').querySelector('a[href="naym_event.php"]');

  if(!current_silver_el)
    show_error('current_silver_el not found');

  var current_silver = /.*?<b>([0-9,]+)<\/b>.*?/gmi.exec(current_silver_el.parentNode.parentNode.parentNode.innerHTML)[1].replace(/,/g, '');

  for(var i = 0; i < creatures_trs.length; ++i){
    var creature_el = creatures_trs[i].querySelector('img.cre_mon_image2');

    if(!creature_el)
      show_error('creature_el not found');

    var creature_name = creature_el.getAttribute('title');

    var price_el = creatures_trs[i].firstChild.nextSibling;

    if(!price_el)
      show_error('price_el not found');

    var creature_price = /.*?Цена\:.+?<b>(\d+)<\/b>.*?/gmi.exec(price_el.innerHTML)[1];

    var min_price = creature_price;
    var max_price = creature_price;

    var creature = get_creature(old_creatures, creature_name);

    if(creature){
      min_price = Math.min(creature.min_price, creature_price);
      max_price = Math.max(creature.max_price, creature_price);

      creature.min_price = min_price;
      creature.max_price = max_price;
    } else {
      creature = { name: creature_name, min_price: min_price, max_price: max_price, last_buy_price: 0 };
      old_creatures.push(creature);
    }

    //if(creature_price > creature.min_price && creature_price < creature.max_price)
    //  creature.max_price = creature_price;

    while(price_el.firstChild)
      price_el.removeChild(price_el.firstChild);

    price_el.appendChild(document.createElement('b'));
    price_el.firstChild.appendChild(document.createTextNode(creature_name));
    price_el.appendChild(document.createElement('br'));

    var diff = (creature.min_price - creature_price);
    var text = document.createElement('div');
    if(diff >= 0)
      text.style.color = 'darkgreen';
    else
      text.style.color = 'red';

    text.appendChild(document.createTextNode('Цена: ' + creature_price));
    price_el.appendChild(text);

    price_el.appendChild(document.createTextNode('Мин. цена: ' + min_price));
    price_el.appendChild(document.createElement('br'));
    price_el.appendChild(document.createTextNode('Макс. цена: ' + max_price));

    var commission = 0.97;

    if(creature.last_buy_price){
      price_el.appendChild(document.createElement('br'));
      price_el.appendChild(document.createTextNode('Цена покупки: ' + creature.last_buy_price));

      diff = Math.floor(creature_price*commission - creature.last_buy_price);
      if(diff){
        price_el.appendChild(document.createElement('br'));
        text = document.createElement('b');
        if(diff > 0)
          text.style.color = 'darkgreen';
        else if(diff < 0){
          text.style.color = 'red';
          creature.max_price = creature.last_buy_price;
        }

        text.appendChild(document.createTextNode('Прибыль с продажи: ' + diff));
        price_el.appendChild(text);
      }
    }
    var input = creatures_trs[i].lastChild.querySelector('input[id*=but]');

    if(input){
      var max_amount = price_el.previousSibling.querySelector('div[id="add_now_count"]').textContent;
      var max_amount_price = max_amount*creature_price;
      var used_silver = (max_amount_price >= current_silver ? current_silver : max_amount_price);

      var profit = Math.floor(used_silver/creature_price)*(Math.floor(creature.max_price*commission) - creature_price);
      if(profit > 0){
        price_el.appendChild(document.createElement('br'));

        text = document.createElement('b');
        text.style.color = 'darkgreen';

        text.appendChild(document.createTextNode('Общая прибыль: ' + profit));
        price_el.appendChild(text);
      }

      input.addEventListener('click', function(e){
        var price_el_i = this.parentNode.parentNode.parentNode.parentNode.parentNode.previousSibling;
        var price_i = /.*?Цена\:.+?<b>(\d+)<\/b>.*?/gmi.exec(price_el_i.innerHTML)[1];

        var creature_el_i = price_el_i.previousSibling.querySelector('img.cre_mon_image2');
        var creature_name_i = creature_el_i.getAttribute('title');

        var creature_i = get_creature(old_creatures, creature_name_i);
        creature_i.last_buy_price = price_i;
        GM_setValue('GN_RobberEventCalcSettings', JSON.stringify(old_creatures));
      });
    }
  }

  GM_setValue('GN_RobberEventCalcSettings', JSON.stringify(old_creatures));
}

//----------------------------------------------------------------------------//

function show_error(str){
  alert(str);
  throw str;
}

//----------------------------------------------------------------------------//

function get_creature(array_, name){
  var creature = null;

  array_.forEach(function(current){
    if(current.name == name)
      creature = current;
  });

  return creature;
}

//----------------------------------------------------------------------------//

})();  // wrapper end

//----------------------------------------------------------------------------//