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

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

模块:Character

来自星砂岛百科

概述

Character 提供角色域字段读取与展示辅助字段,供 {{Infobox character}} 等角色页模板调用。

用法

{{#invoke:Character|getField|晨星|occupation}}
{{#invoke:Character|getBirthday|晨星}}
{{#invoke:Character|getGender|晨星}}

示例

{{#invoke:Character|getField|晨星|occupation}}

函数

  • getField:读取角色字段,也支持 birthdaygender_displayimage 等展示字段。
  • getId:返回角色 ID。
  • getImage:返回角色图片文件名。
  • getBirthday:返回生日展示结果。
  • getGender:返回本地化后的性别文本。

数据来源


local common = require('Module:Common')
local character_common = require('Module:CharacterCommon')

local p = {}

local data_cache
local mapping

local gender_map = {
    male = '男',
    female = '女',
}

local season_map = {
    ['1'] = 'Spring',
    ['2'] = 'Summer',
    ['3'] = 'Autumn',
    ['4'] = 'Winter',
}

local function pick_first(...)
    local values = { ... }
    for _, value in ipairs(values) do
        value = common.trim(value)
        if value ~= '' then
            return value
        end
    end
    return ''
end

local function format_birthday(record)
    if type(record) ~= 'table' then
        return ''
    end

    local month = common.trim(record.birthday_month)
    local day = common.trim(record.birthday_day)
    local season = season_map[month]
    if season ~= nil and day ~= '' then
        return mw.getCurrentFrame():expandTemplate{
            title = 'Season',
            args = { season, day },
        }
    end
    return ''
end

local function localize_gender(value)
    local normalized = character_common.normalizeKey(value)
    return gender_map[normalized] or common.trim(value)
end

local function display_image(record)
    if type(record) ~= 'table' then
        return ''
    end

    local image_key = common.trim(record.image_key)
    if image_key ~= '' then
        return 'Head ' .. image_key .. '.png'
    end

    return pick_first(record.default_image, record.upgrade_icon, record.map_image)
end

local function load_data()
    if data_cache then
        return
    end

    data_cache, mapping = character_common.loadDomainData('数据:Character/character_index.json')
end

local function find_record(key)
    load_data()
    return character_common.findRecord(data_cache, mapping, key)
end

local function get_display_field(record, field)
    if type(record) ~= 'table' then
        return ''
    end

    if field == 'birthday_display' or field == 'birthday' then
        return format_birthday(record)
    end
    if field == 'gender_display' or field == 'gender_zh' then
        return localize_gender(record.gender)
    end
    if field == 'image' or field == 'image_fallback' then
        return display_image(record)
    end
    if field == 'display_name' then
        return pick_first(record.name, record.title, record.id)
    end
    if field == 'display_title' then
        return pick_first(record.title, record.name)
    end

    return record[field]
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

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

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

    return common.toText(record.id)
end

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

    return common.toText(display_image(record))
end

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

    return format_birthday(record)
end

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

    return localize_gender(record.gender)
end

return p