此模块的文档可以在模块:Common/Utility.lua/doc创建

	local utility = {}
	
-- 版本
utility.version={'2.1.1','2.1.0','2.0.4','2.0.2','2.0.1','2.0.0','1.9.1','1.8.3','1.7.4','1.6.2','1.5.1','1.4.1','1.3.2','1.2.5','1.1.0','1.0.3'}
function utility.versions()
	return utility.version
end

function utility.get_version()
	return utility.version[1]
end

--新查询,未实装
function utility.new_query(frame)
	local args = getArgs(frame)
	local range = 'Data:'
	local query = {}
	
	for key, value in pairs(args) do
		if key == 1 then
			query['key'] = value
		elseif key == 'query' then
			range = 'Data:'..value
		elseif key == '名称' then
			query['zhcn_name'] = value
		elseif key == tonumber(key) then
		else
			query[key] = value
		end
	end
	query['_id'] = { ['$regex'] = range }
	return mw.huiji.db.find(query)
end

-- 查询
function utility.get_data(key, key_type, range)
	range = range or 'Data:'
	key_type = key_type or 'zhcn_name'
	
	if not key then
		return {}
	end
 
	local query = {
		['_id'] = { ["$regex"] = range },
		[key_type] = key,
		['version'] = utility.version[1]
	}
 
	local result = mw.huiji.db.find(query)

	table.sort(result,function(a,b)
		return (a.main_category == b.main_category) and (a._index < b._index) or (a.main_category < b.main_category)
		end)
	return result
end

-- 数量查询
function utility.count_data(key, key_type, range)
	range = range or 'Data:'
	key_type = key_type or 'zhcn_name'
	
	if not key then
		return 0
	end
 
	local query = {
		['_id'] = { ["$regex"] = range },
		[key_type] = key
	}
 
	return mw.huiji.db.count(query)
end

-- 取整
function utility.floor(x)
	if x <= 0 then
		return math.ceil(x);
	end

	if math.ceil(x) == x then
		x = math.ceil(x);
	else
		x = math.ceil(x) - 1;
	end
	return x;
end

