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

来自Arcaea中文维基
(fixbug11)
(fixbug12)
第38行: 第38行:
if opChain("audioOverride") then
if opChain("audioOverride") then
p.cfg["bydSongOverride"]={
p.cfg["bydSongOverride"]={
title=p.byd["title_localized"],
title_localized=p.byd["title_localized"]["en"],
artist=p.byd["artist"],
artist=p.byd["artist"],
bpm=p.byd["bpm"],
bpm=p.byd["bpm"],

2022年5月6日 (五) 01:32的版本

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

local p={byd=nil,cfg=nil}
local W=mw

local function opChain(tar)
	-- local status=pcall(function (args)
	-- 	for _,key in ipairs(args) do
	-- 		obj = obj[key]
	-- 	end
	-- end,{...})
	-- if status then return obj else return nil end
	local status,result=pcall(function ()return p.byd[tar] end)
	if status then return result else return nil end
end

local function LoadJson(filename)
    return W.text.jsonDecode(W.getCurrentFrame():expandTemplate{ title = filename })
end

local function cfgPush(key,val)
	if val~=nil then
		if p.cfg[key]==nil then
			p.cfg[key]={}
		end
		p.cfg[key][#p.cfg[key]+1]=val
	end
end

local function NSBydFilter(info)
	-- assert(p.byd==nil or p.byd["ratingClass"]==3,"NS不会有愚人节谱")
	local NsBydSet={["tempestissimo"] = true}
	if not NsBydSet[info["id"]] then
		p.byd, info["difficulties"][4] = nil, nil
	end
	return info
end

local function AudioFilter(info)
	if opChain("audioOverride") then
		p.cfg["bydSongOverride"]={
			title_localized=p.byd["title_localized"]["en"],
			artist=p.byd["artist"],
			bpm=p.byd["bpm"],
			bpm_base=p.byd["bpm_base"],
		}
	end
	return info
end

local function BGFilter(info)
	assert(info["bg"] and info["side"],"'bg'字段必须存在")
	local board={[0]="light",[1]="conflict"}
	if info["bg"]=="" then info["bg"] = "base_"..board[info["side"]] end
	return info
end

local function TitleFilter(info)
	assert(info["title_localized"]["en"],"'en'字段必须存在")
	cfgPush("title",info["title_localized"]["en"])
	cfgPush("title",info["title_localized"]["ja"])
	cfgPush("title",info["title_localized"]["zh-Hans"])
	return info
end

local function JacketFilter(info)
	if info["jacket_localized"] then-- solitarydream
		cfgPush("jacketOverride","lang")
	elseif info["difficulties"][1]["jacket_night"] then-- melodyoflove
		cfgPush("jacketOverride","sunset")
	elseif info["difficulties"][1]["jacketOverride"] then-- stager, avril, lanota2
		cfgPush("jacketOverride","pstprs")
	elseif opChain("jacketOverride") then
		-- assert(not p.cfg["jacketOverride"],"多曲曲绘画出现byd,请更新代码")
		cfgPush("jacketOverride","byd")
	end
	return info
end

function QuerySong(json,key,queryById,...)
	local searcher
	if queryById then
		-- quon, ifi, aiueoon
		searcher = function (x)return x['id']end
	else
		key = W.text.decode(key,true)
		searcher = function (x)return W.ustring.gsub(x['title_localized']['en'],"^%l",W.ustring.upper)end
	end
	for _, song in ipairs(json["songs"]) do
		if searcher(song) == key then
			p.byd, p.cfg = song["difficulties"][4], {}
			for _, filter in ipairs({...}) do filter(song) end
			song.__config=p.cfg
			return song
		end
	end
end

--bg,date,version
function p.GetSong(args)
	local Mobile = QuerySong(LoadJson("Songlist"),args['曲名'],args['id'],BGFilter,TitleFilter,JacketFilter,AudioFilter)
	local NS = QuerySong(LoadJson("Songlist NS"),args['曲名'],args['id'],NSBydFilter,BGFilter,TitleFilter,JacketFilter)
	local main=Mobile or NS
	if Mobile and NS then
		--
	end
	return W.text.jsonEncode({Mobile=Mobile, NS=NS, args=args},W.text.JSON_PRETTY)
end

return p