Example #1
0
File: wh.py Project: Teifion/Rob3
	def setup(self, true_team_list=[]):
		# Teams
		team_dict = self.the_world.teams()
		if true_team_list == []:
			true_team_list = team_dict.keys()
		
		# We use the full list of teams so that we don't get key errors later
		for t in team_dict.keys():
			self.caches[t] = {}
		
		# Team dropdown list
		for t in true_team_list:
			if 'team_list' not in self.caches[t]:
				self.caches[t]['team_list'] = []
				self.caches[t]['trade_list'] = []
			
			for team_id, the_team in team_dict.items():
				if t == team_id or the_team.dead or not the_team.active:
					continue
				
				if path_f.find_trade_route(self.the_world.cursor, the_team.id, t, the_world=self.the_world) != (-1, -1):
					self.caches[t]['trade_list'].append('<option value="%s">%s</option>' % (
						the_team.name, the_team.name)
					)
				
				self.caches[t]['team_list'].append('<option value="%s">%s</option>' % (the_team.name, the_team.name))
		
		# World city menu
		city_dict = self.the_world.cities()
		self.caches[0]['world_city_menu'] = []
		for city_id, the_city in city_dict.items():
			if the_city.dead > 0: continue
			if not team_dict[the_city.team].active: continue
			
			self.caches[0]['world_city_menu'].append(
				'<option value="{0}">{1} ({2})</option>'.format(common.js_name(the_city.name), the_city.name.replace('\\', ''), team_dict[the_city.team].name))
		
		# Army dropdown
		army_dict = self.the_world.armies()
		for army_id, the_army in army_dict.items():
			if the_army.team not in true_team_list: continue
			if the_army.garrison > 0: continue
			
			if 'army_list' not in self.caches[the_army.team]:
				self.caches[the_army.team]['army_list'] = []
			
			self.caches[the_army.team]['army_list'].append(
				'<option value="%s">%s</option>' % (the_army.name, the_army.name)
			)
		
		# Zip them down into strings
		true_team_list = list(true_team_list)
		true_team_list.append(0)
		for t in true_team_list:
			for k, v in self.caches[t].items():
				if type(v) == list or type(v) == tuple:
					self.caches[t][k] = "".join(v)
Example #2
0
def boolean_trade_order(the_line, groups, debug=False):
	results = order_block.default_line_results(the_line)
	
	resources_lookup	= resource_list.data_dict_n_l
	resource_dict		= resource_list.data_dict
	teams_lookup		= the_line.the_world.teams_lookup(lower=True)
	
	team_dict			= the_line.the_world.teams()
	the_team			= the_line.the_world.teams()[the_line.block.team]
	
	# Can we find the resource?
	if groups['item_name'].lower() not in resources_lookup:
		raise Exception("Unable to find %s in resource_list.data_dict_n_l.\n\nresource_list.data_dict_n_l = %s" % (groups['item_name'].lower(), resources_lookup))
	else:
		the_resource = resource_dict[resources_lookup[groups['item_name'].lower()]]
	
	# Can we find the team?
	if groups['team_name'].lower() not in teams_lookup:
		return order_block.fail(results, "there is no team by the name of '%s'" % groups['team_name'])
	else:
		target_team = team_dict[teams_lookup[groups['team_name'].lower()]]
	
	# Is it tradable?:
	if not the_resource.tradable:
		raise Exception("%s is not a tradable resource yet is still matched for trade_o.discrete_trade_order" % the_resource.name)
	
	# Is there a path there?
	path_found = path_f.find_trade_route(the_line.the_world.cursor,
		the_team.id, target_team.id, the_world=the_line.the_world)
	
	if path_found == (-1, -1):
		return order_block.fail(results, "%s could not be sent to %s because no trade route between you could be found" % (the_resource.name, target_team.name))
	
	# Can you afford it?
	if the_team.resources.get(the_resource.name, 0) < 1:
		results['cost'] = res_dict.Res_dict("%s:1" % the_resource.name)
		return order_block.fail_cost(results)
	
	#	EXECUTION
	#------------------------
	# Apply cost
	target_team.resources.set(the_resource.id, 1)
	
	# Result
	results['results'].append("%s was sent to %s" % (the_resource.name, target_team.name))
	results['foreign_results'][target_team.id] = ["%s was sent from %s" % (the_resource.name, the_team.name)]
	results['foreign_costs'][target_team.id] = res_dict.Res_dict("%s:-1" % the_resource.name)
	
	return order_block.success(results)
