跳转到内容

模块:Db:修订间差异

来自DJMAX中文资料库
无编辑摘要
无编辑摘要
第1行: 第1行:
local p = {}
local p = {}


-- 1. 国际化和常量  
-- [[------------------------------------------------------------------]]
 
-- [[ 1. 国际化和常量 ]]
-- [[------------------------------------------------------------------]]
local i18n = {
local i18n = {
['true'] = '是',       
['true'] = '是',       
第13行: 第14行:
}
}


-- 2. 表格标题映射  
-- [[------------------------------------------------------------------]]
 
-- [[ 2. 表格标题映射 (已更新) ]]
-- [[------------------------------------------------------------------]]
local displayNames = {
local displayNames = {
    -- 用户提供的核心字段
     ['title'] = '歌曲标题',
     ['title'] = '歌曲标题',
     ['composer'] = '艺术家',
     ['composer'] = '艺术家',
第24行: 第27行:
     ['key'] = 'KEY音',
     ['key'] = 'KEY音',
     ['bga'] = 'BGA作者',
     ['bga'] = 'BGA作者',
    -- 保留常用字段
    ['duration'] = '时长',
    ['is_featured'] = '精选',
     -- 4B 字段
     -- 4B 字段
     ['4b_nm'] = '4B NM',
     ['4b_nm'] = '4B NM',
第62行: 第70行:
}
}


--  3. 本地辅助函数定义 (Local Helper Functions)
--    这些函数必须在 p.value 等公共函数之前定义。
local function GetValuesTable()
local function GetValuesTable()
     return mw.loadData('Module:db_song')
     return mw.loadData('Module:db_song')
第75行: 第81行:
end
end


-- 格式化单个值的私有函数 (重点:确保此定义被包含)
local function formatSingleValue(value, fieldType)
local function formatSingleValue(value, fieldType)
     if value == nil then
     if value == nil then
第81行: 第86行:
     elseif type(value) == 'boolean' then
     elseif type(value) == 'boolean' then
         return value and i18n['true'] or i18n['false']
         return value and i18n['true'] or i18n['false']
    -- 注意:这里调用了 p.valueMappingMethod,它在 p 被返回之前完成定义
     elseif type(p.valueMappingMethod[fieldType]) == 'function' then
     elseif type(p.valueMappingMethod[fieldType]) == 'function' then
         return p.valueMappingMethod[fieldType](value)
         return p.valueMappingMethod[fieldType](value)
第88行: 第92行:
     end
     end
end
end
-- 判断是否为节奏游戏字段
local function isRhythmGameField(field)
    if field and field:match("^[4568]b_(nm|hd|mx|sc)(_notes)?$") then
        return true
    end
    return false
end


-- 4. 值格式化方法 (valueMappingMethod)
-- 4. 值格式化方法 (valueMappingMethod)
第111行: 第124行:
-- [[------------------------------------------------------------------]]
-- [[------------------------------------------------------------------]]


-- p.value 函数体(调用 formatSingleValue)
function p.value(f)
function p.value(f)
     local args = f
     local args = f
第146行: 第158行:
         argType = mw.text.trim(argType)
         argType = mw.text.trim(argType)
         local value = ValueFromValuesByKey(songEntry, argType)
         local value = ValueFromValuesByKey(songEntry, argType)
        -- 调用本地函数
         local formattedValue = formatSingleValue(value, argType)
         local formattedValue = formatSingleValue(value, argType)
          
          
第155行: 第166行:
end
end


-- p.findKeysByValue 函数体
function p.findKeysByValue(f)
function p.findKeysByValue(f)
     local args = f
     local args = f
第189行: 第199行:
end
end


-- p.generateListTable 函数体(调用 formatSingleValue)
function p.generateListTable(f)
function p.generateListTable(f)
     local args = require('Module:ProcessArgs').merge(true)  
     local args = require('Module:ProcessArgs').merge(true)  
第197行: 第206行:
     local displayFieldsString = mw.text.trim(args.columns or '')  
     local displayFieldsString = mw.text.trim(args.columns or '')  
      
      
     -- 优化:预处理 conditionValue 的类型
     -- 预处理 conditionValue 的类型 (代码保持不变)
     if type(conditionValue) == 'string' then
     if type(conditionValue) == 'string' then
         local trimmedValue = mw.text.trim(conditionValue)
         local trimmedValue = mw.text.trim(conditionValue)
第224行: 第233行:
     local matchingKeys = {}
     local matchingKeys = {}
      
      
    -- 查找匹配的键
     for songId, songEntry in pairs(songData) do
     for songId, songEntry in pairs(songData) do
         local currentValue = ValueFromValuesByKey(songEntry, conditionField)
         local currentValue = ValueFromValuesByKey(songEntry, conditionField)
       
         if currentValue == conditionValue then
         if currentValue == conditionValue then
             table.insert(matchingKeys, songId)
             table.insert(matchingKeys, songId)
