Module:ResourceStack: Difference between revisions

From Corsair Cove Official Wiki
m whitespace nowrap on the name, if shown
added type guard for name argument
Line 8: Line 8:
To test this module in the debug console:
To test this module in the debug console:
=p.main( mw.getCurrentFrame():newChild{ args = {Planks="20", ["Stone Blocks"]="30", Hardwood=""} }:newChild{} )
=p.main( mw.getCurrentFrame():newChild{ args = {Planks="20", ["Stone Blocks"]="30", Hardwood=""} }:newChild{} )
=mw.getCurrentFrame():expandTemplate{ title = 'Resource stack', args = { [1] = '', size = 'sm' } }


@param frame  Frame object.
@param frame  Frame object.
Line 124: Line 125:
     local rows = {}
     local rows = {}
     for name, quantity in pairs(args) do
     for name, quantity in pairs(args) do
         if name ~= ARG_SIZE and name ~= ARG_SHOW_NAMES then
         if type(name) == "string" and name ~= ARG_SIZE and name ~= ARG_SHOW_NAMES then
             local resource = mw.ustring.lower(name)
             local resource = mw.ustring.lower(name)
             local filename = icons[resource] or ICON_PLACEHOLDER_FILENAME
             local filename = icons[resource] or ICON_PLACEHOLDER_FILENAME

Revision as of 21:46, 29 July 2026

This module combines several resource displays visually into a stack order.

It uses data from Module:ResourceData.


--[[
Module:ResourceStack
https://wiki.hoodedhorse.com/Corsair_Cove/Module:ResourceStack

Processes all arguments as resource=quantity pairs and renders a table of
icons, quantities, and (optionally) resource names.

To test this module in the debug console:
=p.main( mw.getCurrentFrame():newChild{ args = {Planks="20", ["Stone Blocks"]="30", Hardwood=""} }:newChild{} )
=mw.getCurrentFrame():expandTemplate{ title = 'Resource stack', args = { [1] = '', size = 'sm' } }

@param frame  Frame object.
              frame:getParent().args holds resource=quantity pairs, where
              each resource name must be spelled and capitalized exactly as
              its article name, since the name is used as the link target.
              An internal lookup table maps each resource name to its icon.

              Special arguments (not treated as resources):
                size        - icon size, one of xs, sm, md, lg, xl.
                              Defaults to "xs".
                show_names  - defaults to true (shows names alongside
                              icons); set to "false" to show icons only.

@return string  Wikitext for a table of resource icons, quantities, and
                 (optionally) names.
--]]
local ResourceStack = {}

local ARG_SIZE = "size"
local ARG_SHOW_NAMES = "show_names"
local ARG_SHOW_NAMES_DISABLE_FLAG = "false"
local ICON_PLACEHOLDER_FILENAME = "T_Button_SelectionBorder_Default.png"

