Anonymous

Module:Skills: Difference between revisions

From Melvor Idle
_formatLootTable: Re-order columns for table output & include total row
m (_formatLootTable: Apply secondary sort on item ID in addition to primary sort on drop chance)
(_formatLootTable: Re-order columns for table output & include total row)
(6 intermediate revisions by 2 users not shown)
Line 70: Line 70:


function p._getThievingNPCStat(npc, stat)
function p._getThievingNPCStat(npc, stat)
  local itemDropChance = 0.75
   local result = npc[stat]
   local result = npc[stat]
   -- Overrides below
   -- Overrides below
   if stat == 'maxHit' then
   if stat == 'maxHit' then
     result = result * 10
     result = result * 10
  elseif stat == 'lootList' then
    return p._formatLootTable(npc['lootTable'], itemDropChance, true)
   elseif stat == 'lootTable' then
   elseif stat == 'lootTable' then
     return p._formatLootTable(npc['lootTable'])
     return p._formatLootTable(npc['lootTable'], itemDropChance, false)
   elseif stat == 'requirements' then
   elseif stat == 'requirements' then
  if npc['level'] ~= nil then
    if npc['level'] ~= nil then
    result = Icons._SkillReq('Thieving', npc['level'], true)
      result = Icons._SkillReq('Thieving', npc['level'], true)
  else
    else
    result = 'None'
      result = 'None'
  end
    end
  elseif (stat == 'lootValue' or stat == 'pickpocketValue') then
    if stat == 'pickpocketValue' then
      local itemBP = Items.getItem("Bobby's Pocket")
      result = (1 + npc['maxCoins']) / 2 + itemBP.sellsFor * (1 / 120)
    else
      result = 0
    end
    result = Shared.round(result + p._getLootTableValue(npc['lootTable']) * itemDropChance, 2, 2)
  elseif stat == 'pageName' then
    local linkOverrides = { ['Golbin'] = 'Golbin (thieving)' }
    result = (linkOverrides[npc['name']] ~= nil and linkOverrides[npc['name']]) or npc['name']
   end
   end


Line 87: Line 101:
end
end


function p._formatLootTable(lootTableIn)
function p._getLootTableValue(lootTable)
  -- Calculates the average GP value of a given loot table
  -- Expects lootTableIn to be in format {{itemID_1, itemWeight_1}, ..., {itemID_n, itemWeight_n}}
  if Shared.tableCount(lootTable) == 0 then
    return 0
  end
 
  local totalWeight = 0
  for i, drop in pairs(lootTable) do
    totalWeight = totalWeight + drop[2]
  end
  if totalWeight == 0 then
    return 0
  end
 
  local avgValue = 0
  for i, drop in pairs(lootTable) do
    local item = Items.getItemByID(drop[1])
    if item ~= nil then
      avgValue = avgValue + item.sellsFor * (drop[2] / totalWeight)
    end
  end
 
  return avgValue
end
 
function p._formatLootTable(lootTableIn, chanceMultIn, asList)
   -- Expects lootTableIn to be in format {{itemID_1, itemWeight_1}, ..., {itemID_n, itemWeight_n}}
   -- Expects lootTableIn to be in format {{itemID_1, itemWeight_1}, ..., {itemID_n, itemWeight_n}}
   if Shared.tableCount(lootTableIn) == 0 then
   if Shared.tableCount(lootTableIn) == 0 then
Line 93: Line 133:
   end
   end


  local chanceMult = (chanceMultIn or 1) * 100
   local lootTable = Shared.clone(lootTableIn)
   local lootTable = Shared.clone(lootTableIn)
   -- Sort table from most to least common drop
   -- Sort table from most to least common drop
