Module:CraftingTable

From Ylvapedia

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

local p = {}

function p.crafttable(f)

	local args = f:getParent().args
	local lang = f.args[1]
	local data = mw.loadData('Module:CraftingTable/i18n')
	local title = mw.title.getCurrentTitle()
	local Recipe = {
		{
			CraftingStation = args[data[lang]['CraftingStation']] or '',
			Ingredient = args[data[lang]['Ingredient']] or '',
			IngredientNum = args[data[lang]['IngredientNum']] or '',
			IngredientExtra = args[data[lang]['IngredientExtra']] or ''
		},
		{
			CraftingStation = args[data[lang]['CraftingStation']..'2'] or '',
			Ingredient = args[data[lang]['Ingredient']..'2'] or '',
			IngredientNum = args[data[lang]['IngredientNum']..'2'] or '',
			IngredientExtra = args[data[lang]['IngredientExtra']..'2'] or ''
		},
		{
			CraftingStation = args[data[lang]['CraftingStation']..'3'] or '',
			Ingredient = args[data[lang]['Ingredient']..'3'] or '',
			IngredientNum = args[data[lang]['IngredientNum']..'3'] or '',
			IngredientExtra = args[data[lang]['IngredientExtra']..'3'] or ''
		}
	}

	local CSs = { '', '', '' }
	local CSCategory = ''
	local Ings = { '', '', '' }
	local Results = {
		args[data[lang]['TitleResult']] or '',
		args[data[lang]['TitleResult']..'2'] or '',
		args[data[lang]['TitleResult']..'3'] or ''
	}

	for n = 1, 3 ,1 do

		if not Recipe[n].CraftingStation or Recipe[n].CraftingStation == '' then
			if n == 1 then CSs[1] = data[lang]['ErrorCraftingStation'] end
		else
			local v = {} ; v = mw.text.split(Recipe[n].CraftingStation, "%s*,%s*")
			f:callParserFunction( '#Set', 'CraftingStation', Recipe[n].CraftingStation )
			for i, item in ipairs(v) do

				if i > 1 then CSs[n] = CSs[n] .. '<br />' end

				if not item or item == '' then
					CSs[n] = CSs[n] .. data[lang]['BlankValue']
				else
					CSs[n] = CSs[n] .. f:expandTemplate{ title = data[lang]['TemplateName'], args = { item,size=imgsize } }
					if title.nsText == "Elin" then
						CSCategory = CSCategory..'[[Category:Elin '..item..']]'
					end
				end
			end
		end

		if not Recipe[n].Ingredient or Recipe[n].Ingredient == '' then
			if n == 1 then Ings[1] = data[lang]['ErrorIngredient'] end
		else
			local v = {} ; v = mw.text.split(Recipe[n].Ingredient, "%s*,%s*")
			local x = {} ; x = mw.text.split(Recipe[n].IngredientNum, "%s*,%s*")
			local y = {} ; y = mw.text.split(Recipe[n].IngredientExtra, "%s*,%s*")
			f:callParserFunction( '#Set', 'Ingredient', Recipe[n].Ingredient )
			f:callParserFunction( '#Set', 'IngredientNum', Recipe[n].IngredientNum )
			f:callParserFunction( '#Set', 'IngredientExtra', Recipe[n].IngredientExtra )
			for i, item in ipairs(v) do

				if i > 1 and check == false then Ings[n] = Ings[n] .. '<br />' end
				check = false

				if not item or item == '' then
					Ings[n] = Ings[n] .. data[lang]['BlankValue']
				else
					local z = string.find(item, "/")
					if z then
						local z = {} ; z = mw.text.split(item, "%s*/%s*")
						local z2 = {} ; z2 = mw.text.split(x[i], "%s*/%s*") or 1
						local z3 = {} ; z3 = mw.text.split(y[i], "%s*/%s*") or ''
						for i2, item2 in ipairs(z) do
							if i2 > 1 then
								Ings[n] = Ings[n] .. '<div class="or-separator">'..data[lang]['Or']..'</div>'
							end
							if z2[i2] == '' then
								Ings[n] = Ings[n] .. f:expandTemplate{ title = data[lang]['TemplateName'], args = { item2,size=imgsize } }
							else
								Ings[n] = Ings[n] .. f:expandTemplate{ title = data[lang]['TemplateName'], args = { item2,z2[i2],size=imgsize } }
							end

							if z3[i2] == data[lang]['TitleExtra'] then
								Ings[n] = Ings[n] .. data[lang]['TextExtra']
							elseif z3[i2] ==  data[lang]['TitleGourmet'] then
								Ings[n] = Ings[n] .. data[lang]['TextGourmet']
							end
						end
						Ings[n] = Ings[n] .. '<div class="separator">&nbsp;</div>'
						check = true
					else
						local v2 = string.find(item, "@")
						if v2 then
							local v2 = {} ; v2 = mw.text.split(item, "%s*@%s*")
							Ings[n] = Ings[n] .. f:expandTemplate{ title = data[lang]['TemplateName'], args = { v2[1],v=v2[2],size=imgsize } }
						else
							if x[i] == '' then
								Ings[n] = Ings[n] .. f:expandTemplate{ title = data[lang]['TemplateName'], args = { item,size=imgsize } }
							else
								Ings[n] = Ings[n] .. f:expandTemplate{ title = data[lang]['TemplateName'], args = { item,x[i],size=imgsize } }
							end
						end

						if y[i] == data[lang]['TitleExtra'] then
							Ings[n] = Ings[n] .. data[lang]['TextExtra']
						elseif y[i] ==  data[lang]['TitleGourmet'] then
							Ings[n] = Ings[n] .. data[lang]['TextGourmet']
						end
					end

				end

			end
		end

		if not Results[n] or Results[n] == '' then
		else
			z = string.find(Results[n], "%s*/%s*")
			if z then
				local x = {} ; x = mw.text.split(Results[n], "%s*/%s*")
				Results[n] = f:expandTemplate{ title = data[lang]['TemplateName'], args = { x[1],x[2],size=imgsize } }
				f:callParserFunction( '#Set', 'CraftingResult', x[1] )
				f:callParserFunction( '#Set', 'CraftingResultNum', x[2] )
			else
				Results[n] = f:expandTemplate{ title = data[lang]['TemplateName'], args = { Results[n],size=imgsize } }
				f:callParserFunction( '#Set', 'CraftingResult', Results[n] )
			end
		end
	end


	local Craftingskill = args[data[lang]['CraftSkill']] or nil
	local CraftingskillLv = args[data[lang]['CraftSkillLv']] or nil

	local html = mw.html.create('p')
	local div = html:tag('div'):addClass('CraftingTable'):cssText('float: left; display: ruby;')
	local table = div:tag('table'):addClass('wikitable')
		if not Craftingskill and not CraftingskillLv then
		else
			local Skills = ''

			if not Craftingskill then
				Skills = '?'..data[lang]['TitleSkill']
			else
				Skills = Craftingskill..data[lang]['TitleSkill']
				f:callParserFunction( '#Set', 'CraftingSkill', Craftingskill )
			end

			Skills = Skills .. '&nbsp;'

			if not CraftingskillLv then
				Skills = Skills .. 'Lv.?'
			else
				Skills = Skills .. 'Lv.'.. CraftingskillLv
				f:callParserFunction( '#Set', 'CraftingSkillLv', CraftingskillLv )
			end

			if Results ~= '' then colspan = 3
			else colspan = 2 end
			table:tag('tr'):tag('th'):attr('colspan',colspan):wikitext(Skills)
		end

		for n = 1, 3 ,1 do
			if CSs[n] ~= '' or Ings[n] ~= '' or Results[n] ~= '' then
				if n > 1 then
					table = div:tag('table'):addClass('wikitable'):cssText('margin-left:1em;')
				end
				local tr1 = table:tag('tr')
				tr1:tag('th'):wikitext(data[lang]['TitleCraftingStation'])
				tr1:tag('th'):wikitext(data[lang]['TitleIngredient'])
				if Results[n] ~= '' then
					tr1:tag('th'):wikitext(data[lang]['TitleResult'])
				end
				local tr2 = table:tag('tr')
				tr2:tag('td'):wikitext(CSs[n])
				tr2:tag('td'):cssText('text-align: center;'):wikitext(Ings[n])
				if Results[n] ~= '' then
					tr2:tag('td'):cssText('text-align: center;'):wikitext(Results[n])
				end
			end
		end

		html:wikitext(string.format('<div style="clear:both;"></div>'))

		local ObtainRecipe = args[data[lang]['ObtainRecipe']] or nil
		if not ObtainRecipe or ObtainRecipe == '' then
		else
			html:wikitext(string.format('<h3>'.. data[lang]['TitleObtainRecipe'] ..'</h3>' .. ObtainRecipe))
			f:callParserFunction( '#Set', 'ObtainRecipe', ObtainRecipe )
		end

		if title.nsText == "Elin" then
			html:wikitext(string.format(data[lang]['Category']))
		end

		if not CSCategory or CSCategory == '' then
		else
			html:wikitext(CSCategory)
		end

	return tostring(html)
	
end

return p