Module:SongCollection

来自Arcaea中文维基

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

local mad = require 'Module:AnotherData'
local lang = mw.language.getContentLanguage()
local p = {}

local dates = {}
local function verQuery(sec)
	local date = tonumber(lang:formatDate('ymd', '@' .. sec)) - 1
	if date > dates[#dates][1] then return '@' end
	local l, r = 1, #dates
	while l < r do
		local m = math.floor((l + r) / 2)
		if dates[m][1] < date then
			l = m + 1
		else
			r = m
		end
	end
	return dates[l][2]
end

local function otherTitles(song)
	local res = {}
	local titleJa = song.title_localized.ja
	if titleJa then table.insert(res, ('<span lang="ja">-{%s}-</span>'):format(titleJa)) end
	table.insert(res, song.title_localized['zh-Hans'])
	for _, diff in ipairs(song.difficulties) do
		if diff.title_localized then
			table.insert(res, diff.title_localized.en .. ' ' .. mad.color {
				rc = diff.ratingClass,
				txt = ('[%s]'):format(({[3] = 'Beyond'})[diff.ratingClass])
			})
		end
	end
	return res
end

local sectionCode = {unknown = 0, free = 1, archive = 2, mainstory2 = 3, mainstory = 4, sidestory = 5, collab = 6, single = 7}
local classMap = {[0] = 'pst', 'prs', 'ftr', 'byd', 'etr'}
local function view(plat)
	local res, idOrder = {}, {}
	local durs = mw.text.jsonDecode(mw.title.new('Song Length.json', 'Template'):getContent()).normal
	for _, song in ipairs(mad.listOf('songs', plat)) do
		if not song.deleted then
		local packItem = mad.packQueryWrap(song.set, plat)
		local resItem = {
			['封面'] = song.id,
			['标题'] = table.concat({'[[' .. mad.linkTitle(song) .. ']]', unpack(otherTitles(song))}, '<br>'),
			['作曲家'] = mw.ustring.gsub(song.artist, '[一-龠ぁ-ゔァ-ヴー々〆〤ヶ]+', function(v) return ('<span lang="ja">-{%s}-</span>'):format(v) end),
			['所属曲包'] = packItem['name'],
			pack_sort = ('%d%02d'):format(sectionCode[packItem['section']] or 0, packItem['numero']),-- or 0以后可以去掉,如果数据没有问题的话
			['BPM'] = song.bpm,
			bpm_base = tostring(song.bpm_base),
			['时长'] = durs[song.id] or '@',
		}
		res[song.id] = resItem
		table.insert(idOrder, song.id)
		if plat ~= 'ns' then
			if #dates <= 0 then
				local vt = mw.text.jsonDecode(mw.title.new('VersionTime.json', 'Template'):getContent())
				for _, vtItem in pairs(vt) do
					for index, value in ipairs(vtItem.time) do
						table.insert(dates, {value, vtItem.ver[index]})
					end
				end
				table.sort(dates, function(a, b) return a[1] < b[1] end)
			end
			resItem.date_sort = song.date - 1483228800
			resItem.version = song.version
			resItem['收录版本'] = verQuery(song.date)
		end
		for i = 1, 4 do
			local diff = song.difficulties[i]
			resItem['难度' .. i] = (not diff) and ' ' or ('<span class="text-%s">%s</span>'):format(classMap[diff.ratingClass],
				diff.ratingPlus and ('data-sort-value=%s.7|%s+'):format(diff.rating, diff.rating) or diff.rating
			)
		end
		-- data:insert((tonumber(song.bpm) ~= bpm and ('data-sort-value=%s|'):format(bpm) or '') .. song.bpm)
		end
	end
	return res, idOrder
end
local function override(v, args)
	for key, value in pairs(args) do
		if type(key) == 'string' then
			local id, tar = key:match '([a-z]+).(.*)'
			if v[id] then
				v[id][tar] = value
			end
		end
	end
end
local function tmpl(v, order)
	local res = mw.html.create()
	for _, o in ipairs(order) do
		local row = v[o]
		res:wikitext('\n|-\n|[[文件:Songs ', row['封面'], '.jpg|75px]]||', row['标题'], '||', row['作曲家'],
			'||data-sort-value=', row.pack_sort, '|', row['所属曲包'],
			'||data-sort-value=', row.bpm_base, '|', row['BPM'], '||', row['时长'])
		if row['收录版本'] then
			res:wikitext('||data-sort-value=', row.date_sort, '|', row.version, '.', row['收录版本'])
		end
		for i = 1, 4 do
			res:wikitext('||', row['难度' .. i])
		end
	end
	return tostring(res)
end

function p.mobile(frame)
	local v, o = view 'mobile'
	override(v, frame:getParent().args)
	return tmpl(v, o)
end
function p.ns(frame)
	local v, o = view 'ns'
	override(v, frame:getParent().args)
	return tmpl(v, o)
end

return p