Modul:DocsUtils

Aus Satisfactory Wiki
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:DocsUtils/Doku erstellt werden

local json = require("Module:Json")
itemsJSON = json.decode(mw.getCurrentFrame():expandTemplate{title='Template:DocsItems.json'})
buildingsJSON = json.decode(mw.getCurrentFrame():expandTemplate{title='Template:DocsBuildings.json'})
recipesJSON = json.decode(mw.getCurrentFrame():expandTemplate{title='Template:DocsRecipes.json'})

-- Used for localization, used in infoboxes, recipe tables, and other templates if needed.
loc = {
    units = {
        sec = " sec",
        m = " m",
        m2 = "&nbsp;m<sup>2</sup>",
        m3 = "&nbsp;m<sup>3</sup>",
        pmin = " /&nbsp;min",
        mj = "&nbsp;MJ",
        mw = "&nbsp;MW",
        kmh = "&nbsp;km/h"
    },
    numbers = {
        decimalsep = ".",
        thousandsep = ",",
    },
    branches = {
        stable = "Early Access",
        experimental = "Experimental",
        ficsmas = "FICSMAS"
    },
    filePage = "File:"
}

-- Format a number (thousands separator, decimal separator)
-- - n - number - number to format
function formatNumber(n)
    local integerPart, decimalPart = string.format("%.5f", n):match("([^%.]+)%.?(.*)")
    decimalPart = decimalPart:gsub("0*$", "")
    return ((string.reverse(integerPart):gsub("(%d%d%d)", "%1" .. loc.numbers.thousandsep):reverse():gsub("^%s*(.-)%s*$", "%1")) .. (#decimalPart > 0 and loc.numbers.decimalsep .. decimalPart or "")):gsub("^,*(.-),*$", "%1")
end

-- Retrieve the same parameter from provided stable and experimental items, returns a table with both values
-- - parameter - string - parameter name to retrieve
-- - stableItem - table - stable item to retrieve the parameter from
-- - experimentalItem - table - experimental item to retrieve the parameter from
function getBranchValues(parameter, stableItem, experimentalItem)
    return {stable = stableItem ~= nil and stableItem[parameter] or nil, experimental = experimentalItem ~= nil and experimentalItem[parameter] or nil}
end

-- Format the differences between both branches using {{EA}}/{{EX}} templates
-- - values - table - table with both values
-- - singleBranch - boolean - if true and the value is only in one branch, it will be formatted with {{EA}}/{{EX}}, otherwise it will not
function formatBranchDiff(values, singleBranch)
    local branchValues = {}
    branchValues.stable = values.stable
    branchValues.experimental = values.experimental
    if (not branchValues.stable or branchValues.stable == 0) and (not branchValues.experimental or branchValues.experimental == 0) then
        return ""
    end

    if type(branchValues.stable) == "number" then
        branchValues.stable = formatNumber(branchValues.stable)
    end
    if type(branchValues.experimental) == "number" then
        branchValues.experimental = formatNumber(branchValues.experimental)
    end

    if branchValues.stable == branchValues.experimental then
        return branchValues.stable
    elseif branchValues.stable ~= nil and branchValues.experimental == nil then
        return singleBranch and ("{{EA|" .. branchValues.stable .. "}}") or branchValues.stable
    elseif branchValues.stable == nil and branchValues.experimental ~= nil then
        return singleBranch and ("{{EX|" .. branchValues.experimental .. "}}") or branchValues.experimental
    else
        return "{{EA|" .. branchValues.stable .. "}} ({{EX|" .. branchValues.experimental .. "}})"
    end
end

-- Resolve a className to a name for both branches
-- - className - string - className to resolve
-- - fileJSON - table - JSON file to search in
function getNameFromClassName(className, fileJSON)
    local result = {}
    if fileJSON[className] ~= nil then
        for _, it in pairs(fileJSON[className]) do
            if it.stable then
                result.stable = it.name
            end
            if it.experimental then
                result.experimental = it.name
            end
        end
    end
    return result
end

-- Get object data from a file from the classname for both branches
-- - className - string - className to resolve
-- - fileJSON - table - JSON file to search in
function getObjectFromClassName(className, fileJSON)
    local result = {}
    if fileJSON[className] ~= nil then
        for _, it in pairs(fileJSON[className]) do
            if it.stable then
                result.stable = it
            end
            if it.experimental then
                result.experimental = it
            end
        end
    end
    return result
end

-- Get a link (EA>EX) and name (branchDiff formatted) for a target
-- - target - string - className to resolve
-- - stable - boolean - whether to use stable branch name
-- - experimental - boolean - whether to use experimental branch name
-- - singleBranch - boolean - whether to format the name with {{EA}}/{{EX}} if available only on one branch
function getLinkAndName(target, stable, experimental, singleBranch)
    local targetNames
    local targetLink
    local targetName
    -- resolve item or building name
    targetNames = getNameFromClassName(target, itemsJSON)
    if targetNames.stable == nil and targetNames.stable == nil then
        targetNames = getNameFromClassName(target, buildingsJSON)
    end

    if stable and experimental then
        targetName = formatBranchDiff(targetNames, singleBranch)
    else
        targetName = experimental and targetNames.experimental or targetNames.stable
        targetName = singleBranch and ("{{" .. (experimental and "EX" or "EA") .. "|" .. targetName .. "}}") or targetName
    end

    targetLink = targetNames.stable ~= nil and targetNames.stable or targetNames.experimental
    -- should never occur
    if targetName == nil or targetLink == nil then
        targetName = target
        targetLink = target
    end
    return targetLink, targetName
end