Jump to content

Module:HistoricalItem: Difference between revisions

From Bconomy Wiki
No edit summary
 
GameReferenceSync (talk | contribs)
No edit summary
 
Line 33: Line 33:
         tr:tag('td'):wikitext(value)
         tr:tag('td'):wikitext(value)
     end
     end
    if entry.status == 'removed' then row('Status', 'Removed') end
     row('Type', escape(entry.type))
     row('Type', escape(entry.type))
     row('Status', entry.status == 'removed' and 'Removed' or 'Former name')
     if entry.effect then row('Effect', escape(entry.effect)) end
     if entry.cost then row('Worth', compact(entry.cost) .. ' BC') end
     if entry.cost then row('Worth', compact(entry.cost) .. ' BC') end
     if entry.properties then row('Properties', escape(entry.properties)) end
     if entry.properties then row('Properties', escape(entry.properties)) end
    if entry.effect then row('Effect', escape(entry.effect)) end
     local recipe = {}
     local recipe = {}
     for _, ingredient in ipairs(entry.recipe or {}) do table.insert(recipe, itemLink(ingredient) .. ' × ' .. number(ingredient.amount)) end
     for _, ingredient in ipairs(entry.recipe or {}) do table.insert(recipe, itemLink(ingredient) .. ' × ' .. number(ingredient.amount)) end

Latest revision as of 23:52, 5 November 2026

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

local p = {}
local function escape(value) return mw.text.nowiki(tostring(value)) end
local function number(value) return mw.language.getContentLanguage():formatNum(value) end
local function compact(value)
    for _, unit in ipairs({{1e12, 'T'}, {1e9, 'B'}, {1e6, 'M'}, {1e3, 'K'}}) do
        if value >= unit[1] then
            local amount = math.floor(value / unit[1] * 100 + 0.5) / 100
            return tostring(mw.html.create('abbr'):attr('title', number(value)):wikitext(number(amount) .. unit[2]))
        end
    end
    return number(value)
end
local function itemLink(entry)
    local icon = entry.imageFile and '<span class="bc-item-inline-icon">[[File:' .. escape(entry.imageFile) .. '|20x20px|middle|link=|alt=]]</span>' or ''
    return icon .. '[[' .. escape(entry.article or entry.name) .. '|' .. escape(entry.name) .. ']]'
end
function p.infobox(frame)
    local a = frame:getParent() and frame:getParent().args or frame.args
    local key = mw.text.trim(a.key or a[1] or '')
    local entry = mw.loadJsonData('GameData:Historical items.json').items[key]
    if not entry then return '<strong class="error">Missing historical Item.</strong>' end
    local box = mw.html.create('table'):addClass('wikitable bc-infobox bc-item-infobox')
    box:tag('caption'):wikitext(escape(entry.name))
    if entry.imageFile or entry.desc then
        local picture = box:tag('tr'):tag('td'):attr('colspan', 2):addClass('bc-item-picture')
        if entry.imageFile then picture:wikitext('[[File:' .. escape(entry.imageFile) .. '|frameless|alt=' .. escape(entry.name) .. ']]') end
        if entry.desc then picture:tag('blockquote'):addClass('bc-item-flavor'):wikitext(escape(entry.desc)) end
    end
    local function row(label, value)
        if not value or value == '' then return end
        local tr = box:tag('tr')
        tr:tag('th'):attr('scope', 'row'):wikitext(label)
        tr:tag('td'):wikitext(value)
    end
    if entry.status == 'removed' then row('Status', 'Removed') end
    row('Type', escape(entry.type))
    if entry.effect then row('Effect', escape(entry.effect)) end
    if entry.cost then row('Worth', compact(entry.cost) .. ' BC') end
    if entry.properties then row('Properties', escape(entry.properties)) end
    local recipe = {}
    for _, ingredient in ipairs(entry.recipe or {}) do table.insert(recipe, itemLink(ingredient) .. ' × ' .. number(ingredient.amount)) end
    if #recipe > 0 then row('Recipe', table.concat(recipe, '<br />')) end
    row('Internal ID', '<code>' .. escape(entry.id) .. '</code>')
    return tostring(box)
end
return p