Module:Modules: Difference between revisions

Current answers. Practical procedures. Reliable reference.
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 2: Line 2:


local INDEX_PAGE = 'Module:Data/Modules/index'
local INDEX_PAGE = 'Module:Data/Modules/index'
-------------------------------------------------------------------------------
-- Data loading
-------------------------------------------------------------------------------


local function loadIndex()
local function loadIndex()
Line 42: Line 46:
     return mw.loadJsonData(entry.data_page)
     return mw.loadJsonData(entry.data_page)
end
end
-------------------------------------------------------------------------------
-- Formatting helpers
-------------------------------------------------------------------------------


local function yesNo(value)
local function yesNo(value)
Line 67: Line 75:
         return '—'
         return '—'
     end
     end
      
 
    local function countValues(values)
     return table.concat(output, ', ')
end
 
local function countValues(values)
     if not values then
     if not values then
         return 0
         return 0
Line 82: Line 93:
end
end


     return table.concat(output, ', ')
-------------------------------------------------------------------------------
-- Internal-link helpers
-------------------------------------------------------------------------------
 
local GROUP_LINKS = {
     ['Core Internal'] = 'Core Internals',
    ['Optional Internal'] = 'Optional Internals',
    ['Hardpoint'] = 'Hardpoints',
    ['Utility'] = 'Utility Mounts'
}
 
local function linkGroup(group)
    if not group or group == '' then
        return nil
    end
 
    local target = GROUP_LINKS[group] or group
 
    if target == group then
        return '[[' .. target .. ']]'
    end
 
    return '[[' .. target .. '|' .. group .. ']]'
end
end
-------------------------------------------------------------------------------
-- Tests
-------------------------------------------------------------------------------


function p.testIndex(frame)
function p.testIndex(frame)
Line 119: Line 156:
         data.group,
         data.group,
         countValues(data.classes),
         countValues(data.classes),
countValues(data.ratings)
        countValues(data.ratings)
     )
     )
end
end
-------------------------------------------------------------------------------
-- Module infobox
-------------------------------------------------------------------------------


function p.infobox(frame)
function p.infobox(frame)
     local args = frame:getParent().args
     local parent = frame:getParent()
    local pargs = parent and parent.args or {}


     local key = mw.text.trim(args.key or '')
     local key = mw.text.trim(pargs.key or '')


    -- If no key was supplied, determine it from the current article title.
     if key == '' then
     if key == '' then
         local currentPage = mw.title.getCurrentTitle().text
         local currentPage = mw.title.getCurrentTitle().text
Line 143: Line 186:
     end
     end


     local output = {}
    ---------------------------------------------------------------------------
    -- Build parameters for EDFM's shared Template:Infobox
    ---------------------------------------------------------------------------
 
     local args = {
        name = data.name
    }
 
    local rowNumber = 0
 
    local function addRow(group, label, value)
        if value == nil or value == '' then
            return
        end


    table.insert(output, '{| class="infobox"')
        rowNumber = rowNumber + 1
    table.insert(output, '|+ ' .. data.name)


    table.insert(output, '|-')
        if group then
    table.insert(output, '! Group')
            args['group' .. rowNumber] = group
    table.insert(output, '| [[' .. data.group .. ']]')
        end


    if data.subcategory then
         args['label' .. rowNumber] = label
         table.insert(output, '|-')
         args['row' .. rowNumber] = value
        table.insert(output, '! Subcategory')
         table.insert(output, '| ' .. data.subcategory)
     end
     end


     table.insert(output, '|-')
     ---------------------------------------------------------------------------
     table.insert(output, '! Classes')
     -- MODULE
     table.insert(output, '| ' .. joinValues(data.classes))
     ---------------------------------------------------------------------------


     table.insert(output, '|-')
     addRow(
     table.insert(output, '! Ratings')
        'MODULE',
     table.insert(output, '| ' .. joinValues(data.ratings))
        'Group',
        linkGroup(data.group)
     )
 
     addRow(
        nil,
        'Subcategory',
        data.subcategory
    )


     table.insert(output, '|-')
     addRow(
    table.insert(output, '! Required')
        nil,
    table.insert(output, '| ' .. yesNo(data.required))
        'Classes',
        joinValues(data.classes)
    )


     table.insert(output, '|-')
     addRow(
     table.insert(output, '! Storable')
        nil,
     table.insert(output, '| ' .. yesNo(data.storable))
        'Ratings',
        joinValues(data.ratings)
    )
 
    ---------------------------------------------------------------------------
    -- OUTFITTING
    ---------------------------------------------------------------------------
 
    addRow(
        'OUTFITTING',
        'Required',
        yesNo(data.required)
     )
 
    addRow(
        nil,
        'Storable',
        yesNo(data.storable)
    )
 
     addRow(
        nil,
        'Transferable',
        yesNo(data.transferable)
    )


     table.insert(output, '|-')
     ---------------------------------------------------------------------------
     table.insert(output, '! Transferable')
     -- ENGINEERING
     table.insert(output, '| ' .. yesNo(data.transferable))
     ---------------------------------------------------------------------------


     table.insert(output, '|-')
     addRow(
    table.insert(output, '! Engineering')
        'ENGINEERING',
    table.insert(
        'Eligible',
        output,
         yesNo(
         '| ' .. yesNo(
             data.engineering and data.engineering.eligible
             data.engineering and data.engineering.eligible
         )
         )
     )
     )


     table.insert(output, '|}')
     ---------------------------------------------------------------------------
    -- Render with EDFM's existing shared infobox
    ---------------------------------------------------------------------------


     return table.concat(output, '\n')
     return frame:expandTemplate {
        title = 'Infobox',
        args = args
    }
