模块:NPC:修订间差异
来自星砂岛百科
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第12行: | 第12行: | ||
end | end | ||
local raw = common.loadJsonData('数据:Character/character_index.json') or {} | local raw = common.loadJsonData('数据:Character/character_index.json') or {} | ||
local map = common.loadJsonData('数据:Character/character_mapping.json') or {} | local map = common.loadJsonData('数据:Character/character_mapping.json') or { | ||
name_to_id = {}, | |||
id_to_name = {}, | |||
aliases = {}, | |||
overrides = { | |||
name_to_id = {}, | |||
aliases = {}, | |||
}, | |||
} | |||
character_data = raw | character_data = raw | ||
mapping = map | mapping = map | ||
| 第31行: | 第39行: | ||
if character_data[normalized] then | if character_data[normalized] then | ||
return character_data[normalized] | return character_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 character_data[normalize_key(override_id)] then | |||
return character_data[normalize_key(override_id)] | |||
end | |||
local override_alias = mapping.overrides and mapping.overrides.aliases and mapping.overrides.aliases[normalized] | |||
if override_alias and character_data[normalize_key(override_alias)] then | |||
return character_data[normalize_key(override_alias)] | |||
end | end | ||
| 第36行: | 第54行: | ||
if mapped_id and character_data[normalize_key(mapped_id)] then | if mapped_id and character_data[normalize_key(mapped_id)] then | ||
return character_data[normalize_key(mapped_id)] | return character_data[normalize_key(mapped_id)] | ||
end | |||
local alias_id = mapping.aliases and mapping.aliases[normalized] | |||
if alias_id and character_data[normalize_key(alias_id)] then | |||
return character_data[normalize_key(alias_id)] | |||
end | end | ||
| 第87行: | 第110行: | ||
local link_text = '[[' .. name .. '|' .. name .. ']]' | local link_text = '[[' .. name .. '|' .. name .. ']]' | ||
local html = mw.html.create('span'):addClass(class_name) | local html = mw.html.create('span'):addClass(class_name) | ||
local has_image = image ~= '' and common.filePageExists(image) | |||
if css_class == 'block' then | if css_class == 'block' then | ||
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]') | if has_image then | ||
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]') | |||
end | |||
html:tag('span'):addClass('npc-name'):wikitext(link_text) | html:tag('span'):addClass('npc-name'):wikitext(link_text) | ||
else | else | ||
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]') | if has_image then | ||
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]') | |||
html:wikitext(' ') | |||
end | |||
html:wikitext(link_text) | html:wikitext(link_text) | ||
end | end | ||
2026年3月16日 (一) 23:20的版本
概述
NPC 用于输出角色头像卡片与名称展示,供 {{NPC}}、赠礼页和人物列表类模板调用。
用法
{{#invoke:NPC|renderNPC|晨星}}
{{#invoke:NPC|renderNPC|晨星|size=32|class=block}}
示例
{{#invoke:NPC|getId|晨星}}
函数
getName:返回角色名称。getId:返回角色 ID。renderNPCByKey:按查找键输出 NPC 卡片,适合 Lua 内部调用。renderNPC:按模板参数输出 NPC 卡片。
数据来源
local common = require('Module:Common')
local css = require('Module:CSS')
local p = {}
local character_data
local mapping
local function load_data()
if character_data then
return
end
local raw = common.loadJsonData('数据:Character/character_index.json') or {}
local map = common.loadJsonData('数据:Character/character_mapping.json') or {
name_to_id = {},
id_to_name = {},
aliases = {},
overrides = {
name_to_id = {},
aliases = {},
},
}
character_data = raw
mapping = map
end
local function normalize_key(value)
return common.normalizeKey(value)
end
local function find_record(key)
load_data()
local resolved = common.trim(key)
if resolved == '' then
resolved = common.getCurrentTitleText()
end
local normalized = normalize_key(resolved)
if character_data[normalized] then
return character_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 character_data[normalize_key(override_id)] then
return character_data[normalize_key(override_id)]
end
local override_alias = mapping.overrides and mapping.overrides.aliases and mapping.overrides.aliases[normalized]
if override_alias and character_data[normalize_key(override_alias)] then
return character_data[normalize_key(override_alias)]
end
local mapped_id = mapping.name_to_id and mapping.name_to_id[normalized]
if mapped_id and character_data[normalize_key(mapped_id)] then
return character_data[normalize_key(mapped_id)]
end
local alias_id = mapping.aliases and mapping.aliases[normalized]
if alias_id and character_data[normalize_key(alias_id)] then
return character_data[normalize_key(alias_id)]
end
return nil
end
function p.getName(frame)
local key = common.getArg(frame, 1, '')
local record = find_record(key)
if record then
return record.name or ''
end
return ''
end
function p.getId(frame)
local key = common.getArg(frame, 1, '')
local record = find_record(key)
if record then
return record.id or ''
end
return ''
end
function p.renderNPCByKey(key, size, css_class)
size = size or '24'
css_class = common.trim(css_class or '')
local record = find_record(key)
if not record then
return ''
end
local name = record.name or ''
local class_name = 'npctemplate'
if css_class == 'block' then
class_name = class_name .. ' npctemplateblock'
end
local image = ''
local image_key = common.trim(record.image_key or '')
if image_key ~= '' then
image = 'Head ' .. image_key .. '.png'
else
local fallback = record.default_image or record.upgrade_icon or ''
if fallback ~= '' then
image = fallback
end
end
local link_text = '[[' .. name .. '|' .. name .. ']]'
local html = mw.html.create('span'):addClass(class_name)
local has_image = image ~= '' and common.filePageExists(image)
if css_class == 'block' then
if has_image then
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]')
end
html:tag('span'):addClass('npc-name'):wikitext(link_text)
else
if has_image then
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]')
html:wikitext(' ')
end
html:wikitext(link_text)
end
local css_out = css.quickCall('NPC') or ''
return css_out .. tostring(html)
end
function p.renderNPC(frame)
local key = common.getArg(frame, 1, '')
local size = common.getArg(frame, 'size', '24')
local css_class = common.trim(common.getArg(frame, 'class', ''))
return p.renderNPCByKey(key, size, css_class)
end
return p