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

来自Arcaea中文维基
(单引号定位)
(字符串连接符写错了)
第91行: 第91行:
end
end
end
end
assert(r,(queryById and "id" or "name")+":"+index)
assert(r,(queryById and "id" or "name")..":"..index)
p.byd=r["difficulties"][4]
p.byd=r["difficulties"][4]
r["__config"]={} --magic
r["__config"]={} --magic

2022年3月14日 (一) 11:41的版本

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

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

local function LoadJson(filename)
    return mw.text.jsonDecode(mw.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
		info["difficulties"][4] = nil
	end
	return info
end

local function MobileBydFilter(info)
	if p.byd then
		if p.byd["audioOverride"] then
			if p.byd["title_localized"] then
				cfgPush("title",p.byd["title_localized"]["en"])
			end
			p.cfg["bydSongOverride"]={
				title=p.byd["title_localized"]["en"],
				artist=p.byd["artist"],
				bpm=p.byd["bpm"],
				bpm_base=p.byd["bpm_base"],
			}
		end
		if p.byd["jacketOverride"] then
			-- assert(not p.cfg["jacketOverride"],"多曲曲绘画出现byd,请更新代码")
			cfgPush("jacketOverride","byd")
		end
	end
	return info
end

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

local function NameAudioFilter(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
		cfgPush("jacketOverride","pstprs")
	end
	return info
end

function QuerySong(json,index,queryById,...)
	if json then
		local r
		if queryById then
			for _, v in ipairs(json["songs"]) do
				if v['id'] == index then
					r = v
					break
				end
			end
		else
			for _, v in ipairs(json["songs"]) do
				if v['title_localized']['en'] == index then
					r = v
					break
				end
			end
		end
		assert(r,(queryById and "id" or "name")..":"..index)
		p.byd=r["difficulties"][4]
		r["__config"]={} --magic
		p.cfg=r["__config"]
		for _, v in ipairs({...}) do
			assert(type(v)=="function","You Input Illegal Filter Type")
			r=v(r)
		end
		return r
	end
end

--bg,date,version
function p.GetSong(args)
	local Mobile = QuerySong(LoadJson("Songlist"),args['曲名'],args['id'],BGFilter,NameAudioFilter,JacketFilter,MobileBydFilter)
	local NS = QuerySong(LoadJson("Songlist NS"),args['曲名'],args['id'],NSBydFilter,BGFilter,NameAudioFilter,JacketFilter)
	return mw.text.jsonEncode({Mobile=Mobile, NS=NS, args=args},mw.text.JSON_PRETTY)
end

return p