Module:沙盒/盐棋/Sandbox1

来自Arcaea中文维基
< Module:沙盒
盐棋讨论 | 贡献2022年3月12日 (六) 17:38的版本 (修bug2)

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

local p={}

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

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

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

local once={}
function once:new(o)
	o = o or {
		val=nil
	}
	setmetatable(o, self)
	self.__index = self
	return o
end
function once:set(val)
	assert(self.val==nil or val==nil,"重复赋值")
	self.val=val
end
local function NameAudioFilter(info)
	assert(info["title_localized"]["en"],"'en'字段必须存在")
	local first=info["title_localized"]["en"]
	local second=once:new()
	local bydOverride=nil
	second.val=info["title_localized"]["ja"]
	second.val=info["title_localized"]["zh-Hans"]
	if info["difficulties"][4] and info["difficulties"][4]["audioOverride"] then
		assert(info["difficulties"][4]["title_localized"]["en"],"byd'en'字段必须存在")
		second.val=info["difficulties"][4]["title_localized"]["en"]
		bydOverride={
			title=info["difficulties"][4]["title_localized"]["en"],
			artist=info["difficulties"][4]["artist"],
			bpm=info["difficulties"][4]["bpm"],
			bpm_base=info["difficulties"][4]["bpm_base"],
		}
	end
	info["title_localized"]={first,second.val,bydOverride}
	-- info["bydOverride"]=bydOverride --magic
	return info
end

function QuerySong(json,index,queryById,...)
	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
	for _, v in ipairs({...}) do
		assert(type(v)=="function","You Input Illegal Filter Type?")
		r=v(r)
	end
	return r
end

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

return p