Module:Rotaeno Data:修订间差异

来自Rotaeno中文维基
无编辑摘要
(简化代码)
 
(未显示4个用户的9个中间版本)
第1行: 第1行:
local lang = mw.language.getContentLanguage()
local p = {}
local p = {}


function jsonAssayForSingleSong(json, index, indexTyped)
local function songClosure(info)
   -- 传入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']['default'] == index then info = k end
     end
   end
 
    --  此处 返回一个函数用于查询内容。
 
    return function(key)
    return function(key)
      -- 如果info无值即无法在songlist中查找到索引值,则返回nil。
      -- 如果info无值即无法在songlist中查找到索引值,则返回nil。


      if info == nil then
      if info == nil then return nil end
       mw.log('无法在Songlist中发现目标,索引值为:' .. index)
       return nil
     end


      -- 此处定义一个switch table以达到switch函数的效用。如果需要查找的参数在switch中不存在,返回nil。
      -- 此处定义一个switch table以达到switch函数的效用。如果需要查找的参数在switch中不存在,返回nil。
第52行: 第33行:
          local list = {}
          local list = {}
          for i, k in pairs(info['title_localized']) do
          for i, k in pairs(info['title_localized']) do
            if k ~= nil and k ~="" and k ~= info['title_localized']['default'] and
            if k ~= nil and k ~= "" and k ~= info['title_localized']['default'] and
              k ~= "en" then
              k ~= "en" then
              table.insert(list, i .. ":" .. "-{" .. k .. "}-" )
              table.insert(list, i .. ":" .. "-{" .. k .. "}-")
            end
            end
          end
          end
        
 
          if list[1]
          if list[1] then
         then
            return table.concat(list, " | ")
            return table.concat(list, " | ")
          else
          else
第66行: 第46行:
        end,
        end,
        ["artist"] = function() return info["artist"] end,
        ["artist"] = function() return info["artist"] end,
        -- Rotaeno谱师 画师都一样 所以随便找一个读取
 
        -- Rotaeno谱师 在v2.0.0后可能不同难度不同 这里为了兼容性保留
        ["ChartDesigner"] = function()
        ["ChartDesigner"] = function()
          return info["difficulties"][1]['chartDesigner']
          return info["difficulties"][1]['chartDesigner']
        end,
        end,
       ["ChartDesigner1"] = function()
         return info["difficulties"][1]['chartDesigner']
       end,
       ["ChartDesigner2"] = function()
         return info["difficulties"][2]['chartDesigner']
       end,
       ["ChartDesigner3"] = function()
         return info["difficulties"][3]['chartDesigner']
       end,
       ["ChartDesigner4"] = function()
         return info["difficulties"][4]['chartDesigner']
       end,
       ["ChartDesignerA"] = function()
         if info["difficulties"][5] then
           return info["difficulties"][5]['chartDesigner']
         end
       end,
       -- Rotaeno画师目前都一样,所以随便找一个读取
        ["JacketDesigner"] = function()
        ["JacketDesigner"] = function()
          return info["difficulties"][1]['jacketDesigner']
          return info["difficulties"][1]['jacketDesigner']
第79行: 第78行:
        end,
        end,
        ["rating1"] = function()
        ["rating1"] = function()
          return info["difficulties"][1]['rating']
          return info["difficulties"][1]['ratingReal']
        end,
        end,
        ["rating2"] = function()
        ["rating2"] = function()
          return info["difficulties"][2]['rating']
          return info["difficulties"][2]['ratingReal']
        end,
        end,
        ["rating3"] = function()
        ["rating3"] = function()
          return info["difficulties"][3]['rating']
          return info["difficulties"][3]['ratingReal']
        end,
        end,
        ["rating4"] = function()
        ["rating4"] = function()
          return info["difficulties"][4]['rating']
          return info["difficulties"][4]['ratingReal']
       end,
       ["ratingA"] = function()
         if info["difficulties"][5] then
           return info["difficulties"][5]['ratingReal']
         end
        end
        end
      }
      }


      if switch[key] == nil then
      if info["difficulties"] == nil or info["difficulties"][1] == nil then
        mw.log('未定义的 引类型,请检查是否拼写错误。')
        mw.log("尝试检 " .. key)
       return nil
        mw.log("Songlist.json 出现 据缺漏 终止全部 据检索 使用 参数填充 。")
     end
 
     return switch[key]()
   end
