Module:Skills/Artisan: Difference between revisions

(Added bag crafting)
(Update for v1.3)
 
(45 intermediate revisions by 5 users not shown)
Line 2: Line 2:
--Contains function for skills that consume resources (ie smithing, cooking, herblore, etc.)
--Contains function for skills that consume resources (ie smithing, cooking, herblore, etc.)
local p = {}
local p = {}
local SkillData = mw.loadData('Module:Skills/data')
local ShopData = mw.loadData('Module:Shop/data')


local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local Common = require('Module:Common')
local GameData = require('Module:GameData')
local SkillData = GameData.skillData
local Modifiers = require('Module:Modifiers')
local Skills = require('Module:Skills')
local Items = require('Module:Items')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Shop = require('Module:Shop')
local ItemSourceTables = require('Module:Items/SourceTables')


function p.getCookedItemsTable(frame)
function p.getCookedItemsTable(frame)
local category = frame.args ~= nil and frame.args[1] or frame
local args = frame.args ~= nil and frame.args or frame
local itemArray = nil
local category = args[1]
local realmName = args.realm
if category == "Cooking Fire" then  
local realm = Skills.getRealmFromName(realmName)
itemArray = Items.getItems(function(item) return item.cookingCategory == 0 end)
if realm == nil then
elseif category == "Furnace" then
return Shared.printError('Failed to find a realm with name ' .. (realmName or 'nil'))
itemArray = Items.getItems(function(item) return item.cookingCategory == 1 and item.name ~= 'Lemon Cake' end)
end
elseif category == "Pot"  then  
local skillID = 'Cooking'
itemArray = Items.getItems(function(item) return item.cookingCategory == 2 end)
 
else
local categoryMap = {
itemArray = Items.getItems(function(item) return item.cookingCategory ~= nil and item.name ~= 'Lemon Cake' end)
["Cooking Fire"] = 'melvorD:Fire',
["Furnace"] = 'melvorD:Furnace',
["Pot"] = 'melvorD:Pot'
}
local categoryID = categoryMap[category]
 
-- Find recipes for the relevant categories
-- Note: Excludes Lemon cake
local recipeArray = GameData.getEntities(SkillData.Cooking.recipes,
function(recipe)
return (
(categoryID == nil or recipe.categoryID == categoryID)
and recipe.noMastery == nil
and Skills.getRecipeRealm(recipe) == realm.id
)
end
)
table.sort(recipeArray, function(a, b) return Skills.standardRecipeSort(skillID, a, b) end)
 
-- Logic for generating some cells of the table which are consistent for normal & perfect items
local getHealingCell = function(item, qty)
if item ~= nil then
return 'data-sort-value="'..(math.floor(item.healsFor) * qty)..'"|'..Icons.Icon({"Hitpoints", type="skill", notext=true})..' '..Shared.formatnum(math.floor(item.healsFor * 10))..(qty > 1 and ' (x'..qty..')' or '')
else
return ' '
end
end
local getSaleValueCell = function(item, qty)
if item ~= nil then
return 'data-sort-value="'..math.floor(item.sellsFor * qty)..'"|'..Items.getValueText(item)..(qty > 1 and ' (x'..qty..')' or '')
else
return ' '
end
end
end
table.sort(itemArray, function(a, b) return a.cookingLevel < b.cookingLevel end)
local result = '{| class="wikitable sortable stickyHeader"'
result = result..'\r\n|- class="headerRow-0"'
result = result..'\r\n!colspan="3"|Cooked Item!!'..Icons.Icon({'Cooking', type='skill', notext=true})..' Level'
result = result..'!!XP!!Healing!!Value!!Ingredients'


for i, item in Shared.skpairs(itemArray) do
local resultPart = {}
table.insert(resultPart, '{| class="wikitable sortable stickyHeader"')
table.insert(resultPart, '\r\n|- class="headerRow-0"')
table.insert(resultPart, '\r\n!colspan="3" rowspan="2"|Cooked Item!!rowspan="2"|Requirements')
table.insert(resultPart, '!!rowspan="2"|Cook Time (s)!!rowspan="2"|XP!!rowspan="2"|XP/s!!colspan="2"|Healing!!colspan="2"|Value!!rowspan="2"|Ingredients')
table.insert(resultPart, '\r\n|- class="headerRow-1"')
table.insert(resultPart, '\r\n!Normal!!' .. Icons.Icon({'Perfect', type='bonus', ext='png', notext=true, nolink=true}))
table.insert(resultPart, '!!Normal!!' .. Icons.Icon({'Perfect', type='bonus', ext='png', notext=true, nolink=true}))
 
for i, recipe in ipairs(recipeArray) do
local level = Skills.getRecipeLevel(skillID, recipe)
local baseXP = recipe.baseAbyssalExperience or recipe.baseExperience
local baseInt = recipe.baseInterval / 1000
local reqText = Skills.getRecipeRequirementText(SkillData.Cooking.name, recipe)
local xpRate = baseXP / baseInt
local item = Items.getItemByID(recipe.productID)
local perfectItem = nil
local perfectItem = nil
if item.perfectItem ~= nil then
if recipe.perfectCookID ~= nil then
perfectItem = Items.getItemByID(item.perfectItem)
perfectItem = Items.getItemByID(recipe.perfectCookID)
end
end
local qty = item.cookingQty
local qty = recipe.baseQuantity or 1
if qty == nil then
 
qty = 1
table.insert(resultPart, '\r\n|-')
end
table.insert(resultPart, '\r\n|class="table-img"|'..Icons.Icon({item.name, type='item', notext=true, size='50'}))
result = result..'\r\n|-'
table.insert(resultPart, '\r\n|class="table-img"| ')
result = result..'\r\n|style="min-width:25px"|'..Icons.Icon({item.name, type='item', notext='true', size='50'})
result = result..'\r\n|style="min-width:25px"| '
if perfectItem ~= nil then
if perfectItem ~= nil then
result = result..Icons.Icon({perfectItem.name, type='item', notext='true', size='50'})
table.insert(resultPart, Icons.Icon({perfectItem.name, type='item', notext=true, size='50'}))
end
end
result = result..'||'
table.insert(resultPart, '||')
if qty > 1 then
if qty > 1 then
result = result..qty..'x '
table.insert(resultPart, qty..'x ')
end
end
result = result..'[['..item.name..']]'
table.insert(resultPart, Icons.getExpansionIcon(item.id))
result = result..'||style="text-align:right"|'..item.cookingLevel
table.insert(resultPart, Icons.Icon({item.name, type='item', noicon = true}))
result = result..'||style="text-align:right"|'..item.cookingXP
table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. level .. '"|' .. reqText)
result = result..'||style="text-align:right" data-sort-value="'..item.healsFor..'"|'..Icons.Icon({"Hitpoints", type="skill", notext=true})..' '..(item.healsFor * 10)
table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. baseInt .. '"|' .. Shared.round(baseInt, 2, 0))
result = result..'||data-sort-value="'..(item.sellsFor * qty)..'"|'..Icons.GP(item.sellsFor)
table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. baseXP .. '"|' .. Shared.formatnum(baseXP))
if qty > 1 then
table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. xpRate .. '"|' .. Shared.formatnum(Shared.round(xpRate, 2, 0)))
result = result..' (x'..qty..')'
table.insert(resultPart, '||'..getHealingCell(item, qty)..'||'..getHealingCell(perfectItem, qty))
table.insert(resultPart, '||'..getSaleValueCell(item, qty)..'||'..getSaleValueCell(perfectItem, qty))
local matArray = {}
for j, mat in ipairs(recipe.itemCosts) do
local matItem = Items.getItemByID(mat.id)
if matItem ~= nil then
table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.quantity}))
end
end
end
    local matArray = {}
