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

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

模块:Indent

来自星砂岛百科

概述

Indent 提供委托域的数据读取、需求奖励展示与条件区块渲染,供 {{Infobox indent}}{{IndentDetails}} 调用。

用法

{{#invoke:Indent|getField|木板收集委托|issuer_name}}
{{#invoke:Indent|requestList|木板收集委托}}
{{#invoke:Indent|rewardList|木板收集委托}}
{{#invoke:Indent|detailBlocks|木板收集委托}}

函数

  • getField:读取委托字段。
  • requestList:渲染需求条目。
  • rewardList:渲染奖励条目。
  • detailBlocks:输出需求、奖励和条件区块。

页面装配

{{Infobox indent}}
{{IndentDetails}}

数据来源


local common = require('Module:Common')
local item_common = require('Module:ItemCommon')
local item = require('Module:Item')
local css = require('Module:CSS')

local p = {}

local FIELD_MAP = {
    issuer_name = 'in',
    issuer_id = 'ii',
    board_name = 'bn',
    indent_key = 'ik',
    indent_type_display = 'td',
    category_display = 'cd',
    indent_level = 'lv',
    indent_score = 'sc',
    mission_lifetime_hours = 'ml',
    board_lifetime_hours = 'bl',
    repeatable = 'rp',
    force_refresh = 'fr',
    request_summary = 'rq',
    reward_summary = 'rw',
    unlock_condition_summary = 'uc',
    weight_condition_summary = 'wc',
    related_mission = 'rm',
    request_items = 'ri',
    reward_items = 'oi',
    reward_currencies = 'cy',
    description = 'd',
    related_systems = 'sy',
}

local data_cache
local mapping_cache

local function default_mapping()
    return {
        name_to_id = {},
        id_to_name = {},
        aliases = {},
        overrides = {
            name_to_id = {},
            aliases = {},
        },
    }
end

local function unwrap_index(raw)
    if type(raw) ~= 'table' then
        return {}
    end
    if type(raw.by_name) == 'table' then
        return raw.by_name
    end
    return raw
end

local function load_data()
    if data_cache then
        return
    end
    data_cache = unwrap_index(common.loadJsonData('数据:Indent/indent_index.json') or {})
    mapping_cache = common.loadJsonData('数据:Indent/indent_mapping.json') or default_mapping()
end

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

local function get_record_value(record, key, field)
    if type(record) ~= 'table' then
        return nil
    end
    if field == 'id' or field == 'name' or field == 'name_en' then
        return item_common.getIdentityField(record, key, field)
    end
    if record[field] ~= nil then
        return record[field]
    end
    local mapped = FIELD_MAP[field]
    if mapped then
        return record[mapped]
    end
    return nil
end

local function normalize_bool(value)
    if value == true or value == 'true' or value == '1' or value == 1 then
        return '是'
    end
    if value == false or value == 'false' or value == '0' or value == 0 then
        return '否'
    end
    return common.toText(value)
end

local function render_item_stack(frame, entry)
    if type(entry) ~= 'table' then
        return ''
    end
    local item_id = common.trim(entry.item_id or entry.id or '')
    if item_id == '' then
        return ''
    end
    local args = { item_id }
    local count = tonumber(entry.count or entry.value or 0) or 0
    if count > 1 then
        args[2] = tostring(count)
    end
    return item.renderItemWithArgs(frame, args)
end

local function currency_template(currency_id)
    local normalized = common.normalizeKey(currency_id)
    if normalized == 'currency.default' then
        return 'Gold'
    end
    if normalized == 'currency.achievementpoints' then
        return 'Star'
    end
    if normalized == 'currency.exp' or normalized == 'currency.achievementexp' then
        return 'Exp'
    end
    return ''
end

local function currency_label(currency_id)
    local normalized = common.normalizeKey(currency_id)
    if normalized == 'currency.default' then
        return '金币'
    end
    if normalized == 'currency.achievementpoints' then
        return '星砂'
    end
    if normalized == 'currency.achievementexp' then
        return '成就经验'
    end
    return common.trim(currency_id)
end

local function render_currency(entry)
    if type(entry) ~= 'table' then
        return ''
    end
    local currency_id = common.trim(entry.currency_id or entry.id or entry.type or '')
    local value = tonumber(entry.value or entry.count or 0) or 0
    if currency_id == '' or value == 0 then
        return ''
    end
    local template = currency_template(currency_id)
    if template ~= '' then
        return ('{{%s|%s}}'):format(template, tostring(value))
    end
    return mw.text.encode(currency_label(currency_id)) .. ' x' .. tostring(value)
end

local function render_entry_list(frame, values)
    if type(values) ~= 'table' then
        return ''
    end
    local parts = {}
    for _, entry in ipairs(values) do
        local rendered = render_item_stack(frame, entry)
        if rendered == '' then
            rendered = render_currency(entry)
        end
        if rendered ~= '' then
            parts[#parts + 1] = rendered
        end
    end
    return table.concat(parts, '、')
end

local function render_detail_block(title, content)
    local text = common.trim(content)
    if text == '' then
        return ''
    end
    return '<h3>' .. mw.text.encode(title) .. '</h3>\n' .. text
end

local function summarize_weight_text(text)
    local value = common.trim(common.toText(text))
    if value == '' then
        return ''
    end
    value = value:gsub('%s*%-%>%s*权重%s+%-?100000', '(不再刷新)')
    value = value:gsub('%s*%-%>%s*权重%s+750', '(高优先刷新)')
    value = value:gsub('%s*%-%>%s*权重%s+375', '(较高优先刷新)')
    value = value:gsub('%s*%-%>%s*权重%s+100', '(更容易刷新)')
    value = value:gsub('%s*%-%>%s*权重%s+30', '(可参与刷新)')
    return value
end

local function summarize_mission_text(text)
    local value = common.trim(common.toText(text))
    if value == '' then
        return ''
    end
    return value:gsub('^Mission%.', '')
end

local function build_rule_summary(record)
    if type(record) ~= 'table' then
        return ''
    end
    local parts = {}

    local repeatable = normalize_bool(get_record_value(record, nil, 'repeatable'))
    if repeatable ~= '' then
        parts[#parts + 1] = '可重复:' .. repeatable
    end

    local force_refresh = normalize_bool(get_record_value(record, nil, 'force_refresh'))
    if force_refresh ~= '' then
        parts[#parts + 1] = '强制刷新:' .. force_refresh
    end

    local mission_lifetime = common.trim(common.toText(get_record_value(record, nil, 'mission_lifetime_hours')))
    if mission_lifetime ~= '' and mission_lifetime ~= '0' then
        parts[#parts + 1] = '持续时间:' .. mission_lifetime .. ' 小时'
    end

    local board_lifetime = common.trim(common.toText(get_record_value(record, nil, 'board_lifetime_hours')))
    if board_lifetime ~= '' and board_lifetime ~= '0' then
        parts[#parts + 1] = '看板停留:' .. board_lifetime .. ' 小时'
    end

    return table.concat(parts, ';')
end

function p.findRecord(key)
    return find_record(key)
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

    if field == 'repeatable_display' then
        return normalize_bool(get_record_value(record, key, 'repeatable'))
    end
    if field == 'force_refresh_display' then
        return normalize_bool(get_record_value(record, key, 'force_refresh'))
    end
    if field == 'request_items_display' then
        local explicit = common.trim(common.toText(get_record_value(record, key, 'request_summary')))
        if explicit ~= '' then
            return explicit
        end
        return render_entry_list(frame, get_record_value(record, key, 'request_items'))
    end
    if field == 'reward_items_display' then
        local explicit = common.trim(common.toText(get_record_value(record, key, 'reward_summary')))
        if explicit ~= '' then
            return explicit
        end
        local parts = {}
        local item_text = render_entry_list(frame, get_record_value(record, key, 'reward_items'))
        local currency_text = render_entry_list(frame, get_record_value(record, key, 'reward_currencies'))
        if item_text ~= '' then
            parts[#parts + 1] = item_text
        end
        if currency_text ~= '' then
            parts[#parts + 1] = currency_text
        end
        return table.concat(parts, '、')
    end
    if field == 'weight_condition_display' then
        return summarize_weight_text(get_record_value(record, key, 'weight_condition_summary'))
    end
    if field == 'related_mission_display' then
        return summarize_mission_text(get_record_value(record, key, 'related_mission'))
    end

    return common.toText(get_record_value(record, key, field))
end

function p.requestList(frame)
    local record = find_record(common.getArg(frame, 1, ''))
    if not record then
        return ''
    end
    local request_text = render_entry_list(frame, get_record_value(record, nil, 'request_items'))
    if request_text == '' then
        request_text = common.trim(common.toText(get_record_value(record, nil, 'request_summary')))
    end
    return request_text
end

function p.rewardList(frame)
    local record = find_record(common.getArg(frame, 1, ''))
    if not record then
        return ''
    end
    local parts = {}
    local item_text = render_entry_list(frame, get_record_value(record, nil, 'reward_items'))
    local currency_text = render_entry_list(frame, get_record_value(record, nil, 'reward_currencies'))
    if item_text ~= '' then
        parts[#parts + 1] = item_text
    end
    if currency_text ~= '' then
        parts[#parts + 1] = currency_text
    end
    if #parts == 0 then
        return common.trim(common.toText(get_record_value(record, nil, 'reward_summary')))
    end
    return (css.quickCall('Item') or '') .. table.concat(parts, '、')
end

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

    local blocks = {}
    local rule_block = render_detail_block('委托规则', build_rule_summary(record))
    if rule_block ~= '' then
        blocks[#blocks + 1] = rule_block
    end
    local request_block = render_detail_block('需求', p.requestList(frame))
    if request_block ~= '' then
        blocks[#blocks + 1] = request_block
    end
    local reward_block = render_detail_block('奖励', p.rewardList(frame))
    if reward_block ~= '' then
        blocks[#blocks + 1] = reward_block
    end
    local unlock_block = render_detail_block('解锁条件', common.toText(get_record_value(record, nil, 'unlock_condition_summary')))
    if unlock_block ~= '' then
        blocks[#blocks + 1] = unlock_block
    end
    local weight_block = render_detail_block('刷新权重条件', summarize_weight_text(get_record_value(record, nil, 'weight_condition_summary')))
    if weight_block ~= '' then
        blocks[#blocks + 1] = weight_block
    end
    local mission_block = render_detail_block('关联任务', summarize_mission_text(get_record_value(record, nil, 'related_mission')))
    if mission_block ~= '' then
        blocks[#blocks + 1] = mission_block
    end
    return table.concat(blocks, '\n')
end

return p