Line 112: Line 153:


   -- Get the length (in characters) of the largest drop chance so that they can be right aligned
   -- Get the length (in characters) of the largest drop chance so that they can be right aligned
   local maxDropLen = string.len(Shared.round(lootTable[1][2] / totalWeight * 100, 2, 2))
  -- [4/16/21]: Adding info for no drop
   local maxDropLen = math.max(string.len(Shared.round(100 - chanceMult, 2, 2)), string.len(Shared.round(lootTable[1][2] / totalWeight * chanceMult, 2, 2)))
 
   local returnPart = {}
   local returnPart = {}
  -- Generate header
  if asList then
    if chanceMult < 100 then
      table.insert(returnPart, '* ' .. string.rep('&nbsp;', math.max(0, (maxDropLen - string.len(Shared.round(100 - chanceMult, 2, 2))) * 2)) .. Shared.round(100 - chanceMult, 2, 2) .. '% No Item')
    end
  else
    table.insert(returnPart, '{|class="wikitable sortable"\r\n!Item!!Price!!colspan="2"|Chance')
  end
  -- Generate row for each item
   for i, drop in pairs(lootTable) do
   for i, drop in pairs(lootTable) do
     local item, itemText, dropChance = Items.getItemByID(drop[1]), nil, Shared.round(drop[2] / totalWeight * 100, 2, 2)
     local item, itemText, sellsFor, dropChance = Items.getItemByID(drop[1]), 'Unknown', 0, Shared.round(drop[2] / totalWeight * chanceMult, 2, 2)
     if item == nil then
     if item ~= nil then
       itemText = 'Unknown'
       itemText, sellsFor = Icons.Icon({item.name, type='item'}), item.sellsFor
    end
    if asList then
      table.insert(returnPart, '* ' .. string.rep('&nbsp;', math.max(0, (maxDropLen - string.len(dropChance)) * 2)) .. dropChance .. '% ' .. itemText)
    else
      table.insert(returnPart, '|-\r\n|' .. itemText)
      table.insert(returnPart, '|style="text-align:right;" data-sort-value="' .. sellsFor .. '"|' .. Icons.GP(sellsFor))
      table.insert(returnPart, '|style="text-align:right;" data-sort-value="' .. dropChance .. '"|' .. Shared.fraction(drop[2] * chanceMult, totalWeight * 100))
      table.insert(returnPart, '|style="text-align:right;"|' .. dropChance .. '%')
    end
  end
  if not asList then
    table.insert(returnPart, '|-class="sortbottom" \r\n!colspan="2"|Total:')
    local textTotChance = ''
    if chanceMult < 100 then
      textTotChance = '|style="text-align:right"|' .. Shared.fraction(chanceMult, 100) .. '\r\n|'
     else
     else
      itemText = Icons.Icon({item.name, type='item'})
      textTotChance = '|colspan="2" '
     end
     end
     table.insert(returnPart, '* ' .. string.rep('&nbsp;', math.max(0, (maxDropLen - string.len(dropChance)) * 2)) .. dropChance .. '% ' .. itemText)
    textTotChance = textTotChance .. 'style="text-align:right;"|' .. Shared.round(chanceMult, 2, 2) .. '%' .. '\r\n|}'
     table.insert(returnPart, textTotChance)
  end
 
  return table.concat(returnPart, '\r\n')
end
 
function p.getThievingNPCTable()
  local returnPart = {}
 
  -- Create table header
  table.insert(returnPart, '{| class="wikitable sortable stickyHeader"')
  table.insert(returnPart, '|- class="headerRow-0"\r\n!Target!!Name!!' .. Icons.Icon({'Thieving', type='skill', notext=true}).. ' Level!!Experience!!Max Hit!!Max Coins!!<abbr title="Assumes all loot is sold, and no GP boosts apply (such as those from Mastery & Gloves of Silence)">GP/Theft</abbr>')
 
  -- Create row for each NPC
  for i, npc in Shared.skpairs(SkillData.Thieving) do
    local linkText = (npc.name ~= p._getThievingNPCStat(npc, 'pageName') and p._getThievingNPCStat(npc, 'pageName') .. '|' .. npc.name) or npc.name
    table.insert(returnPart, '|-\r\n|style="text-align: left;" |' .. Icons.Icon({npc.name, type='thieving', size=50, notext=true}))
    table.insert(returnPart, '|style="text-align: left;" |[[' .. linkText .. ']]')
    table.insert(returnPart, '|style="text-align: right;" |' .. p._getThievingNPCStat(npc, 'level'))
    table.insert(returnPart, '|style="text-align: right;" |' .. p._getThievingNPCStat(npc, 'xp'))
    table.insert(returnPart, '|style="text-align: right;" |' .. p._getThievingNPCStat(npc, 'maxHit'))
    table.insert(returnPart, '|style="text-align: right;" data-sort-value="' .. p._getThievingNPCStat(npc, 'maxCoins') .. '" |' .. Icons.GP(p._getThievingNPCStat(npc, 'maxCoins')))
    table.insert(returnPart, '|style="text-align: right;" data-sort-value="' .. p._getThievingNPCStat(npc, 'pickpocketValue') .. '" |' .. Icons.GP(p._getThievingNPCStat(npc, 'pickpocketValue')))
  end
  table.insert(returnPart, '|}')
 
  return table.concat(returnPart, '\r\n')
end
 
function p.getThievingNavbox()
  local returnPart = {}
 
  -- Create table header
  table.insert(returnPart, '{| class="wikitable" style="text-align:center; clear:both; margin:auto; margin-bottom:1em;"')
  table.insert(returnPart, '|-\r\n!' .. Icons.Icon({'Thieving', type='skill', notext=true}) .. '[[Thieving|Thieving Targets]]')
  table.insert(returnPart, '|-\r\n|')
 
  local npcList = {}
  -- Create row for each NPC
  for i, npc in Shared.skpairs(SkillData.Thieving) do
    local linkText = (npc.name ~= p._getThievingNPCStat(npc, 'pageName') and p._getThievingNPCStat(npc, 'pageName') .. '|' .. npc.name) or npc.name
    table.insert(npcList, Icons.Icon({npc.name, type='thieving', notext=true}) .. ' [[' .. linkText .. ']]')
   end
   end
  table.insert(returnPart, table.concat(npcList, ' • '))
  table.insert(returnPart, '|}')


   return table.concat(returnPart, '\r\n')
   return table.concat(returnPart, '\r\n')