table.insert(resultPart, '\r\n|'..table.concat(matArray, ' '))
    for j, reqSet in pairs(item.recipeRequirements)  do
    for k, mat in pairs(reqSet) do
local matItem = Items.getItemByID(mat.id)
table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.qty}))
    end
end
    result = result..'\r\n|'..table.concat(matArray, ' ')
end
end


result = result..'\r\n|}'
table.insert(resultPart, '\r\n|}')
return result
return table.concat(resultPart)
end
end


local tierSuffix = { 'I', 'II', 'III', 'IV' }
local tierSuffix = { 'I', 'II', 'III', 'IV' }
function p._getHerblorePotionTable(category)
function p._getPotionDescription(potion)
  if string.upper(category) == 'COMBAT' then
-- TODO: Temporary fix below for incorrect Traps Potion descriptions. To amend
    category = 0
-- once corrected within game data
  elseif string.upper(category) == 'SKILL' then
if potion.customDescription and not Shared.contains(potion.id, 'melvorTotH:Traps_Potion_') then
    category = 1
return potion.customDescription
  elseif type(category) == 'string' then
elseif type(potion.modifiers) == 'table' and not Shared.tableIsEmpty(potion.modifiers) then
    category = tonumber(category)
return Modifiers.getModifiersText(potion.modifiers, false, true)
  end
else
return ''
end
end
 
function p._getHerblorePotionTable(categoryName)
local skillID = 'Herblore'
local category = GameData.getEntityByName(SkillData.Herblore.categories, categoryName)
if category == nil then
local catNames = {}
for i, cat in pairs(SkillData.Herblore.categories) do
table.insert(catNames, cat.name)
end
return Shared.printError('No such category ' .. categoryName .. ', the following are available: ' .. table.concat(catNames, ', '))
end
local categoryID = category.id
 
local potionArray = GameData.getEntities(SkillData.Herblore.recipes,
function(potion)
-- Category implies a realm selection, so no ned to check this separately
return potion.categoryID == categoryID
end
)


  local potionArray = {}
table.sort(potionArray, function(a, b) return Skills.standardRecipeSort(skillID, a, b) end)
  for i, potion in Shared.skpairs(SkillData.Herblore.ItemData) do
    if potion.category == category then
      table.insert(potionArray, potion)
    end
  end


  local result = '{|class = "wikitable sortable stickyHeader"'
local resultPart = {}
  result = result..'\r\n|- class="headerRow-0"'
table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
  result = result..'\r\n!Potion!!'..Icons.Icon({'Herblore', type='skill', notext=true})..' Level'
table.insert(resultPart, '\r\n|- class="headerRow-0"')
  result = result..'!!XP!!Ingredients!!colspan="2"|Tier!!Value!!Charges!!Effect'
table.insert(resultPart, '\r\n!Potion!!Requirements')
table.insert(resultPart, '!!XP!!Ingredients!!style="width:30px;"|Tier!!Value!!Charges!!Effect')


  table.sort(potionArray, function(a, b) return a.herbloreLevel < b.herbloreLevel end)
for i, potion in ipairs(potionArray) do
local level = Skills.getRecipeLevel(skillID, potion)
local baseXP = potion.baseAbyssalExperience or potion.baseExperience
local reqText = Skills.getRecipeRequirementText(SkillData.Herblore.name, potion)
local costText = Common.getCostString({
["items"] = potion.itemCosts,
["currencies"] = potion.currencyCosts
}, 'N/A', nil, ', ')


  for i, potion in Shared.skpairs(potionArray) do
table.insert(resultPart, '\r\n|-')
    local tierPots = {}
local expIcon = Icons.getExpansionIcon(potion.potionIDs[1])
    for j = 1, 4, 1 do
table.insert(resultPart, '\r\n|rowspan="4"|'..expIcon..'[['..potion.name..']]')
      table.insert(tierPots, Items.getItemByID(potion.itemID[j]))
table.insert(resultPart, '||rowspan="4" style="text-align:right" data-sort-value="' .. level .. '"|'..reqText)
    end
table.insert(resultPart, '||rowspan="4" style="text-align:right" data-sort-value="' .. baseXP .. '"|'..Shared.formatnum(baseXP))
    result = result..'\r\n|-'
table.insert(resultPart, '||rowspan="4"|'..costText..'||')
    if potion.name == 'Bird Nests Potion' then
      result = result..'\r\n|rowspan="4"|[[Bird Nest Potion]]'
    else
      result = result..'\r\n|rowspan="4"|[['..potion.name..']]'
    end
    result = result..'||rowspan="4" style="text-align:right"|'..potion.herbloreLevel
    result = result..'||rowspan="4" style="text-align:right"|'..potion.herbloreXP


    local matArray = {}
local tierRows = {}
    for j, mat in Shared.skpairs(tierPots[1].herbloreReq) do
for j, potionID in ipairs(potion.potionIDs) do
      local matItem = Items.getItemByID(mat.id)
local rowTxt = {}
      table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.qty}))
local tierPot = Items.getItemByID(potionID)
    end
table.insert(rowTxt, Icons.Icon({tierPot.name, type='item', notext=true}))
    result = result..'||rowspan="4"|'..table.concat(matArray, ', ')..'||'
table.insert(rowTxt, Icons.Icon({tierPot.name, tierSuffix[j], type = 'item', noicon=true}))
   
table.insert(rowTxt, '||style="text-align:right;" data-sort-value="'..tierPot.sellsFor..'"|'..Items.getValueText(tierPot))
    local tierRows = {}
table.insert(rowTxt, '||style="text-align:right;"|'..tierPot.charges..'|| '..p._getPotionDescription(tierPot))
    for j, tierPot in Shared.skpairs(tierPots) do
table.insert(tierRows, table.concat(rowTxt))
      local rowTxt = Icons.Icon({tierPot.name, type='item', notext=true})
end
      rowTxt = rowTxt..'||[['..tierPot.name..'|'..tierSuffix[j]..']]'
table.insert(resultPart, table.concat(tierRows, '\r\n|-\r\n|'))
      rowTxt = rowTxt..'||style="text-align:right;" data-sort-value="'..tierPot.sellsFor..'"|'..Icons.GP(tierPot.sellsFor)
end
      rowTxt = rowTxt..'||style="text-align:right;"|'..tierPot.potionCharges..'||'..tierPot.description
      table.insert(tierRows, rowTxt)
    end
    result = result..table.concat(tierRows, '\r\n|-\r\n|')
  end


  result = result..'\r\n|}'
table.insert(resultPart, '\r\n|}')
  return result
return table.concat(resultPart)
end
end


function p.getHerblorePotionTable(frame)
function p.getHerblorePotionTable(frame)
  local category = frame.args ~= nil and frame.args[1] or frame
local args = frame.args ~= nil and frame.args or frame
  return p._getHerblorePotionTable(category)
local category = args[1]
return p._getHerblorePotionTable(category)
end
end


function p.getCookingFireTable(frame)
function p._getHerbloreHerbTable(realmID)
  local toolArray = {}
local allHerbs = {}
  for i, upgrade in Shared.skpairs(ShopData.Shop.SkillUpgrades) do
 
    if Shared.contains(upgrade.name, 'Cooking Fire') then
-- Finds the herb from a potion along with the level required to make the potion.
      table.insert(toolArray, upgrade)
local function handlePotion(potion)
    end
local potionCosts = potion.itemCosts
  end
local level = Skills.getRecipeLevel('Herblore', potion)


  local result = '{| class="wikitable"'
