Module:RenderRecipe: Difference between revisions

From Against the Storm Official Wiki
(Created page with "------------------------------------------------------------------------------- -- Renders the {{recipe}} template -- -- Takes at least one argument. The first, requried, is the name of the -- resource for which the recipes are needed. Optionally, the second argument -- is the name of the building. -- This module renders small wikimarkup tables to represent one or more -- recipes: one table if the building was specified. several tables -- corresponding to all the buil...")
 
(forgot to write scope for a few functions)
Line 69: Line 69:
for i, place in pairs(tRecipe.places) do
for i, place in pairs(tRecipe.places) do
local tempRecipe = { tRecipe.pattern, place }
local tempRecipe = { tRecipe.pattern, place }
strToReturn = strToReturn .. "\n\n" .. renderTable(tRecipe)
strToReturn = strToReturn .. "\n\n" .. RenderRecipe.renderTable(tRecipe)
end
end
end
end
Line 117: Line 117:
local wikiTable = "{| " .. CSS_CLASS_RECIPE_TABLE .. NL ..
local wikiTable = "{| " .. CSS_CLASS_RECIPE_TABLE .. NL ..
     "| " .. blTemplate(building) .. BR .. starTemplate(stars) .. BR .. speed .. NL
     "| " .. RenderRecipe.blTemplate(building) .. BR .. RenderRecipe.starTemplate(stars) .. BR .. speed .. NL
-- looping over alternative ingredients and quantities simultaneously,  
-- looping over alternative ingredients and quantities simultaneously,  
Line 142: Line 142:
end
end
wikiTable = wikiTable .. quant[i] .. " " .. rlTemplate(alt[i]) .. BR
wikiTable = wikiTable .. quant[i] .. " " .. RenderRecipe.rlTemplate(alt[i]) .. BR
end
end
Line 151: Line 151:
wikiTable = wikiTable ..  
wikiTable = wikiTable ..  
"| = " .. NL ..
"| = " .. NL ..
"| " .. number .. " " .. rlTemplate(product) .. NL ..
"| " .. number .. " " .. RenderRecipe.rlTemplate(product) .. NL ..
"|}"
"|}"
Line 182: Line 182:
-- loop through the buildings and stars and build the templates
-- loop through the buildings and stars and build the templates
for i, building in pairs(buildings) do
for i, building in pairs(buildings) do
listOfTemplates[i] = blTemplate(building) .. " " .. pstarTemplate(stars[i])
listOfTemplates[i] = RenderRecipe.blTemplate(building) .. " " .. RenderRecipe.pstarTemplate(stars[i])
end
end

Revision as of 04:26, 8 February 2023

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

-------------------------------------------------------------------------------
-- Renders the {{recipe}} template
--
-- Takes at least one argument. The first, requried, is the name of the 
-- resource for which the recipes are needed. Optionally, the second argument
-- is the name of the building.
-- This module renders small wikimarkup tables to represent one or more 
-- recipes: one table if the building was specified. several tables 
-- corresponding to all the buildings in which the resource can be produced.
-------------------------------------------------------------------------------

local RenderRecipe = {}

local RecipeData = require("Module:RecipeData") -- lookup table for recipes



-------------------------------------------------------------------------------
-- Constants
-------------------------------------------------------------------------------
local CSS_CLASS_RECIPE_TABLE = "class=\"ATSrecipe\""
local CSS_CLASS_REQUIRED_INGREDIENT = "class=\"ATSrequired\""
local CSS_CLASS_SWAPPABLE_INGREDIENT = "class=\"ATSswappable\""
local TEMPLATE_BUILDING_LINK = "Building_link"
local TEMPLATE_RESOURCE_LINK = "Resource_link"
local BR = " <br /> "
local NL = " \n "



-------------------------------------------------------------------------------
-- Main rendering function
-- uses ResourceData lookup function, parses the result and handles errors 
-- with default values, then assembles a final string to return to the wiki
-------------------------------------------------------------------------------
function RenderRecipe.renderRecipe(frame)

	local argProductName = frame.args.product
	local argBuildingName = frame.args.building
	
	if not argProductName then
		return "Render_Recipe Error: no product given."
	end
	
	local strToReturn = ""
	
	-- decide how to proceed based on whether the building was specified
	if not argBuildingName then -- 1. easier case, when the building was specified
		
		local tRecipe = RecipeData.getRecipeAtBuilding(argProductName, argBuildingName)
		
		if not tRecipe then
			return "Render_Recipe Error: no recipe found."
		end
		
		--renderTable will use just the first place in the recipe, which is perfect for this version
		strToReturn = renderTable(tRecipe)
		
	else -- 2. harder case, when no building was specified, need to loop
		
		local tRecipe = RecipeData.getAllRecipes(argProductName)
		
		if not tRecipe then
			return "Render_Recipe Error: no recipe found."
		end
		
		-- go through each place and send a shallow copy version of tRecipe to the table renderer,
		-- then concatenate them with appropriate wiki markup
		for i, place in pairs(tRecipe.places) do
			local tempRecipe = { tRecipe.pattern, place }
			strToReturn = strToReturn .. "\n\n"	.. RenderRecipe.renderTable(tRecipe)
		end
	end
	
	return strToReturn
