Module:沙盒/盐棋/Sandbox1:修订间差异
小 (…) |
小 (Yet Another Data Module) |
||
第1行: | 第1行: | ||
local p = {} | local checkType = require 'libraryUtil'.checkType | ||
function | local getArgs = require 'Module:Arguments'.getArgs | ||
return | local lang = mw.language.getContentLanguage() | ||
local p = {} | |||
local function chain(obj, expr) | |||
local first = expr:sub(1, 1) | |||
if first ~= '.' and first ~= '?' then return obj[expr] end | |||
for op, sKey in expr:gmatch '([.?])([^.?]+)' do | |||
if op == '?' and obj == nil then break end | |||
local iKey = tonumber(sKey) | |||
local key = iKey or sKey | |||
obj = obj[key] | |||
end | |||
return obj | |||
end | end | ||
function p. | |||
return mw. | local srcFileName = { | ||
['songs'] = {'Songlist.json', 'Songlist NS.json'}, | |||
['packs'] = {'Packlist.json', 'Packlist NS'}, | |||
['unlocks'] = {'Unlocks.json', 'Unlocks NS'}, | |||
} | |||
function p.listOf(type, platform) | |||
return mw.text.jsonDecode(mw.title.new(srcFileName[type][platform == 'ns' and 2 or 1], 'Template'):getContent())[type] | |||
end | end | ||
function | |||
local commonExpr = { | |||
title = '.title_localized.en', | |||
pstChartDesigner = '.difficulties.1.chartDesigner', | |||
pstJacketDesigner = '.difficulties.1.jacketDesigner', | |||
pstRating = '.difficulties.1.rating', | |||
prsChartDesigner = '.difficulties.2.chartDesigner', | |||
prsJacketDesigner = '.difficulties.2.jacketDesigner', | |||
prsRating = '.difficulties.2.rating', | |||
ftrChartDesigner = '.difficulties.3.chartDesigner', | |||
ftrJacketDesigner = '.difficulties.3.jacketDesigner', | |||
-- ftrRating | |||
bydChartDesigner = '.difficulties.4?chartDesigner', | |||
bydJacketDesigner = '.difficulties.4?jacketDesigner', | |||
-- bydRating | |||
pstChange = '.difficulties.1.has_controller_alt_chart', | |||
prsChange = '.difficulties.2.has_controller_alt_chart', | |||
ftrChange = '.difficulties.3.has_controller_alt_chart', | |||
bydAudioChange = '.difficulties.4?audioOverride', | |||
bydBg = '.difficulties.4?bg', | |||
bydBPM = '.difficulties.4?bpm', | |||
bydArtist = '.difficulties.4?artist', | |||
} | |||
local commonFunc = {} | |||
function commonFunc.titleName(obj) | |||
local trans = mw.loadJsonData 'Template:Transition.json' | |||
local name = obj['title_localized']['en'] | |||
local res = trans.songNameToDisplayName[name] | |||
if res then return res end | |||
res = trans.sameName[name] | |||
if res then return res[obj['id']] end | |||
end | end | ||
function commonFunc.ftrRating(obj) | |||
local diff = obj['difficulties'][3] | |||
return diff['rating'] .. (diff['ratingPlus'] and '+' or '') | |||
end | |||
function commonFunc.bydRating(obj) | |||
local diff = obj['difficulties'][4] | |||
if diff == nil then return nil end | |||
return diff['rating'] .. (diff['ratingPlus'] and '+' or '') | |||
end | |||
local SYINFO = '_info' | |||
local function songQuery(target, property) | |||
checkType('songQuery', 2, property, 'string') | |||
local obj = target[SYINFO] | |||
if next(obj) == nil then return nil end | |||
local expr = commonExpr[property] | |||
if expr then return chain(obj, expr) end | |||
local func = commonFunc[property] | |||
if func then return func(obj) end | |||
return chain(obj, property) | |||
end | |||
function p.songQueryWrap(tar) | |||
checkType('songQueryWrap', 1, tar, 'table') | |||
return setmetatable({[SYINFO] = tar}, {__call = songQuery, __index = songQuery}) | |||
end | |||
---传入曲名或ID,获得一个查阅信息的函数。直接在下一个模块使用。 | |||
---@param index string 曲目的索引,和typed一致 | |||
---@param indexType 'id'|'name' 查找曲目的索引值类型 | |||
---@param platform 'mobile'|'ns' | |||
---@return table | |||
function p.singleSongInformation(index, indexType, platform) | |||
local lSong = p.listOf('songs', platform) | |||
local sKey = index | |||
local tar = {} | |||
if indexType == 'id' then | |||
for _, k in ipairs(lSong) do | |||
if k['id'] == sKey then | |||
tar = k | |||
break | |||
end | |||
end | |||
else | |||
sKey = lang:ucfirst(index) | |||
for _, k in ipairs(lSong) do | |||
if lang:ucfirst(k['title_localized']['en']) == sKey then | |||
tar = k | |||
break | |||
end | |||
end | |||
end | |||
if next(tar) == nil then mw.log('无法在Songlist中发现目标,索引值为:' .. index) end | |||
return p.songQueryWrap(tar) | |||
end | |||
--undone | |||
function p.songMapWrap(platform) | |||
local lSong = p.listOf('songs', platform) | |||
local res = {} | |||
for _, k in ipairs(lSong) do | |||
res[k['id']] = k | |||
res[lang:ucfirst(k['title_localized']['en'])] = k | |||
end | |||
return res | |||
end | |||
--undone | |||
function p.packName(id) | |||
-- 将曲包ID转换为曲包名称。 | |||
-- 传入Songlist中定义的曲包ID,返回Packlist中的曲包名称。如果Template:Transition.json中存在转换,则使用转换后的名称。 | |||
checkType('packName', 1, id, 'string', true) | |||
if id == nil then return 'undefined' end | |||
-- 检查曲包ID→曲包名称转换表是否存在转换规则。 | |||
local mTrans = mw.loadJsonData 'Template:Transition.json'.packName | |||
if mTrans[id] then return mTrans[id] end | |||
-- 判断变量是否已有值,为多次调用预备。 | |||
-- 变量为全局变量 | |||
local lPack = mw.loadJsonData 'Template:Packlist.json'['packs'] | |||
for _, k in ipairs(lPack) do | |||
if k['id'] == id then return k['name_localized']['en'] end | |||
end | |||
mw.log('未找到曲包名称,id为' .. id) | |||
return id | |||
end | |||
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 | return p |
2024年2月16日 (五) 10:56的版本
可在Module:沙盒/盐棋/Sandbox1/doc创建此模块的帮助文档
local checkType = require 'libraryUtil'.checkType
local getArgs = require 'Module:Arguments'.getArgs
local lang = mw.language.getContentLanguage()
local p = {}
local function chain(obj, expr)
local first = expr:sub(1, 1)
if first ~= '.' and first ~= '?' then return obj[expr] end
for op, sKey in expr:gmatch '([.?])([^.?]+)' do
if op == '?' and obj == nil then break end
local iKey = tonumber(sKey)
local key = iKey or sKey
obj = obj[key]
end
return obj
end
local srcFileName = {
['songs'] = {'Songlist.json', 'Songlist NS.json'},
['packs'] = {'Packlist.json', 'Packlist NS'},
['unlocks'] = {'Unlocks.json', 'Unlocks NS'},
}
function p.listOf(type, platform)
return mw.text.jsonDecode(mw.title.new(srcFileName[type][platform == 'ns' and 2 or 1], 'Template'):getContent())[type]
end
local commonExpr = {
title = '.title_localized.en',
pstChartDesigner = '.difficulties.1.chartDesigner',
pstJacketDesigner = '.difficulties.1.jacketDesigner',
pstRating = '.difficulties.1.rating',
prsChartDesigner = '.difficulties.2.chartDesigner',
prsJacketDesigner = '.difficulties.2.jacketDesigner',
prsRating = '.difficulties.2.rating',
ftrChartDesigner = '.difficulties.3.chartDesigner',
ftrJacketDesigner = '.difficulties.3.jacketDesigner',
-- ftrRating
bydChartDesigner = '.difficulties.4?chartDesigner',
bydJacketDesigner = '.difficulties.4?jacketDesigner',
-- bydRating
pstChange = '.difficulties.1.has_controller_alt_chart',
prsChange = '.difficulties.2.has_controller_alt_chart',
ftrChange = '.difficulties.3.has_controller_alt_chart',
bydAudioChange = '.difficulties.4?audioOverride',
bydBg = '.difficulties.4?bg',
bydBPM = '.difficulties.4?bpm',
bydArtist = '.difficulties.4?artist',
}
local commonFunc = {}
function commonFunc.titleName(obj)
local trans = mw.loadJsonData 'Template:Transition.json'
local name = obj['title_localized']['en']
local res = trans.songNameToDisplayName[name]
if res then return res end
res = trans.sameName[name]
if res then return res[obj['id']] end
end
function commonFunc.ftrRating(obj)
local diff = obj['difficulties'][3]
return diff['rating'] .. (diff['ratingPlus'] and '+' or '')
end
function commonFunc.bydRating(obj)
local diff = obj['difficulties'][4]
if diff == nil then return nil end
return diff['rating'] .. (diff['ratingPlus'] and '+' or '')
end
local SYINFO = '_info'
local function songQuery(target, property)
checkType('songQuery', 2, property, 'string')
local obj = target[SYINFO]
if next(obj) == nil then return nil end
local expr = commonExpr[property]
if expr then return chain(obj, expr) end
local func = commonFunc[property]
if func then return func(obj) end
return chain(obj, property)
end
function p.songQueryWrap(tar)
checkType('songQueryWrap', 1, tar, 'table')
return setmetatable({[SYINFO] = tar}, {__call = songQuery, __index = songQuery})
end
---传入曲名或ID,获得一个查阅信息的函数。直接在下一个模块使用。
---@param index string 曲目的索引,和typed一致
---@param indexType 'id'|'name' 查找曲目的索引值类型
---@param platform 'mobile'|'ns'
---@return table
function p.singleSongInformation(index, indexType, platform)
local lSong = p.listOf('songs', platform)
local sKey = index
local tar = {}
if indexType == 'id' then
for _, k in ipairs(lSong) do
if k['id'] == sKey then
tar = k
break
end
end
else
sKey = lang:ucfirst(index)
for _, k in ipairs(lSong) do
if lang:ucfirst(k['title_localized']['en']) == sKey then
tar = k
break
end
end
end
if next(tar) == nil then mw.log('无法在Songlist中发现目标,索引值为:' .. index) end
return p.songQueryWrap(tar)
end
--undone
function p.songMapWrap(platform)
local lSong = p.listOf('songs', platform)
local res = {}
for _, k in ipairs(lSong) do
res[k['id']] = k
res[lang:ucfirst(k['title_localized']['en'])] = k
end
return res
end
--undone
function p.packName(id)
-- 将曲包ID转换为曲包名称。
-- 传入Songlist中定义的曲包ID,返回Packlist中的曲包名称。如果Template:Transition.json中存在转换,则使用转换后的名称。
checkType('packName', 1, id, 'string', true)
if id == nil then return 'undefined' end
-- 检查曲包ID→曲包名称转换表是否存在转换规则。
local mTrans = mw.loadJsonData 'Template:Transition.json'.packName
if mTrans[id] then return mTrans[id] end
-- 判断变量是否已有值,为多次调用预备。
-- 变量为全局变量
local lPack = mw.loadJsonData 'Template:Packlist.json'['packs']
for _, k in ipairs(lPack) do
if k['id'] == id then return k['name_localized']['en'] end
end
mw.log('未找到曲包名称,id为' .. id)
return id
end
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