From Brotato Wiki

(1: Break into a new row after 10 items, 2: Wrap the weapon names in `Iconbox` templates)
(Add table class, and pretty print the output (using line breaks and indents))
 
(2 intermediate revisions by the same user not shown)
Line 20: Line 20:
     -- Get the character name from the frame arguments
     -- Get the character name from the frame arguments
     local characterName = frame.args["name"] or ""
     local characterName = frame.args["name"] or ""
 
   
     -- Retrieve the startingwpns data from the CharacterWeapons_Data template
     -- Retrieve the startingwpns data from the Character_Data template
     local startingWeaponsList = frame:expandTemplate{
     local startingWeaponsList = frame:expandTemplate{
         title = "CharacterWeapons_Data",
         title = "Character_Data",
         args = {characterName, "startingwpns"}
         args = {characterName, "startingwpns"}
     }
     }
Line 34: Line 34:
     local weapons = split(startingWeaponsList, ",")
     local weapons = split(startingWeaponsList, ",")


     -- Start building the HTML table
     -- Start building the HTML table with pretty formatting
     local output = "<table><tr>"
     local output = {}
    table.insert(output, '<table class="starting-weapons-table">')
    table.insert(output, "\t<tr>")


     -- Create the table cells for each weapon, adding a new row after every 10 items
     -- Create the table cells for each weapon, adding a new row after every 10 items
     for i, weapon in ipairs(weapons) do
     for i, weapon in ipairs(weapons) do
         weapon = mw.text.trim(weapon)  -- Trim any whitespace around the weapon name
         weapon = mw.text.trim(weapon)  -- Trim any whitespace around the weapon name
         output = output .. "<td>{{Iconbox|" .. weapon .. "|size=60px|maxwidth=110px|rarity=auto}}</td>"
          
 
        -- Expand the Iconbox template for each weapon
        local iconbox = frame:expandTemplate{
            title = "Iconbox",
            args = {weapon, size = "60px", maxwidth = "110px", rarity = "auto"}
        }
       
        table.insert(output, "\t\t<td>" .. iconbox .. "</td>")
       
         -- Insert a new row after every 10th item
         -- Insert a new row after every 10th item
         if i % 10 == 0 then
         if i % 10 == 0 then
             output = output .. "</tr><tr>"
             table.insert(output, "\t</tr>")
            if i ~= #weapons then  -- Avoid adding an extra row if we're at the end
                table.insert(output, "\t<tr>")
            end
         end
         end
     end
     end


     -- Close the table row and table
     -- Close the final row and table
     output = output .. "</tr></table>"
     if #weapons % 10 ~= 0 then
        table.insert(output, "\t</tr>")
    end
    table.insert(output, "</table>")


     return output
     return table.concat(output, "\n")
end
end


return p
return p

Latest revision as of 21:08, 5 November 2024

Documentation for this module may be created at Module:GetStartingWeaponsTableForCharacter/doc

-- Module:GetStartingWeaponsTableForCharacter
-- Used by: Template:GenerateCharacterStartingWeaponsTable (see for more info)

local p = {}

-- Helper function to split a string by a given delimiter
local function split(inputstr, sep)
    if sep == nil then
        sep = ","
    end
    local t = {}
    for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
        table.insert(t, str)
    end
    return t
end

-- Main function to generate a table of starting weapons
function p.getStartingWeaponsTable(frame)
    -- Get the character name from the frame arguments
    local characterName = frame.args["name"] or ""
    
    -- Retrieve the startingwpns data from the Character_Data template
    local startingWeaponsList = frame:expandTemplate{
        title = "Character_Data",
        args = {characterName, "startingwpns"}
    }

    if not startingWeaponsList or startingWeaponsList == "-" then
        return "Starting weapons data not found for character: " .. characterName
    end

    -- Split the starting weapons list into individual items
    local weapons = split(startingWeaponsList, ",")

    -- Start building the HTML table with pretty formatting
    local output = {}
    table.insert(output, '<table class="starting-weapons-table">')
    table.insert(output, "\t<tr>")

    -- Create the table cells for each weapon, adding a new row after every 10 items
    for i, weapon in ipairs(weapons) do
        weapon = mw.text.trim(weapon)  -- Trim any whitespace around the weapon name
        
        -- Expand the Iconbox template for each weapon
        local iconbox = frame:expandTemplate{
            title = "Iconbox",
            args = {weapon, size = "60px", maxwidth = "110px", rarity = "auto"}
        }
        
        table.insert(output, "\t\t<td>" .. iconbox .. "</td>")
        
        -- Insert a new row after every 10th item
        if i % 10 == 0 then
            table.insert(output, "\t</tr>")
            if i ~= #weapons then  -- Avoid adding an extra row if we're at the end
                table.insert(output, "\t<tr>")
            end
        end
    end

    -- Close the final row and table
    if #weapons % 10 ~= 0 then
        table.insert(output, "\t</tr>")
    end
    table.insert(output, "</table>")

    return table.concat(output, "\n")
end

return p