Module:沙盒/盐棋/Sandbox1:修订间差异

来自Arcaea中文维基
(微调mapwrap,增加查询选项)
(调整)
第1行: 第1行:
local checkType = require 'libraryUtil'.checkType
local checkType = require 'libraryUtil'.checkType
local getArgs  = require 'Module:Arguments'.getArgs
local lang = mw.language.getContentLanguage()
local lang    = mw.language.getContentLanguage()
local p = {}
local p      = {}
 
local function chain(obj, expr)
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 = {
local srcFileName = {
第48行: 第37行:
}
}
local commonFunc = {}
local commonFunc = {}
function commonFunc.titleName(obj)
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 not diff then return nil end
return diff.rating .. (diff.ratingPlus and '+' or '')
end
function commonFunc.linkName(obj)
local trans = mw.loadJsonData 'Template:Transition.json'
local trans = mw.loadJsonData 'Template:Transition.json'
local name = obj['title_localized']['en']
local name = obj.title_localized.en
local res = trans.songNameToDisplayName[name]
local res = trans.songNameToDisplayName[name]
if res then return res end
if res then return res 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
end
end
function commonFunc.ftrRating(obj)
function commonFunc.linkTitle(obj)
local diff = obj['difficulties'][3]
local res = {commonFunc.linkName(obj)}
return diff['rating'] .. (diff['ratingPlus'] and '+' or '')
table.insert(res, obj.title_localized.en)
return table.concat(res, '|')
end
end
function commonFunc.bydRating(obj)
 
local diff = obj['difficulties'][4]
local function chain(obj, expr)
if not diff then return nil end
for op, sKey in expr:gmatch '([.?])([^.?]+)' do
return diff['rating'] .. (diff['ratingPlus'] and '+' or '')
if op == '?' and obj == nil then break end
local key = tonumber(sKey) or sKey
obj = obj[key]
end
return obj
end
end
 
local function songQuery(t, prop)
local INFO = '__info'
checkType('songQuery', 2, prop, 'string')
local function songQuery(target, property)
local obj = getmetatable(t)._info_
checkType('songQuery', 2, property, 'string')
local obj = getmetatable(target)[INFO]
if next(obj) == nil then return nil end
if next(obj) == nil then return nil end
local expr = commonExpr[property]
local expr = commonExpr[prop]
if expr then return chain(obj, expr) end
if expr then return chain(obj, expr) end
local func = commonFunc[property]
local func = commonFunc[prop]
if func then return func(obj) end
if func then return func(obj) end
if not property:match '^[.?]' then return target[property] end
if not prop:match '^[.?]' then return t[prop] end
return chain(obj, property)
return chain(obj, prop)
end
end
function p.songQueryWrap(tar)
function p.songQueryWrap(song)
checkType('songQueryWrap', 1, tar, 'table')
checkType('songQueryWrap', 1, song, 'table')
return setmetatable({}, {__call = songQuery, __index = songQuery, [INFO] = tar})
return setmetatable({}, {__call = songQuery, __index = songQuery, _info_ = song})
end
end


第94行: 第95行:
if indexType == 'id' then
if indexType == 'id' then
for _, k in ipairs(lSong) do
for _, k in ipairs(lSong) do
if k['id'] == sKey then
if k.id == sKey then
tar = k
tar = k
break
break
第102行: 第103行:
sKey = lang:ucfirst(index)
sKey = lang:ucfirst(index)
for _, k in ipairs(lSong) do
for _, k in ipairs(lSong) do
if lang:ucfirst(k['title_localized']['en']) == sKey then
if lang:ucfirst(k.title_localized.en) == sKey then
tar = k
tar = k
break
break
第112行: 第113行:
end
end


--undone
local mPlatFormPackData = {}
function p.songMapWrap(lSong)
local function mobileSection(pack)
local res = {}
return pack.section
for _, k in ipairs(lSong) do
end
res[k['id']] = k
local function nsSection(pack)
-- res[lang:ucfirst(k['title_localized']['en'])] = k
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
end
return res
return res
end
local mPlatFormPackData = {}
local function mobilePackSection(pack)
return pack['section']
end
local lSectionNS = {'free', 'mainstory', 'sidestory'}
local function nsPackSection(pack)
if pack['pack_parent'] then return nil end
if mw.loadJsonData 'Template:Transition.json'.packName[pack['id']] then return 'collab' end
return lSectionNS[pack['section']]
end
end
function p.packQueryWrap(id, platform)
function p.packQueryWrap(id, platform)
local packData = mPlatFormPackData[platform]
local packData = mPlatFormPackData[platform]
if not packData then
if not packData then
packData = {}
packData = factPackData(platform)
mPlatFormPackData[platform] = packData
mPlatFormPackData[platform] = packData
local packSection = platform == 'ns' and nsPackSection or mobilePackSection
for idx, pack in ipairs(p.listOf('packs', platform)) do
local item = {pack['pack_parent'], name = pack['name_localized']['en'], section = packSection(pack), numero = idx}
packData[pack['id']] = item
end
packData['single'] = {name = 'Memory Archive', section = 'single', numero = 0}
for _, item in pairs(packData) do
local parentId = unpack(item)
local parent
if parentId then
parent = packData[parentId]
if item.name:match 'Collaboration Chapter' then
item.name = parent.name .. ' ' .. item.name
end
end
item.section = item.section or parent.section
end
end
end
-- return id and packData[id] or packData
-- return id and packData[id] or packData
return packData[id] or {name = '请更新plst', section = 'unknown'}
return packData[id] or {name = '请更新plst', section = 'unknown'}
end
function makeInvokeFunc(funcName)
return function(frame)
local args = getArgs(frame)
return p[funcName](args)
end
end
-- 面向wikitext直接查询
p.Song_Query = makeInvokeFunc '_Song_Query'
function p._Song_Query(args)
return p.singleSongInformation(args[1], args[2], args[4])(args[3])
end
p.Pack_Query = makeInvokeFunc '_Pack_Query'
function p._Pack_Query(args)
return p.packQueryWrap(args[1], args[2]).name
end
end


return p
return p

2024年2月24日 (六) 12:01的版本

可在Module:沙盒/盐棋/Sandbox1/doc创建此模块的帮助文档

local checkType = require 'libraryUtil'.checkType
local lang = mw.language.getContentLanguage()
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

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 commonFunc = {}
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 not diff then return nil end
	return diff.rating .. (diff.ratingPlus and '+' or '')
end
function commonFunc.linkName(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.linkTitle(obj)
	local res = {commonFunc.linkName(obj)}
	table.insert(res, obj.title_localized.en)
	return table.concat(res, '|')
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 function songQuery(t, prop)
	checkType('songQuery', 2, prop, 'string')
	local obj = getmetatable(t)._info_
	if next(obj) == nil then return nil end
	local expr = commonExpr[prop]
	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)
end
function p.songQueryWrap(song)
	checkType('songQueryWrap', 1, song, 'table')
	return setmetatable({}, {__call = songQuery, __index = songQuery, _info_ = song})
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

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)
	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