if potionCosts == nil or level == nil then
  result = result..'\r\n!colspan="4"| !!colspan="2"|Bonus '..Icons.Icon({'Cooking', type='skill', notext=true})..' XP'
return
  result = result..'\r\n|- class="headerRow-0"'
end
  result = result..'\r\n!colspan="2"|Name!!'..Icons.Icon({'Firemaking', type='skill', notext=true})..' Level'
  result = result..'!!Cost!!This Fire!!Total'


  local total = 0
-- Find if this potion uses a herb, and which herb it is.
  local total2 = 0
for _, ingredient in ipairs(potionCosts) do
local ingredientID = ingredient.id
if ingredientID ~= nil and string.sub(ingredientID, -5) == "_Herb" then
-- Set the lowest level of potion this herb is used in.
local currLevel = allHerbs[ingredientID] or 9999999
if level < currLevel then
allHerbs[ingredientID] = level
end
end
end
end


  for i, tool in Shared.skpairs(toolArray) do
local recipes = GameData.getEntities(SkillData.Herblore.recipes,
    result = result..'\r\n|-'
function(obj)
    result = result..'\r\n|style="min-width:25px" data-sort-value="'..tool.name..'"|'..Icons.Icon({tool.name, type='upgrade', size='50', notext=true})
return Skills.getRecipeRealm(obj) == realmID
    result = result..'||'..tool.name
end
    local level = 1
)
    if tool.unlockRequirements ~= nil and tool.unlockRequirements.skillLevel ~= nil then
for _, potion in ipairs(recipes) do
      --Gonna be lazy and assume there's only the one skill level and it's the one we care about
handlePotion(potion)
      level = tool.unlockRequirements.skillLevel[1][2]
end
    end
    result = result..'||style="text-align:right"|'..level
local sortedValues = Shared.sortDictionary(
    result = result..'||style="text-align:right" data-sort-value="'..tool.cost.gp..'"|'..Shop.getCostString(tool.cost)
allHerbs,
function (a, b) return a.value < b.value end)


     local bonusXP = tool.contains.modifiers.increasedSkillXP[1][2]
local tbl = mw.html.create("table")
    total = total + bonusXP
        :addClass("wikitable sortable stickyHeader")
       
    -- Add header
     tbl :tag("tr"):addClass("headerRow-0")
    :tag("th"):wikitext(Icons.Icon({'Herblore', type='skill', notext=true})..' Level')
    :tag("th"):wikitext("Herb")
    :tag("th"):wikitext("Value")
    :tag("th"):wikitext("Herb Sources")
    :done()
   
    -- Fill wikitable.
    for _, v in pairs(sortedValues) do
    local herbItem = Items.getItemByID(v['key'])
    local herbLevel = v['value']
    local dlcIcon = Icons.getExpansionIcon(herbItem.id)


    result = result..'||style="text-align:right"|+'..bonusXP..'%'
    -- Add rows
    result = result..'||style="text-align:right"|+'..total..'%'
    tbl :tag("tr")
  end
    :tag("td"):wikitext(herbLevel)
    :tag("td"):wikitext(dlcIcon .. Icons.Icon({herbItem.name, type='item'}))
    :tag("td"):wikitext(Items.getValueText(herbItem))
    :tag('td'):wikitext(ItemSourceTables._getItemSources(herbItem, false, nil, ' '))
    :done()
    end


  result = result..'\r\n|}'
return tostring(tbl)
  return result
end
end


function p.getRunecraftingTable(frame)
function p.getHerbloreHerbTable(frame)
  local category = frame.args ~= nil and frame.args[1] or frame
local args = frame:getParent().args
  local data = nil
local realmName = args.realm
local realm = Skills.getRealmFromName(realmName)
if realm == nil then
return Shared.printError('Failed to find a realm with name ' .. (realmName or 'nil'))
end
return p._getHerbloreHerbTable(realm.id)
end


  if    category == "Runes"      then data =
function p.getPotionTable(frame)
        Items.getItems(function(item) return item.runecraftingCategory == 0 end)
local potionName = frame.args ~= nil and frame.args[1] or frame
  elseif category == "ComboRunes" then data =
        Items.getItems(function(item) return item.runecraftingCategory == 1 end)
  elseif category == "Weapons"    then data =  
        Items.getItems(function(item) return item.runecraftingCategory == 2 and item.runecraftingID end)
  elseif category == "AirGear"    then data =
        Items.getItems(function(item) return item.runecraftingCategory == 3 end)
  elseif category == "WaterGear"  then data =
        Items.getItems(function(item) return item.runecraftingCategory == 4 end)
  elseif category == "EarthGear"  then data =
        Items.getItems(function(item) return item.runecraftingCategory == 5 end)
  elseif category == "FireGear"  then data =
        Items.getItems(function(item) return item.runecraftingCategory == 6 end)
  end


  if data == nil then
local recipe = GameData.getEntityByName(SkillData.Herblore.recipes, potionName)
    return "ERROR: Invalid Runecrafting category name.[[Category:Pages with script errors]]"
if recipe == nil then
  end
return Shared.printError('No potion named "' .. potionName .. '" was found')
end


  local result = '{| class="wikitable sortable stickyHeader"'
local resultPart = {}
  result = result..'\r\n|- class="headerRow-0"'
table.insert(resultPart, '{| class="wikitable"')
  result = result..'\r\n!Item\r\n!Name\r\n!Runecrafting Level\r\n!Experience'
table.insert(resultPart, '\r\n!colspan=4|[['..potionName..']]')
  result = result..'\r\n!Item Price\r\n!Ingredients\r\n!XP/s\r\n!GP/s'
table.insert(resultPart, '\r\n|-\r\n!Potion!!Tier!!Charges!!Effect')


  table.sort(data, function(a, b) return (a.runecraftingLevel == b.runecraftingLevel and a.id < b.id)  
for i, potionID in ipairs(recipe.potionIDs) do
                                      or a.runecraftingLevel <  b.runecraftingLevel end)
local tier = tierSuffix[i]
local potion = Items.getItemByID(potionID)
if potion ~= nil then
table.insert(resultPart, '\r\n|-')
table.insert(resultPart, '\r\n| ' .. Icons.Icon({potion.name, type='item', notext=true, size='60'}))
table.insert(resultPart, '|| ' .. Icons.getExpansionIcon(potion.id) .. Icons.Icon({potion.name, tier, type='item', noicon=true}))
table.insert(resultPart, '|| ' .. potion.charges .. '|| ' .. p._getPotionDescription(potion))
end
end


  for i, rune in Shared.skpairs(data) do
table.insert(resultPart, '\r\n|}')
    result = result..'\r\n|-'
return table.concat(resultPart)
    result = result..'\r\n| style="text-align: left;" | '..Icons.Icon({rune.name, type='item', size='50', notext=true})
end
    result = result..'\r\n| style ="text-align: left;" |[['..rune.name..']]'
    result = result..'\r\n| style="text-align:right"|'..rune.runecraftingLevel
    result = result..'\r\n| style="text-align:right"|'..rune.runecraftingXP
    result = result..'\r\n| style="text-align:right"|'..rune.sellsFor


    local matArray = {}
function p.getRunecraftingTable(frame)
    for j, mat in Shared.skpairs(rune.runecraftReq) do
local category = frame.args ~= nil and frame.args[1] or frame
      local matItem = Items.getItemByID(mat.id)
return p._getRecipeTable('Runecrafting', category, {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients', 'SkillXPSec', 'CurrencySec'})
      table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.qty}))
    end
    result = result..'\r\n|'..table.concat(matArray, ' ')
 
    local rcCraftTime = 2.00
    local xps = rune.runecraftingXP / rcCraftTime
    local gps = rune.sellsFor / rcCraftTime
    result = result..'\r\n| style="text-align:right"|'..string.format("%.2f", xps)
    result = result..'\r\n| style="text-align:right"|'..string.format("%.2f", gps)
  end
 
  result = result..'\r\n|}'
  return result
