Module:PackSong

来自Arcaea中文维基

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

local p = {}
local mad = require 'Module:AnotherData'

local function collectSong(packId, plat)
	local songList = {}
	for _, song in ipairs(mad.listOf('songs', plat)) do
		if song.set == packId then
			local ftrDiff = song.difficulties[3] or nil
			local level = ftrDiff.rating or 0
			local plus = ftrDiff.ratingPlus and 1 or 0
			local levelCate= level*2 + plus  -- 7 -> 14 ;7+ -> 15 ...
			if not songList[levelCate] then songList[levelCate]={} end
			if songList[levelCate][mad.title(song)] then	--如出现重名则在key后添加songid
				songList[levelCate][mad.title(song)..song.id] = '[[' .. mad.linkTitle(song) .. ']]'
			else	-- 未重名则
				songList[levelCate][mad.title(song)] = '[[' .. mad.linkTitle(song) .. ']]'
			end
		end
	end
	return songList
end

local function collectSongInfo(packId, plat)
	local infoList = {}
	local transId = mw.loadJsonData('Template:Transition.json').multiId
	for _, song in ipairs(mad.listOf('songs', plat)) do
		if song.set == packId then
			local ftrDiff = song.difficulties[3] or nil
			local level = ftrDiff.rating or 0
			local plus = ftrDiff.ratingPlus and 1 or 0
			local levelCate= level*2 + plus  -- 7 -> 14 ;7+ -> 15 ...
			local id = (plat == 'ns') and transId[song.id] or song.id
			infoList[id] = {}
			infoList[id] = {
				['link']=mad.linkTitle(song),
				['name']=mad.title(song),
				['level']=levelCate
			}
		end
	end
	return infoList
end

local function collectSongList(infoList)
	local songList = {}
	local frame = mw.getCurrentFrame()
	local function onlyMobile(text)
		local om = frame:expandTemplate{title = '仅', args = {'移动版', text, '图片角标'}}
		return om
	end
	local function onlyNS(text)
		return frame:expandTemplate{title = '仅', args = {'NS版', text, '图片角标'}} 
	end
	
	for id,info in pairs(infoList) do
		local level = info.level
		local link = '[[' .. info.link .. ']]'
		local name = info.name
		if not songList[level] then songList[level]={} end
		if info.onlyNS then link=onlyNS(link) end
		if info.onlyMobile then link=onlyMobile(link) end
		if songList[level][name] then	--如出现重名则在key后添加songid
			songList[level][name..id] = link
		else	-- 未重名则
			songList[level][name] = link
		end
	end
	return songList
end

local function generateLine(songList)
	local line = ''
	for level = 14,22 do -- 7 ~ 11
		if songList[level] then
			local a2z = {}
			for name in pairs(songList[level]) do table.insert(a2z,name) end
			table.sort(a2z, function(a, b) return string.lower(a) < string.lower(b) end)
			for _,name in ipairs(a2z) do
				line = line .. '\n*'..songList[level][name]
			end
		end
	end
	return line
end
	

local function singlePlat(packId, plat)

	local songList = collectSong(packId, plat)
	-- 等同:
	-- local infoList = collectSongInfo(packId, plat)
	-- local songList = collectSongList(infoList)
	
	local line = generateLine(songList)
	return line
end

local function multiPlat(packId)
	-- packId: base single
	local transId = mw.loadJsonData 'Template:Transition.json'.multiId
	
	local infoListNS = collectSongInfo(packId, 'ns')
	local infoListMO = collectSongInfo(packId, 'mobile')
	
	local infoList = {}
	for id,info in pairs(infoListMO) do
		infoList[id] = info
		if infoListNS[id] then
			infoListNS[id] = nil
		else
			infoList[id]['onlyMobile'] = 1
		end
	end
	for id,info in pairs(infoListNS) do
		infoList[id] = info
		infoList[id]['onlyNS'] = 1
	end
	
	local songList = collectSongList(infoList)
	
	local line = generateLine(songList)
	return line
end
	
function p.single(frame)
	if frame.args and frame.args['packId'] then
		local id = frame.args['packId']
		local plat = frame.args['plat'] or 'mobile'
		return singlePlat(id, plat)
	else
		return 'No args: packId input!'
	end
end

function p.multi(frame)
	if frame.args and frame.args['packId'] then
		local id = frame.args['packId']
		return multiPlat(id)
	else
		return 'No args: packId input!'
	end
end

return p