Module:Songtable:修订间差异

来自Arcaea中文维基
(重构,移除infoList的下标引用)
(重构,merge支持任意长度的列表)
第177行: 第177行:


function merge(list)
function merge(list)
    local merge
    local span = {}
   if list[4] then merge = {1, 1, 1, 1} else merge = {1, 1, 1} end
   local o
   if list[4] then o = 4 else o = 3 end


    if list[4] then
    local lastUniqueIndex = nil
      if list[4] == list[3] then merge[3] = merge[3] + merge[4] merge[4] = 0 end
   local currentIndex = 1
 
   while currentIndex<=#list do
      if lastUniqueIndex and list[lastUniqueIndex] == list[currentIndex] then
       span[lastUniqueIndex] = span[lastUniqueIndex] + 1
       span[currentIndex] = 0
     else
       lastUniqueIndex = currentIndex
       span[currentIndex] = 1
     end
     currentIndex = currentIndex + 1
    end
    end


    if list[3] == list[2] then merge[2] = merge[2] + merge[3] merge[3] = 0 end
    local merge = {}
 
   if list[2] == list[1] then merge[1] = merge[1] + merge[2] merge[2] = 0 end


    for i = 1, o do
    for i = 1, #list do
      if merge[i] == 0 then
      if span[i] == 0 then
        merge[i] = 'display:none'
        merge[i] = 'display:none'
      else
      else
        merge[i] = 'grid-column-start: span '..merge[i] .. ';'
        merge[i] = 'grid-column-start: span '..span[i] .. ';'
      end
      end
    end
    end

2024年3月12日 (二) 14:31的版本

可在Module:Songtable/doc创建此模块的帮助文档

local getArgs = require('Module:Arguments').getArgs
local link = require('Module:Link')._link
local getData = require("Module:Arcaea Data")
local p = {}

function timeCov(t)
    return os.date("(%Y/%m/%d)", t)
end

function dateCov(v, d, t, type)
    if not(v and d) then return nil end
    local box, a, b, c = mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div')
    box:addClass("date-box")
    a:wikitext(t)
    if type == "arg"
        then
        b:wikitext(v..'<br>' .. '('..tostring(d) .. ')')
    else
        b:wikitext('v'..v..'<br>'..tostring(timeCov(d)))
    end
    box:wikitext(tostring(a))
    :wikitext(tostring(b))
    :wikitext(tostring(c))
    return tostring(box)
end

function bgCov(bg)
    if bg == nil
        then
        return nil
    else
        return '[[:分类:采用'..bg..'背景的曲目|'..bg..']]'
    end
end

function choice(a, aKey, b, bKey)
    if aKey then return a end
    if bKey then return b end
    return nil
end

function bydMark(byd, name, inNs, nsByd)
    local diff, mark = mw.html.create('div'), mw.html.create('div')
    diff:wikitext('[Beyond]')
    mark:wikitext('仅移动版')
    if byd and inNs and not(nsByd)
        then
        return tostring(tostring(diff)..tostring(mark))
    else
        return tostring(diff)
    end
end

function labelText(text, class)
    local span = mw.html.create('span')
    span:addClass('label-text')
    span:wikitext(text)
    return tostring(span)
end

