打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

本站正在进行早期测试,目前仍存在许多内容的缺失。

模块:ItemCommon

来自星砂岛百科
Sizau留言 | 贡献2026年3月9日 (一) 20:33的版本

概述

ItemCommon 提供物品域共享的查找、身份回退与配方域构建能力,供各子域模块复用。

常用函数

  • loadItemIdentityData:读取 数据:Item/item_name_index.json数据:Item/item_mapping.json
  • findItemRecord:按中文名、英文名或 ID 查找物品身份记录。
  • resolveItemId:返回统一物品 ID。
  • resolveItemName:返回统一中文名。
  • resolveItemNameEn:返回统一英文名。
  • sortItemKeys:按物品类型、系列、等级/稀有度对物品键进行稳定排序。
  • sortRecordsByItemKey:按记录中的物品键复用同一套排序规则,适合商店、配方等列表域。
  • buildRecipeDomain:为子域模块挂接 getField / processRecipeList / productionRecipeList / machineList
  • getField:会统一处理部分显示映射与“0 视为空值”规则,例如宠物类型、摆放类型、可食用布尔值、发射类型等。

排序规则

  • 先按物品类型聚合,再按系列分组。
  • 同一系列内优先按等级或稀有度从低到高排列。
  • 如果基础款没有 Lv1 后缀,会按同系列最低档处理,便于把 简易制造台 / 标准制造台 / 精良制造台 这类条目排在一起。

数据来源


local common = require('Module:Common')

local p = {}

function p.normalizeKey(value)
    return common.normalizeKey(value)
end

function p.loadDomainData(data_page, mapping_page)
    local data = common.loadJsonData(data_page) or {}
    local mapping = common.loadJsonData(mapping_page) or {
        name_to_id = {},
        id_to_name = {},
        aliases = {},
        overrides = {
            name_to_id = {},
            aliases = {},
        },
    }
    return data, mapping
end

function p.findRecord(data, mapping, key)
    local resolved = common.trim(key)
    if resolved == '' then
        resolved = common.getCurrentTitleText()
    end

    local normalized = p.normalizeKey(resolved)
    if data[normalized] then
        return data[normalized]
    end

    local override_id = mapping.overrides and mapping.overrides.name_to_id and mapping.overrides.name_to_id[normalized]
    if override_id and data[p.normalizeKey(override_id)] then
        return data[p.normalizeKey(override_id)]
    end

    local override_alias = mapping.overrides and mapping.overrides.aliases and mapping.overrides.aliases[normalized]
    if override_alias and data[p.normalizeKey(override_alias)] then
        return data[p.normalizeKey(override_alias)]
    end

    local mapped_id = mapping.name_to_id and mapping.name_to_id[normalized]
    if mapped_id and data[p.normalizeKey(mapped_id)] then
        return data[p.normalizeKey(mapped_id)]
    end

    local alias_id = mapping.aliases and mapping.aliases[normalized]
    if alias_id and data[p.normalizeKey(alias_id)] then
        return data[p.normalizeKey(alias_id)]
    end

    return nil
end

function p.getField(record, field)
    if type(record) ~= 'table' then
        return ''
    end
    return record[field]
end

function p.itemLink(frame, item_name, count)
    if common.trim(item_name) == '' then
        return ''
    end

    local args = { item_name }
    if count and tonumber(count) and tonumber(count) ~= 1 then
        args[2] = tostring(count)
    end

    return frame:expandTemplate{
        title = 'Item',
        args = args,
    }
end

function p.machineLinks(machine_names)
    if type(machine_names) ~= 'table' or #machine_names == 0 then
        return ''
    end

    local parts = {}
    for _, machine_name in ipairs(machine_names) do
        if common.trim(machine_name) ~= '' then
            parts[#parts + 1] = '[[' .. machine_name .. ']]'
        end
    end

    return table.concat(parts, '、')
end

function p.renderRecipeList(frame, recipes)
    if type(recipes) ~= 'table' or #recipes == 0 then
        return ''
    end

    local out = {}
    for _, recipe in ipairs(recipes) do
        if type(recipe) == 'table' then
            local row = {}
            local machine_name = common.trim(recipe.machine)
            if machine_name ~= '' then
                row[#row + 1] = ('[[%s]]'):format(machine_name)
            end

            local material_parts = {}
            if type(recipe.materials) == 'table' then
                for material_name, count in pairs(recipe.materials) do
                    material_parts[#material_parts + 1] = p.itemLink(frame, material_name, count)
                end
            end
            if #material_parts > 0 then
                row[#row + 1] = table.concat(material_parts, '')
            end

            if #row > 0 then
                out[#out + 1] = table.concat(row, ':')
            end
        end
    end

    return table.concat(out, '<br>')
end

function p.buildRecipeDomain(module_table, data_page, mapping_page)
    local data_cache
    local mapping_cache

    local function load_data()
        if data_cache then
            return
        end
        data_cache, mapping_cache = p.loadDomainData(data_page, mapping_page)
    end

    local function find_record(key)
        load_data()
        return p.findRecord(data_cache, mapping_cache, key)
    end

    function module_table.getField(frame)
        local key = common.getArg(frame, 1, '')
        local field = common.getArg(frame, 2, '')
        if field == '' then
            return ''
        end

        local record = find_record(key)
        if not record then
            return ''
        end

        if field == 'production_machines_display' then
            return p.machineLinks(record.production_machines)
        end
        if field == 'process_machines_display' then
            return p.machineLinks(record.process_machines)
        end

        return common.toText(p.getField(record, field))
    end

    function module_table.productionRecipeList(frame)
        local record = find_record(common.getArg(frame, 1, ''))
        if not record then
            return ''
        end
        return p.renderRecipeList(frame, record.production_recipes)
    end

    function module_table.processRecipeList(frame)
        local record = find_record(common.getArg(frame, 1, ''))
        if not record then
            return ''
        end
        return p.renderRecipeList(frame, record.process_recipes)
    end

    function module_table.machineList(frame)
        local record = find_record(common.getArg(frame, 1, ''))
        if not record then
            return ''
        end
        if common.getArg(frame, 2, 'process') == 'production' then
            return p.machineLinks(record.production_machines)
        end
        return p.machineLinks(record.process_machines)
    end
end

return p