Module:Monsters: Difference between revisions

Added getChestDrops
(Added sortvalues to loot chance & qty columns to ensure they sort properly)
(Added getChestDrops)
Line 424: Line 424:
       end
       end
       result = result..'style="text-align:right"|'..lootChance..'.00%'
       result = result..'style="text-align:right"|'..lootChance..'.00%'
    end
    result = result..'\r\n|}'
  end
  return result
end
function p.getChestDrops(frame)
  local ChestName = frame.args ~= nil and frame.args[1] or frame
  local chest = Items.getItem(ChestName)
  if chest == nil then
    return "ERROR: No item named "..ChestName..' found'
  end
  local result = ''
  if chest.dropTable == nil then
    return "ERROR: "..ChestName.." does not have a drop table"
  else
    local lootChance = 100
    local multiDrop = Shared.tableCount(chest.dropTable) > 1
    local totalWt = 0
    for i, row in pairs(chest.dropTable) do
      totalWt = totalWt + row[2]
    end
    result = result..'\r\n{|class="wikitable sortable"'
    result = result..'\r\n!Item!!Qty!!Price'
    result = result..'!!colspan="2"|Chance'
    --Sort the loot table by weight in descending order
    table.sort(chest.dropTable, function(a, b) return a[2] > b[2] end)
    for i, row in pairs(chest.dropTable) do
      local thisItem = Items.getItemByID(row[1])
      local qty = 1
      if chest.dropQty ~= nil then qty = chest.dropQty[i] end
      result = result..'\r\n|-\r\n|'..Icons.Icon({thisItem.name, type='item'})
      result = result..'||style="text-align:right" data-sort-value="'..qty..'"|'
      if qty > 1 then
        result = result.. '1 - '
      end
      result = result..Shared.formatnum(qty)
      local dropChance = (row[2] / totalWt) * 100
      result = result..'||style="text-align:right" data-sort-value="'..row[2]..'"'
      result = result..'|'..Shared.fraction(row[2], totalWt)
      result = result..'||style="text-align:right"|'..Shared.round(dropChance, 2, 2)..'%'
      result = result..'||style="text-align:right" data-sort-value="'..thisItem.sellsFor..'"'
      result = result..'|'..Icons.GP(thisItem.sellsFor)
     end
     end
     result = result..'\r\n|}'
     result = result..'\r\n|}'