Module:HarvestingEfficiency

From Dark and Light Wiki
Jump to: navigation, search

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

local p = {}
function p.HarvestingEfficiency( f )
  local dlclink = require('Module:DLCLink').link
  local args = f:getParent().args
  local rows, num, ogNum, rating = {}, 0, ''
  local topI = {}, 0, ''
  local top = 0

  for item,eff in spairs(args) do
    if item ~= 'note' and string.match(item, '_auto$') == nil then
      rating = 'data-sort-value="0" | N/A'
      if tonumber(eff) ~= nil then
        num = tonumber(eff)
        ogNum = num
        num = ((num * 100) / 10) / 2
        num = math.ceil(num)
        if num > 0 and num < 6 then
          rating = 'data-sort-value="'..num..'" | '..string.rep('★',num)..string.rep('☆',5-num)
        end
      end
      if args[item..'_auto'] ~= nil and string.lower(args[item..'_auto']) == 'yes' then
        rating = rating..'<br><small><i>Gathers autonomously</i></small>[[Category:Creatures that gather autonomously]]'
      end
      if num > 4 and top < 5 then 
	      	table.insert(topI, '<center>[[File: Icon '..item:lower()..'.png|30px|link='..item..']]</center>')
	      	top = top + 1
      end
      table.insert(rows, '|-\n| style="width:85%" | [[File: Icon '..item:lower()..'.png|30px|link='..item..']] '..dlclink(item)..'||'..rating..'||'..ogNum)
    end
  end
  local noteRow = ''
  if args.note ~= nil and string.len(args.note) > 0 then
    noteRow = '\n|-\n! colspan="4" align="left" style="font-weight:normal;font-size:0.8em" | <i>'..args.note..'</i>'
  end
  return '{|class="wikitable" width="350"\n|-\n! colspan="5" | Top 5 [[Resources]] gathered\n|-\n|'..table.concat(topI,'||')..'\n|}\n'..
  '{| class="wikitable sortable mw-collapsible mw-collapsed" width="350" cellspacing="0"\n'..
  '|+Full Gathering List\n|-\n! Resource\n! Raiting\n! Efficiency  \n'..table.concat(rows,'\n')..noteRow..'\n|}'
end

function spairs(t)
  -- collect the keys
  local keys = {}
  for k in pairs(t) do keys[#keys+1] = k end

  -- if order function given, sort by it by passing the table and keys a, b,
  table.sort(keys, function(a,b) return (tonumber(t[a]) ~= nil and (tonumber(t[b]) == nil or t[b] < t[a])) end)

  -- return the iterator function
  local i = 0
  return function()
    i = i + 1
    if keys[i] then
      return keys[i], t[keys[i]]
    end
  end
end

return p