Module:AnotherData:修订间差异
小 (测试改进) |
小 (暂定Duration) |
||
第11行: | 第11行: | ||
end | end | ||
function p.listOfReserve() | function p.listOfReserve() | ||
return mw.text.jsonDecode(mw.title.new('Reserved data.json', 'Template'):getContent()).songs | -- return mw.text.jsonDecode(mw.title.new('Reserved data.json', 'Template'):getContent()).songs | ||
return mw.text.jsonDecode(mw.title.new('Duration.json', 'Template'):getContent()).songs | |||
end | end | ||
2024年3月13日 (三) 04:55的版本
可在Module:AnotherData/doc创建此模块的帮助文档
local checkType = require 'libraryUtil'.checkType
local p = {}
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
function p.listOfReserve()
-- return mw.text.jsonDecode(mw.title.new('Reserved data.json', 'Template'):getContent()).songs
return mw.text.jsonDecode(mw.title.new('Duration.json', 'Template'):getContent()).songs
end
local function chain(obj, expr)
for op, sKey in expr:gmatch '([.?])([^.?]+)' do
if op == '?' and obj == nil then break end
local key = tonumber(sKey) or sKey
obj = obj[key]
end
return obj
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',
bydTitle = '.difficulties.4?title_localized?en',
bydDate = '.difficulties.4?date',
}
local songHandler = {}
function songHandler.ftrRating(obj)
local diff = obj.difficulties[3]
return diff.rating .. (diff.ratingPlus and '+' or '')
end
function songHandler.bydRating(obj)
local diff = obj.difficulties[4]
if not diff then return nil end
return diff.rating .. (diff.ratingPlus and '+' or '')
end
function songHandler.title(obj) return obj.title_localized.en end
function songHandler.linkTitle(obj)
local trans = mw.loadJsonData 'Template:Transition.json'
local name = songHandler.title(obj)
local res = trans.songNameToDisplayName[name]
if res then return res .. '|' .. name end
res = trans.sameName[name]
if res then return res[obj.id] .. '|' end
return name
end
function songHandler.otherTitles(obj)
local res = {}
local titleJa = obj.title_localized.ja
if titleJa then table.insert(res, ('<span lang="ja">-{%s}-</span>'):format(titleJa)) end
table.insert(res, obj.title_localized['zh-Hans'])
local titleByd = chain(obj, commonExpr.bydTitle)
if titleByd then table.insert(res, titleByd .. mw.getCurrentFrame():expandTemplate {title = '难度标签', args = {'Beyond'}}) end
return res
end
-- local chartHandler = {}
-- function chartHandler.ratingText(obj) return obj.rating .. (obj.ratingPlus and '+' or '') end
local RAW = '@raw'
local COMMON = '@common'
local function optQuery(t, prop)
local obj = rawget(t, RAW)
if not obj then return nil end
local res = obj[prop]
if res then return res end
checkType('optQuery', 2, prop, 'string')
local func = rawget(t, COMMON)[prop]
if func then return func(obj) end
local expr = commonExpr[prop]
if expr then return chain(obj, expr) end
return chain(obj, prop)
end
function p.songQueryWrap(song)
checkType('songQueryWrap', 1, song, 'table', true)
return setmetatable({[RAW] = song, [COMMON] = songHandler}, {__call = optQuery, __index = optQuery})
end
-- function p.chartQueryWrap(chart)
-- checkType('chartQueryWrap', 1, chart, 'table', true)
-- return setmetatable({[RAW] = chart, [COMMON] = chartHandler}, {__call = optQuery, __index = optQuery})
-- end
local mPlatFormPackData = {}
local function mobileSection(pack)
return pack.section
end
local function nsSection(pack)
if pack.pack_parent then return nil end
if mw.loadJsonData 'Template:Transition.json'.packName[pack.id] then return 'collab' end
return ({'free', 'mainstory', 'sidestory'})[pack.section]
end
local function factPackData(platform)
local sectionOf = platform == 'ns' and nsSection or mobileSection
local res = {single = {name = 'Memory Archive', section = 'single', numero = 0}}
for idx, pack in ipairs(p.listOf('packs', platform)) do
--- 单个曲包查询的数据结构
local item = {_parentId_ = pack.pack_parent, name = pack.name_localized.en, section = sectionOf(pack), numero = idx}
res[pack.id] = item
end
for _, item in pairs(res) do
local parentId = item._parentId_
local parent
if parentId then
parent = res[parentId]
if item.name:match 'Collaboration Chapter' then
item.name = parent.name .. ' ' .. item.name
end
end
item.section = item.section or parent.section
end
return res
end
function p.packQueryWrap(id, platform)
platform = platform or 'mobile'
local packData = mPlatFormPackData[platform]
if not packData then
packData = factPackData(platform)
mPlatFormPackData[platform] = packData
end
-- return id and packData[id] or packData
return packData[id] or {name = '请更新plst', section = 'unknown'}
end
return p