Anonymous

Module:Skills: Difference between revisions

From Melvor Idle
Added some Thieving NPC related functions
(Added linking of firemaking logs to getFiremakingTable)
(Added some Thieving NPC related functions)
Line 35: Line 35:
   end
   end
   return nil
   return nil
end
function p.getThievingNPCByID(ID)
  local result = Shared.clone(SkillData.Thieving[ID + 1])
  if result ~= nil then
    result.id = ID
  end
  return result
end
function p.getThievingNPC(name)
  local result = nil
  for i, npc in pairs(SkillData.Thieving) do
    if name == npc.name then
      result = Shared.clone(npc)
      result.id = i - 1
      break
    end
  end
  return result
end
function p.getThievingNPCStat(frame)
  local args = frame.args ~= nil and frame.args or frame
  local npcName = args[1]
  local statName = args[2]
  local npc = p.getThievingNPC(npcName)
  if npc == nil then
    return 'ERROR: Failed to find Thieving NPC with name ' .. name .. '[[Category:Pages with script errors]]'
  end
 
  return p._getThievingNPCStat(npc, statName)
end
function p._getThievingNPCStat(npc, stat)
  local result = npc[stat]
  -- Overrides below
  if stat == 'maxHit' then
    result = result * 10
  elseif stat == 'lootTable' then
    return p._formatLootTable(npc['lootTable'])
  elseif stat == 'requirements' then
  if npc['level'] ~= nil then
    result = Icons._SkillReq('Thieving', npc['level'], true)
  else
    result = 'None'
  end
  end
  return result
end
function p._formatLootTable(lootTableIn)
  -- Expects lootTableIn to be in format {{itemID_1, itemWeight_1}, ..., {itemID_n, itemWeight_n}}
  if Shared.tableCount(lootTableIn) == 0 then
    return ''
  end
  local lootTable = Shared.clone(lootTableIn)
  -- Sort table from most to least common drop
  table.sort(lootTable, function(a, b)
                          return a[2] > b[2]
                        end)
  local totalWeight = 0
  for i, drop in pairs(lootTable) do
    totalWeight = totalWeight + drop[2]
  end
  if totalWeight == 0 then
    return ''
  end
  -- 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))
  local returnPart = {}
  for i, drop in pairs(lootTable) do
    local item, itemText, dropChance = Items.getItemByID(drop[1]), nil, Shared.round(drop[2] / totalWeight * 100, 2, 2)
    if item == nil then
      itemText = 'Unknown'
    else
      itemText = Icons.Icon({item.name, type='item'})
    end
    table.insert(returnPart, '* ' .. string.rep(' ', math.max(0, (maxDropLen - string.len(dropChance)) * 2)) .. dropChance .. '% ' .. itemText)
  end
  return table.concat(returnPart, '\n\r')
end
end