用法示例:
使用

{{#invoke:CricketInfo|show|两头枪|图片}}

{{#invoke:CricketInfo|show|两头枪|data96}}

可查询[两头枪]的[图片]属性为:"Cricket_36_8.png"

使用

{{#invoke:CricketInfo|mian|两头枪}}

或在两头枪页面使用

{{#invoke:CricketInfo|mian}}

可生成促织页面,详见模板:促织页面

使用

{{#invoke:CricketInfo|place|1}}

可随机生成促织藏匿处,详见模板:促织藏处/item,参数为随机种子,可为空


local p = {}

local Seed = 0

local otherName=
{
	['名字'] = 'data0',
	['品级'] = 'data1',
	['描述'] = 'data99',
	['图片'] = 'data96',
	['音高'] = 'data9',
	['音量'] = 'data10',
	['耐力'] = 'data11',
	['斗性'] = 'data12',
	['气势'] = 'data21',
	['角力'] = 'data22',
	['牙钳'] = 'data23',
	['暴击概率'] = 'data31',
	['暴击增伤'] = 'data32',
	['击伤'] = 'data36',
	['格挡概率'] = 'data33',
	['格挡数值'] = 'data34',
	['反击概率'] = 'data35',
	['寿命'] = 'data98',
	['细腻'] = 'data52001',
	['聪颖'] = 'data52002',
	['水性'] = 'data52003',
	['勇壮'] = 'data52004',
	['冷静'] = 'data52005',
	['福缘'] = 'data52006',
}

function findOne(name)
	local query = 
	{
		['_id'] = { ["$regex"] = '^Data:AllCricket.tab' },
		['data0'] = name,
	}
	local result = mw.huiji.db.findOne(query)
	--判断table是否为空:
	mw.log('findOne:'..tostring(next(result)~=nil))
	return result --返回包含该蛐蛐各字段的table,若不存在则返回{}
end

function findAll()
	local query = 
	{
		['_id'] = { ["$regex"] = '^Data:AllCricket.tab' },
	}
	local result = mw.huiji.db.find(query)
	return result
end

function getValue(cricket, key)
	key = otherName[key] or key
	result = cricket[key] or ""
	return result
end

--查询某促织的某项属性
function p.show(frame)
	local args = frame.args or frame
	local name = args['name'] or args[1]
	local key = args['key'] or args[2]
	local cricket = findOne(name)
	local result = getValue(cricket, key)
	mw.log(result)
	return result
end

function random(min, max)
	Seed = Seed + 1
	math.randomseed(tostring(os.time()*Seed):reverse():sub(1, 7))--时钟*种子,倒转后取前7位
	local rand = math.random()
	seed = seed or 1
	min = min or 0
	max = max or 1
	local result = min + (max-min)*rand
	--mw.log('rand='..tostring(rand))
	return result
end

--生成促织藏匿处	
function p.place(frame)
	local args = frame.args or frame
	local seed = args['seed'] or args[1]
	Seed = tonumber(seed) or Seed
	local all = findAll()
	local index = math.floor(random(0, #all)) + 1
	local theOne = all[index]
	mw.log(theOne.data0)
	local catch = random(0, 20)
	local time = random(0.95, 1.05)
	local hide = random(0, 10)
	hide = string.format('%d', hide)
	theOne['catch'] = catch
	theOne['time'] = time
	theOne['hide'] = hide
	local result = mw.getCurrentFrame():expandTemplate{ title = '促织藏处/item2', args = theOne }
	--mw.log(result)
	return result
end

--生成促织页面
--mian乃茄语,不是拼写错误
function p.mian(frame)
	local args = frame.args or frame
	local title = mw.title.getCurrentTitle().text
	local name = args[1] or title
	local sp = title ~= name
	local tab = findOne(name)
	tab.sp = sp or nil
	local result = next(tab)==nil and '无此促织' or mw.getCurrentFrame():expandTemplate{ title = '促织页面', args = tab }
	--mw.log(result)
	return result
end

--生成促织品级页面
function p.class(frame)
	local args = frame.args or frame
	local class = tonumber(args['class'] or args[1])
	if class==nil or class>9 or class<1 or math.floor(class)~=class then
		return '输入的品级应为1~9的整数'
	end
	local query = 
	{
		['_id'] = { ["$regex"] = '^Data:AllCricket.tab' },
		['data1'] = class
	}
	local tab = mw.huiji.db.find(query)
	mw.log('数量', tostring(#tab))
	local toLong = #tab > 500
	local result = '<div style="column-count:4; height:auto;"><ul>\n'
	for key, value in pairs(tab) do 
		local pic = value['data96']
		local name = value['data0']
		result = result..'<li>'
		--if not toLong then
		--	result = result..'<div style="transform:rotate(270deg);display:inline-block">'
		--	result = result..'[[文件:'..pic..'|32px|link='..name..']]</div>'
		--end
		result = result..'[['..name..']]'
		result = result..'</li>\n'
	end
	result = result..'</ul></div>'
	--mw.log(result)
	return result
end

function p.t()
	for i=1,10 do
		p.place({})
	end
end

return p
avatar