Module:Materials

From Nookipedia, the Animal Crossing wiki
Revision as of 16:55, March 26, 2022 by PanchamBro (talk | contribs)

Module documentation (view)


Usage

This Lua module outputs the list of materials for the {{Material}} and {{PC Material}} templates to account for preprocessor node count limits. Data is stored in a table, and can determine how certain variables are used.

To add additional materials, see Module:Materials/Data.

See also


local p = {}
data = mw.loadData("Module:Materials/Data")

local function getArgs(frame)
    local args = {}
    for key, value in pairs(frame:getParent().args) do
        args[key] = value
    end
    for key, value in pairs(frame.args) do
        args[key] = value
    end
    return args
end

function p.main(frame)
    local args      = getArgs(frame)
    local game      = args.game or ''
    local nb        = args.nb or ''
    local size      = args.size or ''
    local material  = args[1] or args.material or ''
    local num       = args[2] or args.num or ''
    if game == "PC" then
    	return p.outputMaterialPC(material, num, nb, size)
    elseif game == "NH" then
    	return p.outputMaterialNH(material, num, nb, size)
    end
end

function p.outputMaterialNH(material, num, nb, size)
	local function isEmpty(s)
		return s == nil or s == ''
	end
	
	local print = '<span style="display:inline-block; white-space:nowrap;">'
	if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][1]) then
    	print = print .. '[[File:'.. data.materialNH[material][1]
		if not isEmpty(size) then
			print = print .. '|' .. size
		else
			print = print .. '|' .. data.materialNH[material][2]
		end
		print = print .. '|link=|alt=]] '
	else
		print = print .. '[[File:Leaf NH Icon cropped.png'
		if not isEmpty(size) then
			print = print .. '|' .. size
		else
			print = print .. '|15px'
		end
		print = print .. '|link=|alt=]] '
	end
	
	if not isEmpty(num) then
		print = print .. num .. '× '
	else
		print = print .. ''
	end
	if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][3]) then
    	print = print .. '[[' .. data.materialNH[material][3] .. '|' .. data.materialNH[material][4] .. ']]'
    else
    	if not isEmpty(data.materialNH[material]) and not isEmpty(data.materialNH[material][5]) then
    		print = print .. data.materialNH[material][5]
    	else
    		print = print .. '[[Item:' .. material .. ' (New Horizons)|' .. material .. ']]'
    	end
	end
	print = print .. '</span>'
	if not isEmpty(nb) then
		print = print .. ''
	else
		print = print .. '<br/>'
	end
	return print
end

function p.outputMaterialPC (material, num, nb, size)
	local function isEmpty(s)
		return s == nil or s == ''
	end
	local print = '<span style="display:inline-block; white-space:nowrap;">'
	if not isEmpty(data.materialPC[material]) and not isEmpty(data.materialPC[material][1]) then
    	print = print .. '[[File:'.. data.materialPC[material][1]
		if not isEmpty(size) then
			print = print .. '|' .. size
		else
			print = print .. '|' .. data.materialPC[material][2]
		end
		print = print .. '|link=|alt=]] '
	end
	if not isEmpty(num) then
		print = print .. num .. '× '
	else
		print = print .. ''
	end
	if not isEmpty(data.materialPC[material]) and not isEmpty(data.materialPC[material][3]) then
    	print = print .. '[[' .. data.materialPC[material][3] .. '|' .. data.materialPC[material][4] .. ']]'
    else
    	if not isEmpty(data.materialPC[material]) and not isEmpty(data.materialPC[material][5]) then
    		print = print .. data.materialPC[material][5]
    	else
    		print = print .. '[[Item:' .. material .. ' (Pocket Camp)|' .. material .. ']]'
    	end
	end
	print = print .. '</span>'
	if not isEmpty(nb) then
		print = print .. ''
	else
		print = print .. '<br/>'
	end
	return print
end
return p