function getImage(id, all, pst, prs, ftr, byd, second, normalJacketArgs, bydJacketArgs, normalJacketData, bydJacketData, bydyes, mold, imageConfig)
    --谢邀,这写的什么东西我自己都看不懂了 ———Star0
    if not(id or all) then return nil end

    local container, tab = mw.html.create('div'), mw.html.create('div')
    local tab1, tab2, tab3, part1, part2, part3, jacket1, jacket2
    container:addClass('container')
    tab:attr('id', 'tab-'..mold) tab:addClass('img-tab')

    local normalJacket, bydJacket
    if normalJacketArgs
        then
        if bydJacketArgs
            then
            normalJacket, bydJacket = normalJacketArgs, bydJacketArgs
        else
            normalJacket, bydJacket = normalJacketArgs, normalJacketArgs
        end
    else
        if normalJacketData == "" or normalJacketData == nil
            then
            normalJacket, bydJacket = '未知', '未知'
        else
            normalJacket, bydJacket = link({'画师', tostring(normalJacketData)}), link({'画师', tostring(bydJacketData)})
        end
    end
	
	--Last判断
	
	if id == 'last'
		then
        tab1, tab2, tab3, part1, part2, part3 = mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div')

        tab1:wikitext('Normal') tab2:wikitext('Moment') tab3:wikitext('Eternity')
        tab1:addClass('img-tab-part ftr') tab2:addClass('img-tab-part byd') tab3:addClass('img-tab-part byd')
        tab:wikitext(tostring(tab1)) tab:wikitext(tostring(tab2)) tab:wikitext(tostring(tab3))
        container:wikitext(tostring(tab))

        part1:wikitext('[[文件:Songs last.jpg|256px|none]]'..labelText('画师')..normalJacket) part2:wikitext('[[文件:Songs last byd.jpg|256px|none]]'..labelText('画师')..normalJacket) part3:wikitext('[[文件:Songs last byd2.jpg|256px|none]]'..labelText('画师')..normalJacket)
        part1:addClass('active tab-text-'..mold) part2:addClass('tab-text-'..mold) part3:addClass('tab-text-'..mold)
        container:wikitext(tostring(part1)) container:wikitext(tostring(part2)) container:wikitext(tostring(part3))

        return tostring(container)
	end
	
    if pst and prs and ftr
        then
        tab1, tab2, tab3, part1, part2, part3 = mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div')

        tab1:wikitext('Past') tab2:wikitext('Present') tab3:wikitext('Future')
        tab1:addClass('img-tab-part pst') tab2:addClass('img-tab-part prs') tab3:addClass('img-tab-part ftr')
        tab:wikitext(tostring(tab1)) tab:wikitext(tostring(tab2)) tab:wikitext(tostring(tab3))
        container:wikitext(tostring(tab))

        part1:wikitext('[[文件:'..pst..'|256px|none]]'..labelText('画师')..normalJacket) part2:wikitext('[[文件:'..prs..'|256px|none]]'..labelText('画师')..normalJacket) part3:wikitext('[[文件:'..ftr..'|256px|none]]'..labelText('画师')..normalJacket)
        part1:addClass('tab-text-'..mold) part2:addClass('tab-text-'..mold) part3:addClass('active tab-text-'..mold)
        container:wikitext(tostring(part1)) container:wikitext(tostring(part2)) container:wikitext(tostring(part3))

        return tostring(container)
    end

    if byd and imageConfig~='nobyd'
        then
        tab1, tab2, part1, part2 = mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div')

        tab1:wikitext('Normal') tab2:wikitext('Beyond')
        tab1:addClass('img-tab-part normal') tab2:addClass('img-tab-part byd')
        tab:wikitext(tostring(tab1)) tab:wikitext(tostring(tab2))
        container:wikitext(tostring(tab))

        part1:wikitext('[[文件:'..all..'|256px|none]]'..labelText('画师')..normalJacket) part2:wikitext('[[文件:'..byd..'|256px|none]]'..labelText('画师')..tostring(bydJacket))
        part1:addClass('active tab-text-'..mold) part2:addClass('tab-text-'..mold)
        container:wikitext(tostring(part1)) container:wikitext(tostring(part2))

        return tostring(container)
    end

    if second
        then
        tab1, tab2, part1, part2 = mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div')

        tab1:wikitext('第一曲绘') tab2:wikitext('第二曲绘')
        tab1:addClass('img-tab-part normal') tab2:addClass('img-tab-part normal')
        tab:wikitext(tostring(tab1)) tab:wikitext(tostring(tab2))
        container:wikitext(tostring(tab))

        part1:wikitext('[[文件:'..all..'|256px|none]]'..labelText('画师')..normalJacket) part2:wikitext('[[文件:'..second..'|256px|none]]'..labelText('画师')..normalJacket)
        part1:addClass('active tab-text-'..mold) part2:addClass('tab-text-'..mold)
        container:wikitext(tostring(part1)) container:wikitext(tostring(part2))

        return tostring(container)
    end

    if id and bydyes and imageConfig~='nobyd'
        then
        tab1, tab2, part1, part2 = mw.html.create('div'), mw.html.create('div'), mw.html.create('div'), mw.html.create('div')

        tab1:wikitext('Normal') tab2:wikitext('Beyond')
        tab1:addClass('img-tab-part normal') tab2:addClass('img-tab-part byd')
        tab:wikitext(tostring(tab1)) tab:wikitext(tostring(tab2))
        container:wikitext(tostring(tab))

        part1:wikitext('[[文件:Songs '..id..'.jpg|256px|none]]'..labelText('画师')..normalJacket) part2:wikitext('[[文件:Songs '..id..' byd.jpg|256px|none]]'..labelText('画师')..tostring(bydJacket))
        part1:addClass('active tab-text-'..mold) part2:addClass('tab-text-'..mold)
        container:wikitext(tostring(part1)) container:wikitext(tostring(part2))

        return tostring(container)
    end

    if id
        then
        return '<div>[[文件:Songs '..id..'.jpg|256px|none]]'..labelText('画师')..normalJacket..'</div>'
    end

    return '<div>[[文件:'..all..'|256px|none]]'..labelText('画师')..normalJacket..'</div>'
