Module:Sandbox/Aurelain/Cargo

From Heroes of Might and Magic: Olden Era Official Wiki
Revision as of 04:30, 16 May 2026 by Aurelain (talk | contribs) (Created page with "local p = {} function p.displayOverview(frame) -- 1. Define Cargo query parameters local tables = 'Unit' local fields = '_pageName, hp, offence' local cargoArgs = { orderBy = '_pageName ASC', limit = 100 -- Good practice to set a limit } -- 2. Execute the query -- This returns an array of tables containing your data local results = mw.ext.cargo.query(tables, fields, cargoArgs) -- 3. Handle empty results if not re...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Aurelain/Cargo/doc

local p = {}

function p.displayOverview(frame)
    -- 1. Define Cargo query parameters
    local tables = 'Unit'
    local fields = '_pageName, hp, offence'
    local cargoArgs = {
        orderBy = '_pageName ASC',
        limit = 100 -- Good practice to set a limit
    }

    -- 2. Execute the query
    -- This returns an array of tables containing your data
    local results = mw.ext.cargo.query(tables, fields, cargoArgs)

    -- 3. Handle empty results
    if not results or #results == 0 then
        return "No unit data found."
    end

    -- 4. Create the HTML table using mw.html
    local htmlTable = mw.html.create('table')
        :addClass('wikitable sortable')
        
    -- Build the header row
    htmlTable:tag('tr')
        :tag('th'):wikitext('Unit Name'):done()
        :tag('th'):wikitext('Health'):done()
        :tag('th'):wikitext('Attack'):done()

    -- 5. Loop through the database results to adapt and format data
    for _, row in ipairs(results) do
        -- Extract data from the row
        local unitName = row['_pageName'] or 'Unknown'
        local hp = tonumber(row['hp']) or 0
        local attack = tonumber(row['attack']) or 0

        -- Build the data row
        local tr = htmlTable:tag('tr')
        tr:tag('td'):wikitext('[[' .. unitName .. ']]'):done() -- Makes the name a clickable link
        tr:tag('td'):wikitext(hp):done()
        tr:tag('td'):wikitext(attack):done()
    end

    -- 6. Return the finalized HTML string to the wiki page
    return tostring(htmlTable)
end

return p