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

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

模块:Machine:修订间差异

来自星砂岛百科
Sizau留言 | 贡献
无编辑摘要
Sizau留言 | 贡献
无编辑摘要
第1行: 第1行:
local common = require('Module:Common')
local common = require('Module:Common')
local item_common = require('Module:ItemCommon')
local item = require('Module:Item')
local css = require('Module:CSS')
local css = require('Module:CSS')
local item_module = require('Module:Item')
local npc_module = require('Module:NPC')


local p = {}
local p = {}


local character_cache
local FIELD_MAP = {
local character_records
    id = 'i',
local character_ids
    name = 'n',
local character_mapping
    name_en = 'en',
local global_rules
    type = 't',
local tag_items
    type_display = 'td',
 
    item_level = 'lv',
local item_cache
    rarity = 'r',
local item_by_index
    sell_price = 'sp',
local item_mapping
    base_value = 'bv',
local item_redirects
    max_stack = 'ms',
 
    max_quality = 'mq',
local id_registry
     tags = 'g',
local registry_character_ids
     origin = 'o',
local registry_item_ids
     locked_origin = 'lo',
local registry_character_index_by_id
     use_description = 'ud',
local registry_item_index_by_id
     unlock_title = 'ut',
local summary_cache
     construct_template = 'ct',
 
     construction_template_count = 'ctc',
local LUACACHE_NAMESPACE = 'gifting:npc_tabs'
     energy_capacity = 'ec',
local LUACACHE_VERSION = '2026-03-16-version-1'
     production_formula = 'pf',
 
     production_formula_origin = 'pfo',
local level_to_bucket = {
     craft = 'c',
     resonance = 'resonance',
     upgrade = 'u',
     special = 'special',
     prev_machine = 'pm',
     loved = 'loved',
     next_machine = 'nm',
     liked = 'liked',
     neutral = 'neutral',
     disliked = 'disliked',
     hated = 'hated',
     r = 'resonance',
     s = 'special',
     v = 'loved',
     k = 'liked',
     n = 'neutral',
     d = 'disliked',
     h = 'hated',
}
}


local bucket_to_short = {
local data_cache
    resonance = 'r',
local mapping_cache
    special = 's',
    loved = 'v',
    liked = 'k',
    neutral = 'n',
    disliked = 'd',
    hated = 'h',
}


local function normalize_key(value)
local function is_empty(value)
     return common.normalizeKey(value)
     if value == nil then
end
         return true
 
local function resolve_level_bucket(level)
    if level == nil then
         return nil
     end
     end
 
     for _ in pairs(value) do
     if type(level) == 'string' then
         return false
        local normalized = normalize_key(level)
         return level_to_bucket[normalized]
     end
     end
 
     return true
    local numeric = tonumber(level)
    if numeric == 4 then
        return 'resonance'
    end
    if numeric == 3 then
        return 'special'
    end
    if numeric == 2 then
        return 'loved'
    end
    if numeric == 1 then
        return 'liked'
    end
    if numeric == 0 then
        return 'neutral'
    end
    if numeric == -1 then
        return 'disliked'
    end
    if numeric == -2 then
        return 'hated'
    end
     return nil
end
end


local function empty_summary()
local function load_data()
    return {
     if data_cache then
        resonance = { personal = {}, global = {} },
        special = { personal = {}, global = {} },
        loved = { personal = {}, global = {} },
        liked = { personal = {}, global = {} },
        neutral = { personal = {}, global = {} },
        disliked = { personal = {}, global = {} },
        hated = { personal = {}, global = {} },
    }
end
 
local function load_id_registry()
     if id_registry then
         return
         return
     end
     end
     id_registry = common.loadJsonData('数据:Gifting/gift_id_registry.json') or {}
 
    registry_character_ids = id_registry.characters or {}
     local raw_data
     registry_item_ids = id_registry.items or {}
    raw_data, mapping_cache = item_common.loadDomainData(
     registry_character_index_by_id = {}
        '数据:Machine/machine_index.json',
    for idx, value in ipairs(registry_character_ids) do
        '数据:Machine/machine_mapping.json'
        local key = normalize_key(value)
     )
        if key ~= '' then
     if type(raw_data) == 'table' and type(raw_data.records) == 'table' then
            registry_character_index_by_id[key] = idx - 1
        data_cache = raw_data.records
        end
     else
     end
        data_cache = raw_data or {}
    registry_item_index_by_id = {}
    for idx, value in ipairs(registry_item_ids) do
        local key = normalize_key(value)
        if key ~= '' then
            registry_item_index_by_id[key] = idx - 1
        end
     end
     end
end
end


local function item_id_from_index(index)
local function find_record(key)
     load_id_registry()
     load_data()
    local n = tonumber(index)
     return item_common.findRecord(data_cache, mapping_cache, key)
    if not n then
        return ''
    end
     return common.trim(registry_item_ids[n + 1] or '')
end
end


local function character_id_from_index(index)
local function get_short_field(field)
    load_id_registry()
     return FIELD_MAP[field] or field
    local n = tonumber(index)
    if not n then
        return ''
    end
     return common.trim(registry_character_ids[n + 1] or '')
end
end


local function load_character_data()
local function get_record_field(record, field)
     if character_cache then
     if type(record) ~= 'table' then
         return
         return nil
     end
     end
 
     local short_key = get_short_field(field)
     character_cache = common.loadJsonData('数据:Gifting/gift_preferences.json') or {}
     if record[short_key] ~= nil then
     character_records = character_cache.characters or {}
        return record[short_key]
    global_rules = character_cache.global or {}
    tag_items = character_cache.tag_items or {}
    load_id_registry()
 
    character_ids = {}
    for idx, record in ipairs(character_records) do
        local cid = character_id_from_index(idx - 1)
        if cid ~= '' then
            character_ids[normalize_key(cid)] = idx
        end
     end
     end
 
     return record[field]
     character_mapping = common.loadJsonData('数据:Gifting/gifting_mapping.json') or {
        name_to_id = {},
        id_to_name = {},
        aliases = {},
        overrides = {
            name_to_id = {},
            aliases = {},
        },
    }
end
end


local function load_item_data()
local function get_craft_field(craft, short_key, long_key)
     if item_cache then
     if type(craft) ~= 'table' then
         return
        return nil
    end
    if craft[short_key] ~= nil then
         return craft[short_key]
     end
     end
 
     return craft[long_key]
     item_cache = common.loadJsonData('数据:Gifting/gift_preferences_by_item.json') or {}
    item_by_index = item_cache.items or {}
    item_redirects = item_cache.item_redirects or {}
    item_mapping = common.loadJsonData('数据:Item/item_mapping.json') or {
        name_to_id = {},
        id_to_name = {},
        aliases = {},
        overrides = {
            name_to_id = {},
            aliases = {},
        },
    }
end
end