end

function merge(list)
    local span = {}

    local lastUniqueIndex = nil
    local currentIndex = 1

    while currentIndex<=#list do
        if lastUniqueIndex and list[lastUniqueIndex] == list[currentIndex] then
            span[lastUniqueIndex] = span[lastUniqueIndex] + 1
            span[currentIndex] = 0
        else
            lastUniqueIndex = currentIndex
            span[currentIndex] = 1
        end
        currentIndex = currentIndex + 1
    end

    local merge = {}

    for i = 1, #list do
        if span[i] == 0 then
            merge[i] = 'display:none'
        else
            merge[i] = 'grid-column-start: span '..span[i] .. ';'
        end
    end

    return merge
end

function title(name,nameSecond,online)
    local result=name
    if nameSecond then result = result .. "<br>" .. nameSecond end
    if not(online) then result = result .. "<br>" .. '此曲目暂未上线' end
    return result
end

function p.main(frame)
    local args = getArgs(frame)
    return p._main(args)
end

function p._main(args)

    -- 获取数据查找函数
    local data, dataNs
    if args["id"] and args["id"] ~= ""
    then
        data, dataNs = getData.singleSongInformation(args['id'], "id", "mobile"), getData.singleSongInformation(args['id'], "id", "ns")
    elseif args["曲名"]
    then
        data, dataNs = getData.singleSongInformation(args['曲名'], "name", "mobile"), getData.singleSongInformation(args['曲名'], "name", "ns")
    end

    -- 判断曲目在哪个平台存在。
    local inMobile,inNs = false, false
    if data("id") or args["更新时间"] then
        inMobile = true
    end

    if dataNs("id") or args["更新时间NS"] then
        inNs = true
    end
    
    -- 判断byd谱面是否存在;先假定NS不会有beyond等级。
    local byd, nsByd
    if args['Beyond等级'] or data('bydRating') then byd = true else byd = false end
    if args['Beyond等级NS'] or dataNs('bydRating') then nsByd = true else nsByd = false end

    -- 处理谱师参数,未填写的等级谱师向前级复制(向下兼容)
    local getChartDesigner = {
        [1] = 
        {'mobile', 'PastNote编写', 'note编写', 'pstChartDesigner'},
        [2] = 
        {'mobile', 'PresentNote编写', 'note编写', 'prsChartDesigner'},
        [3] = 
        {'mobile', 'FutureNote编写', 'note编写', 'ftrChartDesigner'},
        [4] = 
        {'mobile', 'BeyondNote编写', 'note编写', 'bydChartDesigner'}}

    local chartDesigner = {}
    for i, k in ipairs(getChartDesigner)
        do
            if args[k[2]]
                then
                chartDesigner[i] = args[k[2]]
            else
                if args[k[3]]
                    then
                    chartDesigner[i] = args[k[3]]
                else
                    chartDesigner[i] = choice(link({'谱师', tostring(data(k[4]))}), data(k[4]), '', true)
                end
            end
    end

    -- 依据byd是否存在处理单元格合并参数。
    local span
    if byd then
        span = merge({chartDesigner[1], chartDesigner[2], chartDesigner[3], chartDesigner[4]})
    else
        span = merge({chartDesigner[1], chartDesigner[2], chartDesigner[3]})
    end

    -- if args['曲名'] == 'Tempestissimo' then spanNs = {'grid-column-start: span 1', 'grid-column-start: span 1', 'grid-column-start: span 1', 'grid-column-start: span 1'} end

    local imageA = getImage(data('id'), args['图片'], args['Past曲绘'], args['Present曲绘'], args['Future曲绘'], args['Beyond曲绘'], args['第二曲绘图片'], args['画师'], args['Beyond画师'], data('ftrJacketDesigner'), data('bydJacketDesigner'), byd, 'a', args['图片显示控制'])
    local imageB = getImage(data('id'), args['图片'], args['Past曲绘'], args['Present曲绘'], args['Future曲绘'], args['Beyond曲绘'], args['第二曲绘图片'], args['画师'], args['Beyond画师'], data('ftrJacketDesigner'), data('bydJacketDesigner'), byd, 'b', args['图片显示控制'])

    -- 生成Grid表格的信息表;在后文中依据格式进行生成。
    -- 

    local pstRating = args['Past等级'] or data('pstRating')
    local prsRating = args['Present等级'] or data('prsRating')
    local ftrRating = args['Future等级'] or data('ftrRating')
    local bydRating = args['Beyond等级'] or data('bydRating')

    local infoList = {
        { title(args['曲名'] or data('title') or dataNs('title'), args['第二曲名'], data('id') or dataNs('id') or #args > 1), { id = 'title'}},
        { imageA or choice('[[文件:Songs '..tostring(data('id')) .. '.jpg|点此上传图片]]', data('id'), '[[文件:Songs '..tostring(dataNs('id')) .. '.jpg|点此上传图片]]', dataNs('id')), { id = 'top-image'}},
        { imageB or choice('[[文件:Songs '..tostring(data('id')) .. '.jpg|点此上传图片]]', data('id'), '[[文件:Songs '..tostring(dataNs('id')) .. '.jpg|点此上传图片]]', dataNs('id')), { id = 'right-image'}},
        { '曲目信息', {class = 'header'} },
        { '曲师', { class = 'label'}},
        { args['编曲'] or choice(link({'曲师', tostring(data('artist'))}) or link({'曲师', tostring(dataNs('artist'))}), data('artist') or dataNs('artist'), '', true), { class = 'data'}},
        { '曲包', { class = 'label'}},
        { args['曲包'] or choice('[['..tostring(choice(getData.packName(data('set')), data('set'), getData.packName(dataNs('set')), dataNs('set')))..']]',data('set') or dataNs('set'),'',true), { class = 'data'}},
        { '时长', { class = 'label'}},
        { args['时长'] or '空', { class = 'data'}},
        { 'BPM', { class = 'label'}},
        { args['BPM'] or data('bpm') or dataNs('bpm'), { class = 'data'}},
        { '谱面信息', { class = 'header'}},
        { '难度', { class = 'label'}},
        { '[Past]', { class = 'pst-label'}},
        { '[Present]', { class = 'prs-label'}},
        { '[Future]', { class = 'ftr-label'}},
        { bydMark(byd, args['曲名'], inNs, nsByd), { class = 'byd-label'}},
        { '等级', { class = 'label'}},
        { pstRating, { class = 'pst-data'}},
        { prsRating, { class = 'prs-data'}},
        { ftrRating, { class = 'ftr-data'}},
        { bydRating, { class = 'byd-data'}},
        { 'note数量', { class = 'label'}},
        { args['PastNote'] or '空', { class = 'pst-data'}},
        { args['PresentNote'] or '空', { class = 'prs-data'}},
        { args['FutureNote'] or '空', {class = 'ftr-data'}},
        { args['BeyondNote'] or '空', { class = 'byd-data'}},
        { '谱面设计', { class = 'label'}},
        { chartDesigner[1] or '空', { class = 'pst-data'}, span[1]},
        { chartDesigner[2] or '空', { class = 'prs-data'}, span[2]},
        { chartDesigner[3] or '空', { class = 'ftr-data'}, span[3]},
        { chartDesigner[4] or '空', { class = 'byd-data'}, span[4]},
        { '谱面信息(Joy-Con)', { class = 'header ns', id = 'ns-play-header'}},
        { '难度', { class = 'label ns'}},
        { '[Past]', { class = 'pst-label ns'}},
        { '[Present]', { class = 'prs-label ns'}},
        { '[Future]', { class = 'ftr-label ns'}},
        { bydMark(byd, args['曲名'], inNs, nsByd), { class = 'byd-label ns'}},
        { 'note数量', { class = 'label ns'}},
        { args['PastNoteNS'] or args['PastNote'], { class = 'pst-data ns'}},
        { args['PresentNoteNS'] or args['PresentNote'], { class = 'prs-data ns'}},
        { args['FutureNoteNS'] or args['FutureNote'], { class = 'ftr-data ns'}},
        { args['BeyondNoteNS'] or '空', { class = 'byd-data ns'}},
        { '背景', { class = 'label'}},
        { bgCov(args['背景']) or bgCov(data('bg')) or bgCov(dataNs('bg')), { class = 'data' , id = 'bg-normal' }},
        { bgCov(args['Future背景']) , { class = 'data' , id = 'bg-ftr' }},
        { '更新时间', { id = 'update-data-label', class = 'label'}},
        { dateCov(args['更新版本'], args['更新时间'], labelText('移动版'), 'arg') or dateCov(data('version'), tonumber(data('date')), labelText('移动版'), 'data'), { id = 'mobile-date-normal', class = 'data'}},
        { dateCov(args['Beyond更新版本'], args['Beyond更新时间'], labelText('移动版'), 'arg') or '空', { id = 'mobile-date-byd', class = 'data'}},
        { dateCov(args['更新版本NS'], args['更新时间NS'], labelText('NS版'), 'arg') or dateCov(dataNs('version'), tonumber(dataNs('date')), labelText('NS版'), 'data'), { id = 'ns-date-normal', class = 'data ns'}},
        { '空', { id = 'ns-date-byd', class = 'data ns'}}
    }

    local box = mw.html.create('div')

    --样式控制
    box:addClass("arcaeabox")
    if byd then box:addClass("byd") end
    if dataNs('side') == 1 or args['侧'] == '1'
    	then
    	box:addClass("conflict") 
    	else if dataNs('side') == 2 or args['侧'] == '2' then box:addClass("colorless") else box:addClass("light") end
    end
    if args['更新版本NS'] or dataNs('id')
        then
        box:addClass("ns-box")
        if not(dataNs('pstChange') or dataNs('prsChange') or dataNs('ftrChange')) --(or byd) -- bdy单独分栏禁用
            then
            box:addClass("ns-same")
        end
    end
    if args['Beyond更新版本'] and args['Beyond更新时间'] then box:addClass('mobile-byd-date') end
    if nsByd then box:addClass('ns-byd-date') box:addClass('ns-date-span') end
    if not(args['更新版本'] or args['更新时间'] or data('id')) then box:addClass('no-mobile') end
    if args['Future背景'] then box:addClass('bg-ftr') end

    --分类添加
    local category = ''

    if args['更新版本NS'] or dataNs('id')
        then
        if args['更新版本'] or args['更新时间'] or data('id')
            then
            category = category..'[[分类:双平台曲目]]'
        else
            category = category..'[[分类:NS版曲目]]'
        end
    else
        category = category..'[[分类:移动版曲目]]'
    end

    if dataNs('pstChange') or dataNs('prsChange') or dataNs('ftrChange') then category = category..'[[分类:多模式谱面有差异的曲目]]' end

    if not(data('id') or dataNs('id') or #args > 1) then category = category..'[[分类:未登陆的曲目]]' end

    if (data('id') or dataNs('id')) and not(args['unset'])
        then
        
        -- 如果没有更新packlist,不自动进行分类。
        if choice(getData.packName(data('set')), data('set'), getData.packName(dataNs('set')), dataNs('set'))
            then
            category = category..'[[分类:'..tostring(choice(getData.packName(data('set')), data('set'), getData.packName(dataNs('set')), dataNs('set'))) .. '曲包曲目]]'
        end

        if data('side') == 1 or dataNs('side') == 1 or args['侧'] == '1'
            then
            category = category..'[[分类:纷争侧曲目]]'
        elseif data('side') == 0 or dataNs('side') == 0 or args['侧'] == '0'
        	then
            category = category..'[[分类:光芒侧曲目]]'
        else
        	category = category..'[[分类:消色侧曲目]]'
        end

		if pstRating then
        	category = category..'[[分类:PST'..pstRating .. '级曲目]]'
		end
    	if prsRating then
        	category = category..'[[分类:PRS'..prsRating .. '级曲目]]'
    	end
    	if ftrRating then
        	category = category..'[[分类:FTR'..ftrRating .. '级曲目]]'
        end
        if byd and bydRating then category = category..'[[分类:BYD'..bydRating .. '级曲目]]' end

        category = category..'[[分类:采用'..(args['背景'] or data('bg') or dataNs('bg')) .. '背景的曲目]]'

        if args['曲目版本'] then category = category..'[[分类:'..args['曲目版本']:gusb('v', 'V') .. '添加的曲目]]' end
        if args['曲目版本NS'] then category = category..'[[分类:NS '..args['曲目版本NS']:gusb('v', 'V') .. '添加的曲目]]' end
    end

    --表格生成
    local boxList = {}

    for _, v  in ipairs(infoList) do
        box:tag('div'):attr(v[2]):cssText(v[3]):wikitext(v[1]):done()
    end

    -- 提示信息
    local noticeText = ''

    if not(data('id') or dataNs('id'))
        then
        noticeText = noticeText..'<li>在Songlist中无法检查到此曲目。请尝试更新[[模板:Songlist]],检查页面名或填写正确的<code>曲名</code>参数。</li>'
    end

    if not(choice(getData.packName(data('set')), data('set'), getData.packName(dataNs('set')), dataNs('set')))
        then
        noticeText = noticeText..'<li>未更新Packlist。曲目信息模板无法进行曲包分类。</li>'
    end

    local noticeTextNote = ''

    if not(args['PastNote']) then noticeTextNote = noticeTextNote..'<code>PastNote</code>' end
    if not(args['PresentNote']) then noticeTextNote = noticeTextNote..'<code>PresentNote</code>' end
    if not(args['FutureNote']) then noticeTextNote = noticeTextNote..'<code>FutureNote</code>' end

    if noticeTextNote ~= ''
        then
        noticeText = noticeText..'<li>此模板缺少'..noticeTextNote..'参数,此参数无法自动读取。</li>'
    end

    if not(args['时长'])
        then
        noticeText = noticeText..'<li>此模板缺少<code>时长</code>参数,此参数无法自动读取。</li>'
    end

    if not(args['更新版本']) and data('id')
        then
        noticeText = noticeText..'<li>此模板的移动版更新版本是未完整状态。请添加<code>更新版本</code>与<code>更新时间</code>参数。</li>'
    end

    if not(args['更新版本NS']) and dataNs('id')
        then
        noticeText = noticeText..'<li>此模板的NS版更新版本是未完整状态。请添加<code>更新版本NS</code>与<code>更新时间NS</code>参数。</li>'
    end

    local notice
    if noticeText ~= ''
        then
        notice = mw.getCurrentFrame():expandTemplate {title = 'Collapse', args = { ['标题'] = '曲目信息模板提示信息', ['状态'] = '折叠',
            ['标题文字方向'] = 'center', ['标题颜色'] = '#dddddd',
            ['内容颜色'] = '#eeeeee', ['内容样式'] = 'width:800px',
        ['style'] = 'display:table;width:200px', ['class'] = 'nomobile',
        ['内容'] = noticeText}}

        category = category..'[[分类:缺少参数的曲目信息模板]]'
    end

    if args['nocat'] then category = '' end

    return tostring(box)..choice(notice, notice, '', true)..category
end

return p