第241行: 第248行:
     local output = {}
     local output = {}
      
      
     -- 构造表格头部
     -- **核心修改:表格样式**
     table.insert(output, '{| class="wikitable sortable"')
    -- width: 90%; 满足 80%-100% 范围
    -- margin: 0 auto; 实现页面居中
    -- text-align: center; 实现内容居中
     table.insert(output, '{| class="wikitable sortable" style="width: 90%; margin: 0 auto; text-align: center;"')
     table.insert(output, '|-')
     table.insert(output, '|-')
   
    -- **移除 ID 列表头**
    -- table.insert(output, '! ' .. (displayNames['id'] or 'ID'))
      
      
     for _, field in ipairs(displayFields) do
     for _, field in ipairs(displayFields) do
第255行: 第268行:
         table.insert(output, '|-')
         table.insert(output, '|-')
          
          
        -- **移除 ID 列单元格**
        -- table.insert(output, '| ' .. songId)
       
        -- 后续列: 数据字段
         for _, field in ipairs(displayFields) do
         for _, field in ipairs(displayFields) do
             field = mw.text.trim(field)
             field = mw.text.trim(field)
             local rawValue = ValueFromValuesByKey(songEntry, field)
             local rawValue = ValueFromValuesByKey(songEntry, field)
            -- 调用本地函数
             local formattedValue = formatSingleValue(rawValue, field)
             local formattedValue = formatSingleValue(rawValue, field)
           
            -- 套用 {{df|}} 模板
            if isRhythmGameField(field) then
                formattedValue = '{{df|' .. formattedValue .. '}}'
            end
           
             table.insert(output, '| ' .. formattedValue)
             table.insert(output, '| ' .. formattedValue)
         end
         end
     end
     end
      
      
    -- 闭合表格
     table.insert(output, '|}')
     table.insert(output, '|}')
      
      

2025年11月29日 (六) 09:30的版本

本模块为自动表核心功能程序,根据输入的信息自动筛选数据。

数据库储存在Module:db_song,通过Template:db可以调用数据库中的信息,也可以通过Template:songlist生成自动表格。


local p = {}

-- [[------------------------------------------------------------------]]
-- [[ 1. 国际化和常量 ]]
-- [[------------------------------------------------------------------]]
local i18n = {
	['true'] = '是',      
	['false'] = '否',     
	['none'] = '无',
	['category_missing_value'] = '缺失%s',
	['error_no_type'] = '<span class="error">Db:调用时必须提供type参数。</span>',
    ['error_find_keys_args'] = '<span class="error">Db:findKeysByValue必须提供field和value参数。</span>', 
    ['error_generate_table_args'] = '<span class="error">Db:generateListTable调用缺少field, value或columns参数。</span>',
}