local function find_character_record(key)
local function format_time(seconds)
     load_character_data()
     seconds = tonumber(seconds) or 0
 
     if seconds <= 0 then
    local resolved = common.trim(key)
         return ''
     if resolved == '' then
         resolved = common.getCurrentTitleText()
     end
     end
 
     if seconds < 60 then
    local normalized = normalize_key(resolved)
         return tostring(math.floor(seconds)) .. '秒'
    local idx = character_ids[normalized]
 
     if not idx then
         local override_id = character_mapping.overrides and character_mapping.overrides.name_to_id and character_mapping.overrides.name_to_id[normalized]
        if override_id then
            idx = character_ids[normalize_key(override_id)]
        end
     end
     end
 
    local minutes = math.floor(seconds / 60)
     if not idx then
     local remain = math.floor(seconds % 60)
        local override_alias = character_mapping.overrides and character_mapping.overrides.aliases and character_mapping.overrides.aliases[normalized]
    if remain == 0 then
        if override_alias then
        return tostring(minutes) .. '分'
            idx = character_ids[normalize_key(override_alias)]
        end
     end
     end
    return tostring(minutes) .. '分' .. tostring(remain) .. '秒'
end


    if not idx then
local function render_materials(frame, materials)
        local mapped_id = character_mapping.name_to_id and character_mapping.name_to_id[normalized]
    if is_empty(materials) then
        if mapped_id then
         return ''
            idx = character_ids[normalize_key(mapped_id)]
         end
     end
     end


     if not idx then
     local parts = {}
         local alias_id = character_mapping.aliases and character_mapping.aliases[normalized]
    for item_id, count in pairs(materials) do
        if alias_id then
         parts[#parts + 1] = frame:expandTemplate{
             idx = character_ids[normalize_key(alias_id)]
            title = 'Item',
         end
             args = { item_id, tostring(count), class = 'block' },
         }
     end
     end
 
     return table.concat(parts, '')
     if not idx then
        return nil
    end
    return character_records[idx], idx - 1
end
end


