Module:Rotaeno Data

Star0讨论 | 贡献2022年7月29日 (五) 19:49的版本 (创建页面,内容为“local p = {} function jsonAssayForSingleSong(json, index, indexTyped) -- 传入JSON文件,索引值和索引值类型(索引是ID还是曲名)。分析JSON文件以获得曲目信息,返回的是一个查找单一曲目中信息的函数。仅获取单一曲目的信息。table中的索引名请查看下方switch表。 -- Songlist是以数字为索引值,因此遍历Songlist,直到发现需要找的曲目,将信息存入info变量中。…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

local p = {}

function jsonAssayForSingleSong(json, index, indexTyped)
    -- 传入JSON文件,索引值和索引值类型(索引是ID还是曲名)。分析JSON文件以获得曲目信息,返回的是一个查找单一曲目中信息的函数。仅获取单一曲目的信息。table中的索引名请查看下方switch表。

    -- Songlist是以数字为索引值,因此遍历Songlist,直到发现需要找的曲目,将信息存入info变量中。
    local s, info = "", {}
    -- 如果模式是ID则遍历songlist直至发现id值为索引值为止,并获取内容。曲名同理。
    if indexTyped == "id" then
        for i, k in ipairs(json['songs']) do
            if k['id'] == index then info = k end
        end
    else
        for i, k in ipairs(json['songs']) do
            if k['title_localized']['en'] == index then info = k end
        end
    end

    -- 此处返回一个函数用于查询内容。

    return function(key)

        -- 如果info无值即无法在songlist中查找到索引值,则返回nil。
        
        if info == nil 
        then 
            mw.log( '无法在Songlist中发现目标,索引值为:'..index )
            return nil 
        end

        -- 此处定义一个switch table以达到switch函数的效用。如果需要查找的参数在switch中不存在,返回nil。
        local switch = {
            ["id"] = function() return info["id"] end,
            ["title"] = function() return info['title_localized']['default'] end,
            ["title-en"] = function() return info['title_localized']['en'] end,
            ["title-zh-Hans"] = function() return info['title_localized']['zh-Hans'] end,
            ["title-zh-Hant"] = function() return info['title_localized']['zh-Hant'] end,
            ["title-ja"] = function() return info['title_localized']['ja'] end,
            ["title-ko"] = function() return info['title_localized']['ko'] end,
            ["artist"] = function() return info["artist"] end,
            -- Rotaeno谱师画师都一样,所以随便找一个读取
            ["ChartDesigner"] = function()
                return info["difficulties"][1]['chartDesigner']
            end,
            ["JacketDesigner"] = function()
                return info["difficulties"][1]['jacketDesigner']
            end,
            ["source"] = function() return info['source_localized']['default'] end,
            ["rating1"] = function()
                return info["difficulties"][1]['rating']
            end,
            ["rating2"] = function()
                return info["difficulties"][2]['rating']
            end,
            ["rating3"] = function()
                return info["difficulties"][3]['rating']
            end,
            ["rating4"] = function()
                return info["difficulties"][4]['rating']
            end,
        }

        if switch[key] == nil 
        then 
            mw.log( '未定义的索引类型,请检查是否拼写错误。' )
            return nil 
        end

        return switch[key]()
    end
end

function p.singleSongInformation(index, indexTyped, platform)
    -- 传入曲名或ID,获得一个查阅信息的函数。直接在下一个模块使用。
    -- index:曲目的索引字符串,和typed一致  indexTyped:id或name,查找曲目的索引值类型  platform:mobile或ns
    if platform == "mobile"
    then
        if indexTyped == "id"  then
            return jsonAssayForSingleSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist.json' } ), index, 'id')
        else
            return jsonAssayForSingleSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist.json' } ), index, 'name')
        end
    else
        if indexTyped == "id"  then
            return jsonAssayForSingleSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist NS.json' } ), index, 'id')
        else
            return jsonAssayForSingleSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist NS.json' } ), index, 'name')
        end
    end
end

function jsonAssayForAllSong(json, indexTyped)
    -- 传入JSON文件,获得一个通过索引值查找指定曲目指定信息的函数。与jsonAssayForSingleSong不同,本函数可以同时查找多个函数。
    -- 索引值由下一步函数使用来指定,但是索引类型必须刚开始就指定。(id或name)

    local s, info = "", {}
    if indexTyped == "id" then
        for i, k in ipairs(json['songs']) do
            info[k['id']] = k
        end
    else
        for i, k in ipairs(json['songs']) do
            info[k['title_localized']['en']] = k
        end
    end

    -- 此处返回一个函数用于查询内容。

    return function(index, key)

        -- 如果info无值即无法在songlist中查找到索引值,则返回nil。
        
        if info[index] == nil 
        then 
            mw.log( '无法在Songlist中发现目标,索引值为:'..index )
            return nil 
        end

        -- 此处定义一个switch table以达到switch函数的效用。如果需要查找的参数在switch中不存在,返回nil。
        local switch = {
            ["id"] = function() return info[index]["id"] end,
            ["title"] = function() return info[index]['title_localized']['default'] end,
            ["title-en"] = function() return info[index]['title_localized']['en'] end,
            ["title-zh-Hans"] = function() return info[index]['title_localized']['zh-Hans'] end,
            ["title-zh-Hant"] = function() return info[index]['title_localized']['zh-Hant'] end,
            ["title-ja"] = function() return info[index]['title_localized']['ja'] end,
            ["title-ko"] = function() return info[index]['title_localized']['ko'] end,
            ["artist"] = function() return info[index]["artist"] end,
            -- Rotaeno谱师画师都一样,所以随便找一个读取
            ["ChartDesigner"] = function()
                return info[index]["difficulties"][1]['chartDesigner']
            end,
            ["JacketDesigner"] = function()
                return info[index]["difficulties"][1]['jacketDesigner']
            end,
            ["source"] = function() return info[index]['source_localized']['default'] end,
            ["rating1"] = function()
                return info[index]["difficulties"][1]['rating']
            end,
            ["rating2"] = function()
                return info[index]["difficulties"][2]['rating']
            end,
            ["rating3"] = function()
                return info[index]["difficulties"][3]['rating']
            end,
            ["rating4"] = function()
                return info[index]["difficulties"][4]['rating']
            end,
        }

        if switch[key] == nil 
        then 
            mw.log( '未定义的索引类型,请检查是否拼写错误。' )
            return nil 
        end

        return switch[key]()
    end

end

function p.allSongInformation(indexTyped, platform)
    -- 获得一个查阅信息的函数,可查阅任何曲目。直接在下一个模块使用。
    -- indexTyped:id或name,查找曲目的索引值类型  platform:mobile或ns
    if platform == "mobile"
    then
        if indexTyped == "id"  then
            return jsonAssayForAllSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist.json' } ), 'id')
        else
            return jsonAssayForAllSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist.json' } ), 'name')
        end
    else
        if indexTyped == "id"  then
            return jsonAssayForAllSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist NS.json' } ), 'id')
        else
            return jsonAssayForAllSong(mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate{ title = 'Songlist NS.json' } ), 'name')
        end
    end
end

local getArgs = require('Module:Arguments').getArgs

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

p.Song_Query = makeInvokeFunc('_Song_Query')

function p._Song_Query(args)
    -- 面向wikitext直接查询
    -- return p.singleSongInformation(songIndex, songIndexType, platform)(attributeName)
    return p.singleSongInformation(args[1], args[2], args[4])(args[3])
end

p.Pack_Query = makeInvokeFunc('_Pack_Query')

function p._Pack_Query(args)
    -- 面向wikitext直接查询
    return p.packName(args[1])
end

return p