-- [[------------------------------------------------------------------]]
-- [[ 2. 表格标题映射 (已更新) ]]
-- [[------------------------------------------------------------------]]
local displayNames = {
    -- 用户提供的核心字段
    ['title'] = '歌曲标题',
    ['composer'] = '艺术家',
    ['bpm'] = 'BPM',
    ['singer'] = '歌手',
    ['genre'] = '流派',
    ['id'] = '歌曲ID',
    ['key'] = 'KEY音',
    ['bga'] = 'BGA作者',

    -- 保留常用字段
    ['duration'] = '时长',
    ['is_featured'] = '精选',

    -- 4B 字段
    ['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按键数',
    -- 5B 字段
    ['5b_nm'] = '5B NM',
    ['5b_nm_notes'] = '5B NM按键数',
    ['5b_hd'] = '5B HD',
    ['5b_hd_notes'] = '5B HD按键数',
    ['5b_mx'] = '5B MX',
    ['5b_mx_notes'] = '5B MX按键数',
    ['5b_sc'] = '5B SC',
    ['5b_sc_notes'] = '5B SC按键数',
    -- 6B 字段
    ['6b_nm'] = '6B NM',
    ['6b_nm_notes'] = '6B NM按键数',
    ['6b_hd'] = '6B HD',
    ['6b_hd_notes'] = '6B HD按键数',
    ['6b_mx'] = '6B MX',
    ['6b_mx_notes'] = '6B MX按键数',
    ['6b_sc'] = '6B SC',
    ['6b_sc_notes'] = '6B SC按键数',
    -- 8B 字段
    ['8b_nm'] = '8B NM',
    ['8b_nm_notes'] = '8B NM按键数',
    ['8b_hd'] = '8B HD',
    ['8b_hd_notes'] = '8B HD按键数',
    ['8b_mx'] = '8B MX',
    ['8b_mx_notes'] = '8B MX按键数',
    ['8b_sc'] = '8B SC',
    ['8b_sc_notes'] = '8B SC按键数',
}

local function GetValuesTable()
    return mw.loadData('Module:db_song')
end

local function ValueFromValuesByKey(values, key)
	if values and key then
		return values[key]
	end
	return nil
end

local function formatSingleValue(value, fieldType)
    if value == nil then
        return i18n['none']
    elseif type(value) == 'boolean' then
        return value and i18n['true'] or i18n['false']
    elseif type(p.valueMappingMethod[fieldType]) == 'function' then
        return p.valueMappingMethod[fieldType](value)
    else
        return tostring(value)
    end
end

-- 判断是否为节奏游戏字段
local function isRhythmGameField(field)
    if field and field:match("^[4568]b_(nm|hd|mx|sc)(_notes)?$") then
        return true
    end
    return false
end


-- 4. 值格式化方法 (valueMappingMethod)
p.valueMappingMethod = {
    ['duration'] = (function (value)
        if type(value) == 'number' then
            local minutes = math.floor(value / 60)
            local seconds = value % 60
            return string.format('%d:%02d', minutes, seconds)
        end
        return value
    end),
    ['artist'] = (function (value)
        if type(value) == 'string' and value ~= '' then
            return '[[' .. value .. ']]'
        end
        return value
    end),
}

-- [[------------------------------------------------------------------]]
-- [[ 5. 外部调用函数 (p.*) ]]
-- [[------------------------------------------------------------------]]

function p.value(f)
    local args = f
	local frame = mw.getCurrentFrame()
	if f == frame then
		args = require('Module:ProcessArgs').merge(true)
	end
    
	local argTargetName = mw.text.trim(args[1] or '') 
	local argTypeString = args.type                 
	local argNocat = args.nocat                     
    
    if not argTypeString then
        return i18n['error_no_type']
    end
    
    local values = GetValuesTable() 
    local songEntry = ValueFromValuesByKey(values, argTargetName)

    if not songEntry then
        if not argNocat then
            local title = mw.title.getCurrentTitle()
            if title.namespace == 0 and not title.isSubpage then
                return '?' .. '[[Category:' .. string.format(i18n['category_missing_value'], '歌曲ID ' .. argTargetName) .. ']]'
            end
        end
        return i18n['none']
    end
    
    local fieldNames = mw.text.split(argTypeString, ',')
    local resultTable = {}

    for _, argType in ipairs(fieldNames) do
        argType = mw.text.trim(argType)
        local value = ValueFromValuesByKey(songEntry, argType)
        local formattedValue = formatSingleValue(value, argType)
        
        table.insert(resultTable, formattedValue)
    end
    
	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

function p.generateListTable(f)
    local args = require('Module:ProcessArgs').merge(true) 
    
    local conditionField = mw.text.trim(args.field or '')   
    local conditionValue = args.value                       
    local displayFieldsString = mw.text.trim(args.columns or '') 
    
    -- 预处理 conditionValue 的类型 (代码保持不变)
    if type(conditionValue) == 'string' then
        local trimmedValue = mw.text.trim(conditionValue)
        
        local lowerValue = string.lower(trimmedValue)
        if lowerValue == 'true' or trimmedValue == '是' then
            conditionValue = true
        elseif lowerValue == 'false' or trimmedValue == '否' then
            conditionValue = false
        else
            local num = tonumber(trimmedValue)
            if num ~= nil then
                conditionValue = num
            else
                conditionValue = trimmedValue
            end
        end
    end
    
    if conditionField == '' or conditionValue == nil or displayFieldsString == '' then
        return i18n['error_generate_table_args']
    end

    local songData = GetValuesTable()
    local displayFields = mw.text.split(displayFieldsString, ',')
    local matchingKeys = {}
    
    for songId, songEntry in pairs(songData) do
        local currentValue = ValueFromValuesByKey(songEntry, conditionField)
        if currentValue == conditionValue then
            table.insert(matchingKeys, songId)
        end
    end
    
    table.sort(matchingKeys)

    if #matchingKeys == 0 then
        return '未找到符合条件的歌曲。'
    end

    local output = {}
    
    -- **核心修改:表格样式**
    -- width: 90%; 满足 80%-100% 范围
    -- margin: 0 auto; 实现页面居中
    -- text-align: center; 实现内容居中
    table.insert(output, '{| class="wikitable sortable" style="width: 90%; margin: 0 auto; text-align: center;"')
    table.insert(output, '|-')
    
    -- **移除 ID 列表头**
    -- table.insert(output, '! ' .. (displayNames['id'] or 'ID')) 
    
    for _, field in ipairs(displayFields) do
        field = mw.text.trim(field)
        table.insert(output, '! ' .. (displayNames[field] or field))
    end
    
    -- 构造表格行
    for _, songId in ipairs(matchingKeys) do
        local songEntry = songData[songId]
        table.insert(output, '|-')
        
        -- **移除 ID 列单元格**
        -- table.insert(output, '| ' .. songId) 
        
        -- 后续列: 数据字段
        for _, field in ipairs(displayFields) do
            field = mw.text.trim(field)
            local rawValue = ValueFromValuesByKey(songEntry, field)
            local formattedValue = formatSingleValue(rawValue, field)
            
            -- 套用 {{df|}} 模板
            if isRhythmGameField(field) then
                formattedValue = '{{df|' .. formattedValue .. '}}'
            end
            
            table.insert(output, '| ' .. formattedValue)
        end
    end
    
    -- 闭合表格
    table.insert(output, '|}')
    
    return table.concat(output, '\n')
end

return p