From Brotato Wiki

(INITIAL \o/)
 
(fix incorrect name for Template:Character_Data)
Line 19: Line 19:
     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"}
     }
     }

Revision as of 20:55, 5 November 2024

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

-- Module:GetStartingWeaponsTableForCharacter
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
    local output = "<table><tr>"

    -- Create the table cells for each weapon
    for _, weapon in ipairs(weapons) do
        weapon = mw.text.trim(weapon)  -- Trim any whitespace around the weapon name
        output = output .. "<td>[[" .. weapon .. "]]</td>"
    end

    -- Close the table row and table
    output = output .. "</tr></table>"

    return output
end

return p