Module:沙盒/盐棋/Sandbox1

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

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

local p = {
	plat={""," NS"}
}

local once={}
function once:new(o)
	o = o or {
		val=nil
	}
	setmetatable(o, self)
	self.__index = self
	return o
end
function once:set(val)
	if self.val~=nil and val~=nil then
		error("重复赋值")
	else
		self.val=val
	end
end

local function MyAssert(expr, err)
	if not expr then
		error(err)
	end
end

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

local function NSBydFilter(info)
	local NsBydSet={["tempestissimo"] = true}
	MyAssert(info["difficulties"][4]==nil or info["difficulties"][4]["ratingClass"]==3,"Do NS Have April Fool's Chart?")
	if not NsBydSet[info["id"]] then
		info["difficulties"][4] = nil
	end
	return info
end

local function BGFilter(info)
	local board={"light","conflict"}
	info["bg"] = info["bg"]~="" and info["bg"] or "base_"..board[info["side"]]
	return info
end

local function NameFilter(info)
	local first=info["title_localized"]["en"]
	local second=once:new()
	second.val=info["title_localized"]["ja"]
	second.val=info["title_localized"]["zh-Hans"]
	if info["difficulties"][4] then
		second.val=info["difficulties"][4]["title_localized"]["en"]
	end
	info["title_localized"]={first,second.val}
	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
		MyAssert(type(v)=="function","You Input Illegal Filter Type?")
		r=v(r)
	end
	return r
end

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

return p