跳到内容

Module:AnotherData:修订间差异

添加903字节 、​ 2024年3月13日 (星期三)
测试改进
(允许platform与target的nil)
(测试改进)
第9行: 第9行:
function p.listOf(type, platform)
function p.listOf(type, platform)
return mw.text.jsonDecode(mw.title.new(srcFileName[type][platform == 'ns' and 2 or 1], 'Template'):getContent())[type]
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
end
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 = {
local commonExpr = {
title = '.title_localized.en',
title = '.title_localized.en',
第35行: 第47行:
bydDate = '.difficulties.4?date',
bydDate = '.difficulties.4?date',
}
}
local commonFunc = {}
local songHandler = {}
function commonFunc.ftrRating(obj)
function songHandler.ftrRating(obj)
local diff = obj.difficulties[3]
local diff = obj.difficulties[3]
return diff.rating .. (diff.ratingPlus and '+' or '')
return diff.rating .. (diff.ratingPlus and '+' or '')
end
end
function commonFunc.bydRating(obj)
function songHandler.bydRating(obj)
local diff = obj.difficulties[4]
local diff = obj.difficulties[4]
if not diff then return nil end
if not diff then return nil end
return diff.rating .. (diff.ratingPlus and '+' or '')
return diff.rating .. (diff.ratingPlus and '+' or '')
end
end
function commonFunc.linkName(obj)
function songHandler.title(obj) return obj.title_localized.en end
function songHandler.linkTitle(obj)
local trans = mw.loadJsonData 'Template:Transition.json'
local trans = mw.loadJsonData 'Template:Transition.json'
local name = obj.title_localized.en
local name = songHandler.title(obj)
local res = trans.songNameToDisplayName[name]
local res = trans.songNameToDisplayName[name]
if res then return res end
if res then return res .. '|' .. name end
res = trans.sameName[name]
res = trans.sameName[name]
if res then return res[obj.id] end
if res then return res[obj.id] .. '|' end
return name
end
end
function commonFunc.linkTitle(obj)
function songHandler.otherTitles(obj)
local res = {commonFunc.linkName(obj)}
local res = {}
table.insert(res, obj.title_localized.en)
local titleJa = obj.title_localized.ja
return table.concat(res, '|')
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
end
 
-- local chartHandler = {}
local function chain(obj, expr)
-- function chartHandler.ratingText(obj) return obj.rating .. (obj.ratingPlus and '+' or '') end
for op, sKey in expr:gmatch '([.?])([^.?]+)' do
local RAW = '@raw'
if op == '?' and obj == nil then break end
local COMMON = '@common'
local key = tonumber(sKey) or sKey
local function optQuery(t, prop)
obj = obj[key]
local obj = rawget(t, RAW)
end
return obj
end
local function songQuery(t, prop)
checkType('songQuery', 2, prop, 'string')
local obj = getmetatable(t)._info_
if not obj then return nil end
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]
local expr = commonExpr[prop]
if expr then return chain(obj, expr) end
if expr then return chain(obj, expr) end
local func = commonFunc[prop]
if func then return func(obj) end
if not prop:match '^[.?]' then return t[prop] end
return chain(obj, prop)
return chain(obj, prop)
end
end
function p.songQueryWrap(song)
function p.songQueryWrap(song)
checkType('songQueryWrap', 1, song, 'table', true)
checkType('songQueryWrap', 1, song, 'table', true)
return setmetatable({}, {__call = songQuery, __index = songQuery, _info_ = song})
return setmetatable({[RAW] = song, [COMMON] = songHandler}, {__call = optQuery, __index = optQuery})
end
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 mPlatFormPackData = {}
2,032

个编辑