FANDOM


--Copied from the Clash of Clans Wiki.
--Original located at http://clashofclans.wikia.com/wiki/Module:PollArchive
local p = {}
 
local function progressBar(total, votes)
   local votetotal = tonumber(total)
   local votecount = tonumber(votes)
 
   if votecount < 0 then
      votecount = 0
   elseif votecount > votetotal then
      votecount = votetotal
   end
 
   if votetotal < 1 then
      votetotal = 1
   end
 
   local barlength = math.floor(votecount * 100 / votetotal + 0.5)
 
   if barlength < 1 then
      barlength = 1
   end
 
   local bar = '<div style="border: 1px inset gray; background: white;"><div style="width: ' .. barlength .. '%; background: lightgreen; height: 10px;"></div></div>'
   return bar
end
 
local function commas(number)
  local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
 
  -- reverse the int-string and append a comma to all blocks of 3 digits
  int = int:reverse():gsub("(%d%d%d)", "%1,")
 
  -- reverse the int-string back remove an optional comma and put the 
  -- optional minus and fractional part back
  return minus .. int:reverse():gsub("^,", "") .. fraction
end
 
local function tablify(tableData, what)
   local tbl = {}
   local i     = 1
 
   while tableData.args[i]
   do
      if what == 'key order' then
         tbl[#tbl + 1] = tableData.args[i]
      else
         tbl[tableData.args[i]] = tonumber(tableData.args[i + 1])
      end
 
      i = i + 2
   end
 
   return tbl
end
 
local function spairs(t, order)
   -- 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,
   -- otherwise just sort the keys 
   if order then
      table.sort(keys, function(a,b) return order(t, a, b) end)
   else
      table.sort(keys)
   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
 
function p.generateTable(tableData)
   local data      = tablify(tableData)
   local keyorder  = tablify(tableData, 'key order')
   local sortorder = {}
   local order     = {}
   local total     = tableData.args['votes']
 
   for k, v in spairs(data, function(t, a, b) return t[b] < t[a] end) do
      sortorder[#sortorder + 1] = k
   end
 
   local tbl = '<table style="border: 1px solid; padding: 10px; width: 500px; font-weight: bold; border-spacing: 0;">'
   tbl = tbl .. '<tr><th colspan="3" style="text-align: center; background: lightgray;">' .. tableData.args['title'] .. '</th></tr>'
 
   if tableData.args['sort'] and string.lower(tableData.args['sort']) == 'yes' then
      order = sortorder
   else
      order = keyorder
   end
 
   local tdborder = 'border: 1px solid #ddd; border-left: 0; border-right: 0; border-bottom: 0;'
 
   for o, k in spairs(order) do
      local v  = data[k]
      local tr = '<tr>'
 
      if tableData.args['top3'] and string.lower(tableData.args['top3']) == 'yes' then
         if sortorder[1] == k then
            tr = '<tr style="background: #FFE7A4;">'
         elseif sortorder[2] == k then
            tr = '<tr style="background: #D3D3E9;">'
         elseif sortorder[3] == k then
            tr = '<tr style="background: #FFCAA9;">'
         end
      end
 
      tbl = tbl .. tr .. '<td style="padding: 0px 10px; ' .. tdborder .. '">' .. k .. ':</td>'
      tbl = tbl .. '<td style="text-align: right; ' .. tdborder .. '">' .. commas(v) .. '&nbsp;votes</td>'
      tbl = tbl .. '<td style="width: 100%; padding-right: 5px; ' .. tdborder .. '">' .. progressBar(total, v) .. '</td></tr>'
   end
 
   tbl = tbl .. '<tr><td colspan="3" style="text-align: center; font-style: italic; font-weight: normal;">' .. commas(total) .. ' votes'
 
   if tableData.args['date1'] then
      tbl = tbl .. '<br/>' .. tableData.args['date1']
 
      if tableData.args['date2'] then
         tbl = tbl .. ' - ' .. tableData.args['date2']
      end
   end
 
   tbl = tbl .. '</td></tr></table>'
   return tbl
end
 
return p