Jump to content
This is a wiki for a reason. Anyone can contribute. If you see something that is inaccurate or can be improved, don't ask that it be fixed--just improve it.
[ Disclaimer, Create new user --- Wiki markup help, Install P99 ]

MediaWiki:TreasureHuntingGuide.js

From p99 Backup

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Count spots

var spots = [];

$('.wikitable tbody tr').each(function(i, tr) {
  var $tds = $(tr).children('td');
  var level = $($tds[0]).text().trim();
  var zone = $($tds[1]).text().trim(); 
  if (level && zone) spots.push({ level: level, zone: zone });
}); 

$('#numSpots').text(spots.length);

// Add price-checking to Treasure Hunting Guide

/**
 * Given the HTML for a <td> extracts the numeric value (and multiplies it by 20 if we're
 * dealing with a stacked item)
 */
var getPriceFromCell = function(cell, isStack) {
  return parseInt(cell.split(' ')[0], 10) * (isStack ? 20 : 1);
};
/**
 * Given a bunch of <td>s from the auction tracker, this function separates out the
 * price info and returns a message containing it
 * @arg isStack boolean that's true if we're getting the price of a stack (20) of items
 */
var buildMessageFromCells = function(cells, itemName, isStack) {
  var thirtyCell = getPriceFromCell(cells[1], isStack);
  var ninetyCell = getPriceFromCell(cells[2], isStack);
  var allCell = getPriceFromCell(cells[3], isStack);
  return '<span style="text-decoration: underline">'+
         (isStack ? 'a stack of (20x) ' : '') +
         itemName + '</span>' +
         '<br/>30-day: ' + thirtyCell +
         '<br/>90-day: ' + ninetyCell +
         '<br/>All-Time: ' + allCell;
}
/**
 * Extracts the HTML for the three price-related <td>s of the auction tracker
 */
var extractCellHtml = function(html, itemName) {
  var eoTableHtml = html.split('eoTable3')[1];
  if (!eoTableHtml) throw new Error('No price information could be found for "' + itemName + '".');
  return eoTableHtml
    .split('<td>')
    .map(function(x) { return x.trim(); });
};

var handlePriceCheckClick = function(e) {
  var $parentTd = $(e.target).parent();
  var isStack = $parentTd.is('.price_check_stack');
  document.body.style.cursor = 'wait';
  var $anchors = $parentTd.prev().find('.hbdiv > a');
  var itemNames = $anchors.map(function(i, a) { return $(a).attr('title'); });
  var promises = itemNames.map(function(i, itemName) {
    return $.ajax('/' + itemName);
  });
  $.when.apply($, promises).then(function(firstArg) {
    document.body.style.cursor = '';
    // Arguments will vary based on whether we did 1 or 2+ AJAX calls (thanks jQuery!)
    var pages = typeof firstArg === 'string' ?
      [firstArg] : // We only looked up one page
      Array.from(arguments).map(function(args) { return args[0]; });  // We looked up multiple
    try {
      var messages = pages.map(function(html) {
        var itemName = html.split('title>')[1].split(' - Project')[0];
        var cells = extractCellHtml(html, itemName);
        return buildMessageFromCells(cells, itemName, isStack);
      });
      $parentTd
        .removeClass('price_check')
        .html(messages.join('<br/><br/>'));
    } catch (err) {
      alert(err.message || 'Something went wrong!');
      document.body.style.cursor = '';
    }
  }).fail(function(message) {
    document.body.style.cursor = '';
    alert(message || 'Something went wrong!');
  });
};

$('.price_check span, .price_check_stack span').click(handlePriceCheckClick);