Anonymous

Module:Navboxes: Difference between revisions

From Melvor Idle
getFarmingNavbox: Revise implementation, ensure all seeds and the grown result of all seeds is included
(Restore getPotionNavbox after unintended removal)
(getFarmingNavbox: Revise implementation, ensure all seeds and the grown result of all seeds is included)
Line 12: Line 12:


function p.getFarmingNavbox(frame)
function p.getFarmingNavbox(frame)
   local allotmentSeedsTable = {}
   local resultPart = {}
   local allotmentProduceTable = {}
   local seedsTable = {}
   local herbSeedsTable = {}
   local produceTable = {}
  local herbProduceTable = {}
  local treeSeedsTable = {}
  local treeProduceTable = {}


   for i, item in Shared.skpairs(ItemData.Items) do
   for i, item in ipairs(ItemData.Items) do
     if item.tier == 'Allotment' then
     if item.farmingLevel ~= nil then
       table.insert(allotmentSeedsTable, Icons.Icon({item.name, type='item'}))
       local tier = item.tier
    elseif item.type == 'Harvest' then
      if seedsTable[tier] == nil then
      table.insert(allotmentProduceTable, Icons.Icon({item.name, type='item'}))
        -- Initialise tier tables
    elseif item.tier == 'Herb' then
        seedsTable[tier] = {}
       table.insert(herbSeedsTable, Icons.Icon({item.name, type='item'})) 
        produceTable[tier] = {}
    elseif item.type == 'Herb' then
       end
      table.insert(herbProduceTable, Icons.Icon({item.name, type='item'}))
 
    elseif item.tier == 'Tree' then
      if item.grownItemID ~= nil then
      table.insert(treeSeedsTable, Icons.Icon({item.name, type='item'}))
        local grownItem = Items.getItemByID(item.grownItemID)
    elseif i == 2 or i == 3 or i == 5 or i == 7 or i == 8 then
        if grownItem ~= nil then
       table.insert(treeProduceTable, Icons.Icon({item.name, type='item'})
          table.insert(produceTable[tier], { ["name"] = grownItem.name, ["level"] = item.farmingLevel })
        end
      end
       table.insert(seedsTable[tier], { ["name"] = item.name, ["level"] = item.farmingLevel })
     end
     end
   end
   end
   
  local result = '{| class="wikitable mw-collapsible" style="margin:auto; clear:both; width: 100%"'
  result = result..'\r\n!colspan="2" style="padding-left:64px;"|'..Icons.Icon({'Farming', type='skill'})
  result = result..'\r\n|-\r\n!colspan="2"|Allotments'
  result = result..'\r\n|-\r\n!scope="row"|Seeds'
  result = result..'\r\n|style="text-align:center;|'..table.concat(allotmentSeedsTable, ' • ')
  result = result..'\r\n|-\r\n!scope="row"|Produce'
  result = result..'\r\n|style="text-align:center;|'..table.concat(allotmentProduceTable, ' • ')
  result = result..'\r\n|-\r\n!colspan="2"|Herbs'
  result = result..'\r\n|-\r\n!scope="row"|Seeds'
  result = result..'\r\n|style="text-align:center;|'..table.concat(herbSeedsTable, ' • ')
  result = result..'\r\n|-\r\n!scope="row"|Produce'
  result = result..'\r\n|style="text-align:center;|'..table.concat(herbProduceTable, ' • ')
  result = result..'\r\n|-\r\n!colspan="2"|Trees'
  result = result..'\r\n|-\r\n!scope="row"|Seeds'
  result = result..'\r\n|style="text-align:center;|'..table.concat(treeSeedsTable, ' • ')
  result = result..'\r\n|-\r\n!scope="row"|Produce'
  result = result..'\r\n|style="text-align:center;|'..table.concat(treeProduceTable, ' • ')
  result = result..'\r\n|}'


   return result
   -- Generate output table
  table.insert(resultPart, '{| class="wikitable mw-collapsible" style="margin:auto; clear:both; width: 100%"')
  table.insert(resultPart, '\r\n!colspan="2" style="padding-left:64px;"|' .. Icons.Icon({'Farming', type='skill'}))
 
  local getItemList = function(itemTable)
      local listPart = {}
      for i, item in ipairs(itemTable) do
        table.insert(listPart, Icons.Icon({item.name, type='item'}))
      end
      return table.concat(listPart, ' • ')
    end
  local sortFunc = function(a, b) return (a.level == b.level and a.name < b.name) or a.level < b.level end
 
  -- Determine tier list & order in which tiers will be listed in output
  local tierList = {}
  for tier, seeds in pairs(seedsTable) do
    table.insert(tierList, tier)
  end
  table.sort(tierList, function(a, b) return a < b end)
 
  -- Generate table section for each tier
  for i, tier in pairs(tierList) do
    -- Sort tables by Farming level order
    table.sort(seedsTable[tier], sortFunc)
    table.sort(produceTable[tier], sortFunc)
 
    table.insert(resultPart, '\r\n|-\r\n!colspan="2"| ' .. tier .. 's')
    table.insert(resultPart, '\r\n|-\r\n!scope="row"| Seeds')
    table.insert(resultPart, '\r\n|style="text-align:center;"| ' .. getItemList(seedsTable[tier]))
    table.insert(resultPart, '\r\n|-\r\n!scope="row"| Produce')
    table.insert(resultPart, '\r\n|style="text-align:center;"| ' .. getItemList(produceTable[tier]))
  end
  table.insert(resultPart, '\r\n|}')
 
  return table.concat(resultPart)
end
end