end
end


function p.getFletchingTable(frame)
function p.getFletchingTable(frame)
  local category = frame.args ~= nil and frame.args[1] or frame
local category = frame.args ~= nil and frame.args[1] or frame
  local data = nil
return p._getRecipeTable('Fletching', category, {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients'})
end


  if    category == "Arrows"    then
function p.getCraftingTable(frame)
  data = Items.getItems(function(item) return item.fletchingCategory == 0 end)
local category = frame.args ~= nil and frame.args[1] or frame
  elseif category == "Shortbows" then
local columns = {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients'}
  data = Items.getItems(function(item) return item.fletchingCategory == 1 end)
if category == 'Rings' or category == 'Necklaces' then
  elseif category == "Longbows"  then
table.insert(columns, "Description")
  data = Items.getItems(function(item) return item.fletchingCategory == 2 end)
end
  elseif category == "Bolts"    then  
return p._getRecipeTable('Crafting', category, columns)
  data =  Items.getItems(function(item) return item.fletchingCategory == 3 end)
end
  elseif category == "Crossbows" then
  data = Items.getItems(function(item) return item.fletchingCategory == 4 end)
  elseif category == "Javelins"  then
  data = Items.getItems(function(item) return item.fletchingCategory == 5 end)
  end


  if data == nil then
function p.getSmithingTable(frame)
    return "ERROR: Invalid Fletching category name.[[Category:Pages with script errors]]"
local category = frame.args ~= nil and frame.args[1] or frame
  end
local columns = {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients'}
if category ~= 'Bars' and category ~= 'Abyssal Bars' then
table.insert(columns, 'CurrencyBar')
end
return p._getRecipeTable('Smithing', category, columns)
end


  local result = '{| class="wikitable sortable stickyHeader"'
-- Given a skill name, category, and column list, produces a table of recipes for that
  result = result..'\r\n|- class="headerRow-0"'
-- skill & category combination. If categoryName is '', all recipes are returned.
  result = result..'\r\n!Item\r\n!Name\r\n!Fletching Level\r\n!Experience'
-- Note: This only supports a number of skills with consistent recipe data structures, being:
  result = result..'\r\n!Quantity\r\n!Sells For\r\n!Ingredients'
-- Fletching, Crafting, and Runecrafting
-- Valid column list options are: Item, SkillLevel, SkillXP, Currency, Ingredients, SkillXPSec, CurrencySec, CurrencyBar
function p._getRecipeTable(skillName, categoryName, columnList)
-- Validation: Parameters
if type(skillName) ~= 'string' then
return Shared.printError('skillName must be a string')
elseif not Shared.contains({'string', 'nil'}, type(categoryName)) then
return Shared.printError('category must be a string or nil')
elseif type(columnList) ~= 'table' or Shared.tableIsEmpty(columnList) then
return Shared.printError('columnList must be a table with 1+ elements')
end


  table.sort(data, function(a, b) return (a.fletchingLevel == b.fletchingLevel and a.id < b.id)  
local supportedSkills = {
                                      or a.fletchingLevel <  b.fletchingLevel end)
'Smithing',
'Crafting',
'Fletching',
'Runecrafting'
}
if not Shared.contains(supportedSkills, skillName) then
return Shared.printError('The ' .. skillName .. ' skill is not supported by this function')
end


  for i, fletch in Shared.skpairs(data) do
-- Validation: Category
    result = result..'\r\n|-'
local category = GameData.getEntityByName(SkillData[skillName].categories, categoryName)
    result = result..'\r\n| style="text-align: left;" | '..Icons.Icon({fletch.name, type='item', size='50', notext=true})
if category == nil then
    result = result..'\r\n| style ="text-align: left;" |[['..fletch.name..']]'
local catNames = {}
    result = result..'\r\n| style="text-align:right"|'..fletch.fletchingLevel
for i, cat in pairs(SkillData[skillName].categories) do
    result = result..'\r\n| style="text-align:right"|'..fletch.fletchingXP
table.insert(catNames, cat.name)
    result = result..'\r\n| style="text-align:right"|'..fletch.fletchQty
end
    result = result..'\r\n| style="text-align:right"|'..fletch.sellsFor
return Shared.printError('No such category ' .. categoryName .. ' for skill ' .. skillName .. ', the following are available: ' .. table.concat(catNames, ', '))
end
local actionInterval = SkillData[skillName].baseInterval / 1000


    local matArray = {}
-- Validation: Skill data
    for j, mat in Shared.skpairs(fletch.fletchReq) do
local recipeKey = 'recipes'
      local matItem = Items.getItemByID(mat.id)
if SkillData[skillName] == nil then
      table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.qty}))
return Shared.printError('Could not locate skill data for ' .. skillName)
    end
elseif SkillData[skillName][recipeKey] == nil then
    result = result..'\r\n|'..table.concat(matArray, ' ')
return Shared.printError('Could not locate recipe data for ' .. skillName)
  end
end


  result = result..'\r\n|}'
-- Validation: Column list
  return result
local columnDef = {
end
["ItemImage"] = {["header"] = 'Item', ["altRepeat"] = false},
["ItemName"] = {["header"] = 'Name', ["altRepeat"] = true},
["SkillLevel"] = {["header"] = Icons.Icon({skillName, type='skill', notext=true}) .. ' Level', ["altRepeat"] = false},
["SkillXP"] = {["header"] = 'XP', ["altRepeat"] = false},
["Currency"] = {["header"] = 'Value', ["altRepeat"] = true},
["Ingredients"] = {["header"] = 'Ingredients', ["altRepeat"] = true},
["SkillXPSec"] = {["header"] = 'XP/s', ["altRepeat"] = false},
["CurrencySec"] = {["header"] = 'Value/s', ["altRepeat"] = true},
["CurrencyBar"] = {["header"] = 'Value/Bar', ["altRepeat"] = true },
["Description"] = {["header"] = "Description", ["altRepeat"] = true}
}
-- Build the table header while we're here
local resultPart, barIDList = {}, {}
table.insert(resultPart, '{| class="wikitable sortable stickyHeader"\r\n|- class="headerRow-0"')
for i, colID in ipairs(columnList) do
if columnDef[colID] == nil then
return Shared.printError('Invalid column ' .. colID .. ' requested')
else
table.insert(resultPart, '\r\n! ' .. columnDef[colID].header)
if colID == 'CurrencyBar' then
-- For Smithing, a currency value per bar column is included. If this
-- is requested, then obtain a list of bar item IDs
barIDList = p.getBarItemIDs()
end
end
end


function p.getCraftingTable(frame)
-- Determine which recipes to include
  local category = frame.args ~= nil and frame.args[1] or frame
local recipeList = GameData.getEntities(SkillData[skillName][recipeKey],
  local data = nil
function(recipe)
return category.id == nil or recipe.categoryID == category.id
end)
if Shared.tableIsEmpty(recipeList) then
return ''
end
table.sort(recipeList, function(a, b) return a.level < b.level end)


  if    category == "Leather"    then data =
-- Build rows based on recipes
        Items.getItems(function(item) return item.tier == "Leather" or item.tier == "Hard Leather" end)
for i, recipe in ipairs(recipeList) do
  elseif category == "Dragonhide" then data =  
local ns, _ = GameData.getLocalID(recipe.id)
        Items.getItems(function(item) return item.tier == "Dragonhide" and item.craftingLevel ~= nil end)
local item = Items.getItemByID(recipe.productID)
  elseif category == "Rings"     then data =  
local level = Skills.getRecipeLevel(skillName, recipe)
        Items.getItems(function(item) return item.type == "Ring" and item.craftingLevel ~= nil end)
local baseXP = recipe.baseAbyssalExperience or recipe.baseExperience
  elseif category == "Necklaces"  then data =
if item ~= nil then
        Items.getItems(function(item) return item.type == "Amulet" and item.craftingLevel ~= nil end)
-- Some recipes have alternative costs, so the recipe may require multiple rows
  elseif category == "Bags"   then data =
local costList = nil
  Items.getItems(function(item) return item.type == 'Bag' and item.craftingLevel ~= nil end)
if recipe.alternativeCosts ~= nil and not Shared.tableIsEmpty(recipe.alternativeCosts) then
  end
costList = recipe.alternativeCosts
else
costList = {{["itemCosts"] = recipe.itemCosts}}
end
local costCount = Shared.tableCount(costList)
-- Build one row per element within costList
for recipeRow, costDef in ipairs(costList) do
local rowspanStr = (recipeRow == 1 and costCount > 1 and 'rowspan="' .. costCount .. '" ') or ''
local qty = (costDef.quantityMultiplier or 1) * (recipe.baseQuantity or 1)
table.insert(resultPart, '\n|-')
for j, colID in ipairs(columnList) do
local altRepeat = columnDef[colID].altRepeat
-- All columns must be generated if this is the very first row for a recipe,
-- for subsequent rows only columns marked as altRepeat = true are generated
if recipeRow == 1 or altRepeat then
local spanStr = (not altRepeat and rowspanStr) or ''
if colID == 'ItemImage' then
table.insert(resultPart, '\n|' .. spanStr .. 'class="table-img"| ' .. Icons.Icon({item.name, type='item', size='50', notext=true}))
elseif colID == "ItemName" then
local namePrefix = spanStr
if qty > 1 then
namePrefix = namePrefix .. 'data-sort-value="' .. item.name .. '"'
end
table.insert(resultPart, '\n|'.. (namePrefix ~= '' and namePrefix .. '| ' or ' ') .. Icons.getExpansionIcon(item.id) .. (qty > 1 and '<b>' .. qty .. 'x</b> ' or '') .. Icons.Icon({item.name, type='item', noicon=true}))
elseif colID == 'SkillLevel' then
table.insert(resultPart, '\n|' .. spanStr .. 'style="text-align:right"| ' .. level)
elseif colID == 'SkillXP' then
table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. baseXP ..'" style="text-align:right"| ' .. Shared.formatnum(baseXP))
elseif colID == 'Currency' then
local val = math.floor(item.sellsFor)
table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. (val * qty) .. '"| ' .. Items.getValueText(item) .. (qty > 1 and ' (x' .. qty .. ')' or ''))
elseif colID == 'Ingredients' then
local matArray = {}
local currCost = { ["items"] = {}, ["currencies"] = recipe.currencyCosts }
local currText = Common.getCostString(currCost, nil, nil, ', ')
if currText ~= nil then
table.insert(matArray, currText)
end


  if data == nil then
