User:Dagger/Widget drafts/API item counts.js

From Guild Wars 2 Wiki
Jump to navigationJump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$(function() {
  if (!window.API || $(".api-youhave").length == 0) return;
  
  $.when(API.fetch("characters"),
         API.fetch("account/materials"),
         API.fetch("account/bank")
  ).then(function(charData, materialsData, bankData) {
    $(".api-youhave").each(function() {
      function isWanted(id) { return itemIds.indexOf(id) != -1; }
      var output = [];
      var total = 0;

      var itemIds = this.getAttribute("data-itemid").split(",").map(function(id) { return parseInt(id); });
      if (itemIds.some(isNaN)) { return; }

      // Count materials.
      itemIds.forEach(function(itemId) {
        if (materialsData[itemId] && materialsData[itemId].count) {
          output.push(materialsData[itemId].count + " in materials");
          total += materialsData[itemId].count;
        }
      });

      // Count bank.
      var bankCount = 0;
      bankData.forEach(function (item) {
        if (item && isWanted(item.id)) bankCount += item.count;
      });
      if (bankCount) output.push(bankCount + " in bank");
      total += bankCount;

      // Count character inventories.
      charData.forEach(function(character) {
        if (!character.bags) return;
        var count = 0;
        character.bags.forEach(function(bag) {
          if (!bag || !bag.inventory) return;

          // Count equipped bags too.
          if (isWanted(bag.id)) count += 1;

          bag.inventory.forEach(function(item) {
            if (item && isWanted(item.id)) count += item.count;
          });
        });
        if (count) output.push(count + " on "+ character.name);
        total += count;
      });

      // Count character equipment. This ends up kinda weird because there's
      // not enough room to write "2 equipped on <char name>" without it wrapping,
      // so instead we just show "On <char name>", with no way to tell if there's
      // multiple of the same item equipped. But that's fairly rare anyway.
      //
      // It's also not really clear if this should take transmutation into
      // account or not (but currently it doesn't; base items only).
      charData.forEach(function(character) {
        if (!character.equipment) return;
        var count = 0;
        character.equipment.forEach(function(slot) {
          if (slot && isWanted(slot.id)) count++;
        });
        if (count) output.push("On " + character.name);
        total += count;
      });

      // Convert array to HTML and add it to the page.
      if (output.length) {
        var fragment = document.createDocumentFragment();
        output.forEach(function(line) {
          fragment.appendChild(document.createTextNode(line));
          fragment.appendChild(document.createElement("br"));
        });
        // Add a total at the bottom. Takes up quite a bit of space and isn't
        // always very useful...
        /*if (output.length > 1) {
          fragment.appendChild(document.createElement("hr"));
          fragment.appendChild(document.createTextNode(total + " in total"));
        }*/
        $(this).empty().append(fragment);
      } else {
        $(this).text("None");
      }
    });
  }, function onFail(f) {
    $(".api-youhave").html("<i>API Error</i>").attr("title", JSON.stringify(f));
    API.log("Error: ", f);
  });
});