Module:Scraps/AutoInfobox

From Lethal Company Wiki

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

local p = {}

local Scraps = require('Module:Scraps')
local I18n = require('Module:I18n')

---@param bool boolean
local function boolToInfoboxArg(bool)
    if bool then
        return 'yes'
    else
        return 'no'
    end
end

function p._infoboxArgs(mName, version, lang)
    local scrapInfo = Scraps.getScrap(mName, version)

    local templateArgs = {}

    templateArgs['name'] = I18n.getTranslation(lang, scrapInfo.name, 'Scraps')
    templateArgs['minprice'] = Scraps.utils.scrapValue(scrapInfo.minValue)
    templateArgs['maxprice'] = Scraps.utils.scrapValue(scrapInfo.maxValue)
    templateArgs['weight'] = Scraps.utils.scrapWeight(scrapInfo.weight)
    templateArgs['conductive'] = boolToInfoboxArg(scrapInfo.conductive)
    templateArgs['twohanded'] = boolToInfoboxArg(scrapInfo.twoHanded)
    templateArgs['isweapon'] = boolToInfoboxArg(scrapInfo.isWeapon)
    templateArgs['hasbattery'] = boolToInfoboxArg(scrapInfo.hasBattery)

    return templateArgs
end

function p.infobox(frame)
    local scrapMName = nil
    local version = nil
    local lang = mw.message.getDefaultLanguage().code

    local templateArgs = {}

    for key, value in pairs(frame.args) do
        if key == 'scrap' then
            scrapMName = value
        elseif key == 'version' then
            version = value
        elseif key == 'lang' then
            lang = value
        else
            templateArgs[key] = value
        end
    end

    if scrapMName == nil then
        scrapMName = frame.args[1]
    end

    if scrapMName == nil then
        error("No scrap specified")
    end

    for key, value in pairs(p._infoboxArgs(scrapMName, version, lang)) do
        if templateArgs[key] == nil then
            templateArgs[key] = value
        end
    end

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

return p