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

	local utility = {}
	
-- 版本
utility.version={'2.2.6', '2.2.4', '2.2.3', '2.2.2', '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'] = '行星修正',
	['static_modifiers'] = '静态修正',
	['tile_blockers'] = '地块障碍',
	['deposits'] = '矿脉',
	['ship_behaviors'] = '舰船行为',
	['ship_sizes'] = '舰船等级',
	['section_templates'] = '舰船区块',
	['component_templates'] = '舰船组件',
	['star_classes'] = '星系类型',
	['starbase_levels'] = '恒星基地等级',
	['starbase_buildings'] = '恒星基地建筑',
	['starbase_modules'] = '恒星基地模块',
	['bombardment_stances'] = '轨道轰炸',
	['personalities'] = 'AI人格',
	['policies'] = '政策',
	['civics'] = '国家理念',
	['traditions'] = '传统',
	['ascension_perks'] = '飞升天赋',
	['governments'] = '政府',
	['edicts'] = '法令',
	['armies'] = '陆军',
	['strategic_resources'] = '资源',
	['districts'] = '区划',
	['megastructures'] = '巨型建筑',
	['pop_categories'] = '人口分类', 
	['pop_jobs'] = '职业', 
	['buildings'] = '建筑',
	['technology'] = '科技', 
	['events'] = '事件',
	['achievements'] = '成就',
}

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, hidden_old)
	range = range or 'Data:'
	range = '^'..range
	key_type = key_type or 'zhcn_name'
	
	if not key then
		return {}
	end
	local query = {}
	if (not hidden_old) or hidden_old == 'false' then
		query = {
			['_id'] = { ["$regex"] = range },
			[key_type] = key,
		}
	else
		query = {
			['_id'] = { ["$regex"] = range },
			[key_type] = key,
			['version'] = utility.version[1]
		}
	end
	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:'
	range = '^'..range
	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

-- 映射表 - 事件
utility.event_types = {
	['country_event'] = '国家事件',
	['planet_event'] = '星球事件',
	['fleet_event'] = '舰队事件',
	['ship_event'] = '舰船事件',
	['pop_event'] = '人口事件',
	['pop_faction_event'] = '派系事件',
	['event'] = '通用事件',
}

-- 映射表 - 建筑
utility.building_types = {
	['government'] = '政府建筑',
	['pop_assembly'] = '人口组建建筑',
	['resource'] = '资源建筑',
	['manufacturing'] = '工厂建筑',
	['research'] = '科研建筑',
	['trade'] = '贸易建筑',
	['amenity'] = '舒适度建筑',
	['unity'] = '凝聚力建筑',
	['army'] = '陆军建筑',
}

-- 映射表 - 组件
utility.component_templates_types = {
	['aura_components'] = '光环组件',
	['combat_computers'] = '战斗电脑',
	['ftl_components'] = '超光速组件',
	['ftl_inhibitor'] = '超光速屏蔽',
	['power_core'] = '能源组件',
	['sensor_components'] = '传感器组件',
	['thruster_components'] = '推进器组件',
	['utility'] = '通用组件',
	['weapon'] = '武器组件',
}

-- 映射表 - 法案
utility.edicts_types = {
	['campaigns'] = '§G运动法案§!',
	['edicts'] = '§G基础法案§!',
	['rare_edicts'] = '§G稀有法案§!',
	['ship_edicts'] = '§G舰船法案§!',
}

-- 映射表 - 科技解锁
utility.unlock_types = {
	['armies'] = '解锁陆军',
	['buildings'] = '解锁建筑',
	['component_templates'] = '解锁部件',
	['deposits'] = '解锁清除£blocker£地块障碍',
	['districts'] = '解锁区划',
	['edicts'] = '解锁法令',
	['section_templates'] = '解锁区块',
	['ship_sizes'] = '解锁飞船类型',
	['starbase_buildings'] = '解锁恒星基地建筑',
	['starbase_modules'] = '解锁恒星基地模块',
	['strategic_resources'] = '解锁开采资源',
	['traits'] = '解锁特性',
}

