无编辑摘要
无编辑摘要
第18行: 第18行:
-- [[------------------------------------------------------------------]]
-- [[------------------------------------------------------------------]]
local displayNames = {
local displayNames = {
    -- 核心字段 (保持不变)
     ['title'] = '歌曲标题', ['composer'] = '艺术家', ['bpm'] = 'BPM', ['singer'] = '歌手', ['genre'] = '流派',
     ['title'] = '歌曲标题',
     ['id'] = '歌曲ID', ['key'] = 'KEY音', ['bga'] = 'BGA作者', ['duration'] = '时长',
    ['composer'] = '艺术家',
    ['bpm'] = 'BPM',
    ['singer'] = '歌手',
    ['genre'] = '流派',
     ['id'] = '歌曲ID',
    ['key'] = 'KEY音',
    ['bga'] = 'BGA作者',
 
    -- 保留常用字段
    ['duration'] = '时长',
 
    -- 节奏游戏字段 (保持不变)
     ['4b_nm'] = '4B NM', ['4b_nm_notes'] = '4B NM按键数', ['4b_hd'] = '4B HD', ['4b_hd_notes'] = '4B HD按键数',  
     ['4b_nm'] = '4B NM', ['4b_nm_notes'] = '4B NM按键数', ['4b_hd'] = '4B HD', ['4b_hd_notes'] = '4B HD按键数',  
     ['4b_mx'] = '4B MX', ['4b_mx_notes'] = '4B MX按键数', ['4b_sc'] = '4B SC', ['4b_sc_notes'] = '4B SC按键数',
     ['4b_mx'] = '4B MX', ['4b_mx_notes'] = '4B MX按键数', ['4b_sc'] = '4B SC', ['4b_sc_notes'] = '4B SC按键数',
第56行: 第44行:
end  
end  


-- 格式化单个值的私有函数 (基础格式化,不应用任何样式)
-- 格式化单个值的私有函数 (基础格式化,确保 ' - ' 被视为字符串)
local function formatSingleValue(value, fieldType)
local function formatSingleValue(value, fieldType)
     if value == nil then
     if value == nil then
第65行: 第53行:
         return p.valueMappingMethod[fieldType](value)
         return p.valueMappingMethod[fieldType](value)
     else
     else
        -- 核心:将所有值转换为字符串,包括 '-'
         return tostring(value)
         return tostring(value)
     end
     end
第77行: 第66行:
end
end


 
-- 获取难度模式
-- 获取难度模式 (用于判断是否为难度字段)
local function getDifficultyDetails(field)
local function getDifficultyDetails(field)
     local checkField = field and string.lower(mw.text.trim(field)) or ''
     local checkField = field and string.lower(mw.text.trim(field)) or ''
第90行: 第78行:


     if difficultyFields[checkField] == true then
     if difficultyFields[checkField] == true then
        -- 提取模式 ('nm', 'hd', 'mx', or 'sc')
         local mode = checkField:sub(-2)
         local mode = checkField:sub(-2)
        -- 返回 (btn, mode)
         return checkField:match("^(%d+)b"), mode
         return checkField:match("^(%d+)b"), mode
     end
     end
   
     return nil, nil
     return nil, nil
end
end


-- **核心:难度样式应用 (仅用于表格)**
-- **核心:难度样式应用 (仅用于表格,并处理可能存在的 'sc' 前缀)**
local function formatDifficultyStyles(valueString, mode)
local function formatDifficultyStyles(valueString, mode)
     -- valueString 预期是经过 formatSingleValue 处理的字符串 (例如 '15', ' - ', 或 '无')
     -- valueString 预期是经过 formatSingleValue 处理的字符串 (例如 '15', ' - ', 或 '无')
   
    -- 1. 特殊值检查:如果是 '-' 或 '无',直接返回
    if valueString == '-' or valueString == i18n['none'] then
        return valueString
    end
   
    local displayString = valueString -- 用于最终输出的字符串 (默认为原始字符串)
     local numValue = tonumber(valueString)
     local numValue = tonumber(valueString)
      
      
     -- 如果无法转换为数字,则返回原始字符串
     if mode == 'sc' and numValue == nil then
        -- 核心修复:如果是 SC 字段且不是纯数字,则尝试剥离 'sc'/'SC' 前缀进行转换
        local stripped = valueString:match("^[Ss][Cc](%d+)$")
        if stripped then
            numValue = tonumber(stripped) -- 使用剥离后的数字进行样式判断
            displayString = stripped      -- 使用剥离后的数字字符串进行显示
        end
    end
   
    -- 如果仍不是数字,则返回原始字符串
     if numValue == nil then
     if numValue == nil then
         return valueString  
         return valueString  
第139行: 第140行:
      
      
     if style ~= '' then
     if style ~= '' then
         -- 返回带样式的纯数字字符串 (不包含 'sc' 前缀)
         -- 返回带样式的纯数字字符串 (例如 <span...>15</span>)
         return '<span style="' .. style .. '">' .. valueString .. '</span>'
         return '<span style="' .. style .. '">' .. displayString .. '</span>'
     end
     end
      
      
     return valueString -- 返回原始数字字符串(如果没有样式应用)
     return displayString -- 返回原始数字字符串(例如 8)
end
end


第150行: 第151行:
     local isSC = string.match(fieldType, '_sc$')
     local isSC = string.match(fieldType, '_sc$')
      
      
     -- 仅当是 SC 字段,且值存在(不是 '')且不为 ' - ' 时,添加 "sc" 前缀
     -- 仅当是 SC 字段,且值存在且不为 '-' '' 时,添加 "sc" 前缀
     if isSC and valueString ~= i18n['none'] and valueString ~= '-' then
     if isSC and valueString ~= i18n['none'] and valueString ~= '-' then
         return 'sc' .. valueString
         if not valueString:lower():find('sc') then
            return 'sc' .. valueString
        end
     end
     end
      
      
第214行: 第217行:
         local formattedValue = formatSingleValue(value, argType)  
         local formattedValue = formatSingleValue(value, argType)  
          
          
         -- 步骤 2: 应用 SC 前缀 (仅 p.value 需要,且不带样式)
         -- 步骤 2: 应用 SC 前缀
         formattedValue = applySCPrefix(formattedValue, argType)
         formattedValue = applySCPrefix(formattedValue, argType)


第221行: 第224行:
      
      
return table.concat(resultTable, ' / ')
return table.concat(resultTable, ' / ')
end
function p.findKeysByValue(f)
    local args = f
    local frame = mw.getCurrentFrame()
    if f == frame then
        args = require('Module:ProcessArgs').merge(true)
    end
   
    local targetField = mw.text.trim(args.field or '')
    local targetValue = args.value
   
    if type(targetValue) == 'string' then
        targetValue = mw.text.trim(targetValue)
    end
   
    if targetField == '' or targetValue == nil then
        return i18n['error_find_keys_args']
    end
    local songData = GetValuesTable()
    local matchingKeys = {}
   
    for songId, songEntry in pairs(songData) do
        local currentValue = ValueFromValuesByKey(songEntry, targetField)
       
        if currentValue == targetValue then
            table.insert(matchingKeys, songId)
        end
    end
   
    table.sort(matchingKeys)
   
    return table.concat(matchingKeys, ', ')
end
end


第307行: 第276行:
     local output = {}
     local output = {}
      
      
     -- 表格样式: 宽度 100%, 居中, 内容居中
     -- 表格样式
     table.insert(output, '{| class="wikitable sortable" style="width: 100%; margin: 0 auto; text-align: center;"')
     table.insert(output, '{| class="wikitable sortable" style="width: 100%; margin: 0 auto; text-align: center;"')
      
      
     -- 构造表格头部 (保持不变)
     -- 构造表格头部
     table.insert(output, '|-')
     table.insert(output, '|-')
     for _, field in ipairs(displayFields) do
     for _, field in ipairs(displayFields) do
第320行: 第289行:
     for _, songId in ipairs(matchingKeys) do
     for _, songId in ipairs(matchingKeys) do
         local songEntry = songData[songId]
         local songEntry = songData[songId]
         table.insert(output, '|-') -- 新行开始
         table.insert(output, '|-')
          
          
        -- 数据字段
         for _, field in ipairs(displayFields) do
         for _, field in ipairs(displayFields) do
             field = mw.text.trim(field)
             field = mw.text.trim(field)
第333行: 第301行:
             -- 步骤 2: **仅在表格中**应用特殊样式
             -- 步骤 2: **仅在表格中**应用特殊样式
              
              
            -- 应用 BPM 样式
             if field == 'bpm' then
             if field == 'bpm' then
                 formattedValue = formatBPMValue(formattedValue)
                 formattedValue = formatBPMValue(formattedValue)
             end
             end


            -- 应用难度样式
             local _, mode = getDifficultyDetails(field)
             local _, mode = getDifficultyDetails(field)
             if mode then
             if mode then
                 -- 调用难度样式函数,它负责所有颜色和光晕 (不添加 'sc' 前缀)
                 -- 核心:formatDifficultyStyles 现在能正确处理 '-'
                 formattedValue = formatDifficultyStyles(formattedValue, mode)
                 formattedValue = formatDifficultyStyles(formattedValue, mode)
             end
             end
              
              
             -- 确保每个单元格都在自己的行上
             -- MediaWiki 表格单元格开始符 ' | '
             table.insert(output, '|' .. formattedValue)
             table.insert(output, '|' .. formattedValue)
         end
         end
第352行: 第318行:
     table.insert(output, '|}')
     table.insert(output, '|}')
      
      
    -- 使用 \n 连接所有行,确保 MediaWiki 解析器能识别所有表格元素
     return table.concat(output, '\n')
     return table.concat(output, '\n')
end
end


return p
return p