TW Trader

Finding items in towns for The West Classic

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         TW Trader
// @namespace    Johnny
// @author       Johnny
// @version      1.1
// @description  Finding items in towns for The West Classic
// @match        https://classic.the-west.net/game.php*
// @grant        none
// ==/UserScript==

window.TWTrader = {
  towns: {},
  items: [],
  buildings: ['tailor', 'gunsmith', 'general'],

  windowName: 'TWTrader',

  init: function() {
    let val = setInterval(function() {
      if (Object.keys(WMap.mapData.towns.obj).length !== 0) {
        clearInterval(val);
        TWTrader.getData();
        TWTrader.addMenuButton();
      }
    }, 500);
  },

  addMenuButton: function() {
    let button = document.createElement('li');
    button.id = 'menu_quests_plus';
    let buttonLink = document.createElement('a');
    buttonLink.onclick = TWTrader.openWindow;
    buttonLink.innerHTML = '<span>Quests+</span>';
    buttonLink.style.cursor = 'pointer';
    buttonLink.style.background = 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAAAZCAMAAADOidZyAAACTFBMVEUzIBowHhl0YUY/LSM2Ix0uHRctHBb///89KyIrGRM8Kh+zr65fTjpJNyqmoqFpWUBIOzeMhoRTQTE4Jh+TfFp+a05eUDrr6urW1NOagl+CblFlVT719fXf08aJdFRbSjY7Fg3h39/KyMeZlJNBHRW3e1+wdF/p28u/vLvYyLneybXPuabEn4mrinbHmHBuZGKMc1qXbVqgYU65akyHXEqAQzDLyMffz77NpIO4lYLUk3fNn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DYGHh6mF1LyEkUNaVXvgdV5D2/vPX29uXn+0nVrGueuf7xx062yC/xFRPFFpFImekPfdIFBqfVPW3D5FJHU6kWEAHnJn79Ov2hceuna5cbGuR9u5+Xlbdp4PibfhBlo8L4FZ0vVLcKJSjiQ0qekyLqiBqnehMn7dx8n976Z//bls1fr17duSCY5wZX/9CoGgH/ZAjy50YhZlE4TUvGiTEnKt6J6psoKntC/AcCZ0qPzEnuMAAAAAElFTkSuQmCC")';
    button.appendChild(buttonLink);
    document.getElementById('right_menu').appendChild(button);
  },

  openWindow: function() {
    if (!AjaxWindow.windows[TWTrader.windowName]) {
      let win = new Element('div', {
        'id': 'window_' + TWTrader.windowName,
        'class': 'window'
      });

      AjaxWindow.windows[TWTrader.windowName] = win;

      let html = '\
        <div class="window_borders"> \
        <h2 id="window_' + TWTrader.windowName + '_title" class="window_title"><span>Quests+</span></h2> \
        <a href="javascript:AjaxWindow.closeAll();" class="window_closeall"></a><a href="javascript:AjaxWindow.toggleSize(\'' + TWTrader.windowName + '\');" class="window_minimize"></a><a href="javascript:AjaxWindow.close(\'' + TWTrader.windowName + '\');" class="window_close"></a> \
        <div id="window_' + TWTrader.windowName + '_content" class="window_content"></div> \
        </div> \
      ';
      win.setHTML(html);
      win.bringToTop();
      win.injectInside('windows');
      win.centerLeft();

      let win_title = $('window_' + TWTrader.windowName + '_title');
      win_title.addEvent('dblclick', function () {
        win.centerLeft();
        win.setStyle('top', 133);
      });
      win.makeDraggable({
        handle: win_title,
        onStart: function () {
        },
        onComplete: function () {
        }.bind(AjaxWindow)
      });
      win.addEvent('mousedown', win.bringToTop.bind(win, []));
      win_title.addEvent('mousedown', win.bringToTop.bind(win, []));

      let win_content = $('window_' + TWTrader.windowName + '_content');
      win_content.style.overflowY = 'auto';
      TWTrader.clear(win_content);
      win_content.appendChild(TWTrader.getItems());
    } else {
      AjaxWindow.maximize(TWTrader.windowName);
      AjaxWindow.windows[TWTrader.windowName].bringToTop();
    }
  },

  getItems: function() {
    let wrap = document.createElement('div');
    wrap.style.textAlign = 'center';

    let towns_div = document.createElement('div');
    towns_div.id = TWTrader.windowName + '_towns';
    towns_div.style.marginBottom = '10px';
    wrap.appendChild(towns_div);

    for (let item of TWTrader.items) {
      if (item) {
        let item_div = new Item(item, true).get_bag_el();
        item_div.style.cursor = 'pointer';
        item_div.style.float = 'none';
        item_div.style.display = 'inline-block';
        item_div.addEventListener('click', function() {
          TWTrader.renderTowns(item.towns);
        }, false);
        wrap.appendChild(item_div);
      }
    }

    return wrap;
  },

  renderTowns: function(towns) {
    let sortedTowns = [];
    for (let town of towns) {
      sortedTowns.push({
        id: town,
        name: TWTrader.towns[town].name,
        way_time: TWTrader.towns[town].way_time
      });
    }
    sortedTowns.sort(function(a, b) {
      return a.way_time - b.way_time;
    });

    let towns_div = document.getElementById(TWTrader.windowName + '_towns');
    TWTrader.clear(towns_div);
    for (let town of sortedTowns) {
      let town_div = document.createElement('div');
      town_div.className = 'questlog_header';
      town_div.style.width = 'auto';
      town_div.style.margin = '0 0 -1px';
      town_div.innerHTML = '<a href="javascript:AjaxWindow.show(\'town\',{town_id:' + town.id + '});">' + town.name + '</a> (distance: ' + town.way_time.formatDuration() + ')';
      towns_div.appendChild(town_div);
    }
  },

  getData: function() {
    WMap.mapData.towns.each(function(town) {
      if (town.town) {
        let way_time = WMap.calcWayTime(Tasks.last_pos, town);
        TWTrader.towns[town.town.town_id] = {
          name: town.town.name,
          way_time: way_time
        };

        let townId = town.town.town_id;
        for (let i = 0; i < TWTrader.buildings.length; i++) {
          let xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function() {
            if (xhr.readyState === XMLHttpRequest.DONE && xhr.status == 200) {
              let data = JSON.parse(xhr.responseText || '{}');
              if (data.js && data.page) {
                let townStart = data.page.indexOf('town_id: ') + 9;
                let townStr = data.page.substring(townStart, data.page.indexOf('<', townStart)).split('});">');
                let townId = townStr[0];

                let itemsStart = data.js.indexOf('var trader_inv = ') + 17;
                let itemsStr = data.js.substring(itemsStart, data.js.indexOf('];', itemsStart) + 1);
                let items = JSON.parse(itemsStr);
                for (let i = 0; i < items.length; i++) {
                  let itemId = items[i].item_id;
                  if (!TWTrader.items[itemId]) {
                    TWTrader.items[itemId] = items[i];
                    TWTrader.items[itemId].towns = [townId];
                  } else {
                    TWTrader.items[itemId].towns.push(townId);
                  }
                }
              }
            }
          };
          xhr.open('POST', 'game.php?window=building_' + TWTrader.buildings[i] + '&town_id=' + townId, true);
          xhr.send();
        }
      }
    });
  },

  clear: function(el) {
    while (el.firstChild) el.removeChild(el.firstChild);
  },
};

TWTrader.init();