for k, mat in ipairs(costDef.itemCosts) do
    return "ERROR: Invalid Crafting category name.[[Category:Pages with script errors]]"
local matItem = Items.getItemByID(mat.id)
  end
if matItem ~= nil then
table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.quantity}))
end
end
table.insert(resultPart, '\n|' .. (spanStr ~= '' and spanStr .. '| ' or ' ') .. table.concat(matArray, ', '))
elseif colID == 'SkillXPSec' then
table.insert(resultPart, '\n|' .. spanStr .. 'style="text-align:right"| ' .. string.format('%.2f', recipe.baseExperience / actionInterval))
elseif colID == 'CurrencySec' then
local saleCurrency = item.sellsForCurrency or 'melvorD:GP'
local val = math.floor(item.sellsFor) * qty / actionInterval
table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. val .. '"| ' .. Icons._Currency(saleCurrency, string.format('%.2f', val)))
elseif colID == 'CurrencyBar' then
local barQty = 0
for k, mat in ipairs(costDef.itemCosts) do
if Shared.contains(barIDList, mat.id) then
barQty = barQty + mat.quantity
end
end
if barQty > 0 then
local saleCurrency = item.sellsForCurrency or 'melvorD:GP'
local barVal = Shared.round(math.floor(item.sellsFor) * qty / barQty, 1, 1)
table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. barVal .. '"| ' .. Icons._Currency(saleCurrency, barVal))
else
table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="0" class="table-na"| N/A')
end
elseif colID == 'Description' then
local descrip = Items._getItemStat(item, 'description')
if descrip == 'No Description' and item.modifiers ~= nil and not Shared.tableIsEmpty(item.modifiers) then
descrip = Modifiers.getModifiersText(item.modifiers, false)
end
table.insert(resultPart, '\n| '..spanStr..'|'..descrip)
else
table.insert(resultPart, '\n| ')
end
end
end
end
end
end
table.insert(resultPart, '\n|}')
return table.concat(resultPart)
end


  table.sort(data, function(a, b) return (a.craftingLevel == b.craftingLevel and a.id < b.id)  
function p.getBarItemIDs()
                                      or a.craftingLevel <  b.craftingLevel end)
local barCategories = {
 
['melvorD:Bars'] = true,
  local result = '{| class="wikitable sortable stickyHeader"'
['melvorItA:AbyssalBars'] = true
  result = result..'\r\n|- class="headerRow-0"'
}
  result = result..'\r\n!Item\r\n!Name\r\n!Crafting Level\r\n!Experience'
local barIDList = {}
  result = result..'\r\n!Item Price\r\n!Ingredients'
for i, recipe in ipairs(SkillData.Smithing.recipes) do
 
if barCategories[recipe.categoryID] then
  for i, craft in Shared.skpairs(data) do
table.insert(barIDList, recipe.productID)
    result = result..'\r\n|-'
end
    result = result..'\r\n| style="text-align: left;" | '..Icons.Icon({craft.name, type='item', size='50', notext=true})
end
    result = result..'\r\n| style ="text-align: left;" |[['..craft.name..']]'
return barIDList
    result = result..'\r\n| style="text-align:right"|'..craft.craftingLevel
    result = result..'\r\n| style="text-align:right"|'..craft.craftingXP
    result = result..'\r\n| style="text-align:right"|'..craft.sellsFor
 
    local matArray = {}
    if craft.craftGPCost ~= nil and craft.craftGPCost > 0 then
    table.insert(matArray, Icons.GP(craft.craftGPCost))
    end
    for j, mat in Shared.skpairs(craft.craftReq) do
      local matItem = Items.getItemByID(mat.id)
      table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.qty}))
    end
    result = result..'\r\n|'..table.concat(matArray, ', ')
  end
 
  result = result..'\r\n|}'
  return result
end
end


return p
return p

Latest revision as of 17:55, 18 June 2024

Documentation for this module may be created at Module:Skills/Artisan/doc

--Splitting some functions into here to avoid bloating a single file
--Contains function for skills that consume resources (ie smithing, cooking, herblore, etc.)
local p = {}

local Shared = require('Module:Shared')
local Common = require('Module:Common')
local GameData = require('Module:GameData')
local SkillData = GameData.skillData
local Modifiers = require('Module:Modifiers')
local Skills = require('Module:Skills')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local ItemSourceTables = require('Module:Items/SourceTables')

