Module:Modules: Difference between revisions

Current answers. Practical procedures. Reliable reference.
Jump to navigation Jump to search
No edit summary
No edit summary
Line 67: Line 67:
         return '—'
         return '—'
     end
     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


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

Revision as of 02:14, 23 August 2026

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

local p = {}

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

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

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
    
    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

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

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,
        #(data.classes or {}),
        #(data.ratings or {})
    )
end

function p.infobox(frame)
    local args = frame:getParent().args

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

    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

    local output = {}

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

    table.insert(output, '|-')
    table.insert(output, '! Group')
    table.insert(output, '| [[' .. data.group .. ']]')

    if data.subcategory then
        table.insert(output, '|-')
        table.insert(output, '! Subcategory')
        table.insert(output, '| ' .. data.subcategory)
    end

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

    table.insert(output, '|-')
    table.insert(output, '! Ratings')
    table.insert(output, '| ' .. joinValues(data.ratings))

    table.insert(output, '|-')
    table.insert(output, '! Required')
    table.insert(output, '| ' .. yesNo(data.required))

    table.insert(output, '|-')
    table.insert(output, '! Storable')
    table.insert(output, '| ' .. yesNo(data.storable))

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

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

    table.insert(output, '|}')

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

return p