Module:Recetas

De Wiki Calamity Mod Oficial
Ir a la navegación Ir a la búsqueda
Lua.svg Documentación La documentación a continuación se incluye desde Module:Recetas/doc. (editar | historial)
[[File:
Cog (Calamity).png
|{{{tamaño-imagen}}}|link=]]
{{{título}}}
{{{texto}}}

local p = {}
local Recipes = mw.loadData( 'Module:Recetas/datos' )

local stations = {
	[ 'Iron Anvil' ] = 'Lead Anvil',
	[ 'Adamantite Forge' ] = 'Titanium Forge',
	[ 'Mythril Anvil' ] = 'Orichalcum Anvil',
	[ 'Demon Altar' ] = 'Crimson Altar',
	[ 'Cooking Pot' ] = 'Cauldron',
	[ 'Placed Bottle' ] = 'Alchemy Table',
	[ 'Water' ] = 'Sink',
}
	
local function lc( name )
	local frame = mw.getCurrentFrame()
	return frame:expandTemplate{
		title = 'LC',
		args = { name }
	}
end

local function get_station_equivalent( name )
	local split_by_and = mw.text.split( name, 'and' )
	if #split_by_and > 1 then
		local result = {}
		for _, item in ipairs( split_by_and ) do
			result[ #result + 1 ] = get_station_equivalent( mw.text.trim( item ) ) -- recursion!
		end
		return table.concat( result, ', ' )
	elseif name:find( 'only' ) ~= nil then
		return mw.text.trim( name:gsub( 'only', '' ) )
	end
	
	if stations[ name ] then
		return ( '%s, %s' ):format( name, stations[ name ] )
	else
		return name
	end
end

function p.craft_item( frame )
	local args = frame:getParent().args
	local name = frame:callParserFunction{ name = '#titleparts', args = { args[ 1 ] or tostring( mw.title.getCurrentTitle() ) } }
	local data = Recipes[ name ]
	if not data then
		return ': \'\'No hay información de recetas para este objeto. Es posible que haga falta una actualización de datos.\'\''
	end
	
	local templates = {}
	for _, recipe_data in ipairs( data ) do
		if not recipe_data.historical then
			local args = {
				estaciones = get_station_equivalent( recipe_data.station ),
				resultado = name,
				[ 'resultado cantidad' ] = recipe_data.amount
			}
			
			local idx = 0
			for material, qty in pairs( recipe_data.materials ) do
				idx = idx + 1
				args[ 'ingrediente ' .. idx ] = material
				args[ 'cantidad ' .. idx ] = qty
			end
			
			templates[ #templates + 1 ] = frame:expandTemplate{
				title = 'Receta',
				args = args
			}
		end
	end
	
	return table.concat( templates, '' )
end

function p.item_uses( frame )
	local args = frame:getParent().args
	local name = frame:callParserFunction{ name = '#titleparts', args = { args[ 1 ] or tostring( mw.title.getCurrentTitle() ) } }
	
	local used_in = {}
	for creation, recipes in pairs( Recipes ) do
		for _, recipe in ipairs( recipes ) do
			if recipe.materials[ name ] then
				used_in[ #used_in + 1 ] = {
					creation = creation,
					recipe = recipe
				}
			end
		end
	end
	
	local args = {}
	local idx = 0
	for _, data in ipairs( used_in ) do
		idx = idx + 1
		local use = data.recipe
		local materials = {}
		
		for material, qty in pairs( use.materials ) do
			materials[ #materials + 1 ] = lc( material ) .. ( ' (%s)' ):format( qty )
		end
		
		local arg_index = idx == 1 and '' or idx
		
		local station = lc( use.station )
		if stations[ use.station ] then
			station = station .. '<br />' .. lc( stations[ use.station ] )
		end
		args[ 'estación' .. arg_index ] = station
		args[ 'resultado' .. arg_index ] = lc( data.creation )
		args[ 'ingredientes' .. arg_index ] = table.concat( materials, '<br />' )
	end
	
	return frame:expandTemplate{
		title = 'Creaciones',
		args = args
	}
end

function p.created_in_station( frame )
	local args = frame:getParent().args
	local name = frame:callParserFunction{ name = '#titleparts', args = { args[ 1 ] or tostring( mw.title.getCurrentTitle() ) } }
	local templates = {}
	
	local used_in = {}
	for creation, recipes in pairs( Recipes ) do
		for _, recipe in ipairs( recipes ) do
			if recipe.station == name then
				used_in[ #used_in + 1 ] = {
					creation = creation,
					recipe = recipe
				}
			end
		end
	end
	
	for _, data in ipairs( used_in ) do
		local use = data.recipe
		local materials = {}
		
		for material, _ in pairs( use.materials ) do
			materials[ #materials + 1 ] = lc( material )
		end
		
		templates[ #templates + 1 ] = frame:expandTemplate{
			title = 'Creaciones',
			args = {
				resultado = lc( data.creation ),
				ingredientes = table.concat( materials, '<br />' )
			}
		}
	end
	
	return table.concat( templates, '\n<br />\n' )
end

function p.inline( frame )
	local args = frame:getParent().args
	local name = frame:callParserFunction{ name = '#titleparts', args = { args[ 1 ] or tostring( mw.title.getCurrentTitle() ) } }
	local recipe = nil
	for result, data in pairs( Recipes ) do
		if result == name then
			recipe = data[ 1 ]
		end
	end
	
	if not recipe then
		error( 'No hay una receta para fabricar ' .. name )
	end
	
	local materials = {}
	for material, qty in pairs( recipe.materials ) do
		materials[ #materials + 1 ] = ( '%s [[File:%s.png|x18px|link=%s]]' ):format( qty, material, material )
	end
	
	local at = mw.html.create( 'span' ):wikitext( '@' ):css{
		[ 'font-weight' ] = 'bold',
		[ 'text-shadow' ] = '0 0 8px #f06',
		[ 'cursor' ] = 'pointer'
	}
	local ws = ' ' -- whitespace
	
	return table.concat( materials, ' + ' ) .. ws .. tostring( at ) .. ws .. ( '[[File:%s.png|x18px|link=%s]]' ):format( recipe.station, recipe.station )
end

function p.test()
	local frame = mw.getCurrentFrame()
	frame.getParent = function ()
		return {
			args = { 'Draedon\'s Forge' }
		}
	end
	return p.created_in_station( frame )
end

return p