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

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

模块:NPC:修订间差异

来自星砂岛百科
Sizau留言 | 贡献
无编辑摘要
Sizau-bot留言 | 贡献
同步 Character 域阅读体验与 Citizen 样式优化
 
第1行: 第1行:
local common = require('Module:Common')
local common = require('Module:Common')
local css = require('Module:CSS')
local css = require('Module:CSS')
local character_common = require('Module:CharacterCommon')


local p = {}
local p = {}
第11行: 第12行:
         return
         return
     end
     end
     local raw = common.loadJsonData('数据:Character/character_index.json') or {}
     character_data, mapping = character_common.loadDomainData('数据:Character/character_index.json')
    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
end


local function find_record(key)
local function find_record(key)
     load_data()
     load_data()
     local resolved = common.trim(key)
     return character_common.findRecord(character_data, mapping, 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
end


第91行: 第47行:
     end
     end
      
      
     local name = record.name or ''
     local name = common.trim(record.name or record.title or record.id or '')
    if name == '' then
        return ''
    end
     local class_name = 'npctemplate'
     local class_name = 'npctemplate'
     if css_class == 'block' then
     if css_class == 'block' then
第102行: 第61行:
         image = 'Head ' .. image_key .. '.png'
         image = 'Head ' .. image_key .. '.png'
     else
     else
         local fallback = record.default_image or record.upgrade_icon or ''
         local fallback = record.default_image or record.upgrade_icon or record.map_image or ''
         if fallback ~= '' then
         if fallback ~= '' then
             image = fallback
             image = fallback

2026年3月28日 (六) 15:57的最新版本

概述

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 character_common = require('Module:CharacterCommon')

local p = {}

local character_data
local mapping

local function load_data()
    if character_data then
        return
    end
    character_data, mapping = character_common.loadDomainData('数据:Character/character_index.json')
end

local function find_record(key)
    load_data()
    return character_common.findRecord(character_data, mapping, key)
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 = common.trim(record.name or record.title or record.id or '')
    if name == '' then
        return ''
    end
    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 record.map_image 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