Module:ChartConstant

来自Arcaea中文维基
Star0讨论 | 贡献2022年8月26日 (五) 11:48的版本

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

local p = {}
local chartConstantList = mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'ChartConstant.json' })
local songlist = mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist.json' })
local trans = mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Template:Transition.json' })

local function cvFunc()
    -- id到曲名的转换表
	local list = {}
	for i,k in pairs(songlist['songs'])
	do
		list[k['id']] = { 
			k['title_localized']['en'], 
			k["date"],
			nil,
			nil
		}
		if k["difficulties"][4] then
			if k["difficulties"][4]["title_localized"] then
				list[k['id']][3] = k["difficulties"][4]["title_localized"]["en"]
			end
			if k["difficulties"][4]["date"] then
				list[k['id']][4] = k["difficulties"][4]["date"]
			end
		end
	end
	return list
end


-- local specialSongNameConverter = {
-- 	["#1f1e33"] = "#1f1e33",
--     ["AI[UE]OON"] = "AI[UE]OON",
--     ["Last | Eternity"] = "Last",
--     ["Last | Moment"] = "Last"
-- }
-- local n = {
--     ["Last | Eternity"] = "Last \| Eternity",
--     ["Last | Moment"] = "Last \| Moment"
-- }
local getArgs = require('Module:Arguments').getArgs

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

function p._main(args)
	local firstList = {}
	local name, link
	local cvList = cvFunc()
	-- firstList架构:
	-- 以曲目(单难度)为单位,数字型数组
	-- {
	-- 	{ "" }
	-- }
    for i, k in pairs(chartConstantList) do

        -- 如果曲目定数存在,则使用Songlist查找曲名
    	if k[2] or k[3] or k[4] then
    		name = cvList[i][1]
        else
            name = i
		end
		
		-- 在转换表中查找页面名称
        if trans["songNameToDisplayName"][name] then
            link = trans["songNameToDisplayName"][name]
		else
			link = name
        end
		-- if n[name] then
		-- 	name = n[name]
		-- end
    	if k[2] and tonumber(k[2]['constant'])>=8 and not(k[2]['old'])
		then
    		table.insert(firstList, { i, name, k[2]['constant'], "PRS", link, cvList[i][2] })
    	elseif k[3] and tonumber(k[3]['constant'])>=8 and not(k[3]['old'])
		then
    		table.insert(firstList, { i, name, k[3]['constant'], "FTR", link, cvList[i][2] })
    	elseif k[4] and tonumber(k[4]['constant'])>=8 and not(k[4]['old'])
		then
    		if cvList[i][3]
			then
				name = cvList[i][3]
			end
			if cvList[i][4]
			then
				table.insert(firstList, { i, name, k[4]['constant'], "BYD", link, cvList[i][4] })
			else
    			table.insert(firstList, { i, name, k[4]['constant'], "BYD", link, cvList[i][2] })
			end
    	end
    end

	-- make lists
	local secondList = {}
    secondList[11], secondList[12] = {}, {}

    for s, k in pairs(firstList) do
        if not(secondList[k[3]]) then secondList[k[3]]={} end
        if k[3]>=11 
		then
            if k[3]>=12 then
                table.insert(secondList[12], k)
            else
                table.insert(secondList[11], k)
            end
        else 
			table.insert(secondList[k[3]], k) 
		end
    end


    -- sort songs
    -- local diffConverter = { PRS=2, FTR=3, BYD=4 }
    for s, k in pairs(secondList) do
        table.sort(k, function (a, b)
            if not(a[3]==b[3]) then
                return a[3]>b[3]
            end
			return a[6]<b[6]
			--[=[
            if not(diffConverter[a[4]]==diffConverter[b[4]]) then
                return diffConverter[a[4]]>diffConverter[b[4]]
            else return string.upper(a[2])<string.upper(b[2]) end
			]=]
        end)
    end

	local result = {}
    result[11], result[12] = {}, {}

	-- use template
	-- 组排单元 第一个参数是曲名,第二个是id,第三个是难度,link字面意思
    for s, k in pairs(secondList) do
    	if s<11 then
	    	if not(result[s]) then result[s]={} end
            for n, m in pairs(k) do
                table.insert(result[s],mw.getCurrentFrame():expandTemplate { title = '组排单元', args = { m[2], m[1], m[4], link=m[5] } } )
            end
	    else
	    	if s<12 then
	    		for n, m in pairs(k) do
                    table.insert(result[11],mw.getCurrentFrame():expandTemplate { title = '组排单元', args = { m[2], m[1], m[4], CC = string.format("%.1f", m[3]), link=m[5] } } )
                end
	    	else
	    		for n, m in pairs(k) do
                    table.insert(result[12],mw.getCurrentFrame():expandTemplate { title = '组排单元', args = { m[2], m[1], m[4], CC = string.format("%.1f", m[3]), link=m[5] } } )
                end
	    	end
	    end
    end


	-- sort constants
	local sortTable = {}
	for i,k in pairs(result) do
		table.insert(sortTable, {i, k})
	end
	table.sort(sortTable, function(a,b) return a[1] > b[1] end)


	-- add head and end
	local finText = "<div class='notaninfobox dstable'>\n"

	for i,k in ipairs(sortTable)
	do	
		if k[1] == 11 or k[1] == 12
		then
			finText = finText .. "<div class='ds'>"..k[1].."</div>\n"
			finText = finText .. "<div class='number'>"..mw.getCurrentFrame():expandTemplate { title = '组排列', args = { ['高度']="154px"  }  }
					..table.concat(k[2])..mw.getCurrentFrame():expandTemplate { title = '组排列-end' }.."</div>\n"
		else
			finText = finText .. "<div class='ds'>"..string.format("%.1f", k[1]).."</div>\n"
			finText = finText .. "<div class='number'>"..mw.getCurrentFrame():expandTemplate { title = '组排列' }
					..table.concat(k[2])..mw.getCurrentFrame():expandTemplate { title = '组排列-end' }.."</div>\n"
		end
	end
	
	finText = finText .. "</div>"


    return finText
end

return p