function p.getCookedItemsTable(frame)
	local args = frame.args ~= nil and frame.args or frame
	local category = args[1]
	local realmName = args.realm
	local realm = Skills.getRealmFromName(realmName)
	if realm == nil then
		return Shared.printError('Failed to find a realm with name ' .. (realmName or 'nil'))
	end
	local skillID = 'Cooking'

	local categoryMap = {
		["Cooking Fire"] = 'melvorD:Fire',
		["Furnace"] = 'melvorD:Furnace',
		["Pot"] = 'melvorD:Pot'
	}
	local categoryID = categoryMap[category]

	-- Find recipes for the relevant categories
	-- Note: Excludes Lemon cake
	local recipeArray = GameData.getEntities(SkillData.Cooking.recipes,
		function(recipe)
			return (
				(categoryID == nil or recipe.categoryID == categoryID)
				and recipe.noMastery == nil
				and Skills.getRecipeRealm(recipe) == realm.id
			)
		end
	)
	table.sort(recipeArray, function(a, b) return Skills.standardRecipeSort(skillID, a, b) end)

	-- Logic for generating some cells of the table which are consistent for normal & perfect items
	local getHealingCell = function(item, qty)
		if item ~= nil then
			return 'data-sort-value="'..(math.floor(item.healsFor) * qty)..'"|'..Icons.Icon({"Hitpoints", type="skill", notext=true})..' '..Shared.formatnum(math.floor(item.healsFor * 10))..(qty > 1 and ' (x'..qty..')' or '')
		else
			return ' '
		end
	end
	local getSaleValueCell = function(item, qty)
		if item ~= nil then
			return 'data-sort-value="'..math.floor(item.sellsFor * qty)..'"|'..Items.getValueText(item)..(qty > 1 and ' (x'..qty..')' or '')
		else
			return ' '
		end
	end

	local resultPart = {}
	table.insert(resultPart, '{| class="wikitable sortable stickyHeader"')
	table.insert(resultPart, '\r\n|- class="headerRow-0"')
	table.insert(resultPart, '\r\n!colspan="3" rowspan="2"|Cooked Item!!rowspan="2"|Requirements')
	table.insert(resultPart, '!!rowspan="2"|Cook Time (s)!!rowspan="2"|XP!!rowspan="2"|XP/s!!colspan="2"|Healing!!colspan="2"|Value!!rowspan="2"|Ingredients')
	table.insert(resultPart, '\r\n|- class="headerRow-1"')
	table.insert(resultPart, '\r\n!Normal!!' .. Icons.Icon({'Perfect', type='bonus', ext='png', notext=true, nolink=true}))
	table.insert(resultPart, '!!Normal!!' .. Icons.Icon({'Perfect', type='bonus', ext='png', notext=true, nolink=true}))

	for i, recipe in ipairs(recipeArray) do
		local level = Skills.getRecipeLevel(skillID, recipe)
		local baseXP = recipe.baseAbyssalExperience or recipe.baseExperience
		local baseInt = recipe.baseInterval / 1000
		local reqText = Skills.getRecipeRequirementText(SkillData.Cooking.name, recipe)
		local xpRate = baseXP / baseInt
		local item = Items.getItemByID(recipe.productID)
		local perfectItem = nil
		if recipe.perfectCookID ~= nil then
			perfectItem = Items.getItemByID(recipe.perfectCookID)
		end
		local qty = recipe.baseQuantity or 1

		table.insert(resultPart, '\r\n|-')
		table.insert(resultPart, '\r\n|class="table-img"|'..Icons.Icon({item.name, type='item', notext=true, size='50'}))
		table.insert(resultPart, '\r\n|class="table-img"| ')
		if perfectItem ~= nil then
			table.insert(resultPart, Icons.Icon({perfectItem.name, type='item', notext=true, size='50'}))
		end
		table.insert(resultPart, '||')
		if qty > 1 then
			table.insert(resultPart, qty..'x ')
		end
		table.insert(resultPart, Icons.getExpansionIcon(item.id))
		table.insert(resultPart, Icons.Icon({item.name, type='item', noicon = true}))
		table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. level .. '"|' .. reqText)
		table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. baseInt .. '"|' .. Shared.round(baseInt, 2, 0))
		table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. baseXP .. '"|' .. Shared.formatnum(baseXP))
		table.insert(resultPart, '||style="text-align:right" data-sort-value="' .. xpRate .. '"|' .. Shared.formatnum(Shared.round(xpRate, 2, 0)))
		table.insert(resultPart, '||'..getHealingCell(item, qty)..'||'..getHealingCell(perfectItem, qty))
		table.insert(resultPart, '||'..getSaleValueCell(item, qty)..'||'..getSaleValueCell(perfectItem, qty))
		local matArray = {}
		for j, mat in ipairs(recipe.itemCosts) do
			local matItem = Items.getItemByID(mat.id)
			if matItem ~= nil then
				table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.quantity}))
			end
		end
		table.insert(resultPart, '\r\n|'..table.concat(matArray, ' '))
	end

	table.insert(resultPart, '\r\n|}')
	return table.concat(resultPart)
end

local tierSuffix = { 'I', 'II', 'III', 'IV' }
function p._getPotionDescription(potion)
	-- TODO: Temporary fix below for incorrect Traps Potion descriptions. To amend
	-- once corrected within game data
	if potion.customDescription and not Shared.contains(potion.id, 'melvorTotH:Traps_Potion_') then
		return potion.customDescription
	elseif type(potion.modifiers) == 'table' and not Shared.tableIsEmpty(potion.modifiers) then
		return Modifiers.getModifiersText(potion.modifiers, false, true)
	else
		return ''
	end
end

function p._getHerblorePotionTable(categoryName)
	local skillID = 'Herblore'
	local category = GameData.getEntityByName(SkillData.Herblore.categories, categoryName)
	if category == nil then
		local catNames = {}
		for i, cat in pairs(SkillData.Herblore.categories) do
			table.insert(catNames, cat.name)
		end
		return Shared.printError('No such category ' .. categoryName .. ', the following are available: ' .. table.concat(catNames, ', '))
	end
	local categoryID = category.id

	local potionArray = GameData.getEntities(SkillData.Herblore.recipes,
		function(potion)
				-- Category implies a realm selection, so no ned to check this separately
				return potion.categoryID == categoryID
			end
		)

	table.sort(potionArray, function(a, b) return Skills.standardRecipeSort(skillID, a, b) end)

	local resultPart = {}
	table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
	table.insert(resultPart, '\r\n|- class="headerRow-0"')
	table.insert(resultPart, '\r\n!Potion!!Requirements')
	table.insert(resultPart, '!!XP!!Ingredients!!style="width:30px;"|Tier!!Value!!Charges!!Effect')

	for i, potion in ipairs(potionArray) do
		local level = Skills.getRecipeLevel(skillID, potion)
		local baseXP = potion.baseAbyssalExperience or potion.baseExperience
		local reqText = Skills.getRecipeRequirementText(SkillData.Herblore.name, potion)
		local costText = Common.getCostString({
			["items"] = potion.itemCosts,
			["currencies"] = potion.currencyCosts
		}, 'N/A', nil, ', ')

		table.insert(resultPart, '\r\n|-')
		local expIcon = Icons.getExpansionIcon(potion.potionIDs[1])
		table.insert(resultPart, '\r\n|rowspan="4"|'..expIcon..'[['..potion.name..']]')
		table.insert(resultPart, '||rowspan="4" style="text-align:right" data-sort-value="' .. level .. '"|'..reqText)
		table.insert(resultPart, '||rowspan="4" style="text-align:right" data-sort-value="' .. baseXP .. '"|'..Shared.formatnum(baseXP))
		table.insert(resultPart, '||rowspan="4"|'..costText..'||')

		local tierRows = {}
		for j, potionID in ipairs(potion.potionIDs) do
			local rowTxt = {}
			local tierPot = Items.getItemByID(potionID)
			table.insert(rowTxt, Icons.Icon({tierPot.name, type='item', notext=true}))
			table.insert(rowTxt, Icons.Icon({tierPot.name, tierSuffix[j], type = 'item', noicon=true}))
			table.insert(rowTxt, '||style="text-align:right;" data-sort-value="'..tierPot.sellsFor..'"|'..Items.getValueText(tierPot))
			table.insert(rowTxt, '||style="text-align:right;"|'..tierPot.charges..'|| '..p._getPotionDescription(tierPot))
			table.insert(tierRows, table.concat(rowTxt))
		end
		table.insert(resultPart, table.concat(tierRows, '\r\n|-\r\n|'))
	end

	table.insert(resultPart, '\r\n|}')
	return table.concat(resultPart)
