Module:UnitInfobox: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
Created page with "-- This module is intended as a supporting muscle for the UnitInfobox template. local p = {} -- Parses and runs a Cargo query from wiki-like syntax ------------------------------------------------------------------------------------------------------------------------ local function runQuery(query, ...) query = string.gsub(query, "%-%-.-\n", "") -- remove "comments" query = select('#', ...) > 0 and mw.ustring.format(query, ...) or query query = mw.text.trim(..."
 
No edit summary
Line 31: Line 31:
     end
     end
     return mw.ext.cargo.query(tables, fields, options)
     return mw.ext.cargo.query(tables, fields, options)
end
------------------------------------------------------------------------------------------------------------------------
local function getEnglishUnitName(id)
    return runQuery([[
        tables =
            Unit = U,
            Translation = T
        | join on =
            U.id = T.target_id
        | where =
            U.id = '%s'
            AND T.language = 'en'
            AND T.type = 'unit'
        | fields =
            T.name = name
        | limit =
            1
    ]], id);
end
end


Line 37: Line 56:
     local unitId = frame.args.id
     local unitId = frame.args.id
     return unitId
     return unitId
end
------------------------------------------------------------------------------------------------------------------------
function p.findSpecialist(frame)
    local unitId = frame.args.id
    local baseId = unitId:gsub('_alt$', ''):gsub('_upg$', '')
    local nameEn = getEnglishUnitName(baseId)
    return nameEn
end
end


return p
return p

Revision as of 12:36, 16 July 2026

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

-- This module is intended as a supporting muscle for the UnitInfobox template.
local p = {}

-- Parses and runs a Cargo query from wiki-like syntax
------------------------------------------------------------------------------------------------------------------------
local function runQuery(query, ...)
    query = string.gsub(query, "%-%-.-\n", "") -- remove "comments"
    query = select('#', ...) > 0 and mw.ustring.format(query, ...) or query
    query = mw.text.trim(query)
    local tables = ''
    local fields = ''
    local options = {}
    for part in query:gmatch('[^|]+') do
        local key, val = part:match('^%s*([^=]+)%s*=%s*(.-)%s*$')
        if key and val then
            key = mw.text.trim(key):lower()
            val = mw.text.trim(val)
            val = val:gsub('%s', ' ')
            if key == 'tables' then
                tables = val
            elseif key == 'fields' then
                fields = val
            elseif key == 'limit' then
                options[key] = tonumber(val) or val
            elseif key == 'join on' then
                options['join'] = val
            else
                options[key] = val
            end
        end
    end
    return mw.ext.cargo.query(tables, fields, options)
end

------------------------------------------------------------------------------------------------------------------------
local function getEnglishUnitName(id)
    return runQuery([[
        tables =
            Unit = U,
            Translation = T
        | join on =
            U.id = T.target_id
        | where =
            U.id = '%s'
            AND T.language = 'en'
            AND T.type = 'unit'
        | fields =
            T.name = name
        | limit =
            1
    ]], id);
end

------------------------------------------------------------------------------------------------------------------------
function p.findDwellingId(frame)
    local unitId = frame.args.id
    return unitId
end

------------------------------------------------------------------------------------------------------------------------
function p.findSpecialist(frame)
    local unitId = frame.args.id
    local baseId = unitId:gsub('_alt$', ''):gsub('_upg$', '')
    local nameEn = getEnglishUnitName(baseId)
    return nameEn
end

return p