Module:Cooked food query

From Little-Known Galaxy Wiki
Jump to navigation Jump to search

This module is used in the {{Cooked food query}} template.

Subpages

local argsUtil = require("Module:ArgsUtil")
local cargoUtil = require("Module:CargoUtil")
local iconListModule = require("Module:Icon list")

local p = {}

function p.main(frame)
    -- Merge arguments from the current frame and its parent
    local args = argsUtil.merge(frame)
    
    -- Build the base where clause
    local whereClause = "Items.itemCategory='Food' AND Recipes.product IS NOT NULL AND (RecipeIngredients.quality IS NULL OR RecipeIngredients.quality='0')"
    
    -- Check if the machine argument is provided
    if args.machine then
        local machineName = args.machine
        whereClause = whereClause .. " AND Recipes.machine='" .. machineName .. "'"
    end
    
    -- Use CargoQueryUtil to query using these options
    local queryOptions = {
        tables = "Items, Recipes, RecipeIngredients",
        fields = "Items.itemName, Items.sellValue, Recipes.machine, GROUP_CONCAT(RecipeIngredients.ingredient, '*', RecipeIngredients.quantity)=ingredients",
        join = "Items.itemName=Recipes.product, Recipes.recipeId=RecipeIngredients.recipeId",
        where = whereClause,
        limit = 300,
        groupBy = "Recipes.recipeId"
    }
    
    local results = cargoUtil.queryData(queryOptions)
    if not results then
        return "Error: No results found."
    end
    
    -- Build the result table
    local html = mw.html.create('table')
        :addClass('lkg-table tablesmall')
        :css('border-collapse', 'collapse')
    local header = html:tag('tr')
            header:tag('th'):wikitext('Item'):done()
            header:tag('th'):wikitext('Ingredients'):done()
            if args.machine then
                header:tag('th'):wikitext('Sell Price'):done()
            else
                header:tag('th'):wikitext('Machine'):done()
            end
        header:done()
    
    local lastItem = nil
    local rowspanCount = 0
    local rows = {}
    local rowspans = {}
    
    for _, row in ipairs(results) do
        local name = row["Items.itemName"]
        local machine = row["Recipes.machine"]
        local sellPrice = row["Items.sellValue"]
        local ingredientList = tostring(iconListModule._main{ row.ingredients, delim = ',' })
        
        if name == lastItem then
            rowspanCount = rowspanCount + 1
            table.insert(rows, {name = nil, ingredients = ingredientList, machine = machine, sellPrice = sellPrice})
        else
            if lastItem then
                rowspans[#rows - rowspanCount + 1] = rowspanCount
            end
            lastItem = name
            rowspanCount = 1
            table.insert(rows, {name = name, ingredients = ingredientList, machine = machine, sellPrice = sellPrice})
        end
    end
    
    -- Handle the last item
    if lastItem then
        rowspans[#rows - rowspanCount + 1] = rowspanCount
    end
    
    -- Now build the HTML table with the correct rowspan
    for i, row in ipairs(rows) do
        html:tag('tr')
        if row.name then
            html:tag('td'):attr('rowspan', rowspans[i] or 1)
                :wikitext("[[File:" .. row.name .. ".png|40px|link=" .. row.name .. "]]<br>[[" .. row.name .. "]]")
                :css('text-align', 'center')
                :done()
        end
        html:tag('td'):wikitext(row.ingredients)
            :css('border-left', '1px solid var(--wiki-content-border-color)')
            :done()
        if args.machine then
            html:tag('td'):wikitext(row.sellPrice .. " [[File:credits.png|15px|link=credits]]"):done()
        else
            html:tag('td'):wikitext(row.machine):done()
        end
        html:done()
    end
    
    return tostring(html)
end

return p