Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.



local p = {}
local cargo = mw.ext.cargo

function p.render(frame)
    local args = frame:getParent().args
    local tbl = mw.html.create("table"):addClass("wikitable")
    
    tbl:tag("tr")
    	:tag("th"):wikitext("Item"):done()
    	:tag("th"):wikitext(""):done()
    	:tag("th"):wikitext("Recycling results"):done()
    	:tag("th"):wikitext("Salvaging results"):done()

	local i = 1
    while args["recycling" .. i] and i < 10 do
    	
    	local input_item = args["input" .. i] or mw.title.getCurrentTitle().text
    	
    	local recycling_out = ""
    	local salvaging_out = ""
    	
        if string.lower(args["recycling" .. i]) == "no" then
        	recycling_out = "Cannot be recycled"
    	else
    		local recycling_tbl = p._parse_ingredients(args["recycling" .. i])
    		
    		recycling_out = p._render_ingredients(recycling_tbl)
    		p._cargo_store("recycling", input_item, recycling_tbl)
        end
	
        if string.lower(args["salvaging" .. i]) == "no" then
        	salvaging_out = "Cannot be salvaged"
    	else
    		local salvaging_tbl = p._parse_ingredients(args["salvaging" .. i] or "")
    		
    		salvaging_out = p._render_ingredients(salvaging_tbl)
    		p._cargo_store("salvaging", input_item, salvaging_tbl)
        end

		tbl:tag("tr")
			:tag("td"):wikitext(input_item):done()
			:tag("td"):wikitext("'''→'''"):done()
			:tag("td"):wikitext(recycling_out):done()
			:tag("td"):wikitext(salvaging_out):done()
			
		i = i + 1
    end

    return tostring(tbl)
end

function p._cargo_query(search_item)
	c_tables = 'recycling'
	c_fields = '_pageName,inputItem,outputQty'
    c_params = {
    	where = 'outputItem="' .. search_item .. '" AND recipeType="recycling"',
    	orderBy = 'inputItem'
    }

	return cargo.query(c_tables, c_fields, c_params)
end

function p._parse_ingredients(ingr_str)
	-- Input looks like: 1 Steel Spring + 3 Advanced Components
	ingredients = {}

	-- First split by + 
	for _, itm in ipairs(mw.text.split(ingr_str, "+", true)) do
		-- Then split the number from the item name
		ingr_qty, ingr_name = string.match(mw.text.trim(itm), "^([0-9]+)(.+)$")
		
		if ingr_qty then
			table.insert(ingredients, {name=mw.text.trim(ingr_name), qty=ingr_qty})
		end
	end
	
	return ingredients
end

function p._render_ingredients(tbl)
	if next(tbl) == nil then
		return "?"
	end
	
	res = {}
	for _,ingredient in pairs(tbl) do
		res[#res+1] = ingredient.qty .. "× [["..ingredient.name.."]]"
	end

	return table.concat(res, "<br>")
end

function p._cargo_store(recipe_type, input_item, output_tbl)
	for _, output_def in pairs(output_tbl) do
		cargo.store('recycling', {
			recipeType = recipe_type,
			inputItem = input_item,
			outputItem = output_def.name,
			outputQty = output_def.qty
		})	
	end
end

return p