end

function p.getHerblorePotionTable(frame)
	local args = frame.args ~= nil and frame.args or frame
	local category = args[1]
	return p._getHerblorePotionTable(category)
end

function p._getHerbloreHerbTable(realmID)
	local allHerbs = {}

	-- Finds the herb from a potion along with the level required to make the potion.
	local function handlePotion(potion)
		local potionCosts = potion.itemCosts
		local level = Skills.getRecipeLevel('Herblore', potion)

		if potionCosts == nil or level == nil then
			return
		end

		-- Find if this potion uses a herb, and which herb it is.
		for _, ingredient in ipairs(potionCosts) do
			local ingredientID = ingredient.id
			if ingredientID ~= nil and string.sub(ingredientID, -5) == "_Herb" then
				-- Set the lowest level of potion this herb is used in.
				local currLevel = allHerbs[ingredientID] or 9999999
				if level < currLevel then
					allHerbs[ingredientID] = level
				end
			end
		end
	end

	local recipes = GameData.getEntities(SkillData.Herblore.recipes,
		function(obj)
			return Skills.getRecipeRealm(obj) == realmID
		end
	)
	for _, potion in ipairs(recipes) do
		handlePotion(potion)
	end
	
	local sortedValues = Shared.sortDictionary(
		allHerbs, 
		function (a, b) return a.value < b.value end)

	local tbl = mw.html.create("table")
        :addClass("wikitable sortable stickyHeader")
        
    -- Add header
    tbl :tag("tr"):addClass("headerRow-0")
    	:tag("th"):wikitext(Icons.Icon({'Herblore', type='skill', notext=true})..' Level')
    	:tag("th"):wikitext("Herb")
    	:tag("th"):wikitext("Value")
    	:tag("th"):wikitext("Herb Sources")
    	:done()
    
    -- Fill wikitable.
    for _, v in pairs(sortedValues) do
    	local herbItem = Items.getItemByID(v['key'])
    	local herbLevel = v['value']
    	local dlcIcon = Icons.getExpansionIcon(herbItem.id)

    	-- Add rows
    	tbl :tag("tr")
    		:tag("td"):wikitext(herbLevel)
    		:tag("td"):wikitext(dlcIcon .. Icons.Icon({herbItem.name, type='item'}))
    		:tag("td"):wikitext(Items.getValueText(herbItem))
    		:tag('td'):wikitext(ItemSourceTables._getItemSources(herbItem, false, nil, ' '))
    		:done()
    end

	return tostring(tbl)
end

function p.getHerbloreHerbTable(frame)
	local args = frame:getParent().args
	local realmName = args.realm
	local realm = Skills.getRealmFromName(realmName)
	if realm == nil then
		return Shared.printError('Failed to find a realm with name ' .. (realmName or 'nil'))
	end
	return p._getHerbloreHerbTable(realm.id)
end

function p.getPotionTable(frame)
	local potionName = frame.args ~= nil and frame.args[1] or frame

	local recipe = GameData.getEntityByName(SkillData.Herblore.recipes, potionName)
	if recipe == nil then
		return Shared.printError('No potion named "' .. potionName .. '" was found')
	end

	local resultPart = {}
	table.insert(resultPart, '{| class="wikitable"')
	table.insert(resultPart, '\r\n!colspan=4|[['..potionName..']]')
	table.insert(resultPart, '\r\n|-\r\n!Potion!!Tier!!Charges!!Effect')

	for i, potionID in ipairs(recipe.potionIDs) do
		local tier = tierSuffix[i]
		local potion = Items.getItemByID(potionID)
		if potion ~= nil then
			table.insert(resultPart, '\r\n|-')
			table.insert(resultPart, '\r\n| ' .. Icons.Icon({potion.name, type='item', notext=true, size='60'}))
			table.insert(resultPart, '|| ' .. Icons.getExpansionIcon(potion.id) .. Icons.Icon({potion.name, tier, type='item', noicon=true}))
			table.insert(resultPart, '|| ' .. potion.charges .. '|| ' .. p._getPotionDescription(potion))
		end
	end

	table.insert(resultPart, '\r\n|}')
	return table.concat(resultPart)
end

