Module:Songtable

来自Rotaeno中文维基

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

local p = {}

local data = require("模块:Rotaeno Data");
local yesno = require("Module:Yesno");
local getArgs = require('Module:Arguments').getArgs;

function toInteger(num)
    local a, b = math.modf(num)
    if b > 0.6 and num >= 10
    then
    	return tostring(a).."+"
    else
    	return tostring(a)
    end
end

function packName(name)
    if string.find(name, "%[")
    then
        return name
    else
        return "[["..name.."]]"
    end
end

function createTable(tab,list)
	local max = #list;
	local cur = 1;
	local col = 1;
	local curCell = tab:tag("td"):wikitext(list[1]);
	for i=2, max do
		if list[i] == list[i-1] then
			col = col + 1;
			curCell:attr("colspan", tostring(col))
		else
			curCell = tab:tag("td"):wikitext(list[i]);
			col = 1;
		end
	end
end

function nonI18n(txt)
	return '-{' .. txt .. '}-'
end

function makeInvokeFunc(funcName)
    return function(frame)
        local args = getArgs(frame)
        return p[funcName](args)
    end
end

p.main = makeInvokeFunc('_main')

function p._main(args)
	local frame = mw.getCurrentFrame();
    --生成HTML的主函数

    -- 从参数或者页面名称读取曲目名称
    local songName 
    if args['曲名'] then
        songName = args['曲名']
    else
        songName = mw.text.decode(frame:preprocess("{{FULLPAGENAME}}"))
    end

    -- 生成查找函数。
    local query
    if args['id'] then
        query = data.singleSongInformation(args['id'], "id")
    else
        query = data.singleSongInformation(songName, "name")
    end

	local Qtitle = args["曲名"] or query("title");
	if not(Qtitle)then error('Songlist中无法找到该歌曲信息,请检查Songlist模板是否更新,若更新后仍出现该错误请尝试手动填写id字段。如上述方案均无法解决请联系管理员进行处理。');end -- 防止不明不白的报错
	
	-- DISPLAYTITLE
	if not yesno(args["disableAutoLock"],false) then
		frame:callParserFunction('DISPLAYTITLE', Qtitle, 'noreplace');
	end
	
    -- 如果没有来源一栏的话,图片合并行的数量减少
    local imageRowspan;
    if query("source") then imageRowspan = 13 else imageRowspan = 12 end
	
	-- 如果有难度alpha,难度合并列数量增加
	local alpha = query("ratingA");
	local diffColspan = alpha and 5 or 4	-- if alpha then 5 else 4 end
	
    -- 分类表
    local cat = {}

    -- 拼html,因为内容比较简单,就不像arcaeawiki一样写数据table了。
    local box = mw.html.create("table"):addClass("rotable")
	
	-- 曲名
	box:tag("tr"):tag("th"):wikitext(nonI18n(Qtitle)):attr("colspan", diffColspan + 2);

	if args["曲名语言表"]
	then
	    box:tag("tr"):tag("th"):wikitext(nonI18n(args["曲名语言表"])):attr("colspan", diffColspan + 2)
	elseif query("title-list")
	then
	    box:tag("tr"):tag("th"):wikitext(nonI18n(query("title-list"))):attr("colspan", diffColspan + 2)
	end
    
    if args["id"]
    then
	    box:tag("tr"):addClass("rotable-title"):tag("th"):wikitext("曲目信息"):attr("colspan", diffColspan + 1)
	        :done():tag("td"):wikitext("[[File:Songs_"..args["id"]..".png|256px]]"):attr("rowspan", imageRowspan)
	        :css("height", "256px"):css("width", "256px")
    elseif query("id")
    then
	    box:tag("tr"):addClass("rotable-title"):tag("th"):wikitext("曲目信息"):attr("colspan", diffColspan + 1)
	        :done():tag("td"):wikitext("[[File:Songs_"..query("id")..".png|256px]]"):attr("rowspan", imageRowspan)
	        :css("height", "256px"):css("width", "256px")
    else
	    box:tag("tr"):addClass("rotable-title"):tag("th"):wikitext("曲目信息"):attr("colspan", diffColspan + 1)
	        :done():tag("td"):wikitext("未指定图片"):attr("rowspan", imageRowspan)
	        :css("height", "256px"):css("width", "256px")
	end

    if args["曲包"]
    then
        box:tag("tr"):tag("td"):wikitext("曲包"):done():tag("td"):wikitext(packName(args["曲包"])):attr("colspan", diffColspan)
        table.insert(cat, "[[分类:"..args["曲包"].."曲包曲目]]")
    else
        box:tag("tr"):tag("td"):wikitext("曲包"):done():tag("td"):attr("colspan", diffColspan)
    end


    if args["曲师"]
    then
        box:tag("tr"):tag("td"):wikitext("曲师"):done():tag("td"):wikitext(nonI18n(args["曲师"])):attr("colspan", diffColspan)
    else
        box:tag("tr"):tag("td"):wikitext("曲师"):done():tag("td"):wikitext(nonI18n(query("artist"))):attr("colspan", diffColspan)
    end

    if args["画师"]
    then
        box:tag("tr"):tag("td"):wikitext("画师"):done():tag("td"):wikitext(nonI18n(args["画师"])):attr("colspan", diffColspan)
    else
        box:tag("tr"):tag("td"):wikitext("画师"):done():tag("td"):wikitext(nonI18n(query("JacketDesigner"))):attr("colspan", diffColspan)
    end
    
    -- 来源有可能没有
    if args["来源"] or query("source")
    then
        if args["来源"] 
        then
            box:tag("tr"):tag("td"):wikitext("来源"):done():tag("td"):wikitext(args["来源"]):attr("colspan", diffColspan)
        else
            box:tag("tr"):tag("td"):wikitext("来源"):done():tag("td"):wikitext(query("source")):attr("colspan", diffColspan)
        end
    end
    
    box:tag("tr"):tag("td"):wikitext("时长"):done():tag("td"):wikitext(args["时长"]):attr("colspan", diffColspan)
    if not args["时长"] then
    	table.insert(cat, "[[Category:需要补充时长的曲目]]")
    end
    
    box:tag("tr"):addClass("rotable-title"):tag("th"):wikitext("谱面信息"):attr("colspan", diffColspan + 1)
    
    local tag = box:tag("tr"):tag("td"):wikitext("难度"):done()
                :tag("td"):wikitext("难度I"):css("color", "#43a047"):done()
                :tag("td"):wikitext("难度Ⅱ"):css("color", "#1565c0"):done()
                :tag("td"):wikitext("难度Ⅲ"):css("color", "#e65100"):done()
                :tag("td"):wikitext("难度Ⅳ"):css("color", "#ab47bc"):done()
    if alpha then tag:tag("td"):wikitext("难度Ⅳ-α"):css("color", "#ca8bd5"):done() end

    if args["谱师"] then
        box:tag("tr"):tag("td"):wikitext("谱师"):done():tag("td"):wikitext(args["谱师"]):attr("colspan", diffColspan)
    elseif args["难度I谱师"] and args["难度II谱师"] and args["难度III谱师"] and args["难度IV谱师"] and (not alpha or args["难度IV_Alpha谱师"]) then
    	tag = box:tag("tr"):tag("td"):wikitext("谱师"):done()
    			:tag("td"):wikitext(args["难度I谱师"]):done()
    			:tag("td"):wikitext(args["难度II谱师"]):done()
    			:tag("td"):wikitext(args["难度III谱师"]):done()
    			:tag("td"):wikitext(args["难度IV谱师"]):done()
    	if alpha then tag:tag("td"):wikitext(args["难度IV_Alpha谱师"]):done() end
    else
    	local ChartDesignerTable = {query("ChartDesigner1"),query("ChartDesigner2"),query("ChartDesigner3"),query("ChartDesigner4"),}
    	if alpha then table.insert(ChartDesignerTable, query("ChartDesignerA")) end
    	createTable(
    		box:tag("tr"):tag("td"):wikitext("谱师"):done(), ChartDesignerTable
    	)
    end

    if args["难度I等级"] and args["难度I等级"] and args["难度III等级"] and args["难度IV等级"] and (not alpha or args["难度IV_Alpha等级"])
    then
        tag = box:tag("tr"):tag("td"):wikitext("等级"):done()
                    -- 删除小数
                    :tag("td"):wikitext(args["难度I等级"]):done()
                    :tag("td"):wikitext(args["难度II等级"]):done()
                    :tag("td"):wikitext(args["难度III等级"]):done()
                    :tag("td"):wikitext(args["难度IV等级"]):done()
        if alpha then tag:tag("td"):wikitext(args["难度IV_Alpha等级"]):done() end
        
        table.insert(cat, "[[分类:难度I中"..args["难度I等级"].."级曲目]]")
        table.insert(cat, "[[分类:难度II中"..args["难度II等级"].."级曲目]]")
        table.insert(cat, "[[分类:难度III中"..args["难度III等级"].."级曲目]]")
        table.insert(cat, "[[分类:难度IV中"..args["难度IV等级"].."级曲目]]")
        if alpha then table.insert(cat, "[[分类:难度IV-α中"..args["难度IV_Alpha等级"].."级曲目]]") end
    else
    	if query("rating1")
    	then
	        tag = box:tag("tr"):tag("td"):wikitext("等级"):done()
	                    -- 删除小数
	                    :tag("td"):wikitext(toInteger(query("rating1"))):done()
	                    :tag("td"):wikitext(toInteger(query("rating2"))):done()
	                    :tag("td"):wikitext(toInteger(query("rating3"))):done()
	                    :tag("td"):wikitext(toInteger(query("rating4"))):done()
	        if alpha then tag:tag("td"):wikitext(toInteger(query("ratingA"))):done() end
	               
	        if query("title")
	        then
	            table.insert(cat, "[[分类:难度I中"..toInteger(query("rating1")).."级曲目]]")
	            table.insert(cat, "[[分类:难度II中"..toInteger(query("rating2")).."级曲目]]")
	            table.insert(cat, "[[分类:难度III中"..toInteger(query("rating3")).."级曲目]]")
	            table.insert(cat, "[[分类:难度IV中"..toInteger(query("rating4")).."级曲目]]")
	            if alpha then table.insert(cat, "[[分类:难度IV-α中"..toInteger(query("ratingA")).."级曲目]]") end
	        end
        end
    end

    tag = box:tag("tr"):tag("td"):wikitext("Note数量"):done()
    		:tag("td"):wikitext(args["难度I物量"]):done()
    		:tag("td"):wikitext(args["难度II物量"]):done()
    		:tag("td"):wikitext(args["难度III物量"]):done()
    		:tag("td"):wikitext(args["难度IV物量"]):done()
    if alpha then tag:tag("td"):wikitext(args["难度IV_Alpha物量"]):done() end


    box:tag("tr"):tag("td"):wikitext("更新时间"):done():tag("td"):wikitext(args["更新时间"]):attr("colspan", diffColspan)

    if args["更新时间"] then table.insert(cat, "[[分类:时间"..args["更新时间"].."更新的曲目]]") end
    
    box:tag("tr"):tag("td"):wikitext("更新版本"):done():tag("td"):wikitext(args["更新版本"]):attr("colspan", diffColspan)

    if args["更新版本"] then table.insert(cat, "[[分类:版本"..args["更新版本"].."更新的曲目]]") end

    return tostring(box)..(frame:preprocess("{{NAMESPACENUMBER}}") == '0' and table.concat(cat) or '')
end

return p