Module:ResourceTable: Difference between revisions

From Corsair Cove Official Wiki
Dorce (talk | contribs)
No edit summary
Dorce (talk | contribs)
No edit summary
Line 42: Line 42:
             table.insert(sourceText, "[[" .. src .. "]]")
             table.insert(sourceText, "[[" .. src .. "]]")


             local qty_str = tostring(resource.production["quantity"..i]) .. "[[File:" .. resource.iconFile .. "|16px]] [[" .. resource .. "]]"  or ""
             local qty_str = tostring(resource.production["quantity"..i]) .. "[[File:" .. resource.iconFile .. "|16px]] [[" .. resource.name .. "]]"  or ""
             table.insert(quantityText, qty_str)
             table.insert(quantityText, qty_str)
         end
         end

Revision as of 14:17, 2 August 2026

This module arranges resource data into a table, providing detailed stats as well as useful cost calculations.

It uses data from Module:ResourceData.


local Export = {}
local resourceData = require("Module:ResourceData")

local function recipeToText(recipe)
    if not recipe or next(recipe) == nil then
        return "''None''"
    end

    local parts = {}

    local i = 1
    while recipe["ingredient"..i] do
		local ingredient = recipe["ingredient"..i]
		local icon = resourceData[ingredient] and resourceData[ingredient].iconFile or ""
		
		table.insert(parts,
		    string.format(
		        "%d [[File:%s|16px]] [[%s]]",
		        recipe["ingredient"..i.."_qty"],
		        icon,
		        ingredient
		    )
		)
        i = i + 1
    end

    if #parts == 0 then
        return "''None''"
    end

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

function Export.resourceToWiki(resource)
    local sourceText = {}
    local quantityText = {}

    for i = 1, 3 do
        local src = resource.production["source"..i]

        if src and src ~= "" then
            table.insert(sourceText, "[[" .. src .. "]]")

            local qty_str = tostring(resource.production["quantity"..i]) .. "[[File:" .. resource.iconFile .. "|16px]] [[" .. resource.name .. "]]"  or ""
            table.insert(quantityText, qty_str)
        end
    end

    local recipe = recipeToText(resource.production.recipe1)

    return table.concat({
        "| [[File: " .. resource.iconFile .. " |x96px]]",
        "[[" .. resource.name .. "]]",
        table.concat(sourceText, "<br/>"),
        table.concat(quantityText, "<br/>"),
        recipe,
        tostring(resource.StackSize),
        tostring(resource.StackValue)
    }, " || ")
end

function Export.wikiResourceTable()

    local rows = {}

    for _, resource in pairs(resourceData) do
        table.insert(rows, Export.resourceToWiki(resource))
    end

    return table.concat({
        '{| class="wikitable sortable"',
        '! class="unsortable" | Icon !! Name !! Production<br/>Building !! Ingredients<br/>per min. !! Production<br/>per min. !! Stack Size !! [[Trade Post|Stack Selling Value]]',
        '|-',
        table.concat(rows, '\n|-\n'),
        '|}',
        ''
    }, '\n')
end

return Export