-- P社的颜色规则替换成HTML颜色规则
function utility.set_style(str)
	local newstr = string.gsub(str,'\\n','<br />')
	newstr = string.gsub(newstr,'(§.)§.(.*?§!)§!','%1%2')
	newstr = string.gsub(newstr,'§M','<span style=\"color:rgb(163,53,238)\">')
	newstr = string.gsub(newstr,'§L','<span style=\"color:rgb(195,176,145)\">')
	newstr = string.gsub(newstr,'§G','<span style=\"color:rgb(41,225,38)\">')
	newstr = string.gsub(newstr,'§R','<span style=\"color:rgb(252,86,70)\">')
	newstr = string.gsub(newstr,'§B','<span style=\"color:rgb(51,167,255)\">')
	newstr = string.gsub(newstr,'§Y','<span style=\"color:rgb(247,252,52)\">')
	newstr = string.gsub(newstr,'§H','<span style=\"color:rgb(251,170,41)\">')
	newstr = string.gsub(newstr,'§T','<span style=\"color:rgb(255,255,255)\">')
	newstr = string.gsub(newstr,'§E','<span style=\"color:rgb(135,255,207)\">')
	newstr = string.gsub(newstr,'§S','<span style=\"color:rgb(228,156,42)\">')
	newstr = string.gsub(newstr,'§W','<span style=\"color:rgb(255,255,255)\">')
	newstr = string.gsub(newstr,'§P','<span style=\"color:rgb(225,110,110)\">')
	newstr = string.gsub(newstr,'§g','<span style=\"color:rgb(128,128,128)\">')
	newstr = string.gsub(newstr,'§l','<span style=\"color:rgb(51,167,255)\">')
	newstr = string.gsub(newstr,'§!','</span>')
	-- P社图标替换
	newstr = string.gsub(newstr,'£pops','[[文件:Pops.png|人口|link=]]')
	newstr = string.gsub(newstr,'£pop','[[文件:Icon_text_pop.png|人口|link=]]')
	newstr = string.gsub(newstr,'£happiness','[[文件:Tooltip_happiness.png|幸福度|link=]]')
	newstr = string.gsub(newstr,'£building','[[文件:Tooltip_building.png|建筑|link=]]')
	newstr = string.gsub(newstr,'£planetsize','[[文件:Planet_size.png|星球大小|link=]]')
	newstr = string.gsub(newstr,'£influence','[[文件:Influence.png|影响力|link=Item:Strategic_resources/influence]]')
	newstr = string.gsub(newstr,'£unity','[[文件:Unity.png|凝聚力|link=Item:Strategic_resources/unity]]')
	newstr = string.gsub(newstr,'£energy','[[文件:Energy.png|能量币|link=Item:Strategic_resources/energy]]')
	newstr = string.gsub(newstr,'£physics','[[文件:Physics_research.png|物理研究|link=Item:Strategic_resources/physics_research]]')
	newstr = string.gsub(newstr,'£society','[[文件:Society_research.png|社会研究|link=Item:Strategic_resources/society_research]]')
	newstr = string.gsub(newstr,'£engineering','[[文件:Engineering_research.png|工程研究|link=Item:Strategic_resources/engineering_research]]')
	newstr = string.gsub(newstr,'£minerals','[[文件:Minerals.png|矿物|link=Item:Strategic_resources/minerals]]')
	newstr = string.gsub(newstr,'£food','[[文件:Food.png|食物|link=Item:Strategic_resources/food]]')
	newstr = string.gsub(newstr,'£sr_terraform_gases','[[文件:Sr_terraform_gases.png|战略资源|link=Item:Strategic_resources/sr_terraform_gases]]')
	newstr = string.gsub(newstr,'£sr_terraform_liquids','[[文件:Sr_terraform_liquids.png|战略资源|link=Item:Strategic_resources/sr_terraform_liquids]]')
	newstr = string.gsub(newstr,'£sr_garanthium','[[文件:Sr_garanthium.png|战略资源|link=Item:Strategic_resources/sr_garanthium]]')
	newstr = string.gsub(newstr,'£sr_lythuric','[[文件:Sr_lythuric.png|战略资源|link=Item:Strategic_resources/sr_lythuric]]')
	newstr = string.gsub(newstr,'£sr_teldar','[[文件:Sr_teldar.png|战略资源|link=Item:Strategic_resources/sr_teldar]]')
	newstr = string.gsub(newstr,'£sr_yuranic','[[文件:Sr_yuranic.png|战略资源|link=Item:Strategic_resources/sr_yuranic]]')
	newstr = string.gsub(newstr,'£sr_orillium','[[文件:Sr_orillium.png|战略资源|link=Item:Strategic_resources/sr_orillium]]')
	newstr = string.gsub(newstr,'£sr_pitharan','[[文件:Sr_pitharan.png|战略资源|link=Item:Strategic_resources/sr_pitharan]]')
	newstr = string.gsub(newstr,'£sr_engos','[[文件:Sr_engos.png|战略资源|link=Item:Strategic_resources/sr_engos]]')
	newstr = string.gsub(newstr,'£sr_neutronium','[[文件:Sr_neutronium.png|战略资源|link=Item:Strategic_resources/sr_neutronium]]')
	newstr = string.gsub(newstr,'£sr_living_metal','[[文件:Sr_living_metal.png|战略资源|link=Item:Strategic_resources/sr_living_metal]]')
	newstr = string.gsub(newstr,'£sr_zro','[[文件:Sr_zro.png|战略资源|link=Item:Strategic_resources/sr_zro]]')
	newstr = string.gsub(newstr,'£sr_dark_matter','[[文件:Sr_dark_matter.png|战略资源|link=Item:Strategic_resources/sr_dark_matter]]')
	newstr = string.gsub(newstr,'£sr_satramene','[[文件:Sr_satramene.png|战略资源|link=Item:Strategic_resources/sr_satramene]]')
	newstr = string.gsub(newstr,'£sr_alien_pets','[[文件:Sr_alien_pets.png|资源|link=Item:Strategic_resources/sr_alien_pets]]')
	newstr = string.gsub(newstr,'£sr_betharian','[[文件:Sr_betharian.png|资源|link=Item:Strategic_resources/sr_betharian]]')
	newstr = string.gsub(newstr,'£sr_riggan','[[文件:Sr_riggan.png|战略资源|link=Item:Strategic_resources/sr_riggan]]')
	newstr = string.gsub(newstr,'£sr_xuran','[[文件:Sr_xuran.png|战略资源|link=Item:Strategic_resources/sr_xuran]]')
	newstr = string.gsub(newstr,'£sr_muutagan','[[文件:Sr_muutagan.png|战略资源|link=Item:Strategic_resources/sr_muutagan]]')
	newstr = string.gsub(newstr,'£sr_yurantic','[[文件:Sr_yurantic.png|战略资源|link=Item:Strategic_resources/sr_yurantic]]')
	newstr = string.gsub(newstr,'£sr_aldar','[[文件:Sr_aldar.png|战略资源|link=Item:Strategic_resources/sr_aldar]]')
	newstr = string.gsub(newstr,'£sr_distar_nanodust','[[文件:Sr_distar_nanodust.png|战略资源|link=Item:Strategic_resources/sr_distar_nanodust]]')
	newstr = string.gsub(newstr,'£sr_distar_nanocrystal','[[文件:Sr_distar_nanocrystal.png|战略资源|link=Item:Strategic_resources/sr_distar_nanocrystal]]')
	newstr = string.gsub(newstr,'£sr_distar_nanoscale','[[文件:Sr_distar_nanoscale.png|战略资源|link=Item:Strategic_resources/sr_distar_nanoscale]]')
	newstr = string.gsub(newstr,'£sr_distar_nanoactuator','[[文件:Sr_distar_nanoactuator.png|战略资源|link=Item:Strategic_resources/sr_distar_nanoactuator]]')

	newstr = string.gsub(newstr,'£time','[[文件:Time.png|时间|link=Item:Strategic_resources/time]]')
	newstr = string.gsub(newstr,'£blocker','[[文件:Text_icon_tile_blocker.png|link=]]')
	newstr = string.gsub(newstr,'£trigger_yes','[[文件:Trigger_yes.png|link=]]')
	newstr = string.gsub(newstr,'£trigger_no','[[文件:Trigger_no.png|link=]]')
	newstr = string.gsub(newstr,'£opinion','[[文件:Thinking_bubble.png|好感度|link=]]')

	
	-- 一些逻辑替换
	newstr = string.gsub(newstr,'▲','[[文件:Trigger_yes.png|link=]]')
	newstr = string.gsub(newstr,'△','[[文件:Trigger_no.png|link=]]')
	newstr = string.gsub(newstr,'〓','<span style="padding:0px 0px 0px 28px"></span>[[文件:Choices.png|不同情况分支|29px|link=]]')
	newstr = string.gsub(newstr,'№','[[文件:Species_right_colonization_rights.png|权重 = 基础值 × 系数|29px|link=]]')
	newstr = string.gsub(newstr,':(#%x%x%x%x%x%x)',':<span style=\"color:%1\">%1</span>')
	newstr = string.gsub(newstr,'※(%d*)','<span style="padding:0px 0px 0px %1px"></span>')

	return newstr
end
return utility
0.0
0人评价
avatar