模块:NPC
来自星砂岛百科
更多操作
概述
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 {}
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 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
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)
if css_class == 'block' then
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]')
html:tag('span'):addClass('npc-name'):wikitext(link_text)
else
html:wikitext('[[File:' .. image .. '|' .. size .. 'px|link=' .. name .. ']]')
html:wikitext(' ')
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