Module:Crafting

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

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

function p.createAcquisitionTable(frame)
	local str = mw.html.create('table'):addClass('table table-dark'):cssText('width:auto')
		
	for dataKey, dataValue in pairs(data) do
		for objectKey, objectValue in pairs(dataValue.canCraftObjects) do
			if objectValue.internalName == frame.args.name then
				local result = objectValue.amount == 1 and {objectValue.name} or {objectValue.name, objectValue.amount}
				local materials = combineMaterials(frame, objectValue, '<br>')
				
				str:tag('tr')
					:tag('td'):wikitext('Crafting station')
					:tag('td'):wikitext(frame:expandTemplate{title = 'Item', args = {dataValue.name}})
				str:tag('tr')
					:tag('td'):wikitext('Result')
					:tag('td'):wikitext(frame:expandTemplate{title = 'Item', args = result})
				str:tag('tr')
					:tag('td'):wikitext('Materials')
					:tag('td'):wikitext(materials)
			end
		end
	end
	
	return str
end

function p.createCraftingTable(frame)
	local item = data[frame.args.name]
	local str = mw.html.create('table'):addClass('table table-dark')
	str:tag('tr')
		:tag('th'):wikitext('Crafting station')
		:tag('th'):wikitext('Result')
		:tag('th'):wikitext('Materials')
		
	for objectKey, objectValue in ipairs(item.canCraftObjects) do
		local result = objectValue.amount == 1 and {objectValue.name} or {objectValue.name, objectValue.amount}
		local materials = combineMaterials(frame, objectValue)
		
		str:tag('tr')
			:tag('td'):wikitext(frame:expandTemplate{title = 'Item', args = {item.name}})
			:tag('td'):wikitext(frame:expandTemplate{title = 'Item', args = result})
			:tag('td'):wikitext(materials)
	end
	
	return str
end

function p.createMaterialTable(frame)
	local str = mw.html.create('table'):addClass('table table-dark')
	str:tag('tr')
		:tag('th'):wikitext('Crafting station')
		:tag('th'):wikitext('Result')
		:tag('th'):wikitext('Materials')
		
	for dataKey, dataValue in pairs(data) do
		for objectKey, objectValue in ipairs(dataValue.canCraftObjects) do
			for matsKey, matsValue in ipairs(objectValue.requiredObjectsToCraft) do
				if matsValue.internalName == frame.args.name then
					local result = objectValue.amount == 1 and {objectValue.name} or {objectValue.name, objectValue.amount}
					local materials = combineMaterials(frame, dataValue.canCraftObjects[objectKey])
					
					str:tag('tr')
						:tag('td'):wikitext(frame:expandTemplate{title = 'Item', args = {dataValue.name}})
						:tag('td'):wikitext(frame:expandTemplate{title = 'Item', args = result})
						:tag('td'):wikitext(materials)
				end
			end
		end
	end
	
	return str
end

function p.createCraftingTableWithHeader(frame)
	local item = data[frame.args.name]
	local str = mw.html.create()
	str:tag('h3'):wikitext('[[File:' .. item.name .. '.png|32x32px|link=' .. item.name .. ']] [[' .. item.name .. ']]'):newline()
	local strTable = str:tag('table'):addClass('table table-dark sortable')
		:tag('tr')
			:tag('th'):wikitext('Result'):attr('width', '1%')
			:tag('th'):wikitext('Materials')
		
	for objectKey, objectValue in ipairs(item.canCraftObjects) do
		local result = objectValue.amount == 1 and {objectValue.name} or {objectValue.name, objectValue.amount}
		local materials = combineMaterials(frame, objectValue)
		local strRow = strTable:tag('tr')
			:tag('td'):wikitext(frame:expandTemplate{title = 'Item', args = result})
			:tag('td'):wikitext(materials)
	end
	
	return str
end

function p.createCondensedCraftingRow(frame)
	local item = data[frame.args.name]
	local results = ''
	for k, v in ipairs(item.canCraftObjects) do
		results = results .. frame:expandTemplate{title = 'Item', args = {v.name}} .. ' '
	end
	
	local str = mw.html.create('')
		:wikitext('|-'):newline()
		:wikitext('| align=center | [[File:' .. item.name .. '.png|link=' .. item.name .. ']]'):newline()
		:wikitext('| [[' .. item.name .. ']]'):newline()
		:wikitext('| ' .. results):newline()
		
	return str
end

function combineMaterials(frame, object, separator)
	local separator = separator == nil and ' ' or separator
	local str = ''
	for k, v in ipairs(object.requiredObjectsToCraft) do
		str = str .. frame:expandTemplate{title = 'Item', args = {v.name, v.amount}} .. separator
	end
	return str
end

return p