Module:Modules: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 5: | Line 5: | ||
local function loadIndex() | local function loadIndex() | ||
return mw.loadJsonData(INDEX_PAGE) | 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 | end | ||
| Line 25: | Line 41: | ||
return mw.loadJsonData(entry.data_page) | 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 or #values == 0 then | |||
return '—' | |||
end | |||
local output = {} | |||
for _, value in ipairs(values) do | |||
table.insert(output, tostring(value)) | |||
end | |||
return table.concat(output, ', ') | |||
end | end | ||
| Line 63: | Line 103: | ||
#(data.ratings 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 | end | ||
return p | return p | ||
Revision as of 02:08, 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 or #values == 0 then
return '—'
end
local output = {}
for _, value in ipairs(values) do
table.insert(output, tostring(value))
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