Module:ChartConstant

DrLee lihr讨论 | 贡献2022年9月2日 (五) 23:21的版本 (重写了重写了 vsc里一顿写之后预览一遍过给我自己都吓到了)

可在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 cv()
	local list = {}
	for i,k in pairs(songlist['songs']) do
		list[k['id']] = {
			k['title_localized']['en'],
			k["date"],
			nil,
			nil
		}
		local byd = k["difficulties"][4]
		if byd then
			if byd["title_localized"] then
				list[k['id']][3] = byd["title_localized"]["en"]
			end
			if byd["date"] then
				list[k['id']][4] = byd["date"]
			end
		end
	end
	return list
end


function p.main()


	local name, link, timestamp, index
	local cvList = cv()

    local result = {}
    local mid_result = {}
	
	local get_table_index = function(i)
		if i>=11 then
			return math.floor(i)
		else return i end
	end

    for i, k in pairs(chartConstantList) do

		name = cvList[i][1]
		link = name
		timestamp = cvList[i][2]

        if trans["songNameToDisplayName"][name] then
            link = trans["songNameToDisplayName"][name]
        end
        if trans["sameName"][name] then
	    	link = trans["sameName"][name][i]
	    end
		if cvList[i][4] then
			timestamp = cvList[i][4]
		end

		for m, q in ipairs({ 2, 3, 4 }) do
			if k[q] and tonumber(k[q]["constant"])>=8 and not k[q]["old"] then

				index = get_table_index(k[q]["constant"])
        		if not mid_result[index] then mid_result[index] = {} end

				table.insert(mid_result[index], { i, name, k[q]["constant"], ({"","PRS","FTR","BYD"})[q], link, timestamp })

			end
		end
    end

	-- use template
	-- 组排单元 第一个参数是曲名,第二个是id,第三个是难度,link字面意思
    for s, k in pairs(mid_result) do
		if not(result[s]) then result[s] = {} end
		index = get_table_index(k[1][3])

		table.sort(k,
			function (a, b)
				if a[3]~=b[3] then
					return a[3]>b[3]
				end
				return a[6]<b[6]
			end)

		for n, m in ipairs(k) do
			local arguments = { m[2], m[1], m[4], link=m[5] }
			if index >= 11 then arguments.CC = string.format("%.1f", m[3]) end
			table.insert(result[index], mw.getCurrentFrame():expandTemplate { title = '组排单元', args = arguments })
		end
    end


	-- sort constants
	local sortedTable = {}
	for i,k in pairs(result) do
		table.insert(sortedTable, {i, k})
	end
	table.sort(sortedTable, 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(sortedTable) do
		if k[1] == 11 or k[1] == 12 then
			finText = finText 
					.. "<div class='ds'>" .. k[1] .. "</div>\n"
					.. "<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"
					.. "<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