Module:SongUnlock

来自Rotaeno中文维基

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

local p = {}

local data = require("Module:Rotaeno Data")
local getArgs = require('Module:Arguments').getArgs

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

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

p.main = makeInvokeFunc('_main')
function p._main(args)-- 生成HTML的主函数 部分代码来源:[[Module:Songtable]]

    local function doArgExist(a) -- 必须放在这,免得出问题
        local arg = args[a]
        return type(arg) == "string" and arg ~= "";
    end

    local function getResult(func,tag)
        if type(func) == "function" then return func(tag);end
        return tag:wikitext(tostring(func))
    end

    local function getCondition(lvl)
        local needRa;
        if type(lvl) == "nil" then needRa = "[音律值要求]"
        elseif lvl > 12.9 then needRa = 12;   -- 13
        elseif lvl > 12.6 then needRa = 11.7; -- 12+
        elseif lvl > 11.9 then needRa = 11.0; -- 12
        elseif lvl > 11.6 then needRa = 10.7; -- 11+
        else                   needRa = 10;   -- 11~8
        end
        return '<span style="color:#e65100">难度Ⅲ</span> 评级达到 S 或 [[音律值]] 达到 ' .. tostring(needRa);
    end

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

    -- 如果是从页面名读取的,那可能因为mediawiki限制导致页面名和曲名不符。如果有这个转换规则的话,使用转换JSON进行转换。
    local covList = mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Transition.json' })["displayNameToSongName"]
    if covList[songName] then songName = covList[songName] end

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

    local DefaultValue;
    local isDefault = {
        not doArgExist("难度I解禁方法"),
        not doArgExist("难度II解禁方法"),
        not doArgExist("难度III解禁方法"),
        not doArgExist("难度IV解禁方法"),
        type(args["disableAuto"]) == "nil", -- 用于判断应不应该初始化难度IV解禁方法的文本
    }
    local unlockCondition;
    

    if args["曲目解禁方法"] then -- 老版本/自定义
        unlockCondition = args["曲目解禁方法"];
    elseif args["需求旅行者徽章"] then -- 
        unlockCondition = "使用" .. args["需求旅行者徽章"] .. "个旅行者徽章解锁"
    elseif args["需求等级"] then -- 
        unlockCondition = "玩家等级达到 " .. args["需求等级"]
    elseif args["旅程模式"] then -- 
        unlockCondition = "于旅程模式 星球“" .. args["旅程模式"] .. "”内解锁"
    elseif args["章节挑战"] then -- 
        unlockCondition = "于旅程模式 星球“" .. args["章节挑战"] .. "”内解锁章节挑战"
        DefaultValue = "通关 “" .. args["章节挑战"] .. "” 章节挑战对应难度后解锁"
        isDefault[5] = false;
    elseif args["曲包"] then -- 
        if packName(args["曲包"]) == "[[星际之声]]" 
            then unlockCondition = "购买单曲 [" .. songName .. "]"
            else unlockCondition = function(tag)
                    --"购买歌曲包 [&zwnj;" .. packName(args["曲包"]) .. "&zwnj;]"
                    return tag:wikitext("购买歌曲包 [")
                                :tag("span"):wikitext(packName(args["曲包"])):done()
                                :wikitext("]")
                end
            end
    else -- 假设前面的条件没一个满足,那么理论上是忘写了
        unlockCondition = "[待补充]"
    end

    if isDefault[5] then -- 初始化难度IV解禁方法
        args["难度IV解禁方法"] = args["难度IV解禁方法"] or getCondition(query("rating4"));
        isDefault[4] = false;
    end

    DefaultValue = args["defaultValue"] or DefaultValue or "-"
    --- 构建HTML ---
    local box = mw.html.create("table"):addClass("rotable unlockTable")

    local function getDefValue(start)
        local tmp = 1;
        for i=start+1,4 do
            if isDefault[i] then tmp = tmp + 1;
            else return tmp;
            end
        end
        return tmp;
    end

    local tagCount = 1;
    local function summonTag(color,name,argname)
        local tag = 
            box:tag("tr")
                :tag("td"):wikitext(name):css("color", color):done()
        
        if isDefault[tagCount] 
        then if not isDefault[tagCount-1] then
            tag = tag:tag("td"):wikitext(DefaultValue):attr("rowspan",getDefValue(tagCount));
        end
            else tag = tag:tag("td"):wikitext(args[argname])
        end
        tag:done()
        tagCount =  tagCount + 1;
    end

    -- 第一行:通用条件
    local tag = 
        box:tag("tr"):tag("th"):wikitext("曲目解锁"):attr("rowspan", "5"):css("width", "6em"):done()
            :tag("td"):wikitext("通用条件"):css("width", "8em"):done()
            :tag("td"); tag = getResult(unlockCondition,tag):done()
        :done();

    summonTag("#43a047","难度Ⅰ","难度I解禁方法")
    summonTag("#1565c0","难度Ⅱ","难度II解禁方法")
    summonTag("#e65100","难度Ⅲ","难度III解禁方法")
    summonTag("#ab47bc","难度Ⅳ","难度IV解禁方法")
    
    return tostring(box)
end

return p