No Man's Sky Wiki
No Man's Sky Wiki

Documentation for this module may be created at Module:PoC-Cooking/doc

local p = {}

--[[
 Return a list entry (<li>...</li>) for each recipe which parameters are provided
 Every parameter is required, except for the recipe name which can be empty (and is then elegantly stripped from output)
]]--
function p.FormatResources(name, resources, output, timeProcessing, recipe)
    local ilink = require("Module:Ilink")
    if resources == '' then
        -- Failsafe
        return ''
    end
    local ingredients = mw.text.split(resources,'µ£',true)
    local ingredientsCount = table.maxn(ingredients)
    text = "\n<li data-ing='" .. table.concat(ingredients, " ") .. "' data-ingN='" .. ingredientsCount .. "' data-out='" .. output .. "' data-time='" .. timeProcessing .. "' data-recp='" .. recipe .. "'> " .. ilink.Main({args={name, ""}}) .. "&nbsp;&nbsp;--&nbsp;&nbsp;"
    for i, ingredient in ipairs(ingredients) do
        local tuple = mw.text.split(ingredient,'; ',true)
        if table.maxn(tuple) <= 1 then
            --[[
             split will always return at least 2 parts
             In case of a single ingredient, splitting over "µ£", the last one will be empty.
             Hence, splitting tis empty part over "; " yields a single part, which is detectable and acts as the end of the ingredients list
            ]]--
            break
        end

        if i > 1 then
            -- From the 2nd ingredient to the end of the list, prefix with "  +  "
            text = text .. "&nbsp;&nbsp;+&nbsp;&nbsp;"
        end
        text = text .. "[[" .. tuple[1] .. "|<span class='itemlink ajaxttlink'>" .. tuple[1] .. "</span>]] x" .. tuple[2]
    end
    text = text .. "&nbsp;&nbsp;&rarr;&nbsp;&nbsp;" .. ilink.Main({args={name,""}}) .. " x" .. output .. "&nbsp;&nbsp;("
    if recipe ~= '' then
        -- Gracefully handle an empty recipe name
        text = text .. "''\"" .. recipe .. "\"'', "
    end
    text = text .. timeProcessing .. " sec./unit output)</li>"
    return text
end

--[[
 Provide an unordered HTML list (<ul>...</ul>) of recipes containing the current page's item
]]--
function p.List(frame) 
    local cargo = mw.ext.cargo
    local ilink = require("Module:Ilink")
    tables = 'PoC_Cooking'
    fields = 'PoC_Cooking._pageName,PoC_Cooking.ResourceQtys,PoC_Cooking.Output,PoC_Cooking.TimeProcessing,PoC_Cooking.Recipe'
    local title = mw.title.getCurrentTitle().rootText
    local args = {
        where = 'PoC_Cooking.Resources HOLDS "' .. title .. '"',
        -- Remove line duplicate in case resource appears several times in the Resources list
        groupBy = 'PoC_Cooking._pageName,ResourceQtys',
        limit = 1000
    }
    if frame.args[1] ~= '' then
        tables = 'PoC_Cooking,Version_info'
        args.join = 'PoC_Cooking._pageName = Version_info._pageName'
        args.where = args.where .. ' AND Version_info.Version = "' .. frame.args[1] .. '"'
    end
    local results = cargo.query( tables, fields, args )
    local text = "'''" .. title .. "''' is "
    if table.maxn(results) > 0 then
        -- Recipes have been found with the current page's item as resource
        text = text .. "used as an [[ingredients|ingredient]] for [[cooking]] the following products using a [[Nutrient Processor]]:\n<ul>"
        for i, v in ipairs(results) do
            -- For each recipe, display a list entry
            local name = v["PoC_Cooking._pageName"]
            local ingredients = v["PoC_Cooking.ResourceQtys"]
            local output = v["PoC_Cooking.Output"]
            local timeProcessing = v["PoC_Cooking.TimeProcessing"]
            local recipe = v["PoC_Cooking.Recipe"]
            text = text .. p.FormatResources(name, ingredients, output, timeProcessing, recipe)
        end
        text = text .. "</ul>"
    else
        -- No recipe has been found with the current page's item as resource; Placeholder could be replaced with empty string or an error message if that should never happen
        text = text .. "not used as an [[ingredients|ingredient]] for [[cooking]] using a [[Nutrient Processor]].\n"
    end
    return text
end

return p