模块:Machine:修订间差异
来自星砂岛百科
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第97行: | 第97行: | ||
end | end | ||
if seconds < 60 then | if seconds < 60 then | ||
return tostring(math.floor(seconds)) .. ' | return tostring(math.floor(seconds)) .. '秒' | ||
end | end | ||
local minutes = math.floor(seconds / 60) | local minutes = math.floor(seconds / 60) | ||
local remain = math.floor(seconds % 60) | local remain = math.floor(seconds % 60) | ||
if remain == 0 then | if remain == 0 then | ||
return tostring(minutes) .. ' | return tostring(minutes) .. '分' | ||
end | end | ||
return tostring(minutes) .. ' | return tostring(minutes) .. '分' .. tostring(remain) .. '秒' | ||
end | end | ||
2026年3月13日 (五) 20:46的版本
概述
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 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',
}
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] = '{| class="wikitable machine-craft-table"'
out[#out + 1] = '! colspan="2" | ' .. title_text
out[#out + 1] = '|-'
out[#out + 1] = '! ' .. station_label
out[#out + 1] = '| ' .. station_value
out[#out + 1] = '|-'
out[#out + 1] = '! ' .. materials_label
out[#out + 1] = '| ' .. materials_value
if time_value ~= '' then
out[#out + 1] = '|-'
out[#out + 1] = '! ' .. time_label
out[#out + 1] = '| ' .. time_value
end
if exp_value ~= '' then
out[#out + 1] = '|-'
out[#out + 1] = '! ' .. exp_label
out[#out + 1] = '| ' .. exp_value
end
out[#out + 1] = '|}'
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
local machine_name = item.getName(frame:newChild{title='Module:Item', args={machine_id}})
if machine_name and machine_name ~= '' then
return '[[' .. machine_name .. ']]'
end
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.getName(frame:newChild{title='Module:Item', args={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
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.getName(frame:newChild{title='Module:Item', args={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.getName(frame:newChild{title='Module:Item', args={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
return p