Module:RecipeTable

From Eco - English Wiki
Revision as of 01:33, 6 October 2020 by ZeelNightwolf (talk | contribs) (Reverts the Images to no BG or Frame until we figure out how to fix error for tag materials)

This module is a part of the Template:GetRecipes, and is used to generate a wikitable of recipes. It returns a wikitable as a string. The wikitable contains all of the recipes that were passed into the module.

If the template is passed, this module is used with the following Modules:


local p = {}

-- Header for the wikitable
function header( args )
    local headerStr = '{| class=\"wikitable mw-collapsible\" style=\"text-align: center;\"\n|-\n'

    -- Show or hide the Crafting Station column
    if args[1] == '1' then
        headerStr = headerStr .. '! Crafting Station !'
    end
	-- Item, Level Needed, Materials, Crafting Time, Labour Cost and XP Gained Specialty
     headerStr = headerStr .. '! colspan=\"4\" | Item !! colspan=\"4\" | Materials !! Level Needed !! Crafting Time <br>(mins) !! Labour Cost || XP Gain !!\n'
    return headerStr
end

function imagedisp( name )
    local str = ''
    local image
    -- Manual override for image file
    if name then
        image = string.gsub(name, ' ', '') .. '_Icon.png'
	else
		image = 'NoImage.png'
	end
		str = str .. '[[File:' .. image .. '|frameless|50px|link=' .. name .. ']] <br> [[' .. name .. ']]'
	return str
end

function stationcell( args )
  local str = ''
	str = str .. '| ' .. imagedisp(args[1])
	return str
end


function itemcell( args )
    local str = ''
	str = str .. '| ' .. imagedisp(args[1])
	str = str .. '<br>x' .. args[2] .. '\n'
	return str
end

function skillreqcell( args )
    local str = ''
	str = str .. '| ' .. imagedisp(args[1])
	str = str .. '<br> Level ' .. args[2] .. ' \n'
	return str
end

function groupedskillcell( args )
    local str = ''
	str = str .. '| ' .. imagedisp(args[1])
	if args[2] ~= nil then
		str = str .. ' <br> ' .. imagedisp(args[2])
	end
	return str
end

