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

local p = {}
local h = require("Module:HF")
local design = require('Module:Design')
local standard = require('Module:StandardizedName')
local getArgs = require('Dev:Arguments').getArgs
--local labels = {'Intelligence', 'Strength', 'Speed', 'Durability', 'Energy Projection', 'Fighting Skills'}
local description = mw.loadData('Module:Power Grid/Description')
local list_of_power_grids = mw.loadData('Module:Power Grid/List')

--------------------------------------------------------------------------------
function p.lua_main(pagename)
	local caption = 'Power Grid'
	local info = list_of_power_grids[pagename]
	local explanation = ''
	local value
	local categories
	local output_categories = {}
	local output = {}

	if  info ~= nil
		then
			table.insert(output, '{{DEFAULTSORT:'..standard.name_for_sorting({pagename})..'|noerror}}')
			table.insert(output_categories, 'Power Grid Added')

			for i = 1,6 do
				value, categories = p.add_tr(i, tostring(info[i]), info[8])
				table.insert(output, value)
				output_categories = h.join_tables(output_categories, categories)
			end
	
			if info[8] ~= nil
				then 
					explanation = tostring(mw.html.create('td'):attr('colspan', '8'):css('width', '350px'):wikitext('* '..info[8]))
					explanation = tostring(mw.html.create('tr'):wikitext(explanation))
			end

			value = info[7]
			if value ~= nil
				then 
					if h.exists(value)
						then value = h.Link(value)
					end
					caption = caption..'<ref>'..value..'</ref>'
					table.insert(output_categories, 'Power Grid Complete')
				else 
					table.insert(output_categories, 'Power Grid Reference Needed')
			end
			caption = mw.html.create('caption')
						:css('color', 'white')
						:css('background-color', 'gray')
						:css('font-weight', 'bold')
						:wikitext(caption)
			caption = tostring(caption)
	
			output = mw.html.create('table')
						:attr('align', 'right')
						:attr('cellspacing', '0')
						:attr('cellpadding', '0')
						:css('clear', 'both')
						:css('background-color', 'white')
						:css('border', '1px solid gray')
						:wikitext(caption..table.concat(output)..explanation)
	end
	
	return tostring(output), output_categories
end
	

function p.main2(frame)
	local args = getArgs (frame)
	local pagename = args.pagename or mw.title.getCurrentTitle().text
	local output_categories = {}
	local output = {}
	output, output_categories = p.lua_main(pagename)
	return frame:preprocess(output)..h.add_categories(output_categories)
end



--------------------------------------------------------------------------------
-- used in Template:Official Power Grid
function p.main(frame)
	local args = getArgs (frame)
	local pagename = mw.title.getCurrentTitle().subpageText
	local explanation = args.Explanation
	local reference = args.Reference
	local caption = 'Power Grid'
	local value
	local categories
	local output_categories = {}
	local output = {}
	
	table.insert(output, '{{DEFAULTSORT:'..standard.name_for_sorting({pagename})..'|noerror}}')
	table.insert(output_categories, 'Power Grid Added')

	for i = 1,6 do
		value, categories = p.add_tr(i, args[description[i].label], explanation)
		table.insert(output, value)
		output_categories = h.join_tables(output_categories, categories)
	end
	
	if not h.isempty(explanation)
		then 
			explanation = tostring(mw.html.create('td'):attr('colspan', '8'):css('width', '350px'):wikitext('* '..explanation))
			explanation = tostring(mw.html.create('tr'):wikitext(explanation))
		else
			explanation = ''
	end
	
	if not h.isempty(reference)
		then 
			caption = caption..'<ref>'..reference..'</ref>'
			table.insert(output_categories, 'Power Grid Complete')
		else 
			table.insert(output_categories, 'Power Grid Reference Needed')
	end
	caption = mw.html.create('caption')
			:css('color', 'white')
			:css('background-color', 'gray')
			:css('font-weight', 'bold')
			:wikitext(caption)
	caption = tostring(caption)
	
	output = mw.html.create('table')
			:attr('align', 'right')
			:attr('cellspacing', '0')
			:attr('cellpadding', '0')
			:css('clear', 'both')
			:css('background-color', 'white')
			:css('border', '1px solid gray')
			:wikitext(caption..table.concat(output)..explanation)
	return frame:preprocess(tostring(output))..h.add_categories(output_categories)
end


--------------------------------------------------------------------------------
function p.add_tr(row_number, values, explanation)
	local label = design.lua_add_tooltip(description[row_number].label, description[row_number].tooltip)
	local output_categories = {}
	local output = {}
	
	if string.find(values, '-', 1, true) ~= nil
		then 
			output = mw.text.split(values, '-')
			for i = 1, #output do
				output[i] = tonumber(output[i])
			end
			if explanation ~= nil
				then label = label .. '*'
			end
		else output = {tonumber(values)}
	end
	label = mw.html.create('td')
			:css('color', 'black')
			:css('text-align', 'left')
			:css('width', '140px')
			:css('padding', '0px 0px 0px 5px')
			:wikitext(label)
	label = tostring(label)
	output, output_categories = p.add_td(output, row_number)
	output = tostring( mw.html.create('tr'):wikitext( label..output ) )
	
	return output, output_categories
end


--------------------------------------------------------------------------------
function p.add_td(values, row_number)
	local function cell(text, color)
		return tostring(mw.html.create('td')
						:css('width', '30px')
						:css('text-align', 'center')
						:css('border-bottom', '1px solid gray')
						:css('background-color', color)
						:wikitext(text)
						)
	end
	local function tooltip(value, row_number)
		local output = design.lua_add_tooltip(value, description[row_number].values[value].tooltip)
		return tostring( mw.html.create('span'):css('color', 'white'):wikitext(output) )
	end
	local colors = {'darkgray', 'dodgerblue', 'tomato', 'mediumseagreen'}
	local i
	local j
	local value1
	local value2
	local category
	local filled = {false, false, false, false, false, false, false}
	local output_categories = {}
	local output = {'', '', '', '', '', '', ''}

	for i = 1,7 do
		output[i] = cell('', 'transparent')
	end
	value1 = values[1]
	for i = 1,7 do
		if i < value1 
			then 
				output[i] = cell('', colors[1])
				filled[i] = true
			elseif i == value1
				then 
					category = description[row_number].values[value1].category
					table.insert(output_categories, category)
					category = h.LinkToCategory(category, tooltip(value1, row_number))
					output[i] = cell(category, colors[1])
					filled[i] = true
		end
	end

	for j = 2, #colors do
		for i = 1, 7 do
			value2 = values[j]
			if not h.isempty(value2)
				then
					if i < value2 and value2 < value1
						then 
							output[i] = cell('', colors[j])
							filled[i] = true
					end
					if i < value2 and filled[i] == false
						then 
							output[i] = cell('', colors[j])
							filled[i] = true
					end
					if i == value2
						then 
							category = description[row_number].values[value2].category
							table.insert(output_categories, category)
							category = h.LinkToCategory(category, tooltip(value2, row_number))
							output[i] = cell(category, colors[j])
							filled[i] = true
					end
			end
		end
	end

	return table.concat(output), output_categories
end

return p
Community content is available under CC-BY-SA unless otherwise noted.