Module:ChartConstant:修订间差异

来自Arcaea中文维基
(sb ipairs, sb lasteternity)
(use new data mod)
第1行: 第1行:
local mad = require 'Module:AnotherData'
local frame = mw.getCurrentFrame()
local p = {}
local p = {}
local null = require('Module:Nullish')
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 = 'Transition.json' })


local function cv()
local function itData(datas, cc)
local list = {}
cc = cc or 13
for i, k in pairs(songlist['songs']) do
local step, format = unpack(cc > 11 and {1, '%d'} or {.1, '%.1f'})
list[k['id']] = {
cc = cc - step
k['title_localized']['en'], k["date"],
local id = format:format(cc)
((k["difficulties"][4] / null)["title_localized"] / null)["en"],
local data = datas[id]
(k["difficulties"][4] / null)["date"]
if not data then return nil end
}
return cc, id, data
end
return list
end
end


function p.main()
function p.main()
local cvList = cv()
local mSong = {}
for _, song in ipairs(mad.listOf 'songs') do
mSong[song.id] = song
end
-- ns待补充


for i = 1, 3 do chartConstantList['lasteternity'][i] = { constant = 0 } end
local datas = {}
local mid_result = {}
-- CD: constant data
-- song key/value : str/array(table)
for id, songCD in pairs(mw.text.jsonDecode(mw.title.new('ChartConstant.json', 'Template'):getContent())) do
for skey, sval in pairs(chartConstantList) do
local song = mSong[id]
local name, timestamp = cvList[skey][1], cvList[skey][2]
local query = mad.songQueryWrap(song)
local link = trans["songNameToDisplayName"][name] or (trans["sameName"][name] / null)[skey] or name
local title, date = query.title, song.date
-- chart index/value : int1~4/{constant=float,old=bool}
local link = query.linkName or title
for cid, cval in ipairs(sval) do
for diffIdx = 2, 4 do
if tonumber(cval.constant) >= 8 and not cval.old then
-- fk lua donot support continue. use single-loop & break for subst
if cid == 4 then
repeat
if cvList[skey][3] then name = cvList[skey][3] end
local diffCD = songCD[diffIdx] or {}
if cvList[skey][4] then timestamp = cvList[skey][4] end
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
end
-- 每排前面的数字
local rowData = datas[rowId] or {attr = rowAttr}
local rowtag = cval.constant >= 11 and math.floor(cval.constant) or cval.constant
datas[rowId] = rowData
mid_result[rowtag] = mid_result[rowtag] or {}
table.insert(rowData, {
table.insert(mid_result[rowtag], {
data = {title, song.id, ({'PST', 'PRS', 'FTR', 'BYD'})[diffIdx], link = link, CC = diasplayCC},
skey, name, cval.constant,
sort = {cc, date}
({"PST", "PRS", "FTR", "BYD"})[cid], link, timestamp
})
})
end
until (true)
end
end
end
end


-- {{组排单元|曲名|id|难度|link=页面链接|CC=定数}}
local text = mw.html.create 'div':addClass 'notaninfobox dstable'
local result = {}
for _, rowId, rowData in itData, datas do
-- row key/value : float/array({str,str,float,str,str,time})
text = text
for rkey, rval in pairs(mid_result) do
:tag 'div':addClass 'ds':wikitext(rowId):done()
-- 组内:时间升序(<11) 、定数降序(>=11)
:tag 'div':addClass 'number'
table.sort(rval, function(a, b)
:wikitext(frame:expandTemplate {title = '组排列', args = rowData.attr})
if a[3] == b[3] then
table.sort(rowData, function(a, b)
return a[6] < b[6]
a, b = a.sort, b.sort
else
if a[1] == b[1] then return a[2] < b[2] end
return a[3] > b[3]
return a[1] > b[1]
end
end)
end)
local units = { ds = rkey }
for _, rowItem in ipairs(rowData) do
-- chart discription
text:wikitext(frame:expandTemplate {title = '组排单元', args = rowItem.data})
for _, cdisc in ipairs(rval) do
table.insert(units, mw.getCurrentFrame():expandTemplate{
title = '组排单元',
args = {
cdisc[2], cdisc[1], cdisc[4], link = cdisc[5],
CC = rkey >= 11 and string.format("%.1f", cdisc[3]) or nil
}
})
end
end
table.insert(result, units)
text = text:wikitext(frame:expandTemplate {title = '组排列-end'}):done()
end
end
-- 组间排序
table.sort(result, function(a, b) return a.ds > b.ds end)


-- final text
return tostring(text)
local finText = "<div class='notaninfobox dstable'>\n"
for _, units in ipairs(result) do
finText = finText .. table.concat{
"<div class='ds'>",
units.ds >= 11 and units.ds or string.format("%.1f", units.ds),
"</div>\n",
"<div class='number'>",
mw.getCurrentFrame():expandTemplate{ title = '组排列', args = units.ds >= 11 and {['高度'] = "154px"} or nil },
table.concat(units),
mw.getCurrentFrame():expandTemplate{ title = '组排列-end' },
"</div>\n"
}
end
finText = finText .. "</div>"
return finText
end
end


return p
return p

2024年2月24日 (六) 12:54的版本

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

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

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 = {}
	for _, song in ipairs(mad.listOf 'songs') do
		mSong[song.id] = song
	end
	-- ns待补充

	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] or {attr = rowAttr}
				datas[rowId] = rowData
				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

return p