Module:PollArchive

--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 = '  ' 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 = ' ' return tbl end

return p