首页
随机
登录
设置
关于DJMAX中文资料库
免责声明
DJMAX中文资料库
搜索
查看“︁模块:Db”︁的源代码
←
模块:Db
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
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或columns参数。</span>', } -- [[------------------------------------------------------------------]] -- [[ 2. 表格标题映射 ]] -- [[------------------------------------------------------------------]] local displayNames = { ['title'] = '歌曲标题', ['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_mx'] = '4B MX', ['4b_mx_notes'] = '4B MX', ['4b_sc'] = '4B SC', ['4b_sc_notes'] = '4B SC', ['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_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_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', } -- [[------------------------------------------------------------------]] -- [[ 3. 本地辅助函数定义 ]] -- [[------------------------------------------------------------------]] 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 formatBPMValue(valueString) if string.find(valueString, '-') then return '<span style="color:red;">' .. valueString .. '</span>' end return valueString end local function getDifficultyDetails(field) local checkField = field and string.lower(mw.text.trim(field)) or '' local difficultyFields = { ['4b_nm'] = true, ['4b_hd'] = true, ['4b_mx'] = true, ['4b_sc'] = true, ['5b_nm'] = true, ['5b_hd'] = true, ['5b_mx'] = true, ['5b_sc'] = true, ['6b_nm'] = true, ['6b_hd'] = true, ['6b_mx'] = true, ['6b_sc'] = true, ['8b_nm'] = true, ['8b_hd'] = true, ['8b_mx'] = true, ['8b_sc'] = true, } if difficultyFields[checkField] == true then local mode = checkField:sub(-2) return checkField:match("^(%d+)b"), mode end return nil, nil end -- **核心:难度样式应用 (仅用于表格,并处理可能存在的 'sc' 前缀)** local function formatDifficultyStyles(valueString, mode) if valueString == '-' or valueString == i18n['none'] then return valueString end local displayString = valueString local numValue = tonumber(valueString) if mode == 'sc' and numValue == nil then local stripped = valueString:match("^[Ss][Cc](%d+)$") if stripped then numValue = tonumber(stripped) displayString = stripped end end if numValue == nil then return valueString end local style = '' if mode == 'sc' then local blueGlow = ' text-shadow: 0 0 5px #3D66FF, 0 0 8px #3D66FF;' style = 'font-weight: bold;' if numValue >= 1 and numValue <= 5 then style = style .. ' color: #E00075;' elseif numValue >= 6 and numValue <= 10 then style = style .. ' color: #C604E3;' elseif numValue >= 11 and numValue <= 12 then style = style .. ' color: #3D66FF;' elseif numValue == 13 then style = style .. ' color: orange;' .. blueGlow elseif numValue == 14 then style = style .. ' color: red;' .. blueGlow elseif numValue == 15 then style = style .. ' color: purple;' .. blueGlow end elseif (mode == 'nm' or mode == 'hd' or mode == 'mx') then if numValue == 14 then style = 'font-weight: bold; color: orange;' elseif numValue == 15 then style = 'font-weight: bold; color: red;' end end if style ~= '' then return '<span style="' .. style .. '">' .. displayString .. '</span>' end return displayString end -- **NOTES 字段加粗/变色逻辑 (新增 context 参数)** local function formatNotesValue(valueString, context) local numValue = tonumber(valueString) -- 检查是否为数字,且不少于 2000 if numValue and numValue >= 2000 then local content = valueString if context == 'table' then -- 表格环境:加粗 + 红色 return '<span style="font-weight: bold; color: red;">' .. content .. '</span>' else -- 其他环境:仅加粗 return '<b>' .. content .. '</b>' end end return valueString end -- **SC 前缀应用 (仅用于 p.value)** local function applySCPrefix(valueString, fieldType) local isSC = string.match(fieldType, '_sc$') if isSC and valueString ~= i18n['none'] and valueString ~= '-' then if not valueString:lower():find('sc') then return 'sc' .. valueString end end return valueString 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), } -- [[------------------------------------------------------------------]] -- [[ 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) formattedValue = applySCPrefix(formattedValue, argType) -- 新增:在非表格环境下应用 NOTES 格式化(仅加粗) if string.match(argType, '_notes$') then formattedValue = formatNotesValue(formattedValue, 'text') end table.insert(resultTable, formattedValue) end return table.concat(resultTable, ' / ') 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 '') 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 -- 仅要求 field 和 columns 存在,value 现在是可选的 if conditionField == '' or displayFieldsString == '' then return i18n['error_generate_table_args'] end local songData = GetValuesTable() local displayFields = mw.text.split(displayFieldsString, ',') local matchingKeys = {} -- 核心逻辑修正:检查是否需要过滤 local shouldFilter = conditionValue ~= nil for songId, songEntry in pairs(songData) do if not shouldFilter then -- 如果未提供 value,则包含所有歌曲 table.insert(matchingKeys, songId) else -- 如果提供了 value,则按值过滤 local currentValue = ValueFromValuesByKey(songEntry, conditionField) if currentValue == conditionValue then table.insert(matchingKeys, songId) end end end table.sort(matchingKeys) if #matchingKeys == 0 then return '未找到符合条件的歌曲。' end local output = {} -- 表格样式 table.insert(output, '{| class="wikitable sortable" style="width: 100%; margin: 0 auto; text-align: center;"') -- 构造表格头部:添加宽度样式控制 table.insert(output, '|-') for _, field in ipairs(displayFields) do field = mw.text.trim(field) local _, mode = getDifficultyDetails(field) local headerContent = displayNames[field] or field local headerStyle = '' local separator = ' ' -- 默认分隔符是空格 if mode then -- 谱面难度字段 (3.2% 固定宽度) headerStyle = ' style="width: 3.2%;"' separator = ' | ' elseif field == 'bpm' then -- BPM 字段 (6.5% 最低宽度) headerStyle = ' style="min-width: 6.5%;"' separator = ' | ' elseif string.match(field, '_notes$') then -- NOTES 字段 (4% 固定宽度) headerStyle = ' style="width: 4%;"' separator = ' | ' end -- MediaWiki 表格头单元格: ! style | Content table.insert(output, '!' .. headerStyle .. separator .. headerContent) end -- 构造表格行 for _, songId in ipairs(matchingKeys) do local songEntry = songData[songId] table.insert(output, '|-') for _, field in ipairs(displayFields) do field = mw.text.trim(field) local rawValue = ValueFromValuesByKey(songEntry, field) local formattedValue = formatSingleValue(rawValue, field) -- 应用内容样式 if field == 'bpm' then formattedValue = formatBPMValue(formattedValue) end local _, mode = getDifficultyDetails(field) if mode then formattedValue = formatDifficultyStyles(formattedValue, mode) end if string.match(field, '_notes$') then -- 表格环境:加粗 + 红色 formattedValue = formatNotesValue(formattedValue, 'table') end local cellContent = formattedValue -- CRITICAL FIX: 如果内容是单个 '-', 增加空格以避免被解析为行分隔符 '|-'. if cellContent == '-' then cellContent = ' -' end table.insert(output, '|' .. cellContent) end end table.insert(output, '|}') return table.concat(output, '\n') end return p
该页面使用的模板:
模块:Db/doc
(
查看源代码
)
返回
模块:Db
。