local icons = {
    ["barrel"]         = "T_ICO_resource_Barrel.png",
    ["birds"]          = "lT_ICO_iresource_bird.png",
    ["black powder"]   = "T_ICO_resource_blackPowder.png",
    ["booze"]          = "T_ICO_resource_DefaultAle.png",
    ["boots"]          = "T_ICO_resource_Boots.png",
    ["cannonball"]     = "T_ICO_resource_CannonballMetall.png",
    ["cassava bread"]  = "T_ICO_resource_CassavaBread.png",
    ["cassava roots"]  = "T_ICO_resource_cassavaRoot.png",
    ["cast iron"]      = "T_ICO_cast_iron.png",
    ["charcoal"]       = "T_ICO_Charburner.png",
    ["cochineal"]      = "T_ICO_resource_Cochineal.png",
    ["colour pigment"] = "T_ICO_resource_ColorPigment.png",
    ["copper"]         = "T_ICO_resource_copperBars.png",
    ["copper lamp"]    = "T_ICO_resource_CopperLamp.png",
    ["copper ore"]     = "T_ICO_resource_copper.png",
    ["eggs"]           = "T_ICO_resource_egg.png",
    ["eyepatch"]       = "T_ICO_resource_EyePatch.png",
    ["fabric"]         = "T_ICO_resource_Canvas2.png",
    ["fancy clothing"] = "T_ICO_resource_Garments.png",
    ["fibre"]          = "T_ICO_Fibres.png",
    ["figurehead"]     = "T_ICO_Figurehead.png",
    ["fish"]           = "T_ICO_resource_Fish.png",
    ["fittings"]       = "T_ICO_resource_Fittings.png",
    ["fresh water"]    = "T_ICO_resource_FreshWater.png",
    ["grain"]          = "T_ICO_resource_Grain.png",
    ["guano"]          = "T_ICO_resource_guano.png",
    ["hard tack"]      = "T_ICO_resource_HardTack.png",
    ["hardwood"]       = "T_ICO_resource_Hardwood.png",
    ["herbs"]          = "T_ICO_resource_herbs.png",
    ["iron ore"]       = "T_ICO_resource_ironOre.png",
    ["jewellery"]      = "T_ICO_resource_Jewelry.png",
    ["jolly roger"]    = "T_ICO_resource_JollyRoger.png",
    ["lead"]           = "T_ICO_resource_Lead.png",
    ["lead ore"]       = "T_ICO_resource_LeadOre.png",
    ["leather"]        = "T_ICO_resource_Leather.png",
    ["lens"]           = "T_ICO_resource_Lens.png",
    ["lime"]           = "T_ICO_resource_limestone.png",
    ["medicine"]       = "T_ICO_resource_medicine.png",
    ["naval gun"]      = "T_ICO_resource_navalCannon.png",
    ["pearl"]          = "T_ICO_resource_Pearl.png",
    ["pig"]            = "T_ICO_resource_pig.png",
    ["pineapple"]      = "T_ICO_resource_pineapple.png",
    ["pistols"]        = "T_ICO_resource_Handgun04.png",
    ["pitch"]          = "T_ICO_resource_Pitch.png",
    ["planks"]         = "T_ICO_resource_plank.png",
    ["prosthesis"]     = "T_ICO_resource_Prosthetics.png",
    ["roast bird"]     = "T_ICO_resource_RoastBird.png",
    ["rope"]           = "T_ICO_resource_rope.png",
    ["rum"]            = "T_ICO_resource_Rum.png",
    ["sabres"]         = "T_ICO_resource_cutlass.png",
    ["sails"]          = "T_ICO_resource_Sails.png",
    ["salt"]           = "T_ICO_resource_salt.png",
    ["seashells"]      = "lT_ICO_iresource_seashell.png",
    ["shot"]           = "T_ICO_resource_Shot.png",
    ["silver"]         = "T_ICO_resource_silverBullion.png",
    ["silver lining"]  = "T_ICO_resource_SilverDeco.png",
    ["slops"]          = "T_ICO_resource_Slops.png",
    ["spyglass"]       = "T_ICO_resource_Telescope.png",
    ["steel"]          = "T_ICO_resource_Steel.png",
    ["steel chain"]    = "T_ICO_resource_SteelChain.png",
    ["stew"]           = "T_ICO_resource_DefaultStew.png",
    ["stone"]          = "T_ICO_resource_Stone.png",
    ["stone blocks"]   = "T_ICO_resource_stoneBlocks.png",
    ["sugar cane"]     = "T_ICO_resource_sugarCane.png",
    ["tabby"]          = "T_ICO_resource_tabbyConcrete.png",
    ["tar"]            = "T_ICO_resource_Tar.png",
    ["tattoo ink"]     = "T_ICO_resource_TattooInk.png",
    ["tobacco"]        = "T_ICO_resource_tabacco.png",
    ["tools"]          = "T_ICO_resource_tools.png",
    ["turtle soup"]    = "T_ICO_resource_turtleSoup.png",
    ["turtles"]        = "lT_ICO_iresource_turtule.png",
    ["whale blubber"]  = "T_ICO_resource_WhaleBlubber.png",
    ["whale oil"]      = "T_ICO_resource_WhaleOil.png",
    ["wine"]           = "T_ICO_resource_wine.png",
    ["wooden awl"]     = "T_ICO_resource_WoodenAwl.png",
}

local sizes = {
    ["xs"] = "x16px",
    ["sm"] = "x32px",
    ["md"] = "x64px",
    ["lg"] = "x96px",
    ["xl"] = "x128px",
}

function ResourceStack.main(frame)
    local args = frame:getParent().args
    local size = sizes[args[ARG_SIZE]] or sizes.sm
    local showNames = args[ARG_SHOW_NAMES] ~= ARG_SHOW_NAMES_DISABLE_FLAG
    local numberStyle = "style=\"text-align: right;\""
    local rows = {}
    for name, quantity in pairs(args) do
        if type(name) == "string" and name ~= ARG_SIZE and name ~= ARG_SHOW_NAMES then
            local resource = mw.ustring.lower(name)
            local filename = icons[resource] or ICON_PLACEHOLDER_FILENAME
            local displayQuantity = (quantity == "0" or quantity == "") and "" or quantity
            local row = "|-\n|" .. numberStyle .. "| " .. displayQuantity .. " || [[File:" .. filename .. "|" .. size .. "|link=" .. name .. "|" .. name .. "]]"
            if showNames then
                row = row .. " ||style=\"white-space: nowrap;\"| [[" .. name .. "]]"
            end
            table.insert(rows, row)
        end
    end
    return "{|\n" .. table.concat(rows, "\n") .. "\n|}"
end

return ResourceStack