Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Slots: Difference between revisions

From Stationeers Community Wiki
Azera (talk | contribs)
Create module for automatically rendering a slot info table. Accepts variable number of arguments consisting of name;slotType;index where index is optional
 
Azera (talk | contribs)
m Rewrite to use UTF-8 markers instead of ASCII
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
-- U+E000 / U+E001: valid UTF-8 private-use characters, cannot appear in template content
local ROW_MARKER = string.char(0xEE, 0x80, 0x80)
local FIELD_SEP  = string.char(0xEE, 0x80, 0x81)
local function splitOn(str, sep)
    local parts  = {}
    local sepLen = mw.ustring.len(sep)
    local start  = 1
    while true do
        local pos = mw.ustring.find(str, sep, start, true)
        if not pos then
            table.insert(parts, mw.ustring.sub(str, start))
            break
        end
        table.insert(parts, mw.ustring.sub(str, start, pos - 1))
        start = pos + sepLen
    end
    return parts
end
local BASE_PARAMS = {
    'Class', 'Damage', 'MaxQuantity', 'Occupied', 'OccupantHash', 'Quantity', 'ReferenceId'
}
local EXTRA_PARAMS = {
    Battery    = {'Charge', 'ChargeRatio'},
    GasCanister = {'Pressure', 'Temperature'},
    GasFilter  = {'FilterType'},
}
local function getLogicParams(slotType)
    local params = {}
    for _, v in ipairs(BASE_PARAMS) do table.insert(params, v) end
    if EXTRA_PARAMS[slotType] then
        for _, v in ipairs(EXTRA_PARAMS[slotType]) do table.insert(params, v) end
    end
    table.sort(params)
    return table.concat(params, ', ')
end
function p.row(frame)
    local args        = frame:getParent().args
    local name        = mw.text.trim(args.name or '')
    local slotType    = mw.text.trim(args.type or 'None')
    local index      = mw.text.trim(args.index or '')
    local logicParams = mw.text.trim(args.logicParams or '')
    return ROW_MARKER .. table.concat({index, name, slotType, logicParams}, FIELD_SEP)
end


function p.render(frame)
function p.render(frame)
     local args = frame:getParent().args
     local args     = frame:getParent().args
     local rows = {}
    local showLogic = args['logic'] ~= nil and args['logic'] ~= ''
    local markerLen = mw.ustring.len(ROW_MARKER)
     local rows     = {}


     for i, v in ipairs(args) do
     for i, v in ipairs(args) do
         local parts     = mw.text.split(v, ';')
         if mw.ustring.sub(v, 1, markerLen) == ROW_MARKER then
        local name      = mw.text.trim(parts[1] or '')
            local parts       = splitOn(mw.ustring.sub(v, markerLen + 1), FIELD_SEP)
        local slotType  = mw.text.trim(parts[2] or 'None')
            local index      = mw.text.trim(parts[1] or '')
        local index    = parts[3] and mw.text.trim(parts[3]) or tostring(i - 1)
            local name        = mw.text.trim(parts[2] or '')
        if name ~= '' then
            local slotType    = mw.text.trim(parts[3] or 'None')
            table.insert(rows, '|-\n| ' .. name .. ' || ' .. slotType .. ' || ' .. index)
            local logicParams = mw.text.trim(parts[4] or '')
 
            if index == '' then index = tostring(i - 1) end
 
            if name ~= '' then
                local row = '|-\n| ' .. index .. ' || ' .. name .. ' || ' .. slotType
                if showLogic then
                    local params = logicParams ~= '' and logicParams or getLogicParams(slotType)
                    row = row .. ' || ' .. params
                end
                table.insert(rows, row)
            end
         end
         end
     end
     end


     return '{| class="wikitable"\n|-\n! Slot Name !! Type !! Index\n'
     local header = '{| class="wikitable"\n|-\n! Index !! Slot Name !! Type'
        .. table.concat(rows, '\n') .. '\n|}'
    if showLogic then
        header = header .. ' !! Logic Parameters'
    end
 
    return header .. '\n' .. table.concat(rows, '\n') .. '\n|}'
end
end


return p
return p

Latest revision as of 01:19, 13 April 2026

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

local p = {}

-- U+E000 / U+E001: valid UTF-8 private-use characters, cannot appear in template content
local ROW_MARKER = string.char(0xEE, 0x80, 0x80)
local FIELD_SEP  = string.char(0xEE, 0x80, 0x81)

local function splitOn(str, sep)
    local parts  = {}
    local sepLen = mw.ustring.len(sep)
    local start  = 1
    while true do
        local pos = mw.ustring.find(str, sep, start, true)
        if not pos then
            table.insert(parts, mw.ustring.sub(str, start))
            break
        end
        table.insert(parts, mw.ustring.sub(str, start, pos - 1))
        start = pos + sepLen
    end
    return parts
end

local BASE_PARAMS = {
    'Class', 'Damage', 'MaxQuantity', 'Occupied', 'OccupantHash', 'Quantity', 'ReferenceId'
}

local EXTRA_PARAMS = {
    Battery     = {'Charge', 'ChargeRatio'},
    GasCanister = {'Pressure', 'Temperature'},
    GasFilter   = {'FilterType'},
}

local function getLogicParams(slotType)
    local params = {}
    for _, v in ipairs(BASE_PARAMS) do table.insert(params, v) end
    if EXTRA_PARAMS[slotType] then
        for _, v in ipairs(EXTRA_PARAMS[slotType]) do table.insert(params, v) end
    end
    table.sort(params)
    return table.concat(params, ', ')
end

function p.row(frame)
    local args        = frame:getParent().args
    local name        = mw.text.trim(args.name or '')
    local slotType    = mw.text.trim(args.type or 'None')
    local index       = mw.text.trim(args.index or '')
    local logicParams = mw.text.trim(args.logicParams or '')
    return ROW_MARKER .. table.concat({index, name, slotType, logicParams}, FIELD_SEP)
end

function p.render(frame)
    local args      = frame:getParent().args
    local showLogic = args['logic'] ~= nil and args['logic'] ~= ''
    local markerLen = mw.ustring.len(ROW_MARKER)
    local rows      = {}

    for i, v in ipairs(args) do
        if mw.ustring.sub(v, 1, markerLen) == ROW_MARKER then
            local parts       = splitOn(mw.ustring.sub(v, markerLen + 1), FIELD_SEP)
            local index       = mw.text.trim(parts[1] or '')
            local name        = mw.text.trim(parts[2] or '')
            local slotType    = mw.text.trim(parts[3] or 'None')
            local logicParams = mw.text.trim(parts[4] or '')

            if index == '' then index = tostring(i - 1) end

            if name ~= '' then
                local row = '|-\n| ' .. index .. ' || ' .. name .. ' || ' .. slotType
                if showLogic then
                    local params = logicParams ~= '' and logicParams or getLogicParams(slotType)
                    row = row .. ' || ' .. params
                end
                table.insert(rows, row)
            end
        end
    end

    local header = '{| class="wikitable"\n|-\n! Index !! Slot Name !! Type'
    if showLogic then
        header = header .. ' !! Logic Parameters'
    end

    return header .. '\n' .. table.concat(rows, '\n') .. '\n|}'
end

return p