Anonymous

Module:Skills: Difference between revisions

From Melvor Idle
_getLootTableValue: Added for average loot table sale value; _formatLootTable: Support output in table form; _getThievingNPCStat: Add lootValue, pickpocketValue, lootList stats
(Added 'no item' drop chance to NPC table)
(_getLootTableValue: Added for average loot table sale value; _formatLootTable: Support output in table form; _getThievingNPCStat: Add lootValue, pickpocketValue, lootList stats)
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'], 0.75)
     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, chanceMultIn)
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 114: Line 154:
   -- 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
   -- [4/16/21]: Adding info for no drop
   -- [4/16/21]: Adding info for no drop
   local maxDropLen = math.max(string.len(Shared.round(25, 2, 2)), string.len(Shared.round(lootTable[1][2] / totalWeight * chanceMult, 2, 2)))
   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 = {}
   table.insert(returnPart, '* ' .. string.rep(' ', math.max(0, (maxDropLen - string.len(Shared.round(25, 2, 2))) * 2)) .. '25.00% No Item')
   -- 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!!colspan="2"|Chance!!Price')
  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 * chanceMult, 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
     else
      itemText = Icons.Icon({item.name, type='item'})
      table.insert(returnPart, '|-\r\n|' .. itemText)
      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 .. '%')
      table.insert(returnPart, '|style="text-align:right;" data-sort-value="' .. sellsFor .. '"|' .. Icons.GP(sellsFor))
     end
     end
     table.insert(returnPart, '* ' .. string.rep('&nbsp;', math.max(0, (maxDropLen - string.len(dropChance)) * 2)) .. dropChance .. '% ' .. itemText)
  end
  if not asList then
     table.insert(returnPart, '|}')
   end
   end


Line 138: Line 194:
   table.insert(returnPart, '|- class="headerRow-0"\r\n!Target!!Name!!' .. Icons.Icon({'Thieving', type='skill', notext=true}).. ' Level!!Experience!!Max Hit!!Max Coins')
   table.insert(returnPart, '|- class="headerRow-0"\r\n!Target!!Name!!' .. Icons.Icon({'Thieving', type='skill', notext=true}).. ' Level!!Experience!!Max Hit!!Max Coins')
    
    
  local linkOverrides = { ['Golbin'] = 'Golbin (thieving)' }
   -- Create row for each NPC
   -- Create row for each NPC
   for i, npc in Shared.skpairs(SkillData.Thieving) do
   for i, npc in Shared.skpairs(SkillData.Thieving) do
    local linkText = npc.name
    if linkOverrides[npc.name] ~= nil then
      linkText = linkOverrides[npc.name] .. '|' .. npc.name
    end
     table.insert(returnPart, '|-\r\n|style="text-align: left;" |' .. Icons.Icon({npc.name, type='thieving', size=50, notext=true}))
     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: left;" |[[' .. p._getThievingNPCStat(npc, 'pageName') .. ']]')
     table.insert(returnPart, '|style="text-align: right;" |' .. p._getThievingNPCStat(npc, 'level'))
     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, 'xp'))
Line 166: Line 217:
    
    
   local npcList = {}
   local npcList = {}
  local linkOverrides = { ['Golbin'] = 'Golbin (thieving)' }
   -- Create row for each NPC
   -- Create row for each NPC
   for i, npc in Shared.skpairs(SkillData.Thieving) do
   for i, npc in Shared.skpairs(SkillData.Thieving) do
    local linkText = npc.name
     table.insert(npcList, Icons.Icon({npc.name, type='thieving', notext=true}) .. ' [[' .. p._getThievingNPCStat(npc, 'pageName') .. ']]')
    if linkOverrides[npc.name] ~= nil then
      linkText = linkOverrides[npc.name] .. '|' .. npc.name
    end
     table.insert(npcList, Icons.Icon({npc.name, type='thieving', notext=true}) .. ' [[' .. linkText .. ']]')
   end
   end
   table.insert(returnPart, table.concat(npcList, ' • '))
   table.insert(returnPart, table.concat(npcList, ' • '))