end
end


return p
return p

Latest revision as of 02:19, 23 August 2026

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

local p = {}

local INDEX_PAGE = 'Module:Data/Modules/index'

-------------------------------------------------------------------------------
-- Data loading
-------------------------------------------------------------------------------

local function loadIndex()
    return mw.loadJsonData(INDEX_PAGE)
end

local function findKeyByPage(pageName)
    local index = loadIndex()

    if not index.modules then
        return nil
    end

    for key, entry in pairs(index.modules) do
        if entry.page == pageName then
            return key
        end
    end

    return nil
end

local function loadModule(key)
    local index = loadIndex()

    if not index.modules then
        return nil, 'Module index does not contain a "modules" object.'
    end

    local entry = index.modules[key]

    if not entry then
        return nil, 'Module "' .. tostring(key) .. '" was not found in the module index.'
    end

    if not entry.data_page then
        return nil, 'Module "' .. tostring(key) .. '" does not have a data_page defined.'
    end

    return mw.loadJsonData(entry.data_page)
end

-------------------------------------------------------------------------------
-- Formatting helpers
-------------------------------------------------------------------------------

local function yesNo(value)
    if value == true then
        return 'Yes'
    elseif value == false then
        return 'No'
    end

    return 'Unknown'
end

local function joinValues(values)
    if not values then
        return '—'
    end

    local output = {}

    for _, value in ipairs(values) do
        output[#output + 1] = tostring(value)
    end

    if #output == 0 then
        return '—'
    end

    return table.concat(output, ', ')
end

local function countValues(values)
    if not values then
        return 0
    end

    local count = 0

    for _ in ipairs(values) do
        count = count + 1
    end

    return count
end

-------------------------------------------------------------------------------
-- Internal-link helpers
-------------------------------------------------------------------------------

local GROUP_LINKS = {
    ['Core Internal'] = 'Core Internals',
    ['Optional Internal'] = 'Optional Internals',
    ['Hardpoint'] = 'Hardpoints',
    ['Utility'] = 'Utility Mounts'
}

local function linkGroup(group)
    if not group or group == '' then
        return nil
    end

    local target = GROUP_LINKS[group] or group

    if target == group then
        return '[[' .. target .. ']]'
    end

    return '[[' .. target .. '|' .. group .. ']]'
end

-------------------------------------------------------------------------------
-- Tests
-------------------------------------------------------------------------------

function p.testIndex(frame)
    local data = loadIndex()

    if not data.modules then
        return '<strong class="error">Module index loaded, but no "modules" object was found.</strong>'
    end

    local module = data.modules.frame_shift_drive

    if not module then
        return '<strong class="error">Module index loaded, but "frame_shift_drive" was not found.</strong>'
    end

    return string.format(
        'Module database connected successfully: [[%s|%s]] — %s',
        module.page,
        module.name,
        module.group
    )
end

function p.testModule(frame)
    local data, err = loadModule('frame_shift_drive')

    if not data then
        return '<strong class="error">' .. err .. '</strong>'
    end

    return string.format(
        'Detailed module data loaded successfully: [[%s|%s]] — %s — %d classes — %d ratings',
        data.page,
        data.name,
        data.group,
        countValues(data.classes),
        countValues(data.ratings)
    )
end

-------------------------------------------------------------------------------
-- Module infobox
-------------------------------------------------------------------------------

function p.infobox(frame)
    local parent = frame:getParent()
    local pargs = parent and parent.args or {}

    local key = mw.text.trim(pargs.key or '')

    -- If no key was supplied, determine it from the current article title.
    if key == '' then
        local currentPage = mw.title.getCurrentTitle().text
        key = findKeyByPage(currentPage)
    end

    if not key then
        return '<strong class="error">No module database entry could be found for this page.</strong>'
    end

    local data, err = loadModule(key)

    if not data then
        return '<strong class="error">' .. err .. '</strong>'
    end

    ---------------------------------------------------------------------------
    -- Build parameters for EDFM's shared Template:Infobox
    ---------------------------------------------------------------------------

    local args = {
        name = data.name
    }

    local rowNumber = 0

    local function addRow(group, label, value)
        if value == nil or value == '' then
            return
        end

        rowNumber = rowNumber + 1

        if group then
            args['group' .. rowNumber] = group
        end

        args['label' .. rowNumber] = label
        args['row' .. rowNumber] = value
    end

    ---------------------------------------------------------------------------
    -- MODULE
    ---------------------------------------------------------------------------

    addRow(
        'MODULE',
        'Group',
        linkGroup(data.group)
    )

    addRow(
        nil,
        'Subcategory',
        data.subcategory
    )

    addRow(
        nil,
        'Classes',
        joinValues(data.classes)
    )

    addRow(
        nil,
        'Ratings',
        joinValues(data.ratings)
    )

    ---------------------------------------------------------------------------
    -- OUTFITTING
    ---------------------------------------------------------------------------

    addRow(
        'OUTFITTING',
        'Required',
        yesNo(data.required)
    )

    addRow(
        nil,
        'Storable',
        yesNo(data.storable)
    )

    addRow(
        nil,
        'Transferable',
        yesNo(data.transferable)
    )

    ---------------------------------------------------------------------------
    -- ENGINEERING
    ---------------------------------------------------------------------------

    addRow(
        'ENGINEERING',
        'Eligible',
        yesNo(
            data.engineering and data.engineering.eligible
        )
    )

    ---------------------------------------------------------------------------
    -- Render with EDFM's existing shared infobox
    ---------------------------------------------------------------------------

    return frame:expandTemplate {
        title = 'Infobox',
        args = args
    }
end

return p