Difference between revisions of "Module:Demographics Basics"

From Ways of Darkness
Jump to navigation Jump to search
Line 61: Line 61:
 
local Origin = {}
 
local Origin = {}
 
local i = 1
 
local i = 1
for key,value in OrderedPairs(tabule[country]) do
+
for key,value in pairs(tabule[country]) do
 
Origin[i] = tabule[country][key]
 
Origin[i] = tabule[country][key]
 
Origin[i]["OrgKey"] = tonumber(key)
 
Origin[i]["OrgKey"] = tonumber(key)

Revision as of 10:16, 5 April 2016

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

require "Module:FormatNum"
require "Module:OrderedPairs"
require "Module:SortedKeys"
require "Module:Compare"

function total_table(table,country)
 local temp = 0
  if table == nil then
        return 0
  elseif table[country] == nil then
        return 0
    elseif table[country] == 0 then
        return 0
    else for i in pairs(table[country]) do
 temp=temp+table[country][i] 
 end
 return temp
end
end

function fetch_table(table,country,race)
    if table == nil then
        return 0
    elseif table[country] == nil then
        return 0
    elseif table[country] == 0 then
        return 0
    end
    if table[country][race] == nil then
        return 0
    elseif table[country][race] == 0 then
        return 0
    else
    return table[country][race]
end end

function compare(a,b)
  return a < b
end

function generate(tabule,country,type)
    local totalled=total_table(tabule,country)
    local Base= "{| class=\"sortable wikitable\" style=\"background:white;\"\n! style=\"text-align: center; font-weight: bold;\" | " .. type .."\n! style=\"text-align: center; font-weight: bold;\" | Amount\n! style=\"text-align: center; font-weight: bold;\" | Percentage\n"
    table.sort(tabule,compare)
    for key,value in pairs(tabule[country]) do
        if tabule[country][key] ~= nil and tabule[country][key] ~= 0 then
                if key ~= "O" and key ~= "Other" then
        Base = Base .. "|-\n| style=\"text-align: center;\" | [[" .. key .. "]]\n"
                else
        Base = Base .. "|-\n| style=\"text-align: center;\" | " .. "Other" .. "\n"
                        end
        Base = Base .. "| style=\"text-align: center;\" | " .. format_num(value,0) .. "\n"
        Base = Base .. "| style=\"text-align: center;\" | " .. (math.ceil(value/totalled*10000))/100 .. "%\n"
        end
    end
    Base = Base .. "|}"
    return Base
end

function generate_historical(tabule,country,type)
	local Origin = {}
	local i = 1
	for key,value in pairs(tabule[country]) do
		Origin[i] = tabule[country][key]
		Origin[i]["OrgKey"] = tonumber(key)
		i = i + 1
	end
	table.sort(Origin, function(a,b) return a["OrgKey"]<b["OrgKey"] end)
	local Base= "{| class=\"sortable wikitable\"\n! style=\"text-align: center; font-weight: bold;\" | Year\n! style=\"text-align: center; font-weight: bold;\" | Pop\n! style=\"text-align: center; font-weight: bold;\" | ±%\n! style=\"text-align: center; font-weight: bold;\" | Notes\n|-\n"
	for key,value in pairs(Origin) do
		if Origin[key] ~= nil and Origin[key] ~= 0 then
	Base = Base .. "|-\n"
	Base = Base .. "| style=\"text-align: center; font-weight: bold;\" |" .. Origin[key]["OrgKey"] .. "\n"
	Base = Base .. "| style=\"text-align: center;\" |" .. format_num(Origin[key][1]()) .. "\n"
	if key == 1 then
		Base = Base .. "| style=\"text-align: center;\" |" .. "-" .. "\n"
	else
		Base = Base .. "| style=\"text-align: center;\" |" .. Compare(Origin[key][1](),Origin[key-1][1](),true) .. "\n"
	end
	if Origin[key][2] ~= nil and Origin[key][2] ~= 0 then
		Base = Base .. "| style=\"text-align: center;\" |" .. Origin[key][2] .. "\n"
	else
		Base = Base .. "| style=\"text-align: center;\" | \n"
		end
	end
	end
	Base = Base .. "|}"
	return Base
end