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

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

模块:ItemCommon:修订间差异

来自星砂岛百科
Sizau留言 | 贡献
无编辑摘要
Sizau-bot留言 | 贡献
同步更新
第3行: 第3行:
local p = {}
local p = {}


function p.normalizeKey(value)
local item_name_cache
    return common.normalizeKey(value)
local item_mapping_cache
end


function p.loadDomainData(data_page, mapping_page)
local function default_mapping()
     local data = common.loadJsonData(data_page) or {}
     return {
    local mapping = common.loadJsonData(mapping_page) or {
         name_to_id = {},
         name_to_id = {},
         id_to_name = {},
         id_to_name = {},
第18行: 第16行:
         },
         },
     }
     }
    return data, mapping
end
end


function p.findRecord(data, mapping, key)
local function find_mapped_record(data, mapping, normalized)
     local resolved = common.trim(key)
     if type(mapping) ~= 'table' then
    if resolved == '' then
         return nil
        resolved = common.getCurrentTitleText()
    end
 
    local normalized = p.normalizeKey(resolved)
    if data[normalized] then
         return data[normalized]
     end
     end


第53行: 第44行:


     return nil
     return nil
end
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 = nil
    if common.trim(mapping_page or '') ~= '' then
        mapping = common.loadJsonData(mapping_page) or default_mapping()
    end
    return data, mapping
end
function p.loadItemIdentityData()
    if item_name_cache then
        return item_name_cache, item_mapping_cache
    end
    item_name_cache = common.loadJsonData('数据:Item/item_name_index.json') or {}
    item_mapping_cache = common.loadJsonData('数据:Item/item_mapping.json') or default_mapping()
    return item_name_cache, item_mapping_cache
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 by_domain_mapping = find_mapped_record(data, mapping, normalized)
    if by_domain_mapping then
        return by_domain_mapping
    end
    local _, item_mapping = p.loadItemIdentityData()
    local by_item_mapping = find_mapped_record(data, item_mapping, normalized)
    if by_item_mapping then
        return by_item_mapping
    end
    return nil
end
function p.findItemRecord(key)
    local item_data, item_mapping = p.loadItemIdentityData()
    return p.findRecord(item_data, item_mapping, key)
end
function p.getItemField(key, field)
    local record = p.findItemRecord(key)
    if not record then
        return ''
    end
    return common.toText(record[field])
end
function p.getIdentityField(record, key, field)
    if type(record) == 'table' and record[field] ~= nil and common.trim(record[field]) ~= '' then
        return record[field]
    end
    local item_record = p.findItemRecord(key)
    if type(item_record) == 'table' then
        return item_record[field] or ''
    end
    if field == 'id' then
        return common.trim(key)
    end
    return ''
end
function p.resolveItemId(record, key)
    return common.trim(p.getIdentityField(record, key, 'id'))
end
function p.resolveItemName(record, key)
    local value = common.trim(p.getIdentityField(record, key, 'name'))
    if value ~= '' then
        return value
    end
    return common.trim(key)
end
function p.resolveItemNameEn(record, key)
    return common.trim(p.getIdentityField(record, key, 'name_en'))
end
end


第141行: 第225行:
         return p.findRecord(data_cache, mapping_cache, key)
         return p.findRecord(data_cache, mapping_cache, key)
     end
     end
    module_table.findRecord = find_record


     function module_table.getField(frame)
     function module_table.getField(frame)
第151行: 第237行:
         local record = find_record(key)
         local record = find_record(key)
         if not record then
         if not record then
            return ''
        end
        if field == 'id' or field == 'name' or field == 'name_en' then
            return common.toText(p.getIdentityField(record, key, field))
        end
        if field == 'image' then
            local item_id = p.resolveItemId(record, key)
            if item_id == '' then
                return ''
            end
            local image_name = item_id .. '.png'
            if common.filePageExists(image_name) then
                return image_name
            end
             return ''
             return ''
         end
         end

2026年3月17日 (二) 12:01的版本

概述

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 = {}

local item_name_cache
local item_mapping_cache

local function default_mapping()
    return {
        name_to_id = {},
        id_to_name = {},
        aliases = {},
        overrides = {
            name_to_id = {},
            aliases = {},
        },
    }
end

local function find_mapped_record(data, mapping, normalized)
    if type(mapping) ~= 'table' then
        return nil
    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.normalizeKey(value)
    return common.normalizeKey(value)
end

function p.loadDomainData(data_page, mapping_page)
    local data = common.loadJsonData(data_page) or {}
    local mapping = nil
    if common.trim(mapping_page or '') ~= '' then
        mapping = common.loadJsonData(mapping_page) or default_mapping()
    end
    return data, mapping
end

function p.loadItemIdentityData()
    if item_name_cache then
        return item_name_cache, item_mapping_cache
    end

    item_name_cache = common.loadJsonData('数据:Item/item_name_index.json') or {}
    item_mapping_cache = common.loadJsonData('数据:Item/item_mapping.json') or default_mapping()
    return item_name_cache, item_mapping_cache
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 by_domain_mapping = find_mapped_record(data, mapping, normalized)
    if by_domain_mapping then
        return by_domain_mapping
    end

    local _, item_mapping = p.loadItemIdentityData()
    local by_item_mapping = find_mapped_record(data, item_mapping, normalized)
    if by_item_mapping then
        return by_item_mapping
    end

    return nil
end

function p.findItemRecord(key)
    local item_data, item_mapping = p.loadItemIdentityData()
    return p.findRecord(item_data, item_mapping, key)
end

function p.getItemField(key, field)
    local record = p.findItemRecord(key)
    if not record then
        return ''
    end
    return common.toText(record[field])
end

function p.getIdentityField(record, key, field)
    if type(record) == 'table' and record[field] ~= nil and common.trim(record[field]) ~= '' then
        return record[field]
    end

    local item_record = p.findItemRecord(key)
    if type(item_record) == 'table' then
        return item_record[field] or ''
    end

    if field == 'id' then
        return common.trim(key)
    end
    return ''
end

function p.resolveItemId(record, key)
    return common.trim(p.getIdentityField(record, key, 'id'))
end

function p.resolveItemName(record, key)
    local value = common.trim(p.getIdentityField(record, key, 'name'))
    if value ~= '' then
        return value
    end
    return common.trim(key)
end

function p.resolveItemNameEn(record, key)
    return common.trim(p.getIdentityField(record, key, 'name_en'))
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

    module_table.findRecord = find_record

    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 == 'id' or field == 'name' or field == 'name_en' then
            return common.toText(p.getIdentityField(record, key, field))
        end

        if field == 'image' then
            local item_id = p.resolveItemId(record, key)
            if item_id == '' then
                return ''
            end
            local image_name = item_id .. '.png'
            if common.filePageExists(image_name) then
                return image_name
            end
            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