-- 替换
local replace_table = {
	-- 基础替换
	['\\n'] = '<br />', 
	['§.(§.-§!)§!'] = '%1',
	['§!'] = '</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)\">',
	
	-- 一些逻辑替换
	['▲'] = '£trigger_yes',
	['△'] = '£trigger_no',
	['〓'] = '<span style="padding:0px 0px 0px 28px"></span>[[File:Choices.png|不同情况分支|16px|link=]]',
	['№'] = '[[File:Species_right_colonization_rights.png|权重 = 基础值 × 系数|16px|link=]]',
	[':(#%x%x%x%x%x%x)'] = ':<span style=\"color:%1\">%1</span>',
	['※(%d*)'] = '<span style="padding:0px 0px 0px %1px"></span>',
}

local replace_icon_table = {
	-- P社图标替换
	['£resource_time'] = '[[File:Time.png|16px|时间|link=Item:Strategic_resources/time]]',
	['£time'] = '[[File:Time.png|16px|时间|link=Item:Strategic_resources/time]]',
	['£physics_research'] = '[[File:Physics_research.png|16px|物理研究|link=Item:Strategic_resources/physics_research]]',
	['£society_research'] = '[[File:Society_research.png|16px|社会研究|link=Item:Strategic_resources/society_research]]',
	['£engineering_research'] = '[[File:Engineering_research.png|16px|工程研究|link=Item:Strategic_resources/engineering_research]]',
	['£blocker'] = '[[File:Text_icon_tile_blocker.png|16px|link=]]',
	['£trigger_yes'] = '[[File:Trigger_yes.png|16px|link=]]',
	['£trigger_no'] = '[[File:Trigger_no.png|16px|link=]]',
	['£opinion'] = '[[File:Thinking_bubble.png|16px|好感度|link=]]',
	['£pops'] = '[[File:Pops.png|16px|人口|link=]]',
	['£pop'] = '[[File:Icon_text_pop.png|16px|人口|link=]]',
	['£happiness'] = '[[File:Tooltip_happiness.png|16px|幸福度|link=]]',
	['£building'] = '[[File:Tooltip_building.png|16px|建筑|link=]]',
	['£planetsize'] = '[[File:Planet_size.png|16px|星球大小|link=]]',
	['£influence'] = '[[File:Influence.png|16px|影响力|link=Item:Strategic_resources/influence]]',
	['£unity'] = '[[File:Unity.png|16px|凝聚力|link=Item:Strategic_resources/unity]]',
	['£energy'] = '[[File:Energy.png|16px|能量币|link=Item:Strategic_resources/energy]]',
	['£physics'] = '[[File:Physics_research.png|16px|物理研究|link=Item:Strategic_resources/physics_research]]',
	['£society'] = '[[File:Society_research.png|16px|社会研究|link=Item:Strategic_resources/society_research]]',
	['£engineering'] = '[[File:Engineering_research.png|16px|工程研究|link=Item:Strategic_resources/engineering_research]]',
	['£minerals'] = '[[File:Minerals.png|16px|矿物|link=Item:Strategic_resources/minerals]]',
	['£food'] = '[[File:Food.png|16px|食物|link=Item:Strategic_resources/food]]',
	['£sr_terraform_gases'] = '[[File:Sr_terraform_gases.png|16px|战略资源|link=Item:Strategic_resources/sr_terraform_gases]]',
	['£sr_terraform_liquids'] = '[[File:Sr_terraform_liquids.png|16px|战略资源|link=Item:Strategic_resources/sr_terraform_liquids]]',
	['£sr_garanthium'] = '[[File:Sr_garanthium.png|16px|战略资源|link=Item:Strategic_resources/sr_garanthium]]',
	['£sr_lythuric'] = '[[File:Sr_lythuric.png|16px|战略资源|link=Item:Strategic_resources/sr_lythuric]]',
	['£sr_teldar'] = '[[File:Sr_teldar.png|16px|战略资源|link=Item:Strategic_resources/sr_teldar]]',
	['£sr_yuranic'] = '[[File:Sr_yuranic.png|16px|战略资源|link=Item:Strategic_resources/sr_yuranic]]',
	['£sr_orillium'] = '[[File:Sr_orillium.png|16px|战略资源|link=Item:Strategic_resources/sr_orillium]]',
	['£sr_pitharan'] = '[[File:Sr_pitharan.png|16px|战略资源|link=Item:Strategic_resources/sr_pitharan]]',
	['£sr_engos'] = '[[File:Sr_engos.png|16px|战略资源|link=Item:Strategic_resources/sr_engos]]',
	['£sr_neutronium'] = '[[File:Sr_neutronium.png|16px|战略资源|link=Item:Strategic_resources/sr_neutronium]]',
	['£sr_living_metal'] = '[[File:Sr_living_metal.png|16px|战略资源|link=Item:Strategic_resources/sr_living_metal]]',
	['£sr_zro'] = '[[File:Sr_zro.png|16px|战略资源|link=Item:Strategic_resources/sr_zro]]',
	['£sr_dark_matter'] = '[[File:Sr_dark_matter.png|16px|战略资源|link=Item:Strategic_resources/sr_dark_matter]]',
	['£sr_satramene'] = '[[File:Sr_satramene.png|16px|战略资源|link=Item:Strategic_resources/sr_satramene]]',
	['£sr_alien_pets'] = '[[File:Sr_alien_pets.png|16px|资源|link=Item:Strategic_resources/sr_alien_pets]]',
	['£sr_betharian'] = '[[File:Sr_betharian.png|16px|资源|link=Item:Strategic_resources/sr_betharian]]',
	['£sr_riggan'] = '[[File:Sr_riggan.png|16px|战略资源|link=Item:Strategic_resources/sr_riggan]]',
	['£sr_xuran'] = '[[File:Sr_xuran.png|16px|战略资源|link=Item:Strategic_resources/sr_xuran]]',
	['£sr_muutagan'] = '[[File:Sr_muutagan.png|16px|战略资源|link=Item:Strategic_resources/sr_muutagan]]',
	['£sr_yurantic'] = '[[File:Sr_yurantic.png|16px|战略资源|link=Item:Strategic_resources/sr_yurantic]]',
	['£sr_aldar'] = '[[File:Sr_aldar.png|16px|战略资源|link=Item:Strategic_resources/sr_aldar]]',
	['£sr_distar_nanodust'] = '[[File:Sr_distar_nanodust.png|16px|战略资源|link=Item:Strategic_resources/sr_distar_nanodust]]',
	['£sr_distar_nanocrystal'] = '[[File:Sr_distar_nanocrystal.png|16px|战略资源|link=Item:Strategic_resources/sr_distar_nanocrystal]]',
	['£sr_distar_nanoscale'] = '[[File:Sr_distar_nanoscale.png|16px|战略资源|link=Item:Strategic_resources/sr_distar_nanoscale]]',
	['£sr_distar_nanoactuator'] = '[[File:Sr_distar_nanoactuator.png|16px|战略资源|link=Item:Strategic_resources/sr_distar_nanoactuator]]',
	['£job'] = '[[File:pop_job.png|16px|职业|link=职业]]',
	['£alloys'] = '[[File:Alloys.png|16px|合金|link=Item:Strategic_resources/alloys]]',
	['£consumer_goods'] = '[[File:consumer_goods.png|16px|消费品|link=Item:Strategic_resources/consumer_goods]]',
	['£volatile_motes'] = '[[File:volatile_motes.png|16px|易爆微粒|link=Item:Strategic_resources/volatile_motes]]',
	['£exotic_gases'] = '[[File:exotic_gases.png|16px|异星天然气|link=Item:Strategic_resources/exotic_gases]]',
	['£rare_crystals'] = '[[File:rare_crystals.png|16px|稀有水晶|link=Item:Strategic_resources/rare_crystals]]',
	['£nanites'] = '[[File:nanites.png|16px|纳米机器人|link=Item:Strategic_resources/nanites]]',
	['£trade_value'] = '[[File:trade_value.png|16px|贸易值|link=贸易值]]',
	['£mod_trade_value_add'] = '[[File:trade_value.png|16px|贸易值|link=贸易值]]',
	['£mod_planet_amenities_add'] = '[[File:planet_amenities.png|16px|舒适度]]',
	['£mod_planet_stability_add'] = '[[File:stability.png|16px|稳定度]]',
	['£crime'] = '[[File:crime.png|16px|犯罪率]]',
}

-- P社的颜色规则替换成HTML颜色规则
function utility.set_style(str)
	local newstr = str
	newstr = string.gsub(newstr, '£(job_.-)£', '[[File:%1.png|16px|link=]]')
	
	for old_key, new_value in pairs(replace_table) do
		newstr = string.gsub(newstr, old_key, new_value)
	end
	
	for old_key, new_value in pairs(replace_icon_table) do
		newstr = string.gsub(newstr, (old_key..'£'), new_value)
		newstr = string.gsub(newstr, old_key, new_value)
	end
	
	return newstr
end
return utility
0.0
0人评价
avatar