end



-- writes the wiki table markup to represent a recipe
-- takes a recipe table (pattern and place), and uses only the first place
function RenderRecipe.renderTable(tRecipe)

	if not tRecipe then
		return nil
	end
	
	-- call the helper functions to extract the data, make sure we have something usable,
	-- then get only the first values since we're assuming that there's only one top-level record in tRecipe
	local building, stars = RecipeData.getBuildingsAndStarsLists(tRecipe)
	if building and stars then
		building, stars = building[1], stars[1]
	else
		return "Render_Recipe Error: did not find building or stars in recipe."
	end
	
	local speed = RecipeData.getProductionSpeed(tRecipe)
	if speed then
		speed = speed[1]
	else
		return "Render_Recipe Error: did not find production speed in recipe."
	end
	
	local product, number = RecipeData.getProductAndNumbers(tRecipe)
	if product and number then
		number = number[1]
	else
		return "Render_Recipe Error: did not find product or output numbers in recipe."
	end
	
	-- no else needed here, we don't want only the first like we did for the stuff that's stored
	-- in the places subtable!
	local ingredients, quantities = RecipeData.getIngredientsAndQuantities(tRecipe)
	if not ingredients or not quantities or 0 == #ingredients or 0 == #quantities then
		return "Render_Recipe Error: did not find ingredients or quantities in recipe."
	end
	
	local wikiTable = "{| " .. CSS_CLASS_RECIPE_TABLE .. NL ..
    "| " .. RenderRecipe.blTemplate(building) .. BR .. RenderRecipe.starTemplate(stars) .. BR .. speed .. NL
	
	-- looping over alternative ingredients and quantities simultaneously, 
	-- so use local groups and groupsOfQuantities for each i
	for i, groups in ipairs(ingredients) do
		local groupsOfQuantities = quantities[i]
		
		-- on all but the first, create a new cell with a + sign to separate ingredient groups
		if i > 1 then
			wikiTable = wikiTable ..
			"| +" .. NL
		end
		
		for j, alt in ipairs(groups) do
			local quant = groupsOfQuantities[j]
			
			-- prefix the table cell based on whether there's only one (required) or more (swappable) ingredients
			if 1 == #groups then
				wikiTable = wikiTable ..
				"| " .. CSS_CLASS_REQUIRED_INGREDIENT .. " | "
			else
				wikiTable = wikiTable ..
				"| " .. CSS_CLASS_SWAPPABLE_INGREDIENT .. " | "
			end
			
			wikiTable = wikiTable .. quant[i] .. " " .. RenderRecipe.rlTemplate(alt[i]) .. BR
		end
		
		-- finish the last cell with a new-line
		wikiTable = wikiTable .. NL
	end
    
	wikiTable = wikiTable .. 
	"| = " .. NL ..
	"| " .. number .. " " .. RenderRecipe.rlTemplate(product) .. NL ..
	"|}"
	
    return wikiTable
end



-- redirect a rendering request to the recipe data and create appropriate 
-- templates for the list. The caller can decide how to output the list
function RenderRecipe.getBuildingsAndStarsForProduct(frame)

	local argProductName = frame.args.product
	
	-- make sure there's an argument to work with
	if not tableRecipe then
		return "Render_Recipe Error: no product given."
	end
	
	-- get the associated recipe data, including all buildings
	local tableRecipe = RecipeData.getAllRecipes(argProductName)
	-- make sure what's returned is valid before we loop through it
	if not tableRecipe or not tableRecipe.places or #tableRecipe.places < 1 then
		return "Render_Recipe Error: no recipes found"
	end
	
	local listOfTemplates = {}
	local buildings, stars = RecipeData.getBuildingsAndStarsLists(tRecipe)
	
	-- loop through the buildings and stars and build the templates
	for i, building in pairs(buildings) do
		listOfTemplates[i] = RenderRecipe.blTemplate(building) .. " " .. RenderRecipe.pstarTemplate(stars[i])
	end
	
	return listOfTemplates
end



-------------------------------------------------------------------------------
-- Helper rendering functions
-- these do not call member functions, but write out the actual templates
-- (which will in turn invoke the code. this is to protect variations in the
-- modules, but we may decide later it should be different)
-------------------------------------------------------------------------------
--use huge size for recipes
function RenderRecipe.blTemplate(strBuilding)
	
	return "{{" .. TEMPLATE_BUILDING_LINK .. "|" .. strBuilding .. "|huge}}"
end

-- use recipe-sized icons, "med"
function RenderRecipe.rlTemplate(strResource)
	
	return "{{" .. TEMPLATE_RESOURCE_LINK .. "|" .. strResource .. "|med}}"
end

-- use the parentheses version, with "P"
function RenderRecipe.pstarTemplate(intStars)
	
	return "{{P" .. intStars .. "star}}"
end

-- no parens version
function RenderRecipe.starTemplate(intStars)
	
	return "{{" .. intStars .. "star}}"
end



-------------------------------------------------------------------------------
-- Return when required into another Module.
-------------------------------------------------------------------------------
return RenderRecipe