Attention Mobile version Mobile Users! Registration for Mobile 1.3 Beta Testing is live! Register here!

Module:Crafts/row

From Terraria Wiki
Jump to: navigation, search

Internal sub-module pertaining to the set of {{crafts}}.

Creates a row in the crafting table.


local ingredient_format = require('Module:Crafts/ingredient').format
local item_link = require('Module:Item').go

local result_cell = function(frame, result_info)
	local item, nolink, image, id, icons, amount, version = unpack(mw.text.split(result_info, '¦', true))
	if version ~= '' then
		icons='n'
	end
	local str = item_link(frame, {item, nolink=nolink, image=image, id=id, icons=icons, small='y'})..amount
	if version ~= '' then
		str = str..' ('..frame:expandTemplate{ title = 'version icons', args = {version} }..')'
	end
	return str
end

local row = function(frame, row_info, page)
	if row_info == '' then
		return ''
	end
	
	local result_info, ingredients_info = unpack(mw.text.split(row_info, '«', true))

	-- result cell
	local str = '<tr><td>'..result_cell(frame, result_info)
	--ingredients cell
	str = str .. '</td><td>' .. ingredient_format(frame, ingredients_info, page).. '</td></tr>'

	return str
end

local group = function(frame)

	-- {{ROOTPAGENAME}}
	local page = frame.args['page']

	-- first row: result, ingredient list, craftstation(if needed)
	local tool_info, result_info, ingredients_info = frame.args['group_buffer_first']:match("^(.-)«(.-)«(.-)$")

	-- result cell
	local str = result_cell(frame, result_info)
	-- ingredients cell
	str = str .. '</td><td>'..ingredient_format(frame, ingredients_info, page).. '</td>'
	-- craft station
	if frame.args['nostation'] == '' then
		str = str .. '<td class="station" rowspan="'..frame.args['group_count']..'">' ..frame:expandTemplate{title = 'crafts/tool', args = {tool_info}}..'</td>'
	end
	str = str .. '</tr>'

	-- other rows in this group:(only result and ingredient list cells.)
	for row_info in mw.text.gsplit(frame.args['group_buffer'], '»', true) do
		str = str .. row(frame, row_info, page)
	end
	return str
end


-- main return object 
return {

-- serialize args
serialize = function(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		args[k] = v
	end
	args['tool'] = frame.args['tool']
	args['link'] = nil
	args['id'] = nil
	args['icons'] = nil
	local str = ''
	for k, v in pairs( args ) do
		str = str .. '»' .. k .. '¦' .. mw.text.trim(v)
	end
	return str
end,


-- restore crafts row using serialized args
restore = function(frame)
	local args = {}
	for str in mw.text.gsplit(frame.args[1], '»', true) do
		if str~='' then
			local k,v = unpack(mw.text.split(str, '¦', true))
			args[k] = v
		end
	end
	args['link'] = nil
	args['id'] = nil
	args['icons'] = nil
	return frame:expandTemplate{ title = 'crafts row', args = args }
end,


-- format

format = function(frame)
	local str = ''
	if frame.args['new'] ~= '' then
		if frame.args['group_count'] ~= '' then
			str = '</div>'..group(frame)..'<tr><td><div style="display:none">'
		end
		frame:preprocess('{{#dplvar:set|group_buffer_first|{{#dplvar:group_tool}}«{{#dplvar:_result_info}}«{{#dplvar:_ingredients_info}}|group_count|1|group_buffer|}}')
	else
		frame:preprocess('{{#dplvar:set|group_count|{{#expr:{{#dplvar:group_count}}+1}}|group_buffer|{{#dplvar:group_buffer}}»{{#dplvar:_result_info}}«{{#dplvar:_ingredients_info}}}}')
	end
	return str
end,

lastgroup = function(frame)
	-- empty table.
	if frame.args['group_count'] == '' then
		return '</td></tr>'
	else
		return group(frame)
	end
end,

}