Example #3
0
File: oh.py Project: Teifion/Rob3
	def setup(self, true_team_list=[]):
		# Teams
		team_dict = self.the_world.teams()
		if true_team_list == []:
			true_team_list = team_dict.keys()
		
		# We use the full list of teams so that we don't get key errors later
		for t in team_dict.keys():
			self.caches[t] = {}
		
		# Wonders
		cities_with_wonders	= self.the_world.cities_with_wonders()
		city_dict			= self.the_world.cities()
		
		for c in cities_with_wonders:
			if 'wonder_menu' not in self.caches[city_dict[c].team]:
				self.caches[city_dict[c].team]['wonder_menu'] = []
			
			self.caches[city_dict[c].team]['wonder_menu'].append(
				'<option value="%s">%s</option>' % (
					common.js_name(city_dict[c].name), city_dict[c].name
				)
			)
		
		# Founding
		for c, the_city in city_dict.items():
			if the_city.team not in true_team_list:
				continue
			
			if the_city.dead > 0 or the_city.population < 5000:
				continue
			
			if 'founding_dropdown' not in self.caches[the_city.team]:
				self.caches[the_city.team]['founding_dropdown'] = ['<option value="">All cities</option>']
			
			self.caches[the_city.team]['founding_dropdown'].append(
				'<option value="%s">%s</option>' % (the_city.name, the_city.name)
			)
		
		# Relocation, same as founding minus the first item
		for t in true_team_list:
			self.caches[t]['relocation_dropdown'] = list(self.caches[t].get('founding_dropdown', '<option value=""></option>'))
			del(self.caches[t]['relocation_dropdown'][0])
		
		
		# Army dropdown
		army_dict = self.the_world.armies()
		for army_id, the_army in army_dict.items():
			if the_army.team not in true_team_list: continue
			
			if 'army_list' not in self.caches[the_army.team]:
				self.caches[the_army.team]['army_list'] = []
			
			self.caches[the_army.team]['army_list'].append(
				'<option value="%s">%s</option>' % (the_army.name, the_army.name)
			)
		
		# Unit dropdown
		unit_dict = self.the_world.units()
		for unit_id, the_unit in unit_dict.items():
			if the_unit.team != 0 and the_unit.team not in true_team_list:
				continue
			
			if 'unit_list' not in self.caches[the_unit.team]:
				self.caches[the_unit.team]['unit_list'] = []
			
			self.caches[the_unit.team]['unit_list'].append(
				'<option value="%s">%s %s</option>' % (the_unit.name, the_unit.name, unit_rules.print_unit_cost(the_unit, the_world=self.the_world))
			)
		
		# List of _all_ units
		for t in true_team_list:
			self.caches[t]['all_unit_list'] = list(self.caches[t].get('unit_list', []))
			self.caches[t]['all_unit_list'].extend(self.caches[0]['unit_list'])
		
		# Squad dropdown
		squad_dict = self.the_world.squads()
		for squad_id, the_squad in squad_dict.items():
			if the_squad.team not in true_team_list: continue
			
			if 'squad_list' not in self.caches[the_squad.team]:
				self.caches[the_squad.team]['squad_list'] = []
			
			self.caches[the_squad.team]['squad_list'].append(
				'<option value="%s,%s">%s (%s)</option>' % (common.js_name(the_squad.name), common.js_name(army_dict[the_squad.army].name), the_squad.name, army_dict[the_squad.army].name)
			)
		
		# Operative city dropdown
		city_dict = self.the_world.cities()
		self.caches[0]['operative_city_dropdown'] = []
		for city_id, the_city in city_dict.items():
			if the_city.dead > 0: continue
			if not team_dict[the_city.team].active: continue
			
			self.caches[0]['operative_city_dropdown'].append(
				'<option value="{0}">{1} ({2})</option>'.format(common.js_name(the_city.name), the_city.name.replace('\\', ''), team_dict[the_city.team].name))
		
		# Spell list
		spell_dict = self.the_world.spells()
		for s, the_spell in spell_dict.items():
			if the_spell.name == "NONE": continue
			
			for t in true_team_list:
				if 'spell_list' not in self.caches[t]:
					self.caches[t]['spell_list'] = ['<option value="">&nbsp;</option>']
				
				the_team = team_dict[t]
				
				if s in the_team.spell_levels:
					if the_team.spell_levels[s] >= the_spell.max_level:
						if the_spell.max_level > 0:
							self.caches[t]['spell_list'].append(
								'<option value="" disabled="disabled">%s</option>' % (the_spell.name)
							)
							continue
					
					if the_team.spell_levels[s] > 0:
						points_needed = spell_rules.cost_for_next_level(None,
							the_spell,
							level=the_team.spell_levels[s],
							in_spell_points=True).get("Spell points")
					
						self.caches[t]['spell_list'].append(
							'<option value="%s">%s (%s: %s/%s)</option>' % (
								the_spell.name, the_spell.name, the_team.spell_levels[s], the_team.spell_points[s], points_needed
							)
						)
					else:
						self.caches[t]['spell_list'].append(
							'<option value="%s">%s (%s)</option>' % (
								the_spell.name, the_spell.name, the_team.spell_levels[s]
							)
						)
				else:
					self.caches[t]['spell_list'].append(
						'<option value="%s">%s</option>' % (
							the_spell.name, the_spell.name
						)
					)
		
		# Tech list
		tech_dict = self.the_world.techs()
		for s, the_tech in tech_dict.items():
			if the_tech.name == "NONE": continue
			
			for t in true_team_list:
				if 'tech_list' not in self.caches[t]:
					self.caches[t]['tech_list'] = ['<option value="">&nbsp;</option>']
				
				the_team = team_dict[t]
				
				if s in the_team.tech_levels:
					if the_team.tech_levels[s] >= the_tech.max_level:
						if the_tech.max_level > 0:
							self.caches[t]['tech_list'].append(
								'<option value="" disabled="disabled">%s</option>' % (the_tech.name)
							)
							continue
					
					if the_team.tech_levels[s] > 0:
						points_needed = tech_rules.cost_for_next_level(None,
							the_tech,
							level=the_team.tech_levels[s]).get("Tech points")
					
						self.caches[t]['tech_list'].append(
							'<option value="%s">%s (%s: %s/%s)</option>' % (
								the_tech.name, the_tech.name, the_team.tech_levels[s], the_team.tech_points[s], points_needed
							)
						)
					else:
						self.caches[t]['tech_list'].append(
							'<option value="%s">%s (%s)</option>' % (
								the_tech.name, the_tech.name, the_team.tech_levels[s]
							)
						)
				else:
					self.caches[t]['tech_list'].append(
						'<option value="%s">%s</option>' % (
							the_tech.name, the_tech.name
						)
					)
		
		# Team dropdown list
		for t in true_team_list:
			if 'team_list' not in self.caches[t]:
				self.caches[t]['team_list'] = ['<option value="">&nbsp;</option>']
				self.caches[t]['trade_list'] = ['<option value="">&nbsp;</option>']
			
			for team_id, the_team in team_dict.items():
				if t == team_id or the_team.dead or not the_team.active:
					continue
				
				if path_f.find_trade_route(self.the_world.cursor, the_team.id, t, the_world=self.the_world) != (-1, -1):
					self.caches[t]['trade_list'].append('<option value="%s">%s</option>' % (
						the_team.name, the_team.name)
					)
				
				self.caches[t]['team_list'].append('<option value="%s">%s</option>' % (the_team.name, the_team.name))
		
		# Monster list
		monster_list = []
		for k, v in self.the_world.monsters().items():
			monster_list.append('<option value="%s">%s</option>' % (v.name, v.name))
			
		
		for t in true_team_list:
			self.caches[t]['monster_list'] = monster_list
		
		# Research trade
		master_tier = spell.tiers.index("Master")
		for t in true_team_list:
			if 'spell_trade' not in self.caches[t]:
				self.caches[t]['spell_trade'] = ['<option value="">&nbsp;</option>']
			
			for s, l in team_dict[t].spell_levels.items():
				if l < 1: continue
				
				if spell_dict[s].tier == master_tier: continue
				if not spell_dict[s].tradable: continue
				if spell_dict[s].name == "NONE": continue
				
				self.caches[t]['spell_trade'].append(
					'<option value="%s">%s</option>' % (
						common.js_name(spell_dict[s].name), spell_dict[s].name
					)
				)
		
		for t in true_team_list:
			if 'tech_trade' not in self.caches[t]:
				self.caches[t]['tech_trade'] = ['<option value="">&nbsp;</option>']
			
			for s, l in team_dict[t].tech_levels.items():
				if l < 1: continue
				
				if not tech_dict[s].tradable: continue
				if tech_dict[s].name == "NONE": continue
				
				self.caches[t]['tech_trade'].append(
					'<option value="%s">%s</option>' % (
						common.js_name(tech_dict[s].name), tech_dict[s].name
					)
				)
		
		# Resources dropdowns
		for t in true_team_list:
			team_resource = team_dict[t].resources
			
			if 'boolean_resources' not in self.caches[t]:
				self.caches[t]['boolean_resources'] = ['<option value="">&nbsp;</option>']
				self.caches[t]['discrete_resources'] = ['<option value="">&nbsp;</option>']
			
			for i, r in enumerate(resource_list.data_list):
				if not r.tradable: continue
				
				if r.type == "boolean":
					if team_resource.value[i] < 1: continue
					self.caches[t]['boolean_resources'].append('<option value="%s">%s</option>' % (r.name, r.name))
		
				elif r.type == "discrete":
					if team_resource.value[i] < 1: continue
					self.caches[t]['discrete_resources'].append('<option value="%s">%s</option>' % (r.name, r.name))
		
		# Zip them down into strings
		true_team_list = list(true_team_list)
		true_team_list.append(0)
		for t in true_team_list:
			for k, v in self.caches[t].items():
				if type(v) == list or type(v) == tuple:
					self.caches[t][k] = "".join(v)