function p.getRunecraftingTable(frame)
	local category = frame.args ~= nil and frame.args[1] or frame
	return p._getRecipeTable('Runecrafting', category, {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients', 'SkillXPSec', 'CurrencySec'})
end

function p.getFletchingTable(frame)
	local category = frame.args ~= nil and frame.args[1] or frame
	return p._getRecipeTable('Fletching', category, {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients'})
end

function p.getCraftingTable(frame)
	local category = frame.args ~= nil and frame.args[1] or frame
	local columns = {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients'}
	if category == 'Rings' or category == 'Necklaces' then
		table.insert(columns, "Description")
	end
	return p._getRecipeTable('Crafting', category, columns)
end

function p.getSmithingTable(frame)
	local category = frame.args ~= nil and frame.args[1] or frame
	local columns = {'ItemImage', 'ItemName', 'SkillLevel', 'SkillXP', 'Currency', 'Ingredients'}
	if category ~= 'Bars' and category ~= 'Abyssal Bars' then
		table.insert(columns, 'CurrencyBar')
	end
	return p._getRecipeTable('Smithing', category, columns)
end

-- Given a skill name, category, and column list, produces a table of recipes for that
-- skill & category combination. If categoryName is '', all recipes are returned.
-- Note: This only supports a number of skills with consistent recipe data structures, being:
-- Fletching, Crafting, and Runecrafting
-- Valid column list options are: Item, SkillLevel, SkillXP, Currency, Ingredients, SkillXPSec, CurrencySec, CurrencyBar
function p._getRecipeTable(skillName, categoryName, columnList)
	-- Validation: Parameters
	if type(skillName) ~= 'string' then
		return Shared.printError('skillName must be a string')
	elseif not Shared.contains({'string', 'nil'}, type(categoryName)) then
		return Shared.printError('category must be a string or nil')
	elseif type(columnList) ~= 'table' or Shared.tableIsEmpty(columnList) then
		return Shared.printError('columnList must be a table with 1+ elements')
	end

	local supportedSkills = {
		'Smithing',
		'Crafting',
		'Fletching',
		'Runecrafting'
	}
	if not Shared.contains(supportedSkills, skillName) then
		return Shared.printError('The ' .. skillName .. ' skill is not supported by this function')
	end

	-- Validation: Category
	local category = GameData.getEntityByName(SkillData[skillName].categories, categoryName)
	if category == nil then
		local catNames = {}
		for i, cat in pairs(SkillData[skillName].categories) do
			table.insert(catNames, cat.name)
		end
		return Shared.printError('No such category ' .. categoryName .. ' for skill ' .. skillName .. ', the following are available: ' .. table.concat(catNames, ', '))
	end
	local actionInterval = SkillData[skillName].baseInterval / 1000

	-- Validation: Skill data
	local recipeKey = 'recipes'
	if SkillData[skillName] == nil then
		return Shared.printError('Could not locate skill data for ' .. skillName)
	elseif SkillData[skillName][recipeKey] == nil then
		return Shared.printError('Could not locate recipe data for ' .. skillName)
	end

	-- Validation: Column list
	local columnDef = {
		["ItemImage"] = {["header"] = 'Item', ["altRepeat"] = false},
		["ItemName"] = {["header"] = 'Name', ["altRepeat"] = true},
		["SkillLevel"] = {["header"] = Icons.Icon({skillName, type='skill', notext=true}) .. ' Level', ["altRepeat"] = false},
		["SkillXP"] = {["header"] = 'XP', ["altRepeat"] = false},
		["Currency"] = {["header"] = 'Value', ["altRepeat"] = true},
		["Ingredients"] = {["header"] = 'Ingredients', ["altRepeat"] = true},
		["SkillXPSec"] = {["header"] = 'XP/s', ["altRepeat"] = false},
		["CurrencySec"] = {["header"] = 'Value/s', ["altRepeat"] = true},
		["CurrencyBar"] = {["header"] = 'Value/Bar', ["altRepeat"] = true },
		["Description"] = {["header"] = "Description", ["altRepeat"] = true}
	}
	-- Build the table header while we're here
	local resultPart, barIDList = {}, {}
	table.insert(resultPart, '{| class="wikitable sortable stickyHeader"\r\n|- class="headerRow-0"')
	for i, colID in ipairs(columnList) do
		if columnDef[colID] == nil then
			return Shared.printError('Invalid column ' .. colID .. ' requested')
		else
			table.insert(resultPart, '\r\n! ' .. columnDef[colID].header)
			if colID == 'CurrencyBar' then
				-- For Smithing, a currency value per bar column is included. If this
				-- is requested, then obtain a list of bar item IDs
				barIDList = p.getBarItemIDs()
			end
		end
	end

	-- Determine which recipes to include
	local recipeList = GameData.getEntities(SkillData[skillName][recipeKey],
		function(recipe)
			return category.id == nil or recipe.categoryID == category.id
		end)
	if Shared.tableIsEmpty(recipeList) then
		return ''
	end
	table.sort(recipeList, function(a, b) return a.level < b.level end)

	-- Build rows based on recipes
	for i, recipe in ipairs(recipeList) do
		local ns, _ = GameData.getLocalID(recipe.id)
		local item = Items.getItemByID(recipe.productID)
		local level = Skills.getRecipeLevel(skillName, recipe)
		local baseXP = recipe.baseAbyssalExperience or recipe.baseExperience
		if item ~= nil then
			-- Some recipes have alternative costs, so the recipe may require multiple rows
			local costList = nil
			if recipe.alternativeCosts ~= nil and not Shared.tableIsEmpty(recipe.alternativeCosts) then
				costList = recipe.alternativeCosts
			else
				costList = {{["itemCosts"] = recipe.itemCosts}}
			end
			local costCount = Shared.tableCount(costList)
			-- Build one row per element within costList
			for recipeRow, costDef in ipairs(costList) do
				local rowspanStr = (recipeRow == 1 and costCount > 1 and 'rowspan="' .. costCount .. '" ') or ''
				local qty = (costDef.quantityMultiplier or 1) * (recipe.baseQuantity or 1)
				table.insert(resultPart, '\n|-')
				for j, colID in ipairs(columnList) do
					local altRepeat = columnDef[colID].altRepeat
					-- All columns must be generated if this is the very first row for a recipe,
					-- for subsequent rows only columns marked as altRepeat = true are generated
					if recipeRow == 1 or altRepeat then
						local spanStr = (not altRepeat and rowspanStr) or ''
						if colID == 'ItemImage' then
							table.insert(resultPart, '\n|' .. spanStr .. 'class="table-img"| ' .. Icons.Icon({item.name, type='item', size='50', notext=true}))
						elseif colID == "ItemName" then
							local namePrefix = spanStr
							if qty > 1 then
								namePrefix = namePrefix .. 'data-sort-value="' .. item.name .. '"'
							end
							table.insert(resultPart, '\n|'.. (namePrefix ~= '' and namePrefix .. '| ' or ' ') .. Icons.getExpansionIcon(item.id) .. (qty > 1 and '<b>' .. qty .. 'x</b> ' or '') .. Icons.Icon({item.name, type='item', noicon=true}))
						elseif colID == 'SkillLevel' then
							table.insert(resultPart, '\n|' .. spanStr .. 'style="text-align:right"| ' .. level)
						elseif colID == 'SkillXP' then
							table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. baseXP ..'" style="text-align:right"| ' .. Shared.formatnum(baseXP))
						elseif colID == 'Currency' then
							local val = math.floor(item.sellsFor)
							table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. (val * qty) .. '"| ' .. Items.getValueText(item) .. (qty > 1 and ' (x' .. qty .. ')' or ''))
						elseif colID == 'Ingredients' then
							local matArray = {}
							local currCost = { ["items"] = {}, ["currencies"] = recipe.currencyCosts }
							local currText = Common.getCostString(currCost, nil, nil, ', ')
							if currText ~= nil then
								table.insert(matArray, currText)
							end

							for k, mat in ipairs(costDef.itemCosts) do
								local matItem = Items.getItemByID(mat.id)
								if matItem ~= nil then
									table.insert(matArray, Icons.Icon({matItem.name, type='item', notext=true, qty=mat.quantity}))
								end
							end
							table.insert(resultPart, '\n|' .. (spanStr ~= '' and spanStr .. '| ' or ' ') .. table.concat(matArray, ', '))
						elseif colID == 'SkillXPSec' then
							table.insert(resultPart, '\n|' .. spanStr .. 'style="text-align:right"| ' .. string.format('%.2f', recipe.baseExperience / actionInterval))
						elseif colID == 'CurrencySec' then
							local saleCurrency = item.sellsForCurrency or 'melvorD:GP'
							local val = math.floor(item.sellsFor) * qty / actionInterval
							table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. val .. '"| ' .. Icons._Currency(saleCurrency, string.format('%.2f', val)))
						elseif colID == 'CurrencyBar' then
							local barQty = 0
							for k, mat in ipairs(costDef.itemCosts) do
								if Shared.contains(barIDList, mat.id) then
									barQty = barQty + mat.quantity
								end
							end
							if barQty > 0 then
								local saleCurrency = item.sellsForCurrency or 'melvorD:GP'
								local barVal = Shared.round(math.floor(item.sellsFor) * qty / barQty, 1, 1)
								table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="' .. barVal .. '"| ' .. Icons._Currency(saleCurrency, barVal))
							else
								table.insert(resultPart, '\n|' .. spanStr .. 'data-sort-value="0" class="table-na"| N/A')
							end
						elseif colID == 'Description' then
							local descrip = Items._getItemStat(item, 'description')
							if descrip == 'No Description' and item.modifiers ~= nil and not Shared.tableIsEmpty(item.modifiers) then
								descrip = Modifiers.getModifiersText(item.modifiers, false)
							end
							table.insert(resultPart, '\n| '..spanStr..'|'..descrip)
						else
							table.insert(resultPart, '\n| ')
						end
					end
				end
			end
		end
	end
	table.insert(resultPart, '\n|}')
	return table.concat(resultPart)
end

function p.getBarItemIDs()
	local barCategories = {
		['melvorD:Bars'] = true,
		['melvorItA:AbyssalBars'] = true
	}
	local barIDList = {}
	for i, recipe in ipairs(SkillData.Smithing.recipes) do
		if barCategories[recipe.categoryID] then
			table.insert(barIDList, recipe.productID)
		end
	end
	return barIDList
end

return p