2,032
次編輯
小 (允许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 | local songHandler = {} | ||
function | 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 | 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 | 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 | 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 | function songHandler.otherTitles(obj) | ||
local res = { | local res = {} | ||
table.insert(res, obj.title_localized. | 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 | 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) | |||
local function | |||
local obj = | |||
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 | ||
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 = | 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 = {} |
次編輯