Jump to: navigation, search

Module:Crafting DPL

Revision as of 14:17, 6 April 2014 by Mastergalen (talk | contribs) (Created page with "local p = {} function p.dpl( f ) local debug = f:callParserFunction( '#dplvar:debug' ) local args = require( 'Module:ProcessArgs' ).merge() if args.ignoreusage then return ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}
function p.dpl( f )
	local debug = f:callParserFunction( '#dplvar:debug' )
	local args = require( 'Module:ProcessArgs' ).merge()
	if args.ignoreusage then return end
	
	local title = f:callParserFunction( '#dplvar:craftingdplingredient' )
	if title == '' then
		title = mw.title.getCurrentTitle().text
	end
	
	local craftingArgs = {
		args[1] or args.A1 or '', args[2] or args.B1 or '', args[3] or args.C1 or '',
		args[4] or args.A2 or '', args[5] or args.B2 or '', args[6] or args.C2 or '',
		args[7] or args.A3 or '', args[8] or args.B3 or '', args[9] or args.C3 or '',
		Output = args.Output
	}
	
	local matchType = f:callParserFunction( '#dplvar:craftingdplmatch' )
	function matchPattern( ingredient )
		local pattern
		local escaped = mw.ustring.lower( ingredient ):gsub( '([%(%)])', '%%%1' )
		if matchType == 'start' then
			pattern = '[;:%]]%s*' .. escaped
		elseif matchType == 'end' then
			pattern = escaped .. '%s*[,;%[]'
		elseif matchType == 'any' then
			pattern = escaped
		else
			pattern = '[;:%]]%s*' .. escaped .. '%s*[,;%[]'
		end
		
		return pattern
	end
	
	local usedRecipes = f:callParserFunction( '#dplvar:craftingdplrecipes' )
	local hasIngredient
	for _, v in ipairs( craftingArgs ) do
		if ( ';' .. mw.ustring.lower( v ) .. ';' ):find( matchPattern( title ) ) then
			if usedRecipes:find( matchPattern( args.Output ) ) then return end
			if not v:find( ';' ) then
				hasIngredient = 'static'
			elseif not hasIngredient then
				hasIngredient = 'animated'
			end
		end
	end
	if not hasIngredient then return end
	
	local maxFrames = 1
	local requiredFrames = {}
	if hasIngredient == 'animated' then
		for _, v in ipairs( craftingArgs ) do
			if v:find( ';' ) then
				local frameNum = 0
				for frame in mw.text.gsplit( v, '%s*;%s*' ) do
					frameNum = frameNum + 1
					if frameNum > maxFrames then maxFrames = frameNum end
					
					if not requiredFrames[frameNum] and ( ';' .. mw.ustring.lower( frame ) .. ';' ):find( matchPattern( title ) ) then
						requiredFrames[frameNum] = true
					end
				end
			end
		end
	end
	
	if maxFrames > 1 then
		local frames = {}
		for k, arg in pairs( craftingArgs ) do
			if arg:find( ';' ) then
				local frameNum = 0
				for frame in mw.text.gsplit( arg, '%s*;%s*' ) do
					frameNum = frameNum + 1
					if requiredFrames[frameNum] then
						if not frames[k] then frames[k] = {} end
						table.insert( frames[k], frame )
					end
				end
				
				frames[k] = table.concat( frames[k], ';' )
			else
				frames[k] = arg
			end
		end
		f:callParserFunction( '#dplvar:set', 'craftingdplrecipes', usedRecipes .. ';' .. mw.ustring.lower( frames.Output ) .. ';' )
		
		if args[1] then
			local argNum = 0
			for _, arg in ipairs( frames ) do
				if arg ~= '' then
					argNum = argNum + 1
					args[argNum] = arg
				end
			end
			args.Output = frames.Output
		else
			args.A1 = frames[1]; args.B1 = frames[2]; args.C1 = frames[3]
			args.A2 = frames[4]; args.B2 = frames[5]; args.C2 = frames[6]
			args.A3 = frames[7]; args.B3 = frames[8]; args.C3 = frames[9]
			args.Output = frames.Output
		end
		
		-- If not all frames are used, overwrite custom name and ingredient values
		if maxFrames > #requiredFrames then
			args.name = nil
			args.ingredients = nil
		end
	else
		f:callParserFunction( '#dplvar:set', 'craftingdplrecipes', usedRecipes .. ';' .. mw.ustring.lower( args.Output ) .. ';' )
	end
	
	if debug == '1' then
		args.name = 'From: [[' .. args['%PAGE%'] .. ']]'
	end
	
	local crafting = require( 'Module:Crafting' ).table
	args.head = '1'
	args.showname = nil
	args.foot = nil
	args.nocat = '1'
	args.showdescription = f:callParserFunction( '#dplvar:craftingdescription' )
	return crafting( args )
end
return p


Cookies help us deliver our services. By using our services, you agree to our use of cookies.