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

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

模块:Building

来自星砂岛百科

模板:Documentation subpage Building 用于读取地点级建筑数据,主要服务于 {{Infobox building}}

示例

  • {{#invoke:Building|getField|零点杂货店|name}}
  • {{#invoke:Building|getField|AreaPlace.CoralTown_Grocery|services_display}}
  • {{#invoke:Building|getField|AreaPlace.CoralTown_BeachShop|entity_ids}}

导出函数

  • getField:读取字段或计算字段。

字段

  • name
  • name_display
  • description
  • map_description
  • kind
  • kind_display
  • services
  • services_display
  • open_duration
  • shop_ref
  • map_visibility
  • entity_count
  • entity_ids
  • tags_display
  • auto_categories

说明

  • 数据来源是 Resource/Config/Entity/AreaPlace.*
  • 这里的 building 指地点级建筑或功能地点,不是建造件 Construction/Part.*
  • 少量同名地点会按语义标签拆开;如果页面标题有歧义,建议直接传 AreaPlace.* ID。

local common = require('Module:Common')
local item_common = require('Module:ItemCommon')

local p = {}

local FIELD_MAP = {
    id = 'id',
    name = 'n',
    description = 'd',
    map_description = 'md',
    kind = 'k',
    services = 'sv',
    open_duration = 'od',
    entity_ids = 'e',
    tags = 't',
    shop_ref = 'sr',
    map_visible = 'mv',
    minimap_visible = 'mm',
}

local SERVICE_LABELS = {
    festival_shop = '节日商店',
    shop = '商店',
    npc_store = 'NPC 商店',
    restaurant = '餐饮',
    player_restaurant = '玩家餐馆',
    functional = '功能设施',
}

local KIND_LABELS = {
    shop = '商业建筑',
    restaurant = '餐饮建筑',
    functional = '功能建筑',
    building = '建筑',
}

local GENERIC_TAGS = {
    Building = true,
    Store = true,
    LogicBuilding = true,
}

local data_cache
local mapping_cache

local function get_record_field(record, field)
    if type(record) ~= 'table' then
        return nil
    end
    local short_key = FIELD_MAP[field] or field
    if record[short_key] ~= nil then
        return record[short_key]
    end
    return record[field]
end

local function load_data()
    if data_cache then
        return
    end
    data_cache, mapping_cache = item_common.loadDomainData('数据:Building/building_index.json', '数据:Building/building_mapping.json')
    if type(data_cache) == 'table' and type(data_cache.records) == 'table' then
        data_cache = data_cache.records
    end
end

local function find_record(key)
    load_data()
    return item_common.findRecord(data_cache or {}, mapping_cache, key)
end

local function get_current_page_name()
    local title = mw.title.getCurrentTitle()
    if not title then
        return ''
    end
    return common.trim(title.text or '')
end

local function render_services(record)
    local services = get_record_field(record, 'services')
    if type(services) ~= 'table' then
        return ''
    end

    local parts = {}
    for _, service in ipairs(services) do
        local label = SERVICE_LABELS[common.trim(service)] or common.trim(service)
        if label ~= '' then
            parts[#parts + 1] = label
        end
    end
    return table.concat(parts, ' / ')
end

local function has_service(record, expected_service)
    local services = get_record_field(record, 'services')
    if type(services) ~= 'table' then
        return false
    end

    local expected = common.trim(expected_service)
    if expected == '' then
        return false
    end

    for _, service in ipairs(services) do
        if common.trim(service) == expected then
            return true
        end
    end
    return false
end

local function render_visibility(record)
    local on_map = get_record_field(record, 'map_visible')
    local on_minimap = get_record_field(record, 'minimap_visible')
    if on_map and on_minimap then
        return '地图与小地图可见'
    end
    if on_map then
        return '仅地图可见'
    end
    if on_minimap then
        return '仅小地图可见'
    end
    return '隐藏'
end

local function render_entity_ids(record)
    local entity_ids = get_record_field(record, 'entity_ids')
    if type(entity_ids) ~= 'table' then
        return ''
    end

    local parts = {}
    for _, entity_id in ipairs(entity_ids) do
        local text = common.trim(entity_id)
        if text ~= '' then
            parts[#parts + 1] = '<code>' .. mw.text.nowiki(text) .. '</code>'
        end
    end
    return table.concat(parts, '<br>')
end

local function render_tags(record)
    local tags = get_record_field(record, 'tags')
    if type(tags) ~= 'table' then
        return ''
    end

    local parts = {}
    for _, tag in ipairs(tags) do
        local text = common.trim(tag)
        if text ~= '' and not GENERIC_TAGS[text] then
            parts[#parts + 1] = text
        end
    end
    return table.concat(parts, ' / ')
end

local function render_kind(record)
    local kind = common.trim(get_record_field(record, 'kind') or '')
    if kind == '' then
        return ''
    end
    return KIND_LABELS[kind] or kind
end

local function render_auto_categories(record)
    local categories = {
        '[[分类:建筑]]',
    }

    local kind = common.trim(get_record_field(record, 'kind') or '')
    local shop_ref = common.trim(get_record_field(record, 'shop_ref') or '')

    if kind == 'shop' or shop_ref ~= '' then
        categories[#categories + 1] = '[[分类:商店建筑]]'
    end
    if has_service(record, 'festival_shop') then
        categories[#categories + 1] = '[[分类:节日商店]]'
    end
    if has_service(record, 'npc_store') then
        categories[#categories + 1] = '[[分类:NPC商店]]'
    end
    if kind == 'functional' then
        categories[#categories + 1] = '[[分类:功能建筑]]'
    end
    if kind == 'restaurant' or has_service(record, 'restaurant') or has_service(record, 'player_restaurant') then
        categories[#categories + 1] = '[[分类:餐饮建筑]]'
    end

    return table.concat(categories, '')
end

local function get_field_value(record, field)
    if field == 'name_display' then
        local name = common.trim(get_record_field(record, 'name') or '')
        if name ~= '' then
            return name
        end
        return get_current_page_name()
    end
    if field == 'kind_display' then
        return render_kind(record)
    end
    if field == 'services_display' then
        return render_services(record)
    end
    if field == 'map_visibility' then
        return render_visibility(record)
    end
    if field == 'entity_ids' then
        return render_entity_ids(record)
    end
    if field == 'tags_display' then
        return render_tags(record)
    end
    if field == 'entity_count' then
        local entity_ids = get_record_field(record, 'entity_ids')
        if type(entity_ids) == 'table' then
            return tostring(#entity_ids)
        end
        return '0'
    end
    if field == 'auto_categories' then
        return render_auto_categories(record)
    end
    return get_record_field(record, field)
end

function p.findRecord(frame)
    local key = common.getArg(frame, 1, '')
    local record = find_record(key)
    if not record then
        return ''
    end
    return common.toText(get_record_field(record, 'id') or '')
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_field_value(record, field))
end

return p