local function resolve_item_index(index)
local function render_craft_table(title_text, station_label, station_value, materials_label, materials_value, time_label, time_value, exp_label, exp_value)
     local key = tostring(index)
     local out = {}
     local num_key = tonumber(index)
     out[#out + 1] = '<table class="wikitable machine-craft-table">'
      
     out[#out + 1] = '<tr><th colspan="2">' .. title_text .. '</th></tr>'
     if item_by_index[key] then
     out[#out + 1] = '<tr><th>' .. station_label .. '</th><td>' .. station_value .. '</td></tr>'
        return key
     out[#out + 1] = '<tr><th>' .. materials_label .. '</th><td>' .. materials_value .. '</td></tr>'
    end
     if time_value ~= '' then
      
         out[#out + 1] = '<tr><th>' .. time_label .. '</th><td>' .. time_value .. '</td></tr>'
     if num_key and item_by_index[num_key] then
         return num_key
     end
     end
      
     if exp_value ~= '' then
    local redirected = item_redirects and item_redirects[key]
         out[#out + 1] = '<tr><th>' .. exp_label .. '</th><td>' .. exp_value .. '</td></tr>'
    if not redirected and num_key then
         redirected = item_redirects[num_key]
     end
     end
      
     out[#out + 1] = '</table>'
    if redirected then
     return table.concat(out, '\n')
        local redirected_key = tostring(redirected)
        local redirected_num = tonumber(redirected)
       
        if item_by_index[redirected_key] then
            return redirected_key
        end
       
        if redirected_num and item_by_index[redirected_num] then
            return redirected_num
        end
    end
   
     return nil
end
end


local function find_item_record(key)
function p.getField(frame)
     load_item_data()
     local key = common.getArg(frame, 1, '')
    load_id_registry()
     local field = common.getArg(frame, 2, '')
 
     if field == '' then
     local resolved = common.trim(key)
         return ''
     if resolved == '' then
         resolved = common.getCurrentTitleText()
     end
     end


     local normalized = normalize_key(resolved)
     local record = find_record(key)
    local item_id = ''
     if not record then
    local item_index = registry_item_index_by_id[normalized]
         return ''
     if item_index ~= nil then
         item_id = registry_item_ids[item_index + 1] or ''
     end
     end


     if item_id == '' then
    local craft = get_record_field(record, 'craft')
         local override_id = item_mapping.overrides and item_mapping.overrides.name_to_id and item_mapping.overrides.name_to_id[normalized]
     if field == 'craft_machine' and craft then
         if override_id and override_id ~= '' then
         local machine_id = get_craft_field(craft, 'm', 'machine')
             item_id = override_id
         if machine_id and machine_id ~= '' then
             return item.renderItemWithArgs(frame, {machine_id})
         end
         end
        return ''
     end
     end
 
     if field == 'craft_time' and craft then
     if item_id == '' then
         return format_time(get_craft_field(craft, 't', 'time'))
         local override_alias = item_mapping.overrides and item_mapping.overrides.aliases and item_mapping.overrides.aliases[normalized]
        if override_alias and override_alias ~= '' then
            item_id = override_alias
        end
     end
     end
 
     if field == 'craft_exp' and craft then
     if item_id == '' then
         return common.toText(get_craft_field(craft, 'e', 'exp'))
         local mapped_id = item_mapping.name_to_id and item_mapping.name_to_id[normalized]
        if mapped_id and mapped_id ~= '' then
            item_id = mapped_id
        end
     end
     end
 
    local upgrade = get_record_field(record, 'upgrade')
     if item_id == '' then
     if field == 'upgrade_from' and upgrade then
         local alias_id = item_mapping.aliases and item_mapping.aliases[normalized]
         local from_id = get_craft_field(upgrade, 'f', 'from_machine')
        if alias_id and alias_id ~= '' then
        if from_id and from_id ~= '' then
             item_id = alias_id
            local from_name = item.getNameByKey(from_id)
            if from_name and from_name ~= '' then
                return '[[' .. from_name .. ']]'
             end
         end
         end
        return ''
     end
     end
 
     if field == 'upgrade_time' and upgrade then
     if item_id == '' then
         return format_time(get_craft_field(upgrade, 't', 'time'))
         return nil
     end
     end
 
     if field == 'upgrade_exp' and upgrade then
     if item_index == nil then
         return common.toText(get_craft_field(upgrade, 'e', 'exp'))
         item_index = registry_item_index_by_id[normalize_key(item_id)]
     end
     end
     if item_index == nil then
     if field == 'construction_template_count' then
         return nil
         local value = tonumber(get_record_field(record, field)) or 0
    end
         if value <= 0 then
 
             return ''
    local resolved_index = resolve_item_index(item_index)
    if not resolved_index then
        return nil
    end
 
    return item_by_index[resolved_index]
end
 
local function append_unique_indexes(values, seen, target)
    if type(values) ~= 'table' then
        return
    end
    for _, value in ipairs(values) do
        local index = tonumber(value)
         if index ~= nil and not seen[index] then
             seen[index] = true
            target[#target + 1] = index
         end
         end
        return tostring(value)
     end
     end
end
     if field == 'energy_capacity' then
 
        local value = tonumber(get_record_field(record, field)) or 0
local function expand_rule_items(rule)
         if value <= 0 then
     local result = {}
             return ''
    local seen = {}
    for _, tag in ipairs(rule.t or {}) do
        append_unique_indexes(tag_items[tag], seen, result)
    end
    append_unique_indexes(rule.s, seen, result)
    return result
end
 
local function build_character_summary(record, record_index)
    if record_index ~= nil then
        summary_cache = summary_cache or {}
         if summary_cache[record_index] then
             return summary_cache[record_index]
         end
         end
        return tostring(value)
     end
     end
     local summary = empty_summary()
     if field == 'prev_machine' then
    local global_seen = {
         local prev_id = get_record_field(record, 'prev_machine')
         resonance = {}, special = {}, loved = {}, liked = {}, neutral = {}, disliked = {}, hated = {},
         if prev_id and prev_id ~= '' then
    }
            return item.renderItemWithArgs(frame, {prev_id})
    local personal_seen = {
         resonance = {}, special = {}, loved = {}, liked = {}, neutral = {}, disliked = {}, hated = {},
    }
    local all_personal_items = {}
 
    for _, rule in ipairs(record.p or {}) do
        for _, item_idx in ipairs(expand_rule_items(rule)) do
            all_personal_items[item_idx] = true
         end
         end
        return ''
     end
     end
 
     if field == 'next_machine' then
     for _, rule in ipairs(global_rules) do
         local next_id = get_record_field(record, 'next_machine')
         local bucket = resolve_level_bucket(rule.l)
         if next_id and next_id ~= '' then
         if bucket and summary[bucket] then
             return item.renderItemWithArgs(frame, {next_id})
             for _, item_idx in ipairs(expand_rule_items(rule)) do
                if not all_personal_items[item_idx] and not global_seen[bucket][item_idx] then
                    global_seen[bucket][item_idx] = true
                    summary[bucket].global[#summary[bucket].global + 1] = item_idx
                end
            end
         end
         end
        return ''
     end
     end


     for _, rule in ipairs(record.p or {}) do
     return common.toText(get_record_field(record, field))
        local bucket = resolve_level_bucket(rule.l)
        if bucket and summary[bucket] then
            for _, item_idx in ipairs(expand_rule_items(rule)) do
                if not personal_seen[bucket][item_idx] then
                    personal_seen[bucket][item_idx] = true
                    summary[bucket].personal[#summary[bucket].personal + 1] = item_idx
                end
            end
        end
    end
 
    if record_index ~= nil then
        summary_cache[record_index] = summary
    end
    return summary
end
end


local function get_summary_bucket(record, bucket, record_index)
function p.ingredientList(frame)
     if type(record) ~= 'table' then
    local key = common.getArg(frame, 1, '')
         return nil
    local record = find_record(key)
     if not record then
         return ''
     end
     end
     local resolved_bucket = level_to_bucket[bucket] or normalize_key(bucket)
     local craft = get_record_field(record, 'craft')
    local summary = build_character_summary(record, record_index)
     if not craft then
    return summary[resolved_bucket]
end
 
local function item_label_from_index(item_index)
    load_item_data()
    local item_id = item_id_from_index(item_index)
     if item_id == '' then
         return ''
         return ''
     end
     end
     return common.trim(item_mapping.id_to_name and item_mapping.id_to_name[normalize_key(item_id)] or '')
     return render_materials(frame, get_craft_field(craft, 'mat', 'materials'))
end
end


local function item_display_meta(item_index)
function p.upgradeIngredientList(frame)
     local item_id = item_id_from_index(item_index)
     local key = common.getArg(frame, 1, '')
    if item_id == '' then
     local record = find_record(key)
        return nil
     if not record then
    end
         return ''
 
     local name = item_label_from_index(item_index)
     if name == '' then
         name = item_id
     end
     end
 
     local upgrade = get_record_field(record, 'upgrade')
    local file_name = item_id
     if not upgrade then
    if file_name == '' then
        file_name = name
    end
 
    return {
        name = name,
        image = file_name .. '.png',
    }
end
 
local character_index_cache
 
local function load_character_index()
    if character_index_cache then
        return
    end
    character_index_cache = common.loadJsonData('数据:Character/character_index.json') or {}
end
 
local function is_creature(name)
    load_character_index()
     local normalized = normalize_key(name)
    local record = character_index_cache[normalized]
    if record and record.entity_kind == 'creature' then
        return true
    end
    return false
end
 
local function item_template_from_name(frame, name)
     if not frame or common.trim(name) == '' then
        return nil
    end
 
    if is_creature(name) then
        return '[[' .. name .. ']]'
    end
 
    local output = npc_module.renderNPC(frame:newChild{
        title = 'Module:NPC',
        args = { [1] = name, class = 'block' }
    })
    return common.trim(output) ~= '' and output or ''
end
 
local function character_label_from_index(character_index)
    load_character_data()
    local character_id = character_id_from_index(character_index)
    if character_id == '' then
         return ''
         return ''
     end
     end
     return common.trim(character_mapping.id_to_name and character_mapping.id_to_name[normalize_key(character_id)] or '')
     return render_materials(frame, get_craft_field(upgrade, 'mat', 'materials'))
end
end


local function join_indexes(values, label_resolver, fallback_resolver)
function p.craftInfo(frame)
     if type(values) ~= 'table' or #values == 0 then
    local key = common.getArg(frame, 1, '')
     local record = find_record(key)
    if not record then
         return ''
         return ''
     end
     end


     local parts = {}
     local out = {}
     for _, value in ipairs(values) do
     local craft = get_record_field(record, 'craft')
        local idx = tonumber(value)
    local craft_materials = get_craft_field(craft, 'mat', 'materials')
        if idx ~= nil then
    if craft and not is_empty(craft_materials) then
            local label = label_resolver and label_resolver(idx) or ''
        local machine_id = get_craft_field(craft, 'm', 'machine')
            if label ~= '' then
        local machine_display = ''
                parts[#parts + 1] = ('[[%s]]'):format(label)
        if machine_id and common.trim(machine_id) ~= '' then
             else
             local machine_name = item.getNameByKey(machine_id)
                local fallback = fallback_resolver and fallback_resolver(idx) or ''
            if machine_name and machine_name ~= '' then
                if fallback ~= '' then
                machine_display = '[[' .. machine_name .. ']]'
                    parts[#parts + 1] = fallback
                end
             end
             end
         end
         end
        out[#out + 1] = render_craft_table(
            '制造配方',
            '制造站',
            machine_display,
            '材料',
            render_materials(frame, craft_materials),
            '时间',
            format_time(get_craft_field(craft, 't', 'time')),
            '经验',
            common.toText(get_craft_field(craft, 'e', 'exp'))
        )
     end
     end
    return table.concat(parts, '、')
end


local function render_bucket(bucket_data, personal_label, global_label, label_resolver, fallback_resolver)
    local upgrade = get_record_field(record, 'upgrade')
    if type(bucket_data) ~= 'table' then
     local upgrade_materials = get_craft_field(upgrade, 'mat', 'materials')
        return ''
     if upgrade and not is_empty(upgrade_materials) then
    end
         local from_id = get_craft_field(upgrade, 'f', 'from_machine')
    local parts = {}
        local from_display = '—'
    local personal = join_indexes(bucket_data.personal, label_resolver, fallback_resolver)
        if from_id and common.trim(from_id) ~= '' then
     local global = join_indexes(bucket_data.global, label_resolver, fallback_resolver)
             local from_name = item.getNameByKey(from_id)
    if personal ~= '' then
             if from_name and from_name ~= '' then
        parts[#parts + 1] = personal_label .. '' .. personal
                 from_display = '[[' .. from_name .. ']]'
    end
     if global ~= '' then
         parts[#parts + 1] = global_label .. '' .. global
    end
    return table.concat(parts, '<br />')
end
 
local function render_item_grid(frame, values)
    local items = {}
    local seen = {}
    if type(values) == 'table' then
        for _, value in ipairs(values) do
             local idx = tonumber(value)
             if idx ~= nil then
            local meta = item_display_meta(idx)
                 if meta then
                    local dedupe_key = normalize_key(meta.name) .. '|' .. (meta.image or "default")
                    if not seen[dedupe_key] then
                        seen[dedupe_key] = true
                        local item_markup = item_template_from_name(frame, meta.name)
                        if item_markup and item_markup ~= '' then
                            items[#items + 1] = item_markup
                        end
                    end
                end
             end
             end
         end
         end
        out[#out + 1] = render_craft_table(
            '升级配方',
            '升级自',
            from_display,
            '材料',
            render_materials(frame, upgrade_materials),
            '时间',
            format_time(get_craft_field(upgrade, 't', 'time')),
            '经验',
            common.toText(get_craft_field(upgrade, 'e', 'exp'))
        )
     end
     end


     if #items == 0 then
     return table.concat(out, '\n')
        return '<div class="gifting-empty">暂无可显示物品</div>'
    end
 
    local root = mw.html.create('div'):addClass('gifting-item-grid')
    for _, item_markup in ipairs(items) do
        root:tag('div')
            :addClass('gifting-item-entry')
            :wikitext(item_markup)
    end
 
    return tostring(root)
end
end


local function render_preference_panel(frame, title, values)
local recipe_cache
    local panel = mw.html.create('div'):addClass('gifting-preference-panel')
local recipe_list
    panel:tag('div'):addClass('gifting-preference-panel-title'):wikitext(title)
local field_map
    panel:tag('div'):addClass('gifting-preference-panel-body'):wikitext(render_item_grid(frame, values))
    return tostring(panel)
end


local function render_npc_level(frame, record, record_index, bucket)
local function load_recipes()
    local bucket_data = get_summary_bucket(record, bucket, record_index)
     if recipe_cache then
     if type(bucket_data) ~= 'table' then
         return
         return '<div class="gifting-level-grid"><div class="gifting-empty">暂无数据</div></div>'
     end
     end
 
     local data = common.loadJsonData('数据:Machine/machine_recipes.json') or {}
     local root = mw.html.create('div')
     recipe_list = data.recipes or {}
        :addClass('gifting-level-grid')
     recipe_cache = data.by_machine or {}
        :addClass('gifting-level-grid--' .. bucket)
     field_map = (data._meta or {}).field_map or {
     root:wikitext(render_preference_panel(frame, '个人喜好', bucket_data.personal or {}))
         result = 'r',
     root:wikitext(render_preference_panel(frame, '通用喜好', bucket_data.global or {}))
         result_count = 'rc',
     return tostring(root)
         materials = 'm',
end
         time = 't',
 
         machine = 'mc'
local function build_tabber_content(frame, record, record_index)
    local tabs = {
        { key = 'loved', label = '最爱' },
        { key = 'liked', label = '喜欢' },
         { key = 'neutral', label = '一般' },
         { key = 'disliked', label = '不喜欢' },
         { key = 'hated', label = '讨厌' },
         { key = 'resonance', label = '共鸣' },
         { key = 'special', label = '特殊' },
     }
     }
    local parts = {}
    for index, tab in ipairs(tabs) do
        parts[#parts + 1] = tab.label .. '=' .. render_npc_level(frame, record, record_index, tab.key)
        if index < #tabs then
            parts[#parts + 1] = '|-|'
        end
    end
    return table.concat(parts, '\n')
end
end


function p.getField(frame)
function p.recipeList(frame)
     local key = common.getArg(frame, 1, '')
     local key = common.getArg(frame, 1, '')
    local field = common.getArg(frame, 2, '')
     local record = find_record(key)
    if field == '' then
        return ''
    end
     local record, character_index = find_character_record(key)
     if not record then
     if not record then
         return ''
         return ''
     end
     end
 
      
     if field == 'id' then
     -- 获取机器ID
        return character_id_from_index(character_index)
     local machine_id = get_record_field(record, 'id')
     end
     if not machine_id then
     if field == 'name' then
        return common.toText(record.n)
    end
    if field == 'name_en' then
        return common.toText(record.en)
    end
    if field == 'character_type' then
        return common.toText(record.t)
    end
    if field == 'personal_rules' then
        return common.toText(record.p)
    end
    return common.toText(record[field])
end
 
function p.preferenceList(frame)
    local key = common.getArg(frame, 1, '')
    local field = common.getArg(frame, 2, '')
     if field == '' then
         return ''
         return ''
     end
     end
     local record = find_character_record(key)
   
     if not record then
     local machine_key = mw.ustring.lower(machine_id)
         return ''
   
    load_recipes()
    local recipe_ids = recipe_cache[machine_key]
     if not recipe_ids then
         return '该机器暂无独有配方。'
     end
     end
     return render_bucket(get_summary_bucket(record, field), '个人喜好', '通用喜好', item_label_from_index, item_id_from_index)
      
end
     -- 检查是否有配方需要显示时间
 
     local has_time = false
function p.renderNpcTabs(frame)
     for _, rid in ipairs(recipe_ids) do
     local key = common.getArg(frame, 1, '')
         local recipe = recipe_list[rid + 1]
     local resolved = common.trim(key)
         if recipe and recipe[field_map.time] and recipe[field_map.time] > 0 then
     if resolved == '' then
             has_time = true
        resolved = common.getCurrentTitleText()
             break
    end
 
    local cache_key = common.buildLuaCacheKey(LUACACHE_NAMESPACE, LUACACHE_VERSION, normalize_key(resolved))
    local content = common.luaCacheGet(cache_key)
    if content == nil then
         local record, record_index = find_character_record(resolved)
         if not record then
             common.luaCacheSet(cache_key, '')
            content = ''
        else
             content = build_tabber_content(frame, record, record_index)
            common.luaCacheSet(cache_key, content)
         end
         end
     end
     end
 
   
     local css_out = (css.quickCall('GiftsByNPC') or '') .. (css.quickCall('Item') or '')
    -- 构建HTML表格
     if content == '' then
     local out = {}
        return css_out .. '<div class="gifting-empty">未找到送礼数据</div>'
     out[#out + 1] = '<table class="wikitable">'
     end
     out[#out + 1] = '<tr><th>产物</th><th>材料</th>'
    return css_out .. frame:extensionTag('tabber', content, { class = 'tabber-no-active-indicator gifting-tabber' })
     if has_time then
end
         out[#out + 1] = '<th>时间</th>'
 
local function render_character_list(frame, values)
     if type(values) ~= 'table' then
         return ''
     end
     end
    out[#out + 1] = '</tr>'
      
      
    local parts = {}
     for _, rid in ipairs(recipe_ids) do
     for _, value in pairs(values) do
         local recipe = recipe_list[rid + 1]
         local idx = tonumber(value)
         if recipe then
         if idx ~= nil then
            out[#out + 1] = '<tr>'
             local name = character_label_from_index(idx)
             local result_count = recipe[field_map.result_count] or 1
             if name ~= '' then
             if result_count > 1 then
                 if is_creature(name) then
                 out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], tostring(result_count), class = 'block'}) .. '</td>'
                    parts[#parts + 1] = '[[' .. name .. ']]'
            else
                else
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], class = 'block'}) .. '</td>'
                    local output = npc_module.renderNPC(frame:newChild{
            end
                        title = 'Module:NPC',
           
                        args = { [1] = name }
            -- 渲染材料
                    })
            local materials = {}
                    if common.trim(output) ~= '' then
            local mats = recipe[field_map.materials] or {}
                        parts[#parts + 1] = output
            for mat_id, count in pairs(mats) do
                     end
                materials[#materials + 1] = item.renderItemWithArgs(frame, {mat_id, tostring(count), class = 'block'})
            end
            out[#out + 1] = '<td>' .. table.concat(materials, '') .. '</td>'
           
            -- 渲染时间
            if has_time then
                local time_str = ''
                if recipe[field_map.time] and recipe[field_map.time] > 0 then
                     time_str = format_time(recipe[field_map.time])
                 end
                 end
                out[#out + 1] = '<td>' .. time_str .. '</td>'
             end
             end
           
            out[#out + 1] = '</tr>'
         end
         end
     end
     end
     return table.concat(parts, '')
   
    out[#out + 1] = '</table>'
   
    local css_out = css.quickCall('Item') or ''
     return css_out .. table.concat(out, '\n')
end
end


function p.preferenceListByItem(frame)
local item_recipe_cache
    local key = common.getArg(frame, 1, '')
 
    local field = common.getArg(frame, 2, '')
local function load_item_recipes()
     if field == '' then
     if item_recipe_cache then
         return ''
         return
     end
     end
    load_recipes()
    local data = common.loadJsonData('数据:Machine/machine_recipes.json') or {}
    item_recipe_cache = data.by_item or {}
end


     local record = find_item_record(key)
function p.itemRecipes(frame)
     if not record then
    local item_key = common.getArg(frame, 1, '')
    if item_key == '' then
        item_key = common.getCurrentTitleText()
    end
   
    -- 获取物品ID
     local item_id = item.getIdByKey(item_key)
     if not item_id or item_id == '' then
         return ''
         return ''
     end
     end
 
   
     local bucket = level_to_bucket[field] or normalize_key(field)
     local normalized_id = mw.ustring.lower(item_id)
     local short = bucket_to_short[bucket]
   
     local compact_bucket = record.s and short and record.s[short] or nil
    load_item_recipes()
     if type(compact_bucket) ~= 'table' then
     local item_data = item_recipe_cache[normalized_id]
      
     if not item_data then
         return ''
         return ''
     end
     end
    local parts = {}
    local personal = render_character_list(frame, compact_bucket.p or {})
    local global = render_character_list(frame, compact_bucket.g or {})
      
      
     if personal ~= '' then
     local product_ids = item_data.p or {}
        parts[#parts + 1] = '个人喜好角色:' .. personal
     local ingredient_ids = item_data.i or {}
    end
     if global ~= '' then
        parts[#parts + 1] = '通用喜好角色:' .. global
    end
      
      
     return table.concat(parts, '<br />')
     -- 计算数组长度(处理有metatable的情况)
end
    local function count_array(arr)
 
        local count = 0
local BLOCKED_CHARACTERS = { '程洲', '弦一', '貂', '熊猫', '鹦鹉' }
        for _ in pairs(arr) do
local PET_ANIMALS = { '水豚', '猫', '狗', '狐狸', '小熊猫', '松鼠' }
            count = count + 1
 
local function is_blocked_character(name)
    for _, blocked in ipairs(BLOCKED_CHARACTERS) do
        if name == blocked then
            return true
         end
         end
        return count
     end
     end
     return false
      
end
    local product_count = count_array(product_ids)
 
     local ingredient_count = count_array(ingredient_ids)
local function is_pet_animal(name)
      
     for _, pet in ipairs(PET_ANIMALS) do
     if product_count == 0 and ingredient_count == 0 then
        if name == pet then
            return true
        end
     end
    return false
end
 
local function render_character_list_filtered(frame, values)
     if type(values) ~= 'table' then
         return ''
         return ''
     end
     end
      
      
     local npc_parts = {}
     local out = {}
    local pet_parts = {}
    local seen = {}
      
      
     for _, value in pairs(values) do
     -- 作为产物
         local idx = tonumber(value)
    if product_count > 0 then
         if idx ~= nil then
         out[#out + 1] = '<h3>制作配方</h3>'
            local name = character_label_from_index(idx)
         out[#out + 1] = '<table class="wikitable">'
             if name ~= '' and not is_blocked_character(name) and not seen[name] then
        out[#out + 1] = '<tr><th>产物</th><th>机器</th><th>材料</th><th>时间</th></tr>'
                 seen[name] = true
       
                 if is_pet_animal(name) then
        for _, rid in ipairs(product_ids) do
                    pet_parts[#pet_parts + 1] = '<span class="npctemplate">' .. name .. '</span>'
             local recipe = recipe_list[rid + 1]
                 else
            if recipe then
                    local output = npc_module.renderNPC(frame:newChild{
                 out[#out + 1] = '<tr>'
                        title = 'Module:NPC',
                 out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], class = 'block'}) .. '</td>'
                        args = { [1] = name }
                 out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.machine], class = 'block'}) .. '</td>'
                    })
               
                     if common.trim(output) ~= '' then
                local materials = {}
                        npc_parts[#npc_parts + 1] = output
                for mat_id, count in pairs(recipe[field_map.materials] or {}) do
                    end
                     materials[#materials + 1] = item.renderItemWithArgs(frame, {mat_id, tostring(count), class = 'block'})
                 end
                 end
                out[#out + 1] = '<td>' .. table.concat(materials, '') .. '</td>'
               
                local time_str = ''
                if recipe[field_map.time] and recipe[field_map.time] > 0 then
                    time_str = format_time(recipe[field_map.time])
                end
                out[#out + 1] = '<td>' .. time_str .. '</td>'
                out[#out + 1] = '</tr>'
             end
             end
         end
         end
       
        out[#out + 1] = '</table>'
     end
     end
      
      
     local all_parts = {}
     -- 作为材料
    for _, part in ipairs(npc_parts) do
    if ingredient_count > 0 then
        all_parts[#all_parts + 1] = part
        if #out > 0 then
    end
            out[#out + 1] = ''
    for _, part in ipairs(pet_parts) do
        end
         all_parts[#all_parts + 1] = part
        out[#out + 1] = '<h3>用于制作</h3>'
        out[#out + 1] = '<table class="wikitable">'
        out[#out + 1] = '<tr><th>产物</th><th>材料</th><th>机器</th></tr>'
       
        for _, rid in ipairs(ingredient_ids) do
            local recipe = recipe_list[rid + 1]
            if recipe then
                out[#out + 1] = '<tr>'
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], class = 'block'}) .. '</td>'
               
                -- 渲染材料
                local materials = {}
                for mat_id, count in pairs(recipe[field_map.materials] or {}) do
                    materials[#materials + 1] = item.renderItemWithArgs(frame, {mat_id, tostring(count), class = 'block'})
                end
                out[#out + 1] = '<td>' .. table.concat(materials, '') .. '</td>'
               
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.machine], class = 'block'}) .. '</td>'
                out[#out + 1] = '</tr>'
            end
        end
       
         out[#out + 1] = '</table>'
     end
     end
      
      
     return table.concat(all_parts, ' · ')
    local css_out = css.quickCall('Item') or ''
     return css_out .. table.concat(out, '\n')
end
end


function p.renderGiftsByItem(frame)
function p.machineAsIngredient(frame)
     local key = common.getArg(frame, 1, '')
     local key = common.getArg(frame, 1, '')
     if key == '' then
    local record = find_record(key)
         key = common.getCurrentTitleText()
     if not record then
         return ''
     end
     end
      
      
     local record = find_item_record(key)
     local machine_id = get_record_field(record, 'id')
     if not record or type(record.s) ~= 'table' then
     if not machine_id then
         return "'''" .. key .. "'''不可用于送礼。"
         return ''
     end
     end
      
      
     local buckets = {
     local machine_key = mw.ustring.lower(machine_id)
        { key = 'loved', short = 'v', label = '最爱', icon = 'Gifting love.png' },
    load_item_recipes()
        { key = 'liked', short = 'k', label = '喜欢', icon = 'Gifting like.png' },
    local item_data = item_recipe_cache[machine_key]
        { key = 'neutral', short = 'n', label = '一般', icon = '' },
        { key = 'disliked', short = 'd', label = '不喜欢', icon = '' },
        { key = 'hated', short = 'h', label = '讨厌', icon = '' },
        { key = 'resonance', short = 'r', label = '共鸣', icon = '' },
        { key = 'special', short = 's', label = '特殊', icon = '' },
    }
      
      
     local rows = {}
     if not item_data or not item_data.i or #item_data.i == 0 then
    for _, bucket in ipairs(buckets) do
        return ''
        local compact_bucket = record.s[bucket.short]
        if type(compact_bucket) == 'table' then
            local personal = render_character_list_filtered(frame, compact_bucket.p or {})
            local global = render_character_list_filtered(frame, compact_bucket.g or {})
           
            local all_chars = {}
            if personal ~= '' then
                all_chars[#all_chars + 1] = personal
            end
            if global ~= '' then
                all_chars[#all_chars + 1] = global
            end
           
            if #all_chars > 0 then
                local label_html = bucket.label
                if bucket.icon ~= '' then
                    label_html = '[[File:' .. bucket.icon .. '|16px|link=]] ' .. bucket.label
                end
                rows[#rows + 1] = { label = label_html, content = table.concat(all_chars, ' • ') }
            end
        end
     end
     end
      
      
     if #rows == 0 then
     local out = {}
        return "'''" .. key .. "'''不可用于送礼。"
    out[#out + 1] = '=== 用于制作 ==='
    end
    out[#out + 1] = '<table class="wikitable">'
    out[#out + 1] = '<tr><th>产物</th><th>所需数量</th></tr>'
      
      
     local html = mw.html.create('table'):addClass('wikitable')
     for _, rid in ipairs(item_data.i) do
    html:tag('tr')
        local recipe = recipe_list[rid + 1]
        :tag('th'):css('white-space', 'nowrap'):wikitext('类型'):done()
        if recipe then
        :tag('th'):wikitext('人物'):done()
            local count = (recipe[field_map.materials] or {})[machine_id] or 0
   
            if count > 0 then
    for _, row in ipairs(rows) do
                out[#out + 1] = '<tr>'
        html:tag('tr')
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result]}) .. '</td>'
             :tag('th'):css('white-space', 'nowrap'):wikitext(row.label):done()
                out[#out + 1] = '<td>' .. tostring(count) .. '</td>'
            :tag('td'):wikitext(row.content):done()
                out[#out + 1] = '</tr>'
             end
        end
     end
     end
      
      
     return tostring(html)
    out[#out + 1] = '</table>'
    local css_out = css.quickCall('Item') or ''
     return css_out .. table.concat(out, '\n')
end
end


return p
return p

2026年3月16日 (一) 09:56的版本

概述

Machine 提供机器域的字段读取、材料列表、配方信息与反查展示,供 {{Infobox machine}}{{MachineRecipes}}{{RecipeUsage}} 调用。

用法

{{#invoke:Machine|getField|工作台|type_display}}
{{#invoke:Machine|craftInfo|工作台}}
{{#invoke:Machine|recipeList|工作台}}
{{#invoke:Machine|itemRecipes|木板}}

函数

  • getField:读取机器字段。
  • ingredientList:渲染建造材料。
  • upgradeIngredientList:渲染升级材料。
  • craftInfo:渲染机器信息块。
  • recipeList:渲染机器产出配方列表。
  • itemRecipes:渲染物品的机器配方反查。
  • machineAsIngredient:渲染机器作为材料的反查结果。

数据来源


local common = require('Module:Common')
local item_common = require('Module:ItemCommon')
local item = require('Module:Item')
local css = require('Module:CSS')

local p = {}

local FIELD_MAP = {
    id = 'i',
    name = 'n',
    name_en = 'en',
    type = 't',
    type_display = 'td',
    item_level = 'lv',
    rarity = 'r',
    sell_price = 'sp',
    base_value = 'bv',
    max_stack = 'ms',
    max_quality = 'mq',
    tags = 'g',
    origin = 'o',
    locked_origin = 'lo',
    use_description = 'ud',
    unlock_title = 'ut',
    construct_template = 'ct',
    construction_template_count = 'ctc',
    energy_capacity = 'ec',
    production_formula = 'pf',
    production_formula_origin = 'pfo',
    craft = 'c',
    upgrade = 'u',
    prev_machine = 'pm',
    next_machine = 'nm',
}

local data_cache
local mapping_cache

local function is_empty(value)
    if value == nil then
        return true
    end
    for _ in pairs(value) do
        return false
    end
    return true
end

local function load_data()
    if data_cache then
        return
    end

    local raw_data
    raw_data, mapping_cache = item_common.loadDomainData(
        '数据:Machine/machine_index.json',
        '数据:Machine/machine_mapping.json'
    )
    if type(raw_data) == 'table' and type(raw_data.records) == 'table' then
        data_cache = raw_data.records
    else
        data_cache = raw_data or {}
    end
end

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

local function get_short_field(field)
    return FIELD_MAP[field] or field
end

local function get_record_field(record, field)
    if type(record) ~= 'table' then
        return nil
    end
    local short_key = get_short_field(field)
    if record[short_key] ~= nil then
        return record[short_key]
    end
    return record[field]
end

local function get_craft_field(craft, short_key, long_key)
    if type(craft) ~= 'table' then
        return nil
    end
    if craft[short_key] ~= nil then
        return craft[short_key]
    end
    return craft[long_key]
end

local function format_time(seconds)
    seconds = tonumber(seconds) or 0
    if seconds <= 0 then
        return ''
    end
    if seconds < 60 then
        return tostring(math.floor(seconds)) .. '秒'
    end
    local minutes = math.floor(seconds / 60)
    local remain = math.floor(seconds % 60)
    if remain == 0 then
        return tostring(minutes) .. '分'
    end
    return tostring(minutes) .. '分' .. tostring(remain) .. '秒'
end

local function render_materials(frame, materials)
    if is_empty(materials) then
        return ''
    end

    local parts = {}
    for item_id, count in pairs(materials) do
        parts[#parts + 1] = frame:expandTemplate{
            title = 'Item',
            args = { item_id, tostring(count), class = 'block' },
        }
    end
    return table.concat(parts, '')
end

local function render_craft_table(title_text, station_label, station_value, materials_label, materials_value, time_label, time_value, exp_label, exp_value)
    local out = {}
    out[#out + 1] = '<table class="wikitable machine-craft-table">'
    out[#out + 1] = '<tr><th colspan="2">' .. title_text .. '</th></tr>'
    out[#out + 1] = '<tr><th>' .. station_label .. '</th><td>' .. station_value .. '</td></tr>'
    out[#out + 1] = '<tr><th>' .. materials_label .. '</th><td>' .. materials_value .. '</td></tr>'
    if time_value ~= '' then
        out[#out + 1] = '<tr><th>' .. time_label .. '</th><td>' .. time_value .. '</td></tr>'
    end
    if exp_value ~= '' then
        out[#out + 1] = '<tr><th>' .. exp_label .. '</th><td>' .. exp_value .. '</td></tr>'
    end
    out[#out + 1] = '</table>'
    return table.concat(out, '\n')
end

function p.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

    local craft = get_record_field(record, 'craft')
    if field == 'craft_machine' and craft then
        local machine_id = get_craft_field(craft, 'm', 'machine')
        if machine_id and machine_id ~= '' then
            return item.renderItemWithArgs(frame, {machine_id})
        end
        return ''
    end
    if field == 'craft_time' and craft then
        return format_time(get_craft_field(craft, 't', 'time'))
    end
    if field == 'craft_exp' and craft then
        return common.toText(get_craft_field(craft, 'e', 'exp'))
    end
    local upgrade = get_record_field(record, 'upgrade')
    if field == 'upgrade_from' and upgrade then
        local from_id = get_craft_field(upgrade, 'f', 'from_machine')
        if from_id and from_id ~= '' then
            local from_name = item.getNameByKey(from_id)
            if from_name and from_name ~= '' then
                return '[[' .. from_name .. ']]'
            end
        end
        return ''
    end
    if field == 'upgrade_time' and upgrade then
        return format_time(get_craft_field(upgrade, 't', 'time'))
    end
    if field == 'upgrade_exp' and upgrade then
        return common.toText(get_craft_field(upgrade, 'e', 'exp'))
    end
    if field == 'construction_template_count' then
        local value = tonumber(get_record_field(record, field)) or 0
        if value <= 0 then
            return ''
        end
        return tostring(value)
    end
    if field == 'energy_capacity' then
        local value = tonumber(get_record_field(record, field)) or 0
        if value <= 0 then
            return ''
        end
        return tostring(value)
    end
    if field == 'prev_machine' then
        local prev_id = get_record_field(record, 'prev_machine')
        if prev_id and prev_id ~= '' then
            return item.renderItemWithArgs(frame, {prev_id})
        end
        return ''
    end
    if field == 'next_machine' then
        local next_id = get_record_field(record, 'next_machine')
        if next_id and next_id ~= '' then
            return item.renderItemWithArgs(frame, {next_id})
        end
        return ''
    end

    return common.toText(get_record_field(record, field))
end

function p.ingredientList(frame)
    local key = common.getArg(frame, 1, '')
    local record = find_record(key)
    if not record then
        return ''
    end
    local craft = get_record_field(record, 'craft')
    if not craft then
        return ''
    end
    return render_materials(frame, get_craft_field(craft, 'mat', 'materials'))
end

function p.upgradeIngredientList(frame)
    local key = common.getArg(frame, 1, '')
    local record = find_record(key)
    if not record then
        return ''
    end
    local upgrade = get_record_field(record, 'upgrade')
    if not upgrade then
        return ''
    end
    return render_materials(frame, get_craft_field(upgrade, 'mat', 'materials'))
end

function p.craftInfo(frame)
    local key = common.getArg(frame, 1, '')
    local record = find_record(key)
    if not record then
        return ''
    end

    local out = {}
    local craft = get_record_field(record, 'craft')
    local craft_materials = get_craft_field(craft, 'mat', 'materials')
    if craft and not is_empty(craft_materials) then
        local machine_id = get_craft_field(craft, 'm', 'machine')
        local machine_display = '—'
        if machine_id and common.trim(machine_id) ~= '' then
            local machine_name = item.getNameByKey(machine_id)
            if machine_name and machine_name ~= '' then
                machine_display = '[[' .. machine_name .. ']]'
            end
        end
        out[#out + 1] = render_craft_table(
            '制造配方',
            '制造站',
            machine_display,
            '材料',
            render_materials(frame, craft_materials),
            '时间',
            format_time(get_craft_field(craft, 't', 'time')),
            '经验',
            common.toText(get_craft_field(craft, 'e', 'exp'))
        )
    end

    local upgrade = get_record_field(record, 'upgrade')
    local upgrade_materials = get_craft_field(upgrade, 'mat', 'materials')
    if upgrade and not is_empty(upgrade_materials) then
        local from_id = get_craft_field(upgrade, 'f', 'from_machine')
        local from_display = '—'
        if from_id and common.trim(from_id) ~= '' then
            local from_name = item.getNameByKey(from_id)
            if from_name and from_name ~= '' then
                from_display = '[[' .. from_name .. ']]'
            end
        end
        out[#out + 1] = render_craft_table(
            '升级配方',
            '升级自',
            from_display,
            '材料',
            render_materials(frame, upgrade_materials),
            '时间',
            format_time(get_craft_field(upgrade, 't', 'time')),
            '经验',
            common.toText(get_craft_field(upgrade, 'e', 'exp'))
        )
    end

    return table.concat(out, '\n')
end

local recipe_cache
local recipe_list
local field_map

local function load_recipes()
    if recipe_cache then
        return
    end
    local data = common.loadJsonData('数据:Machine/machine_recipes.json') or {}
    recipe_list = data.recipes or {}
    recipe_cache = data.by_machine or {}
    field_map = (data._meta or {}).field_map or {
        result = 'r',
        result_count = 'rc',
        materials = 'm',
        time = 't',
        machine = 'mc'
    }
end

function p.recipeList(frame)
    local key = common.getArg(frame, 1, '')
    local record = find_record(key)
    if not record then
        return ''
    end
    
    -- 获取机器ID
    local machine_id = get_record_field(record, 'id')
    if not machine_id then
        return ''
    end
    
    local machine_key = mw.ustring.lower(machine_id)
    
    load_recipes()
    local recipe_ids = recipe_cache[machine_key]
    if not recipe_ids then
        return '该机器暂无独有配方。'
    end
    
    -- 检查是否有配方需要显示时间
    local has_time = false
    for _, rid in ipairs(recipe_ids) do
        local recipe = recipe_list[rid + 1]
        if recipe and recipe[field_map.time] and recipe[field_map.time] > 0 then
            has_time = true
            break
        end
    end
    
    -- 构建HTML表格
    local out = {}
    out[#out + 1] = '<table class="wikitable">'
    out[#out + 1] = '<tr><th>产物</th><th>材料</th>'
    if has_time then
        out[#out + 1] = '<th>时间</th>'
    end
    out[#out + 1] = '</tr>'
    
    for _, rid in ipairs(recipe_ids) do
        local recipe = recipe_list[rid + 1]
        if recipe then
            out[#out + 1] = '<tr>'
            local result_count = recipe[field_map.result_count] or 1
            if result_count > 1 then
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], tostring(result_count), class = 'block'}) .. '</td>'
            else
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], class = 'block'}) .. '</td>'
            end
            
            -- 渲染材料
            local materials = {}
            local mats = recipe[field_map.materials] or {}
            for mat_id, count in pairs(mats) do
                materials[#materials + 1] = item.renderItemWithArgs(frame, {mat_id, tostring(count), class = 'block'})
            end
            out[#out + 1] = '<td>' .. table.concat(materials, '') .. '</td>'
            
            -- 渲染时间
            if has_time then
                local time_str = ''
                if recipe[field_map.time] and recipe[field_map.time] > 0 then
                    time_str = format_time(recipe[field_map.time])
                end
                out[#out + 1] = '<td>' .. time_str .. '</td>'
            end
            
            out[#out + 1] = '</tr>'
        end
    end
    
    out[#out + 1] = '</table>'
    
    local css_out = css.quickCall('Item') or ''
    return css_out .. table.concat(out, '\n')
end

local item_recipe_cache

local function load_item_recipes()
    if item_recipe_cache then
        return
    end
    load_recipes()
    local data = common.loadJsonData('数据:Machine/machine_recipes.json') or {}
    item_recipe_cache = data.by_item or {}
end

function p.itemRecipes(frame)
    local item_key = common.getArg(frame, 1, '')
    if item_key == '' then
        item_key = common.getCurrentTitleText()
    end
    
    -- 获取物品ID
    local item_id = item.getIdByKey(item_key)
    if not item_id or item_id == '' then
        return ''
    end
    
    local normalized_id = mw.ustring.lower(item_id)
    
    load_item_recipes()
    local item_data = item_recipe_cache[normalized_id]
    
    if not item_data then
        return ''
    end
    
    local product_ids = item_data.p or {}
    local ingredient_ids = item_data.i or {}
    
    -- 计算数组长度(处理有metatable的情况)
    local function count_array(arr)
        local count = 0
        for _ in pairs(arr) do
            count = count + 1
        end
        return count
    end
    
    local product_count = count_array(product_ids)
    local ingredient_count = count_array(ingredient_ids)
    
    if product_count == 0 and ingredient_count == 0 then
        return ''
    end
    
    local out = {}
    
    -- 作为产物
    if product_count > 0 then
        out[#out + 1] = '<h3>制作配方</h3>'
        out[#out + 1] = '<table class="wikitable">'
        out[#out + 1] = '<tr><th>产物</th><th>机器</th><th>材料</th><th>时间</th></tr>'
        
        for _, rid in ipairs(product_ids) do
            local recipe = recipe_list[rid + 1]
            if recipe then
                out[#out + 1] = '<tr>'
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], class = 'block'}) .. '</td>'
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.machine], class = 'block'}) .. '</td>'
                
                local materials = {}
                for mat_id, count in pairs(recipe[field_map.materials] or {}) do
                    materials[#materials + 1] = item.renderItemWithArgs(frame, {mat_id, tostring(count), class = 'block'})
                end
                out[#out + 1] = '<td>' .. table.concat(materials, '') .. '</td>'
                
                local time_str = ''
                if recipe[field_map.time] and recipe[field_map.time] > 0 then
                    time_str = format_time(recipe[field_map.time])
                end
                out[#out + 1] = '<td>' .. time_str .. '</td>'
                out[#out + 1] = '</tr>'
            end
        end
        
        out[#out + 1] = '</table>'
    end
    
    -- 作为材料
    if ingredient_count > 0 then
        if #out > 0 then
            out[#out + 1] = ''
        end
        out[#out + 1] = '<h3>用于制作</h3>'
        out[#out + 1] = '<table class="wikitable">'
        out[#out + 1] = '<tr><th>产物</th><th>材料</th><th>机器</th></tr>'
        
        for _, rid in ipairs(ingredient_ids) do
            local recipe = recipe_list[rid + 1]
            if recipe then
                out[#out + 1] = '<tr>'
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result], class = 'block'}) .. '</td>'
                
                -- 渲染材料
                local materials = {}
                for mat_id, count in pairs(recipe[field_map.materials] or {}) do
                    materials[#materials + 1] = item.renderItemWithArgs(frame, {mat_id, tostring(count), class = 'block'})
                end
                out[#out + 1] = '<td>' .. table.concat(materials, '') .. '</td>'
                
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.machine], class = 'block'}) .. '</td>'
                out[#out + 1] = '</tr>'
            end
        end
        
        out[#out + 1] = '</table>'
    end
    
    local css_out = css.quickCall('Item') or ''
    return css_out .. table.concat(out, '\n')
end

function p.machineAsIngredient(frame)
    local key = common.getArg(frame, 1, '')
    local record = find_record(key)
    if not record then
        return ''
    end
    
    local machine_id = get_record_field(record, 'id')
    if not machine_id then
        return ''
    end
    
    local machine_key = mw.ustring.lower(machine_id)
    load_item_recipes()
    local item_data = item_recipe_cache[machine_key]
    
    if not item_data or not item_data.i or #item_data.i == 0 then
        return ''
    end
    
    local out = {}
    out[#out + 1] = '=== 用于制作 ==='
    out[#out + 1] = '<table class="wikitable">'
    out[#out + 1] = '<tr><th>产物</th><th>所需数量</th></tr>'
    
    for _, rid in ipairs(item_data.i) do
        local recipe = recipe_list[rid + 1]
        if recipe then
            local count = (recipe[field_map.materials] or {})[machine_id] or 0
            if count > 0 then
                out[#out + 1] = '<tr>'
                out[#out + 1] = '<td>' .. item.renderItemWithArgs(frame, {recipe[field_map.result]}) .. '</td>'
                out[#out + 1] = '<td>' .. tostring(count) .. '</td>'
                out[#out + 1] = '</tr>'
            end
        end
    end
    
    out[#out + 1] = '</table>'
    local css_out = css.quickCall('Item') or ''
    return css_out .. table.concat(out, '\n')
end

return p