Module:ChartConstant

来自Arcaea中文维基
盐棋讨论 | 贡献2024年2月24日 (六) 16:26的版本 (微调)

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

local mad = require 'Module:AnotherData'
local frame = mw.getCurrentFrame()
local p = {}

local function songMap()
	local res = {}
	for _, song in ipairs(mad.listOf 'songs') do res[song.id] = song end
	-- ns待补充
	return res
end

local function itData(datas, cc)
	cc = cc or 13
	local step, format = unpack(cc > 11 and {1, '%d'} or {.1, '%.1f'})
	cc = cc - step
	local id = format:format(cc)
	local data = datas[id]
	if not data then return nil end
	return cc, id, data
end
function p.main()
	local mSong = songMap()
	local datas = {}
	-- CD: constant data
	for id, songCD in pairs(mw.text.jsonDecode(mw.title.new('ChartConstant.json', 'Template'):getContent())) do
		local song = mSong[id]
		local query = mad.songQueryWrap(song)
		local title, date = query.title, song.date
		local link = query.linkName or title
		for diffIdx = 2, 4 do
			-- fk lua donot support continue. use single-loop & break for subst
			repeat
				local diffCD = songCD[diffIdx] or {}
				if diffCD.old ~= false then break end
				local cc = diffCD.constant
				if cc < 8 then break end
				if diffIdx == 4 then
					title = query.bydTitle or title
					date = query.bydDate or date
				end
				local rowId, diasplayCC, rowAttr
				if cc >= 11 then
					rowId = ('%d'):format(cc)
					diasplayCC = ('%.1f'):format(cc)
					rowAttr = {['高度'] = '154px'}
				else
					rowId = ('%.1f'):format(cc)
				end
				local rowData = datas[rowId]
				if not rowData then
					rowData = {attr = rowAttr}
					datas[rowId] = rowData
				end
				table.insert(rowData, {
					data = {title, song.id, ({'PST', 'PRS', 'FTR', 'BYD'})[diffIdx], link = link, CC = diasplayCC},
					sort = {cc, date}
				})
			until (true)
		end
	end

	local text = mw.html.create 'div':addClass 'notaninfobox dstable'
	for _, rowId, rowData in itData, datas do
		text = text
			:tag 'div':addClass 'ds':wikitext(rowId):done()
			:tag 'div':addClass 'number'
			:wikitext(frame:expandTemplate {title = '组排列', args = rowData.attr})
		table.sort(rowData, function(a, b)
			a, b = a.sort, b.sort
			if a[1] == b[1] then return a[2] < b[2] end
			return a[1] > b[1]
		end)
		for _, rowItem in ipairs(rowData) do
			text:wikitext(frame:expandTemplate {title = '组排单元', args = rowItem.data})
		end
		text = text:wikitext(frame:expandTemplate {title = '组排列-end'}):done()
	end

	return tostring(text)
end

local function constantFormat(a)
	if not a then return ' ' end
	local sCC = ('%.1f'):format(a.constant)
	return a.old and tostring(mw.html.create 'span':addClass 'old-constant':wikitext(sCC)) or sCC
end
function p.detail()
	local mSong = songMap()
	local datas = {}
	for id, songCD in pairs(mw.text.jsonDecode(mw.title.new('ChartConstant.json', 'Template'):getContent())) do
		local song = mSong[id]
		local query = mad.songQueryWrap(song)
		local data = {query.linkTitle}
		local rowData = {data = data, sort = {(songCD[query.bydDate and 3 or 4] or songCD[3]).constant, song.date}}
		table.insert(datas, rowData)
		for i = 1, 4 do
			table.insert(data, constantFormat(songCD[i]))
		end
	end

	table.sort(datas, function(a, b)
		a, b = a.sort, b.sort
		if a[1] == b[1] then return a[2] < b[2] end
		return a[1] > b[1]
	end)

	local texts = {}
	local template = '|-\n|[[%s]]' .. ('||%s'):rep(4)
	for _, value in ipairs(datas) do
		table.insert(texts, template:format(unpack(value.data)))
	end

	return table.concat(texts, '\n')
end

return p