Module:CraftingTable

From Ylvapedia
Revision as of 14:23, 31 March 2025 by Neko Knight (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

local function isBlank(value)
	return value == nil or not value or value == ''
end

local function TSafely(f, templateName, args)
	local result = f:expandTemplate{ title=templateName, args=args }
	if not result then
		mw.log('Template '..templateName..' expansion failed')
		return ''
	end
	return result
end

function p.crafttable(f)
	local args = f:getParent().args
	local lang = f.args[1]
	local i18n = mw.loadData('Module:CraftingTable/i18n') or {}
	local title = mw.title.getCurrentTitle()
	local recipes = {
		{
			craftingStation = args[i18n[lang]['CraftingStation']] or '',
			ingredient = args[i18n[lang]['Ingredient']] or '',
			ingredientNum = args[i18n[lang]['IngredientNum']] or '',
			ingredientExtra = args[i18n[lang]['IngredientExtra']] or '',
			craftResult = args[i18n[lang]['TitleResult']] or '',
		},
		{
			craftingStation = args[i18n[lang]['CraftingStation']..'2'] or '',
			ingredient = args[i18n[lang]['Ingredient']..'2'] or '',
			ingredientNum = args[i18n[lang]['IngredientNum']..'2'] or '',
			ingredientExtra = args[i18n[lang]['IngredientExtra']..'2'] or '',
			craftResult = args[i18n[lang]['TitleResult']..'2'] or '',
		},
		{
			craftingStation = args[i18n[lang]['CraftingStation']..'3'] or '',
			ingredient = args[i18n[lang]['Ingredient']..'3'] or '',
			ingredientNum = args[i18n[lang]['IngredientNum']..'3'] or '',
			ingredientExtra = args[i18n[lang]['IngredientExtra']..'3'] or '',
			craftResult = args[i18n[lang]['TitleResult']..'3'] or '',
		}
	}

	local css = { '', '', '' }
	local csCategory = ''
	local ingredients = { '', '', '' }
	local results = { '', '', '' }

	for tableNum = 1, 3 do
		local recipe = recipes[tableNum]

		if isBlank(recipe.craftingStation) then
			if tableNum == 1 then css[1] = i18n[lang]['ErrorCraftingStation'] end
		else
			local craftingStations = mw.text.split(recipe.craftingStation, '%s*,%s*')
			if tableNum == 1 then
				f:callParserFunction('#Set','CraftingStation', recipe.craftingStation)
			else
				f:callParserFunction('#Set','CraftingStation'..tableNum, recipe.craftingStation)
			end
			css[tableNum] = ''
			for i, item in ipairs(craftingStations) do
				if i > 1 then css[tableNum] = css[tableNum]..'<br />' end
				css[tableNum] = css[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{item,size=imgsize})
				if title.nsText == 'Elin' then csCategory = csCategory..'[[Category:Elin '..item..']]' end
			end
		end

		if isBlank(recipe.ingredient) then
			if tableNum == 1 then ingredients[1] = i18n[lang]['ErrorIngredient'] end
		else
			local ingredientItems = mw.text.split(recipe.ingredient, '%s*,%s*')
			local ingredientNums = mw.text.split(recipe.ingredientNum, '%s*,%s*') or {}
			local ingredientExtras = mw.text.split(recipe.ingredientExtra, '%s*,%s*') or {}
			if tableNum == 1 then
				f:callParserFunction('#Set','Ingredient', recipe.ingredient)
				f:callParserFunction('#Set','IngredientNum', recipe.ingredientNum)
				f:callParserFunction('#Set','IngredientExtra', recipe.ingredientExtra)
			else
				f:callParserFunction('#Set','Ingredient'..tableNum, recipe.ingredient)
				f:callParserFunction('#Set','IngredientNum'..tableNum, recipe.ingredientNum)
				f:callParserFunction('#Set','IngredientExtra'..tableNum, recipe.ingredientExtra)
			end
			ingredients[tableNum] = ''
			for i, item in ipairs(ingredientItems) do
				if i > 1 then
					ingredients[tableNum] = ingredients[tableNum]..'<div class="plus or-separator">&plus;</div>'
				end
				local itemName, itemVar = item:match('([^@]+)@([^@]+)')
				local itemNum = ingredientNums[i]
				local itemExt = ingredientExtras[i]

				if string.find(item, '/') then
					local subItems = mw.text.split(item, '%s*/%s*')
					local subNums = ingredientNums[i] and mw.text.split(ingredientNums[i], '%s*/%s*') or {}
					local subExtras = ingredientExtras[i] and mw.text.split(ingredientExtras[i], '%s*/%s*') or {}
					for j, subItem in ipairs(subItems) do
						if j > 1 then
							ingredients[tableNum] = ingredients[tableNum]..'<div class="or-separator">'..i18n[lang]['Or']..'</div>'
						end
						local subItemName, subItemVar = subItem:match('([^@]+)@([^@]+)')
						local subItemNum = subNums[j] or 1
						local subItemExt = subExtras[j] or ''
						if subItemVar then
							if not subItemNum then
								ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{subItemName,v=subItemVar,link='Elin:'..subItemName..' '..subItemVar,size=imgsize})
							else
								ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{subItemName,subItemNum,v=subItemVar,link='Elin:'..subItemName..' '..subItemVar,size=imgsize})
							end
						else
							if not subItemNum then
								ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{subItem,size=imgsize})
							else
								ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{subItem,subItemNum,size=imgsize})
							end
						end
						if subItemExt == i18n[lang]['TitleExtra'] then ingredients[tableNum] = ingredients[tableNum]..i18n[lang]['TextExtra']
						elseif subItemExt == i18n[lang]['TitleGourmet'] then ingredients[tableNum] = ingredients[tableNum]..i18n[lang]['TextGourmet'] end
					end
				elseif itemVar then
					if not itemNum then
						ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{itemName,v=itemVar,link='Elin:'..itemName..' '..itemVar,size=imgsize})
					else
						ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{itemName,itemNum,v=itemVar,link='Elin:'..itemName..' '..itemVar,size=imgsize})
					end
				else
					if not itemNum then
						ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{item,size=imgsize})
					else
						ingredients[tableNum] = ingredients[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{item,itemNum,size=imgsize})
					end
				end
				if itemExt == i18n[lang]['TitleExtra'] then ingredients[tableNum] = ingredients[tableNum]..i18n[lang]['TextExtra']
				elseif itemExt == i18n[lang]['TitleGourmet'] then ingredients[tableNum] = ingredients[tableNum]..i18n[lang]['TextGourmet'] end
			end
		end

		if not isBlank(recipe.craftResult) then
			f:callParserFunction('#Set','CraftingResult'..tableNum, recipe.craftResult)
			local resultItems = mw.text.split(recipe.craftResult, '%s*,%s*')
			results[tableNum] = ''
			for i, item in ipairs(resultItems) do
				if i > 1 then
					results[tableNum] = results[tableNum]..'<div class="or-separator">'..i18n[lang]['Or']..'</div>'
				end
        local subItemName = item
				local subItemVar
				local subItemNum = 1
        local match1 = item:match('([^@]+)@([^/]+)/([^/]+)')
        local match2 = item:match('([^@]+)@([^@]+)')
        local match3 = item:match('([^/]+)/([^/]+)')
        if match1 then
          subItemName, subItemVar, subItemNum = item:match('([^@]+)@([^/]+)/([^/]+)')
        elseif match2 then
          subItemName, subItemVar = item:match('([^@]+)@([^@]+)')
        elseif match3 then
          subItemName, subItemNum = item:match('([^/]+)/([^/]+)')
        end
        if match1 or match2 then
          results[tableNum] = results[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{subItemName,subItemNum,v=subItemVar,link='Elin:'..subItemName..' '..subItemVar,size=imgsize})
        else
          results[tableNum] = results[tableNum]..TSafely(f,i18n[lang]['TemplateName'],{subItemName,subItemNum,size=imgsize})
        end
			end
		end
	end

	local craftingSkill = args[i18n[lang]['CraftSkill']]
	local craftingSkillLv = args[i18n[lang]['CraftSkillLv']]

	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 craftingSkill or craftingSkillLv then
		local skills = (isBlank(craftingSkill) and '?' or craftingSkill)..i18n[lang]['TitleSkill']..'&nbsp;'..(isBlank(craftingSkillLv) and 'Lv.?' or 'Lv.'..craftingSkillLv)
		f:callParserFunction('#Set','CraftingSkill', craftingSkill)
		f:callParserFunction('#Set','CraftingSkillLv', craftingSkillLv)
		table:tag('tr'):tag('th'):attr('colspan', results[1] ~= '' and 3 or 2):wikitext(skills)
	end

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

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

	local craftBonus = args[i18n[lang]['CraftBonus']]
	if isBlank(craftBonus) then
	else
		html:tag('li'):wikitext('<b>'..i18n[lang]['TitleCraftBonus']..'</b> : '..craftBonus):done()
		f:callParserFunction('#Set','CraftBonus', craftBonus)
	end

	local obtainRecipe = args[i18n[lang]['ObtainRecipe']]
	if isBlank(obtainRecipe) then
	else
		html:tag('li'):wikitext('<b>'..i18n[lang]['TitleObtainRecipe']..'</b> : '..obtainRecipe):done()
		f:callParserFunction('#Set','ObtainRecipe', obtainRecipe)
	end

	if title.nsText == 'Elin' then
		html:wikitext(i18n[lang]['Category'])
		if csCategory ~= '' then
			html:wikitext(csCategory)
		end
	end

	return tostring(html)
	
end

return p