-- Create a wikitable of recipes
function p.main( recipes )
    local rows = ''
	
    --Not relevant for v0.9
    -- find number of times each Affect by Skill occurs
    --local prevSkill = recipes[1].efficiencySkills[1]
    --local skillIndex = {}
    --local prevSkillnum = 0
	--local count = 0
    --for num = 1, #recipes do
		--if prevSkill ~= recipes[num].efficiencySkills[1] then
			--skillIndex[#skillIndex + 1] = prevSkillnum + count
			--prevSkillnum = 1
			--if #recipes[num].ingredients > 4 then
				--count = 1
			--else
				--count = 0
			--end
			--prevSkill = recipes[num].efficiencySkills[1]
		--else
			--prevSkillnum = prevSkillnum + 1
			--if #recipes[num].ingredients > 4 then
				--count = count + 1
			--end
		--end		
    --end
    --skillIndex[#skillIndex + 1] = prevSkillnum + count
	
    -- Get each row
    local j = 0
    local k = 1
    for i = 1, #recipes do

	-- get information from the current recipe for building
		local checkImage = recipes[i].checkImage
		local craftStn = recipes[i].craftStn
		local skillNeeds = recipes[i].skillNeeds
		--use last variant as this is always the default variant
		local numberOfVariants = recipes[i].numberOfVariants
		local products = {{'',''}}
		local ingredients = {{'',''}}
		
		--loop through variants to select default variant ingredients
		for k, v in pairs(recipes[i].variants) do
			--given pairs doesn't guarantee order, check that product matches default variant
			if v.products[1][1] == recipes[i].defaultVariant then
				ingredients = v.ingredients
				products = v.products
			else
				--handle scenarios where product ~= default variant (for example Refine Tallow)
				ingredients = v.ingredients
				products = v.products			
			end
		end
		local baseCraftTime = recipes[i].baseCraftTime
		local baseLaborCost = recipes[i].baseLaborCost
		local baseXPGain = recipes[i].baseXPGain
			
		-- determine the size needed for products to display correctly
		local ingrednum = #ingredients
		local rowspan = '|'
		local rowspanProducts = '|'
		
		--Code to incorporate rowspan for ingrediants and variants here
		local rowspanCount = 0

		--accomodate both variants and multiple ingredients
		
		if ingrednum > 4 then
			rowspanCount = rowspanCount + 2
		end
		--suspect there will be a bug here for many variants & many ingredients - might need x2 multipler per variant.
		if(#recipes == 1) then --only add variant rowspans if there is one recipe (i.e. variants will be displayed)
			if tonumber(recipes[i].numberOfVariants) > 1 then
				rowspanCount = rowspanCount + tonumber(recipes[i].numberOfVariants)
				rowspanProducts = '| rowspan=\"' .. rowspanCount - recipes[i].numberOfVariants - 1 .. '\" '
			end
		end
		if rowspanCount > 0 then
			rowspan = '| rowspan=\"' .. rowspanCount .. '\" '
			if(#recipes > 1) then
				rowspanProducts = '| rowspan=\"' .. rowspanCount .. '\" '
			else
				rowspanProducts = '| rowspan=\"' .. rowspanCount - recipes[i].numberOfVariants - 1 .. '\" '
			end
		end

		
		--original rowspan working
		--if ingrednum > 4 then
		--	rowspan = '| rowspan=\"2\" '
		--end
		
		-- String to return
		local row = '|-\n'
	
		-- Add new row if products or ingredients are passed in with at least 1 item
		if (products[1] and ingredients[1]) then
      	
			-- Show or hide the Crafting Station column
			if recipes[i].dispCraftStn == '1' then
				row = row .. rowspan .. stationcell({craftStn[1]}) .. '\n'
			end
        
			-- Add the products columns
			local prodnum = #products
			for a = 1, prodnum do
				if prodnum == 1 then
					row = row .. rowspanProducts .. ' colspan=\"4\" '
				end
				if ((prodnum == 2) or (prodnum == 3 and a == 2)) then
					row = row .. rowspanProducts .. ' colspan=\"2\" '
				end
				row = row .. itemcell({products[a][1], products[a][2]})
			end
			
			-- Add the FIRST row of Ingredients column	
			local numing = ingrednum
			if ingrednum > 4 then 
				numing = 4
			end
			for b = 1, numing do
				if (ingrednum == 1) then
					row = row .. '| colspan=\"4\" '
				end
				if ((ingrednum == 2) or (ingrednum == 3 and b == 2)) then
					row = row .. '| colspan=\"2\" '
				end
				row = row .. itemcell({ingredients[b][2], ingredients[b][3]})
			end

			-- Add the Skill needed column	
			if (skillNeeds[1] ~= '' and skillNeeds[1] ~= nil) then
				row = row .. rowspan .. skillreqcell({skillNeeds[1][1], skillNeeds[1][2]})
			else
				row = row .. rowspan 
				if ingrednum > 4 then
					row = row .. '|'
				end
				row = row .. ' \'\'None\'\' \n'
			end
			
			-- Add the Crafting time column
			row = row .. rowspan 
			if rowspanCount > 1 then
				row = row .. '|'
			end
			if baseCraftTime then					
				row = row .. baseCraftTime .. '\n'
			else
				
				row = row .. rowspan .. ' \'\'missing\'\' \n'
			end
			
			-- Add the Labour time column
			row = row .. rowspan 
			if rowspanCount > 1 then
				row = row .. '|'
			end
			if baseLaborCost then					
				row = row .. baseLaborCost .. '\n'
			else
				
				row = row .. rowspan .. ' \'\'missing\'\' \n'
			end
			
			-- Add the XP gain column
			row = row .. rowspan 
			if rowspanCount > 1 then
				row = row .. '|'
			end
			if baseXPGain then					
				row = row .. baseXPGain .. '\n'
			else
				
				row = row .. rowspan .. ' \'\'missing\'\' \n'
			end
				
			-- not in v0.9
			-- Add the Affect by Skills column IF new skill grouping
			--if j == 0 and (recipes[i].efficiencySkills[1] ~= nil and recipes[i].efficiencySkills[1] ~= 'nil')  then
				--row = row .. '| rowspan=\"' .. skillIndex[k] .. '\" ' .. groupedskillcell({recipes[i].efficiencySkills[1], recipes[i].speedSkills}) .. '\n'
			--elseif (recipes[i].efficiencySkills[1] == nil or recipes[i].efficiencySkills[1] == 'nil')  then
				--row = row .. '| \n'
			--end
			--j = j + 1
			--if #ingredients > 4 then
				--j = j + 1
			--end
			--if j == skillIndex[k] then
				--k = k + 1
				--j = 0
			--end
				
			-- Add the SECOND row of ingredients to ingredients column if applicable
			if ingrednum > 4 then
				row = row .. '|-\n'
				for b = 5, ingrednum do
					if (ingrednum == 5) then
						row = row .. '| colspan=\"4\" '
					end
					if ((ingrednum == 6) or (ingrednum == 7 and b == 6)) then
						row = row .. '| colspan=\"2\" '
					end
					row = row .. itemcell({ingredients[b][2], ingredients[b][3]})
				end
			end
			
			rows = rows .. row
		end

		--Show variants in table if only 1 recipe in list
		if #recipes == 1 and tonumber(recipes[i].numberOfVariants) > 1 then
			for k, v in pairs(recipes[i].variants) do
				--start new row for variants
				row = '|-\n'
				--Skip default variant
				if v.products[1][1] ~= recipes[i].defaultVariant then
					local prodnum = #products
					for a = 1, prodnum do
						if prodnum == 1 then
							row = row .. rowspanProducts .. ' colspan=\"4\" '
						end
						if ((prodnum == 2) or (prodnum == 3 and a == 2)) then
							row = row .. rowspanProducts .. ' colspan=\"2\" '
						end
						row = row .. itemcell({v.products[a][1], v.products[a][2]})
					end

					--will need to cycle ingrediants to fill out complete table
					--row = row .. itemcell({v.products[1][1], v.products[1][2]})
					local numing = ingrednum
					if ingrednum > 4 then 
						numing = 4
					end
					for b = 1, numing do
						if (ingrednum == 1) then
							row = row .. '| colspan=\"4\" '
						end
							if ((ingrednum == 2) or (ingrednum == 3 and b == 2)) then
							row = row .. '| colspan=\"2\" '
						end
							row = row .. itemcell({v.ingredients[b][2], v.ingredients[b][3]})
					end
					
					
				end
				rows = rows .. row
			end			

		end
	end
		

	
			
    -- Return the full wikitable
	return header({recipes[1].dispCraftStn}) .. rows .. '|-\n|}\n'
end

return p