Example #4
0
def discrete_trade_order(the_line, groups, debug=False):
	results = order_block.default_line_results(the_line)
	
	resources_lookup	= resource_list.data_dict_n_l
	resource_dict		= resource_list.data_dict
	teams_lookup		= the_line.the_world.teams_lookup(lower=True)
	
	team_dict			= the_line.the_world.teams()
	the_team			= the_line.the_world.teams()[the_line.block.team]
	
	# Can we find the resource?
	if groups['item_name'].lower() not in resources_lookup:
		raise Exception("Unable to find %s in resource_list.data_dict_n_l.\n\nresource_list.data_dict_n_l = %s" % (groups['item_name'].lower(), resources_lookup))
	else:
		the_resource = resource_dict[resources_lookup[groups['item_name'].lower()]]
	
	# Can we find the team?
	if groups['team_name'].lower() not in teams_lookup:
		return order_block.fail(results, "there is no team by the name of '%s'" % groups['team_name'])
	else:
		target_team = team_dict[teams_lookup[groups['team_name'].lower()]]
	
	# Is it tradable?:
	if not the_resource.tradable:
		raise Exception("%s is not a tradable resource yet is still matched for trade_o.discrete_trade_order" % the_resource.name)
	
	# Are they sending at least 1?
	try:
		amount = float(groups['amount'])
		
		if amount < 1:
			return order_block.fail(results, "you need to send at least 1 unit of %s" % the_resource.name)
	except Exception as e:
		raise Exception("The amount of '%s' cannot be parsed as an integer but was still matched for trade_o.discrete_trade_order")
	
	# Is there a path there?
	path_found = path_f.find_trade_route(the_line.the_world.cursor,
		the_team.id, target_team.id, the_world=the_line.the_world)
	
	if path_found == (-1, -1):
		return order_block.fail(results, "%s %s could not be sent to %s because no trade route between you could be found" % (amount, the_resource.name, target_team.name))
	
	# Can you afford it?
	results['cost'] = res_dict.Res_dict("%s:%s" % (the_resource.name, amount))
	affordability = the_team.resources.affordable(results['cost'])[0]
	
	if not affordability:
		return order_block.fail_cost(results)
	
	#	EXECUTION
	#------------------------
	# Apply cost
	the_team.resources -= results['cost'].discrete()
	target_team.resources += results['cost'].discrete()
	
	# Result
	results['results'].append("%s %s were sent to %s" % (amount, the_resource.name, target_team.name))
	results['foreign_results'][target_team.id] = ["%s %s were sent from %s" % (amount, the_resource.name, the_team.name)]
	results['foreign_costs'][target_team.id] = res_dict.Res_dict("%s:-%s" % (the_resource.name, amount))
	
	return order_block.success(results)