end
 
function p.singleSongInformation(index, indexTyped)
   -- 传入曲名或ID,获得一个查阅信息的函 。直接在下一个模块使用。
   -- index:曲目的索引字符串,和typed一致  indexTyped:id或name,查找曲目的索引值类型
 
   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
 
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
        return nil
      end
      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
      if switch[key] == nil then
第196行: 第109行:
      return switch[key]()
      return switch[key]()
    end
    end
end
end


function p.allSongInformation(indexTyped)
--  传入JSON文件,索引值和索引值类型(索引是ID还是曲名)。分析JSON文件以 获得 曲目信息,返回的是 一个查 找单一曲目中 信息的函数 。仅获取单一 曲目 的信息。table中的索引名请查看 方switch表
   -- 获得一个查 信息的函数 ,可查阅任何 曲目 。直接在 一个模块使用
-- Songlist是以数字为索引值,因此遍历Songlist 直到发现需要 曲目 ,将信息存入info变量中。
   -- indexTyped:id或name 找曲目 索引值 类型
-- 如果模式是ID则遍历songlist直至发现id值为 索引值 为止,并获取内容。曲名同理。
   if indexTyped == "id" then
local function findFirstByTitle(src, title)
     return jsonAssayForAllSong(mw.text.jsonDecode(
   title = lang:lc(title)
                    mw.getCurrentFrame():expandTemplate{
   for _, k in ipairs(src) do
         title = 'Songlist.json'
      if lang:lc(k['title_localized']['default']) == title then return k end
       }), 'id')
   else
      return jsonAssayForAllSong(mw.text.jsonDecode(
                    mw.getCurrentFrame():expandTemplate{
         title = 'Songlist.json'
       }), 'name')
    end
    end
end
end


local getArgs = require('Module:Arguments').getArgs
local function findFirstById(src, id)
 
    for _, k in ipairs(src) do
function makeInvokeFunc(funcName)
      if k['id'] == id then return k end
    return function(frame)
      local args = getArgs(frame)
     return p[funcName](args)
    end
    end
end
end


p.Song_Query = makeInvokeFunc('_Song_Query')
function p.singleSongInformation(index, indexTyped)
 
    --  传入曲名或ID,获得一个查阅信息的函数。 直接 在下一个模块使用。
function p._Song_Query(args)
    -- index:曲目的索引字符串,和typed一致  indexTyped:id或name,查找曲目的索引值类型
    --  面向wikitext 直接 查询
   local findFirst = indexTyped == "id" and findFirstById or findFirstByTitle
    -- return p.singleSongInformation(songIndex, songIndexType, platform)(attributeName)
   local append = findFirst(
   return p.singleSongInformation(args[1], args[2], args[4])(args[3])
     mw.text.jsonDecode(mw.title.new('Songlist append.json', 'Template'):getContent())['songs'],
end
     index)
 
   if append ~= nil then return songClosure(append) end
p.Pack_Query = makeInvokeFunc('_Pack_Query')
   append = findFirst(
 
     mw.text.jsonDecode(mw.title.new('Songlist.json', 'Template'):getContent())['songs'],
function p._Pack_Query(args)
     index)
   -- 面向wikitext直接查询
   if append == nil then mw.log('无法在Songlist中发现目标,索引值为:' .. index) end
    return p.packName(args[1])
    return songClosure(append)
end
end


return p
return p

2024年7月29日 (一) 14:14的最新版本

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

local lang = mw.language.getContentLanguage()
local p = {}

