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

	local utility = {}
	
-- 版本
utility.version={'2.1.3','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

--分类
utility.category = {
	['ethics'] = '思潮',
	['agendas'] = '议程',
	['authorities'] = '权力制度',
	['subjects'] = '附庸',
	['traits'] = '特质',
	['pop_faction_types'] = '派系',
	['war_goals'] = '战争目标',
	['casus_belli'] = '宣战借口',
	['planet_classes'] = '星球类型',
	['terraform'] = '地貌改造',
	['planet_modifiers'] = '行星修正',
	['tile_blockers'] = '地块障碍',
	['deposits'] = '资源站点',
	['ship_behaviors'] = '舰船行为',
	['ship_sizes'] = '舰船等级',
	['section_templates'] = '舰船区块',
	['component_templates'] = '舰船组件',
	['starbase_levels'] = '恒星基地等级',
	['starbase_buildings'] = '恒星基地建筑',
	['starbase_modules'] = '恒星基地模块',
	['bombardment_stances'] = '轨道轰炸',
	['personalities'] = 'AI人格',
	['policies'] = '政策',
	['civics'] = '国家理念',
	['traditions'] = '传统',
	['ascension_perks'] = '飞升天赋',
	['governments'] = '政府',
	['edicts'] = '法令',
	['armies'] = '陆军',
	['strategic_resources'] = '资源',
	['megastructures'] = '巨型建筑',
	['buildings'] = '建筑',
	['technology'] = '科技', 
	['events'] = '事件'
}

function utility.get_category(result)
	if #result~= 1 then
		return -1
	else
		data = result[1]
		return utility.category[data.main_category]
	end
end

-- 查询翻译
function utility.get_trans_data(sets)
	range = 'Data:'
	if not sets then
		return {}
	end
	local query = {
		['_id'] = { ["$regex"] = range },
	}
	result = mw.huiji.db.find(query, sets)

	return result
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, sets)
	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]
	}
	if sets then
		result = mw.huiji.db.find(query, sets)
	else
		result = mw.huiji.db.find(query)
	end

	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)
	replace_table = {
		-- 基础替换
		['\\n'] = '<br />', 
		['(§.)§.(.*?§!)§!'] = '%1%2',
		['§!'] = '</span>',
		
		-- P社颜色替换
		['§M'] = '<span style=\"color:rgb(163,53,238)\">',
		['§L'] = '<span style=\"color:rgb(195,176,145)\">',
		['§G'] = '<span style=\"color:rgb(41,225,38)\">',
		['§R'] = '<span style=\"color:rgb(252,86,70)\">',
		['§B'] = '<span style=\"color:rgb(51,167,255)\">',
		['§Y'] = '<span style=\"color:rgb(247,252,52)\">',
		['§H'] = '<span style=\"color:rgb(251,170,41)\">',
		['§T'] = '<span style=\"color:rgb(255,255,255)\">',
		['§E'] = '<span style=\"color:rgb(135,255,207)\">',
		['§S'] = '<span style=\"color:rgb(228,156,42)\">',
		['§W'] = '<span style=\"color:rgb(255,255,255)\">',
		['§P'] = '<span style=\"color:rgb(225,110,110)\">',
		['§g'] = '<span style=\"color:rgb(128,128,128)\">',
		['§l'] = '<span style=\"color:rgb(51,167,255)\">',
		
		-- P社图标替换
		['£time'] = '[[文件:Time.png|时间|link=Item:Strategic_resources/time]]',
		['£blocker'] = '[[文件:Text_icon_tile_blocker.png|link=]]',
		['£trigger_yes'] = '[[文件:Trigger_yes.png|link=]]',
		['£trigger_no'] = '[[文件:Trigger_no.png|link=]]',
		['£opinion'] = '[[文件:Thinking_bubble.png|好感度|link=]]',
		['£pops'] = '[[文件:Pops.png|人口|link=]]',
		['£pop'] = '[[文件:Icon_text_pop.png|人口|link=]]',
		['£happiness'] = '[[文件:Tooltip_happiness.png|幸福度|link=]]',
		['£building'] = '[[文件:Tooltip_building.png|建筑|link=]]',
		['£planetsize'] = '[[文件:Planet_size.png|星球大小|link=]]',
		['£influence'] = '[[文件:Influence.png|影响力|link=Item:Strategic_resources/influence]]',
		['£unity'] = '[[文件:Unity.png|凝聚力|link=Item:Strategic_resources/unity]]',
		['£energy'] = '[[文件:Energy.png|能量币|link=Item:Strategic_resources/energy]]',
		['£physics'] = '[[文件:Physics_research.png|物理研究|link=Item:Strategic_resources/physics_research]]',
		['£society'] = '[[文件:Society_research.png|社会研究|link=Item:Strategic_resources/society_research]]',
		['£engineering'] = '[[文件:Engineering_research.png|工程研究|link=Item:Strategic_resources/engineering_research]]',
		['£minerals'] = '[[文件:Minerals.png|矿物|link=Item:Strategic_resources/minerals]]',
		['£food'] = '[[文件:Food.png|食物|link=Item:Strategic_resources/food]]',
		['£sr_terraform_gases'] = '[[文件:Sr_terraform_gases.png|战略资源|link=Item:Strategic_resources/sr_terraform_gases]]',
		['£sr_terraform_liquids'] = '[[文件:Sr_terraform_liquids.png|战略资源|link=Item:Strategic_resources/sr_terraform_liquids]]',
		['£sr_garanthium'] = '[[文件:Sr_garanthium.png|战略资源|link=Item:Strategic_resources/sr_garanthium]]',
		['£sr_lythuric'] = '[[文件:Sr_lythuric.png|战略资源|link=Item:Strategic_resources/sr_lythuric]]',
		['£sr_teldar'] = '[[文件:Sr_teldar.png|战略资源|link=Item:Strategic_resources/sr_teldar]]',
		['£sr_yuranic'] = '[[文件:Sr_yuranic.png|战略资源|link=Item:Strategic_resources/sr_yuranic]]',
		['£sr_orillium'] = '[[文件:Sr_orillium.png|战略资源|link=Item:Strategic_resources/sr_orillium]]',
		['£sr_pitharan'] = '[[文件:Sr_pitharan.png|战略资源|link=Item:Strategic_resources/sr_pitharan]]',
		['£sr_engos'] = '[[文件:Sr_engos.png|战略资源|link=Item:Strategic_resources/sr_engos]]',
		['£sr_neutronium'] = '[[文件:Sr_neutronium.png|战略资源|link=Item:Strategic_resources/sr_neutronium]]',
		['£sr_living_metal'] = '[[文件:Sr_living_metal.png|战略资源|link=Item:Strategic_resources/sr_living_metal]]',
		['£sr_zro'] = '[[文件:Sr_zro.png|战略资源|link=Item:Strategic_resources/sr_zro]]',
		['£sr_dark_matter'] = '[[文件:Sr_dark_matter.png|战略资源|link=Item:Strategic_resources/sr_dark_matter]]',
		['£sr_satramene'] = '[[文件:Sr_satramene.png|战略资源|link=Item:Strategic_resources/sr_satramene]]',
		['£sr_alien_pets'] = '[[文件:Sr_alien_pets.png|资源|link=Item:Strategic_resources/sr_alien_pets]]',
		['£sr_betharian'] = '[[文件:Sr_betharian.png|资源|link=Item:Strategic_resources/sr_betharian]]',
		['£sr_riggan'] = '[[文件:Sr_riggan.png|战略资源|link=Item:Strategic_resources/sr_riggan]]',
		['£sr_xuran'] = '[[文件:Sr_xuran.png|战略资源|link=Item:Strategic_resources/sr_xuran]]',
		['£sr_muutagan'] = '[[文件:Sr_muutagan.png|战略资源|link=Item:Strategic_resources/sr_muutagan]]',
		['£sr_yurantic'] = '[[文件:Sr_yurantic.png|战略资源|link=Item:Strategic_resources/sr_yurantic]]',
		['£sr_aldar'] = '[[文件:Sr_aldar.png|战略资源|link=Item:Strategic_resources/sr_aldar]]',
		['£sr_distar_nanodust'] = '[[文件:Sr_distar_nanodust.png|战略资源|link=Item:Strategic_resources/sr_distar_nanodust]]',
		['£sr_distar_nanocrystal'] = '[[文件:Sr_distar_nanocrystal.png|战略资源|link=Item:Strategic_resources/sr_distar_nanocrystal]]',
		['£sr_distar_nanoscale'] = '[[文件:Sr_distar_nanoscale.png|战略资源|link=Item:Strategic_resources/sr_distar_nanoscale]]',
		['£sr_distar_nanoactuator'] = '[[文件:Sr_distar_nanoactuator.png|战略资源|link=Item:Strategic_resources/sr_distar_nanoactuator]]',
		
		-- 一些逻辑替换
		['▲'] = '[[文件:Trigger_yes.png|link=]]',
		['△'] = '[[文件:Trigger_no.png|link=]]',
		['〓'] = '<span style="padding:0px 0px 0px 28px"></span>[[文件:Choices.png|不同情况分支|29px|link=]]',
		['№'] = '[[文件:Species_right_colonization_rights.png|权重 = 基础值 × 系数|29px|link=]]',
		[':(#%x%x%x%x%x%x)'] = ':<span style=\"color:%1\">%1</span>',
		['※(%d*)'] = '<span style="padding:0px 0px 0px %1px"></span>',
	}
	local newstr = str
	for old_key, new_value in pairs(replace_table) do
		newstr = string.gsub(newstr, old_key, new_value)
	end
	return newstr
end
return utility
0.0
0人评价
avatar