local function songClosure(info)
    -- 返回一个函数用于查询内容。
    return function(key)
        -- 如果info无值即无法在songlist中查找到索引值,则返回nil。

        if info == nil then 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,
            ["title-list"] = function()
                local list = {}
                for i, k in pairs(info['title_localized']) do
                    if k ~= nil and k ~= "" and k ~= info['title_localized']['default'] and
                        k ~= "en" then
                        table.insert(list, i .. ":" .. "-{" .. k .. "}-")
                    end
                end

                if list[1] then
                    return table.concat(list, " | ")
                else
                    return nil
                end
            end,
            ["artist"] = function() return info["artist"] end,

            -- Rotaeno谱师在v2.0.0后可能不同难度不同,这里为了兼容性保留
            ["ChartDesigner"] = function()
                return info["difficulties"][1]['chartDesigner']
            end,
            ["ChartDesigner1"] = function()
                return info["difficulties"][1]['chartDesigner']
            end,
            ["ChartDesigner2"] = function()
                return info["difficulties"][2]['chartDesigner']
            end,
            ["ChartDesigner3"] = function()
                return info["difficulties"][3]['chartDesigner']
            end,
            ["ChartDesigner4"] = function()
                return info["difficulties"][4]['chartDesigner']
            end,
            ["ChartDesignerA"] = function()
                if info["difficulties"][5] then
                    return info["difficulties"][5]['chartDesigner']
                end
            end,
            -- Rotaeno画师目前都一样,所以随便找一个读取
            ["JacketDesigner"] = function()
                return info["difficulties"][1]['jacketDesigner']
            end,
            ["source"] = function()
                if info['source_localized'] then
                    return info['source_localized']['default']
                end
            end,
            ["rating1"] = function()
                return info["difficulties"][1]['ratingReal']
            end,
            ["rating2"] = function()
                return info["difficulties"][2]['ratingReal']
            end,
            ["rating3"] = function()
                return info["difficulties"][3]['ratingReal']
            end,
            ["rating4"] = function()
                return info["difficulties"][4]['ratingReal']
            end,
            ["ratingA"] = function()
                if info["difficulties"][5] then
                    return info["difficulties"][5]['ratingReal']
                end
            end
        }

        if info["difficulties"] == nil or info["difficulties"][1] == nil then
            mw.log("尝试检索参数:" .. key)
            mw.log("Songlist.json出现数据缺漏,终止全部数据检索。请使用参数填充。")
            return nil
        end

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

        return switch[key]()
    end
end

-- 传入JSON文件,索引值和索引值类型(索引是ID还是曲名)。分析JSON文件以获得曲目信息,返回的是一个查找单一曲目中信息的函数。仅获取单一曲目的信息。table中的索引名请查看下方switch表。
-- Songlist是以数字为索引值,因此遍历Songlist,直到发现需要找的曲目,将信息存入info变量中。
-- 如果模式是ID则遍历songlist直至发现id值为索引值为止,并获取内容。曲名同理。
local function findFirstByTitle(src, title)
    title = lang:lc(title)
    for _, k in ipairs(src) do
        if lang:lc(k['title_localized']['default']) == title then return k end
    end
end

local function findFirstById(src, id)
    for _, k in ipairs(src) do
        if k['id'] == id then return k end
    end
end

function p.singleSongInformation(index, indexTyped)
    -- 传入曲名或ID,获得一个查阅信息的函数。直接在下一个模块使用。
    -- index:曲目的索引字符串,和typed一致  indexTyped:id或name,查找曲目的索引值类型
    local findFirst = indexTyped == "id" and findFirstById or findFirstByTitle
    local append = findFirst(
        mw.text.jsonDecode(mw.title.new('Songlist append.json', 'Template'):getContent())['songs'],
        index)
    if append ~= nil then return songClosure(append) end
    append = findFirst(
        mw.text.jsonDecode(mw.title.new('Songlist.json', 'Template'):getContent())['songs'],
        index)
    if append == nil then mw.log('无法在Songlist中发现目标,索引值为:' .. index) end
    return songClosure(append)
end

return p