Пример #1
0
def main(verbose=False):
	cursor = database.get_cursor()
	output = []
	
	output.append(players_reports.run(cursor, verbose))
	output.append(city_reports.run(cursor, verbose))
	
	if output != []:
		print(database.shell_text("\n".join(output)))
	else:
		print(database.shell_text("[g]All reports green[/g]\n"))
Пример #2
0
def pre_orders(options):
	start_time = time.time()
	cursor = database.get_cursor()
	the_world = spy_world.Spy_world(cursor)
	team_dict = the_world.active_teams()
	
	#	Stats first, it's used for some favour stuff
	#------------------------
	for team_id, the_team in team_dict.items():
		stat_f.build_team_stats(the_world.cursor, the_team, the_world)
	
	# Now to assign the stats
	team_q.mass_get_team_stats(the_world.cursor, team_dict, common.current_turn())
	
	#	Pre orders - Teams
	#------------------------
	cursor.execute("BEGIN")
	print(database.shell_text("Team pre-orders"), end="")
	for team_id, the_team in team_dict.items():
		try:
			team_f.pre_orders(the_world, the_team)
		except Exception as e:
			cursor.execute("ROLLBACK")
			print(database.shell_text(" - [r]Failure[/r]"))
			print("Failure running pre-orders for %s" % the_team.name)
			print(database.shell_text("[r]Re run as 'rob3 start -l True[/r]'"))
			raise
	
	print(database.shell_text(" - [g]Done[/g]"))
	
	#	Pre orders - System
	#------------------------
	print(database.shell_text("System pre-orders"), end="")
	try:
		# Army history
		army_f.location_history(the_world)
		
		# Player history
		player_f.turn_history(the_world)
		
		# Power history
		power_f.turn_history(the_world)
		
		# Artefact history
		artefact_f.turn_history(the_world)
		
		# Operatives catching
		operative_f.catch_operatives(the_world)
		
		# Border history
		team_f.border_history(the_world)
	
	except Exception as e:
		cursor.execute("ROLLBACK")
		print(database.shell_text(" - [r]Failure[/r]"))
		print(database.shell_text("[r]Re run as 'rob3 start -l True[/r]'"))
		raise
	
	cursor.execute("COMMIT")
	print(database.shell_text("\nNow run [g]rob orders[/g]"))
Пример #3
0
def main(cursor=None, check_all=False, verbose=True):
	if not cursor:
		cursor = database.get_cursor()
	
	if verbose:
		print(database.shell_text("''Starting system checks''"))
	
	cities_check.run(cursor, check_all, verbose)
	military_check.run(cursor, check_all, verbose)
	players_check.run(cursor, check_all, verbose)
	ops_check.run(cursor, check_all, verbose)
	teams_check.run(cursor, check_all, verbose)
	
	if verbose:
		print(database.shell_text("''Checks complete''"))
Пример #4
0
def output_json_map(options, the_world=None, skip_upload=False):
	from json_lib import mapper_j
	
	if the_world != None:
		cursor = the_world.cursor
	else:
		cursor = database.get_cursor()
	files = {}
	
	# create map
	#------------------------
	the_map = mapper_j.JSON_map_maker()
	map_source = the_map.map_grid(cursor)
	# print(map_source)
	# exit()
	
	with open('%s/map/latest.json' % common.data['woa_folder'], 'w') as f:
		f.write(map_source)
		f.write(padding)
	
	with open('%s/map/turn_%d_normal.json' % (common.data['woa_folder'], common.current_turn()), 'w') as f:
		f.write(map_source)
		f.write(padding)
	
	# Save as files
	files['latest.json'] = '%s/map/latest.json' % common.data['woa_folder']
	files['turn_%d_normal.json' % common.current_turn()] = '%s/map/turn_%d_normal.json' % (common.data['woa_folder'], common.current_turn())
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['map'], files, options.delay, options.verbose)
		print(database.shell_text('[g]Json map uploaded[/g]'))
Пример #5
0
def output_tmap(options, the_world=None, skip_upload=False):
	from pages.map import team_map
	
	if not the_world:
		cursor = database.get_cursor()
		the_world = world.World(cursor)
	else:
		cursor = the_world.cursor
	
	team_dict = team_q.get_real_active_teams(cursor)
	# if len(args) == 0:
	# 	team_list, team_dict = team_q.get_real_active_teams()
	# else:
	# 	team_list, team_dict = team_q.get_teams_in_list(team_list=args, by_id=False)
	
	files = {}
	# for team_id in team_dict.keys():
	for team_id in progressbar(team_dict.keys(), "Creating TMaps: ", 60, True):
		the_team = the_world.teams()[team_id]
		md5_name = team_f.team_hash(the_team.name)
		
		html_source = team_map._draw_map(cursor, team_id, build=1)
		
		f = open('%s/tmap/%s.html' % (common.data['woa_folder'], md5_name), 'w')
		f.write(html_source)
		f.write(padding)
		f.close()
		
		files['%s.html' % (md5_name)] = '%s/tmap/%s.html' % (common.data['woa_folder'], md5_name)
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['tmap'], files, options.delay, options.verbose)
		print(database.shell_text('[g]Team maps uploaded[/g]'))
Пример #6
0
def output_json_data(options, the_world=None, skip_upload=False):
	from json_lib import data_j, oh_j
	
	if not the_world:
		cursor = database.get_cursor()
	else:
		cursor = the_world.cursor
	
	# Output the OH.js first
	output = oh_j.get_data(cursor)
	with open('%s/oh_data.js' % (common.data['cache_path']), 'w') as f:
		f.write(output)
		f.write(padding)
	
	# Write a second copy to the Rob3 localhost folder for Rob3 CGI to see
	with open('%s/oh_data.js' % (common.data['rob_fpath']), 'w') as f:
		f.write(output)
		f.write(padding)
	
	files = {
		'oh_data.js': '%s/oh_data.js' % (common.data['cache_path'])
	}
	for d, func in data_j.handle_dict.items():
		output = func(cursor)
		with open('%s/data_%s.json' % (common.data['cache_path'], d), 'w') as f:
			f.write(output)
			f.write(padding)
		
		files['%s.json' % (d)] = '%s/data_%s.json' % (common.data['cache_path'], d)
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['data'], files, options.delay, options.verbose)
		print(database.shell_text('[g]Data lists uploaded[/g]'))
Пример #7
0
def output_wh(options, the_world=None, skip_upload=False):
	from classes import wh
	
	if not the_world:
		cursor = database.get_cursor()
		the_world = world.World(cursor)
	else:
		cursor = the_world.cursor
	
	team_dict = team_q.get_real_active_teams(cursor)
	the_wh = wh.Wh(the_world.cursor, the_world)
	the_wh.setup(true_team_list=team_dict.keys())
	
	files = {}
	for team_id in progressbar(team_dict.keys(), "Creating WHs: ", 60, True):
		try:
			the_team = the_world.teams()[team_id]
			md5_name = team_f.team_hash(the_team.name)
			
			output = the_wh.make_wh(team_id)
			
			f = open('%s/wh/%s.html' % (common.data['woa_folder'], md5_name), 'w')
			f.write(output)
			f.write(padding)
			f.close()
			
			files['%s.html' % (md5_name)] = '%s/wh/%s.html' % (common.data['woa_folder'], md5_name)
		
		except Exception as e:
			print("Team name: %s" % the_team.name)
			raise
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['wh'], files, options.delay, options.verbose)
		print(database.shell_text('[g]War helpers uploaded[/g]'))
Пример #8
0
def worker(options):
	import socket, imp
	
	tests(options)
	print("Worker running")
	options.verbose = True
	options.delay = 7
	
	last_upload = time.time()
	
	backup(options)
	try:
		while True:
			imp.reload(database)
			
			update_function = cli_f.Timeout_function(urllib.request.urlopen, 20)
			requests_function = cli_f.Timeout_function(find_requests, 30)
			try:
				requests_function(options)
			except cli_f.Timeout_exception:
				print("Timeout for find_requests")
			except urllib.request.URLError as e:
				print("URL request error")
			except Exception as e:
				raise
			
			# Upload twice a day
			if time.time() - last_upload > 60*60*12:
				check(options)
				"""
				the_world = spy_world.Spy_world(database.get_cursor())
				cli_f.output_to(options, the_world)
				cli_f.output_map(options, the_world)
				cli_f.output_ti(options, the_world)
				cli_f.output_stats(options, the_world)
				cli_f.output_oh(options, the_world)
				cli_f.output_spyrep(options, the_world)
				cli_f.output_tmap(options, the_world)
				"""
				
				try:
					getter_results = update_function(common.data['rob_url'], "mode=get_players").read()
				except cli_f.Timeout_exception:
					print("Timeout for get_players")
				
				try:
					getter_results = update_function(common.data['rob_url'], "mode=get_teams").read()
				except cli_f.Timeout_exception:
					print("Timeout for get_teams")
				
				last_upload = time.time()
			
			time.sleep(60*2)
			
	except KeyboardInterrupt as e:
		print(database.shell_text("[c]Exiting from keyboard interrupt[/c]"))
		exit()
		
	except Exception as e:
		raise
Пример #9
0
def output_to(options, the_world=None, skip_upload=False):
	from functions import to_f
	
	files = {}
	
	if not the_world:
		cursor = database.get_cursor()
		the_world = world.World(cursor)
	else:
		cursor = the_world.cursor
	the_world.prep_for_to()
	
	headers = to_f.headers(the_world)
	footers = to_f.footers(the_world)
	js = to_f.javascript(the_world)
	output = to_f.make_to(the_world)
	
	to_source = "".join([headers, js, output, footers])
	
	# the_to = urllib.request.urlopen(common.data['rob_url'], 'mode=to&dev_mode=0&ajax=1')
	# the_to = urllib.request.urlopen(web_url, 'mode=to&dev_mode=0&ajax=1')
	# to_source = the_to.read()
	
	f = open('%s/to/index.html' % common.data['woa_folder'], 'w')
	f.write(to_source)
	f.write(padding)
	f.close()
	
	files['index.html'] = '%s/to/index.html' % common.data['woa_folder']
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['to'], files, options.delay, options.verbose)
		print(database.shell_text('[g]Team Overview uploaded[/g]'))
Пример #10
0
def run(cursor, check_all, verbose):
	check_operative_locations(cursor, verbose)
	
	if check_all:
		pass
	
	if verbose:
		print(database.shell_text("[g]Operative checks complete[/g]"))
Пример #11
0
def backup(options):
	file_path = "%s/backup/turn_%s.sql" % (common.data['server_fpath'], common.current_turn())
	args = "-h localhost -U postgres"
	
	os.system("pg_dump %s rob3 -f %s" % (args, file_path))
	
	if options.verbose:
		print(database.shell_text("[g]Database backed up[/g]"))
Пример #12
0
	def test_shell_text(self):
		vals = (
			("", ""),
			("BLUE", "BLUE"),
			("''BLUE''", "\033[1;1mBLUE\033[30;0m"),
		)
		
		for arg, expected in vals:
			self.assertEqual(expected, database.shell_text(arg))
Пример #13
0
def run(cursor, check_all, verbose):
	clear_out_old_team_caches(cursor, verbose)
	innactive_teams_cities(cursor, verbose)
	
	if check_all:
		pass
	
	if verbose:
		print(database.shell_text("[g]Team checks complete[/g]"))
Пример #14
0
def run(cursor, check_all, verbose):
	ir_player_activity(cursor, verbose)
	innactive_players_on_teams(cursor, verbose)
	
	if check_all:
		pass
	
	if verbose:
		print(database.shell_text("[g]Player checks complete[/g]"))
Пример #15
0
def catch_operatives(the_world, verbose=False):
	relations	= the_world.relations()# [Host][Visitor]
	operative_dict = the_world.operatives()
	city_dict = the_world.cities()
	team_dict = the_world.teams()
	
	# Get list of all operatives in cities where they're in cities they are not meant to be
	hostile_ops = []
	for op_id, the_op in operative_dict.items():
		if the_op.died > 0: continue
		
		if city_dict[the_op.city].team != the_op.team:
			try:
				host_team = team_dict[city_dict[the_op.city].team]
				if relations.get(host_team.id, {}).get(the_op.team, {}).get('borders', host_team.default_borders) < spy_report.friendly_border:
					# Ensure that they are at least seggregated (and thus want to catch you)
					hostile_ops.append(op_id)
			except Exception as e:
				print(the_op.city)
				print(city_dict[the_op.city].team)
				print(the_op.team)
				print(borders)
				raise

	
	capture_list = []
	for o in hostile_ops:
		c = capture_chance(the_world, o)
		caught = spy_report_f._success(c)
		
		if verbose:
			if caught:
				caught_str = database.shell_text(" - [r]Caught[/r]")
			else:
				caught_str = ""
			
			print("Id: {id}, Chance: {c}%, City: {city}{caught_str}".format(
				id=o,
				c=round(c*100,3),
				city=city_dict[operative_dict[o].city].name,
				caught_str=caught_str,
			))
		
		if caught:
			capture_list.append(str(o))
	
	if verbose:
		print("Caught %d out of %d ops" % (len(capture_list), len(hostile_ops)))
	
	if len(capture_list) > 0:
		# Update capture status
		query = """UPDATE operatives SET died = %d WHERE id in (%s);""" % (common.current_turn(), ",".join(capture_list))
		try: the_world.cursor.execute(query)
		except Exception as e:
			raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
Пример #16
0
def print_res_dict(res_dict, the_world, title="", one_line=False):
	city_dict = the_world.live_cities()
	output = []
	
	if title != "":
		output.append(database.shell_text(title))
	
	for r in sad_rules.res_list:
		output.append("{0}: {1} avg: {2}".format(r, res_dict[r], round(res_dict[r]/len(the_world.live_cities()), 2)))
	
	return "\n".join(output)
Пример #17
0
def check_table(cursor, table_info, fix=False, show_fixes=False, die_queitly=False):
	"""A wrapper for the check table function, simpy so we can have some decent exception handling"""
	
	# Run some checks
	has_pk = False
	for f in table_info['Fields']:
		if f.primary_key:
			has_pk = True
		
	if not has_pk:
		return database.shell_text("''%s'' has [r]no primary key[/r]" % table_info['Name'])
	
	# Now to actually build it
	try:
		return check_table_func(cursor, table_info, fix=fix, show_fixes=show_fixes)
	except Exception as e:
		if die_queitly:
			return database.shell_text("ERROR: Was not able to create [r]%s[/r]" % table_info['Name'])
		else:
			print("Error on table: %s" % table_info['Name'])
			raise
Пример #18
0
def output_all(options, nightly=False):
	start_time = time.time()
	
	print(database.shell_text("''Running tests''"))
	tests(options)
	
	print(database.shell_text("''Running checks''"))
	check_all(options)
	
	print(database.shell_text("''Building world''"))
	the_world = spy_world.Spy_world(database.get_cursor())
	
	cli_f.output_ti(options, the_world)
	cli_f.output_to(options, the_world)
	cli_f.output_stats(options, the_world)
	cli_f.output_map(options, the_world)
	cli_f.output_wh(options, the_world)
	cli_f.output_oh(options, the_world)
	cli_f.output_spyrep(options, the_world)
	cli_f.output_tmap(options, the_world)
	cli_f.output_json(options)
	
	print(database.shell_text("''Completed in %s seconds (delay of %s)''" % (int(time.time() - start_time), options.delay)))
Пример #19
0
def find_requests(options):
	from queries import team_q
	from functions import request_f
	cursor = database.get_cursor()
	
	team_dict = team_q.get_real_active_teams(cursor)
	topic_list = []
	lookup = {}
	for k, v in team_dict.items():
		if v.request_topic > 0:
			topic_list.append(str(v.request_topic))
			lookup[str(v.request_topic)] = v.id
	
	topic_list = ",".join(topic_list)
	# print(topic_list)
	getter_data = "p=%s&mode=find_request_topics&topic_list=%s" % (common.data['getterPass'], topic_list)
	
	topics_to_read = urllib.request.urlopen(common.data['getter_url'], getter_data).read().strip().decode('utf-8')
	
	topic_list = topics_to_read.split("\n")
	
	if len(topic_list) < 1:
		if options.verbose:
			print(database.shell_text("No requests found"))
		return
	
	teams_to_read = []
	for t in topic_list:
		if t == '': continue
		teams_to_read.append(lookup[t])
	
	for t in teams_to_read:
		print(request_f.msn_run_orders(database.get_cursor(), t))
		time.sleep(options.delay)
	
	if options.verbose:
		print(database.shell_text("[g]Ran %d requests[/g]" % len(teams_to_read)))
Пример #20
0
def run(cursor, check_all, verbose):
	check_unit_ownership(cursor, verbose)
	
	check_unit_categories(cursor, verbose)
	delete_empty_squads_from_dead_garrisons(cursor, verbose)
	build_unit_equipment_strings(cursor, verbose)
	check_for_garrisons(cursor, verbose)
	check_available(cursor, verbose)
	merge_squads(cursor, verbose)
	
	if check_all:
		check_army_bases(cursor, verbose)
	
	if verbose:
		print(database.shell_text("[g]Military checks complete[/g]"))
Пример #21
0
def run(cursor, check_all, verbose):
	city_terrain(cursor, verbose)
	check_for_dead_cities(cursor, verbose)
	check_for_amount(cursor, verbose)
	check_wonder_completion(cursor, verbose)
	build_supplies(cursor, verbose)
	overlapping_cities(cursor, verbose)
	
	if check_all:
		trade_build(cursor, verbose)
	
	city_wealth(cursor, verbose)
	get_happiness(cursor, verbose)
	
	if verbose:
		print(database.shell_text("[g]City checks complete[/g]"))
Пример #22
0
def output_oh(options, the_world=None, skip_upload=False):
	from classes import oh
	
	if not the_world:
		cursor = database.get_cursor()
		the_world = world.World(cursor)
	else:
		cursor = the_world.cursor
	
	team_dict = team_q.get_real_active_teams(cursor)
	the_oh = oh.Oh(the_world.cursor, the_world)
	the_oh.setup(true_team_list=team_dict.keys())
	# if len(args) == 0:
	# 	team_list, team_dict = team_q.get_real_active_teams()
	# else:
	# 	team_list, team_dict = team_q.get_teams_in_list(team_list=args, by_id=False)
	
	files = {}
	# for team_id in team_dict.keys():
	for team_id in progressbar(team_dict.keys(), "Creating OHs: ", 60, True):
		try:
			the_team = the_world.teams()[team_id]
			md5_name = team_f.team_hash(the_team.name)
			
			# # Start of output related stuff
			# headers = ti_f.headers(the_team)
			# footers = ti_f.footers(the_team)
			# js = ti_f.javascript(the_team)
			
			output = the_oh.make_oh(team_id)
		
			f = open('%s/orders/%s.html' % (common.data['woa_folder'], md5_name), 'w')
			# f.write("".join([headers, output, footers]))
			f.write(output)
			f.write(padding)
			f.close()
			
			files['%s.html' % (md5_name)] = '%s/orders/%s.html' % (common.data['woa_folder'], md5_name)
		
		except Exception as e:
			print("Team name: %s" % the_team.name)
			raise
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['orders'], files, options.delay, options.verbose)
		print(database.shell_text('[g]Order helpers uploaded[/g]'))
Пример #23
0
def output_ti(options, the_world=None, skip_upload=False):
	from functions import ti_f
	
	if not the_world:
		cursor = database.get_cursor()
		the_world = spy_world.Spy_world(cursor)
	else:
		cursor = the_world.cursor
	
	the_world.prep_for_ti()
	
	team_dict = team_q.get_real_active_teams(cursor)
	# if len(args) == 0:
	# 	team_dict = team_q.get_real_active_teams(cursor)
	# else:
	# 	team_dict = team_q.get_teams_in_list(cursor, team_list=args, by_id=False)
	
	files = {}
	for team_id in progressbar(team_dict.keys(), "Creating TIs: ", 60, True):
	# for team_id in team_dict.keys():
		try:
			the_team = the_world.teams()[team_id]
			md5_name = team_f.team_hash(the_team.name)
			
			# Start of output related stuff
			headers = ti_f.headers(the_team)
			footers = ti_f.footers(the_team)
			js = ti_f.javascript(the_team)
			
			output = ti_f.make_ti(cursor, the_world, the_team)
		
			f = open('%s/ti/%s.html' % (common.data['woa_folder'], md5_name), 'w')
			f.write("".join([headers, output, footers]))
			f.write(padding)
			f.close()
			
			files['%s.html' % (md5_name)] = '%s/ti/%s.html' % (common.data['woa_folder'], md5_name)
		
		except Exception as e:
			print("Team name: %s" % the_team.name)
			raise
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['ti'], files, options.delay, options.verbose)
		print(database.shell_text('[g]Team Infos uploaded[/g]'))
Пример #24
0
def fill_table(cursor, table_info, data_module):
	"""Fills a list table"""
	inserts		= []
	field_names	= []
	
	# Make the table
	query = "DROP TABLE %s" % table_info['Name']
	try: cursor.execute(query)
	except Exception as e:
		pass
	# return ""
	create_table(cursor, table_info)
	
	# Field names
	field_names = [f.name for f in table_info['Fields']]
	
	# Rows
	for i, block in enumerate(data_module.data_list):
		values = []
		for f in table_info['Fields']:
			if f.name == "id":
				current_val = i
			else:
				current_val = getattr(block, f.name)
			
			if current_val.__class__ == float:
				if current_val == int(current_val):
					current_val = int(current_val)
			
			if current_val.__class__ == str:
				current_val = "%s" % database.escape(current_val)
			
			values.append("'%s'" % current_val)
		
		inserts.append("(%s)" % ",".join(values))
	
	query = "INSERT INTO %s (%s) values %s;" % (table_info['Name'], ",".join(field_names), ",".join(inserts))
	try:
		cursor.execute(query)
		return database.shell_text("[y]Filled %s[/y] with %s rows" % (table_info['Name'], len(inserts)))
	except Exception as e:
		print("Query: %s\n" % query)
		raise e
Пример #25
0
def output_json_ti(options, the_world=None, skip_upload=False):
	from json_lib import ti_j
	
	if not the_world:
		cursor = database.get_cursor()
		the_world = spy_world.Spy_world(cursor)
	else:
		cursor = the_world.cursor
	
	the_world.prep_for_ti()
	
	team_dict = team_q.get_real_active_teams(cursor)
	
	files = {}
	for team_id in progressbar(team_dict.keys(), "Creating JSON TIs: ", 60, True):
	# for team_id in team_dict.keys():
		try:
			the_team = the_world.teams()[team_id]
			md5_name = team_f.team_hash(the_team.name)
			
			# Make actual output
			output = ti_j.make_ti(the_world, the_team)
			
			# Save to DB
			database.query(cursor, ti_f.save_json(team_id, output))
			
			# Save to file
			with open('%s/ti_%s.json' % (common.data['cache_path'], md5_name), 'w') as f:
				f.write(output)
				f.write(padding)
			
			files['%s.json' % (md5_name)] = '%s/ti_%s.json' % (common.data['cache_path'], md5_name)
		
		except Exception as e:
			print("Team name: %s" % the_team.name)
			raise
	
	if not skip_upload:
		upload("ftp.woarl.com", "*****@*****.**", ftp_pass['ti'], files, options.delay, options.verbose)
		print(database.shell_text('[g]Json team infos uploaded[/g]'))
Пример #26
0
def run(cursor):
	reset_tables(cursor)
	
	# Make mapper_q build the terrain dictionary
	mapper_q.get_terrain(cursor, 0, 0)
	
	# Get a list of the continents so we can loop through them
	continent_list = []
	continent_x = []
	continent_y = []
	continent_names = []
	query = """SELECT id, x, y, name FROM map_continents"""
	try: cursor.execute(query)
	except Exception as e:
		raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
	for row in cursor:
		continent_list.append(row['id'])
		continent_x.append(row['x'])
		continent_y.append(row['y'])
		continent_names.append(row['name'])
	
	for c in range(0, len(continent_list)):
		# Tile list is a list of all the tiles on that continent
		time_at_start = time.time()
		tile_list = explore_continent(continent_list[c], continent_x[c], continent_y[c])
		
		# print("Mapped %s to a length of %s in %s seconds" % (continent_names[c], len(tile_list), round(time.time() - time_at_start, 2)))
		insert_list = ["(%s, %s, %s)" % (t[0], t[1], continent_list[c]) for t in tile_list]
		
		if insert_list == []:
			print(database.shell_text("[r]Zero length insert_list for %s[/r]" % continent_names[c]))
			continue
		
		query = """INSERT INTO map_continent_tiles (x, y, continent) values %s;""" % ",".join(insert_list)
		try: cursor.execute(query)
		except Exception as e:
			raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
	
	print("Map preprocesses complete")
Пример #27
0
def print_reports(the_world, report_dict, reports=['res_summary', 'production', 'demand', 'wealth']):
	city_dict = the_world.cities()
	
	city_count = len(city_dict.keys())
	city_size = sum([c.size for i, c in city_dict.items()])/city_count
	
	if 'res_summary' in reports:
		print(print_res_dict(report_dict['pre']['res_summary'],		the_world, "''Summary''"), "\n")
	
	if 'res_surplus' in reports:
		print(print_res_dict(report_dict['pre']['res_surplus'],		the_world, "''Surplus''"), "\n")
	
	if 'res_demand' in reports:
		print(print_res_dict(report_dict['pre']['res_demand'],		the_world, "''Demand''"), "\n")
	
	if 'res_producers' in reports:
		print(print_res_dict(report_dict['pre']['res_producers'],	the_world, "''Producers'' (out of %d)" % city_count), "\n")
	
	ksize = city_size/1000
	
	if 'production' in reports:
		production = report_dict['production']
		print(database.shell_text("''Production''           Min    Max    Avg   Ratio"))
		for r in sad_rules.res_list:
			avg = production[r][0]/city_count
		
			print("{res:17} {min:6} {max:6} {avg:6} {ratio:6}".format(
				res = r,
				total = production[r][0],
				min = round(production[r][1], 2),
				max = round(production[r][2], 2),
				avg = round(avg, 2),
				ratio = round(avg/ksize, 2),
			))
		print("")
	
	if 'demand' in reports:
		demand = report_dict['demand']
		
		print(database.shell_text("\n''Demand''               Min    Max    Avg   Ratio"))
		for r in sad_rules.res_list:
			avg = demand[r][0]/city_count
		
			print("{res:17} {min:6} {max:6} {avg:6} {ratio:6}".format(
				res = r,
				total = demand[r][0],
				min = round(demand[r][1], 2),
				max = round(demand[r][2], 2),
				avg = round(avg, 2),
				ratio = round(avg/ksize, 2),
			))
	
	if 'wealth' in reports:
		wealth = report_dict['wealth']
		avg = wealth[0]/city_count
		
		print(database.shell_text("\n''Wealth''\n   Min    Max    Avg   Ratio"))
		print("{min:6} {max:6} {avg:6} {ratio:6}".format(
			total = wealth[0],
			min = round(wealth[1], 2),
			max = round(wealth[2], 2),
			avg = round(avg, 2),
			ratio = round(avg/ksize, 2),
		))
	
	# Attempt at ratios
	if 'ratios' in reports:
		print(database.shell_text("\n''Ratios''"))
		# print(report_dict['pre'].keys())
		production = report_dict['production']
		demand = report_dict['demand']
		for r in sad_rules.res_list:
			print("{res:17} {ratio:6}".format(
				res = r,
				ratio = round(production[r][0]/demand[r][0], 2),
			))
Пример #28
0
def main(options):
	orders_str = ""
	if not options.local:
		while orders_str == "":
			orders_input = input("Orders closing text (default 'Turn %d approaches'): " % (common.current_turn() + 2))
		
			if orders_input == "":
				orders_str = "Turn %d approaches" % (common.current_turn() + 2)
			else:
				orders_str = orders_input
			
			orders_input = input("Confirm that you are happy with '%s': " % orders_str)
			if orders_input.lower() not in ["y", "yes"]:
				orders_str = ""
	# orders_str = "IGNORE"
	
	start_time = time.time()
	cursor = database.get_cursor()
	the_world = spy_world.Spy_world(cursor)
	cursor.track_queries = True
	team_dict = the_world.active_teams()
	turn = common.current_turn()
	
	cursor.execute("BEGIN")
	#	Timestamp
	#------------------------
	print(database.shell_text("Updating timestamp"), end="")
	try:
		database.query(cursor,
			system_f.add_turn_timestamp(common.current_turn()+2))
		
		# Refresh timestamp
		common.data['current_turn'] += 1
		turn = common.data['current_turn']
	except Exception as e:
		print(database.shell_text(" - [r]Failure[/r]"))
		raise
	
	print(database.shell_text(" - [g]Done[/g]"))
	
	#	Close orders
	#------------------------
	print(database.shell_text("Closing orders"), end="")
	try:
		result = order_post_f.close_orders(cursor, orders_str, test_mode=options.verbose)
	except Exception as e:
		print(database.shell_text(" - [r]Failure[/r]"))
		raise
	
	print(database.shell_text(" - [g]Done[/g]"))
	
	#	Get orders
	#------------------------
	print(database.shell_text("Downloading orders:"))
	for team_id, the_team in team_dict.items():
		try:
			if the_team.orders_topic > 0:
				order_post_f.get_orders(cursor, the_team.orders_topic, turn, team_id,
					recache=True, delay=options.delay)
			
			if the_team.intorders_topic > 0:
				order_post_f.get_orders(cursor, the_team.intorders_topic, turn, team_id,
					recache=True, delay=options.delay)
			
			print(database.shell_text("%s - [g]Done[/g]" % the_team.name))
		except Exception as e:
			print(database.shell_text("[r]ERROR[/r]"))
			print("Failure downloading orders for %s" % the_team.name)
			raise
	
	cursor.execute("COMMIT")
	print(database.shell_text("\nNow run [g]rob preorders[/g]"))
Пример #29
0
def run_orders(options):
	start_time = time.time()
	cursor = database.get_cursor()
	cursor.track_queries = True
	the_world = spy_world.Spy_world(cursor)
	team_dict = the_world.active_teams()
	
	#	Run orders
	#------------------------
	print(database.shell_text("Running orders"), end="")
	queries, orders, debug = [], ["Team orders\n"], []
	the_world.prep_for_orders()
	
	try:
		player_updates = {}
		the_orders = order_post_f.get_turn_orders(cursor, the_world)
		
		blocks = []
		# for o in the_orders:
		for o in cli_f.progressbar(the_orders, "Splitting: ", 60, True):
			o.split()
			blocks.extend(o.blocks)
		
		# Setup
		# for b in blocks:
		for b in cli_f.progressbar(blocks, "Setting up: ", 60, True):
			b.setup()
			b.always_debug = True
		
		# Execution, in order of priority
		for priority in order_block.priorities:
			c = 0
			
			for b in blocks:
				if b.priority != priority:
					continue
				
				try:
					b.execute()
				except Exception as e:
					print("")
					print(b.title_name)
					print(the_world.teams()[b.team].name)
					print("\n".join(cursor.queries[-5:-1]))
					print("")
					raise
		
		# Ensure we've handled all blocks
		for b in blocks:
			if not b.handled:
				raise Exception("Block with priority '%s' not handled" % b.priority)
		
		team_output = {}
		manual_output = {}
		team_debug = {}
		team_failures = {}
		for b in cli_f.progressbar(blocks, "Running Blocks: ", 60, True):
		# for b in blocks:
			if b.team not in team_output:
				team_output[b.team] = []
			
			if b.team not in team_debug:
				team_debug[b.team] = []
			
			if b.team not in team_failures:
				team_failures[b.team] = []
			
			if b.team not in manual_output:
				manual_output[b.team] = []
			
			# Player activity needs to get updated
			player_updates[b.post.player] = b.post.team
			
			# team_output[b.team].append("[o]%s[/o]" % b.title_name)
			team_output[b.team].append("\n".join(b.results))
			team_output[b.team].append("")
			
			if b.manual_handle:
				manual_output[b.team].append("\n".join(b.results))
				manual_output[b.team].append("")
			
			# Failures
			if len(b.failures) > 0:
				team_failures[b.team].append("\n".join(b.failures))
				team_failures[b.team].append("")
			
			# Debug
			team_debug[b.team].append(b.debug[0])
			team_debug[b.team].append("\n---\n".join(b.debug[1:len(b.debug)]))
			team_debug[b.team].append("")
			
			# Foreign results
			for team_id, res in b.foreign_results.items():
				if team_id not in team_output:
					team_output[team_id] = []
				team_output[team_id].insert(0, "")
				team_output[team_id].insert(0, "\n".join(res))
			
			# Queries
			for team_id, fqueries in b.foreign_queries.items():
				queries.extend(fqueries)
			
			queries.extend(b.queries)
		
		
		
		for team_id, the_team in team_dict.items():
			if the_team.ir: continue
			
			orders.append("""# %s
######################################################################
[fullbox=#EEF,#AAF][h4]Turn %d Results[/h4][/fullbox]

[url=http://woarl.com/stats/%s.html]Rob results[/url]

""" % (the_team.name, common.current_turn(), team_f.team_hash(the_team.name)))
			
			if team_id in team_output:
				if team_id in manual_output:
					# orders.extend(team_output[team_id])
					orders.extend(manual_output[team_id])
			
			if team_id in team_debug:
				debug.extend(team_debug[team_id])
		
		# Update player activity
		database.query(cursor,
			player_f.update_player_activity(player_updates))
		
		# Save results
		results_f.save_results(cursor, team_output)
		results_f.save_failures(cursor, team_failures)
		
		# Now we work out the costs
		team_costs = {}
		for t, the_team in team_dict.items():
			r = res_dict.Res_dict(the_team.resources)
			queries.extend(r.make_set_queries(t))
		
		# Write queries to file
		f = open('%s/script_output/queries.sql' % common.data['server_fpath'], 'w')
		f.write("\n".join(queries))
		f.close()
		
		# Write orders to file
		f = open('%s/script_output/orders.txt' % common.data['server_fpath'], 'w')
		f.write("\n".join(orders))
		f.close()
		
		# Write debug to file
		f = open('%s/script_output/debug.txt' % common.data['server_fpath'], 'w')
		f.write("Team orders\n\n")
		f.write("\n".join(debug))
		f.close()
		
	except Exception as e:
		print(database.shell_text("[r]Failure[/r]"))
		print(database.shell_text("[r]Re run as 'rob3 start -l True[/r]'"))
		raise
	
	# print(database.shell_text(" - [g]Done[/g]"))
	# os.system('mate %s' % '%s/queries.sql' % common.data['server_fpath'])
	# os.system('mate %s' % '%s/orders.txt' % common.data['server_fpath'])
	
	# Failable orders
	failable = (
		re.compile(r'DELETE FROM squads WHERE id = [0-9]*;?'),
	)
	
	failed_queries = []
	
	#	Execute order queries
	#------------------------
	# print(database.shell_text("Executing order queries"), end="")
	for q in cli_f.progressbar(queries, "Running queries: ", 60, True):
	# for q in queries:
		if q == '': continue
		if q[0:2] == '--': continue
		
		try:
			cursor.execute(q)
		except Exception as e:
			ignorable = False
			
			for f in failable:
				if f.search(q) != None:
					ignorable = True
					failed_queries.append(q)
			
			if not ignorable:
				for f in failable:
					print("")
					print(f.search(q))
					print("")
			
				cursor.execute('ROLLBACK')
				print(database.shell_text(" - [r]Failure[/r]"))
				print("Query: %s\n" % q)
				print(database.shell_text("[r]Re run as 'rob3 start -l True'[/r]"))
				raise
	
	# print(database.shell_text(" - [g]Done[/g]"))
	
	if len(failed_queries) > 0:
		print("Failed queries")
		print("\n".join(failed_queries))
	
	# Build up a dict of the queries
	query_dict = {}
	for q in cursor.queries:
		if q in ("BEGIN", "COMMIT", "ROLLBACK"): continue
		
		if q not in query_dict:
			query_dict[q] = 0
		
		query_dict[q] += 1
	
	# What's our most popular query?
	pop_count, pop_query = 0, ""
	for q, c in query_dict.items():
		if c > pop_count:
			pop_count = c
			pop_query = q
	
	print("\n\n--- Info ---")
	print("Time taken: %s" % str(round(time.time() - start_time, 3))[0:5])
	print("Queries: %d" % len(cursor.queries))
	print("Uniques: %d" % len(set(cursor.queries)))
	print("Most queried: %s (%d)" % (pop_query, pop_count))
	
	#	Verbose mode
	#------------------------
	if options.verbose:
		cursor.execute("ROLLBACK")
		print("Rolled back")
	else:
		cursor.execute("COMMIT")
		print("Startup scripts executed")
		os.system("open %s/script_output/orders.txt" % common.data['server_fpath'])
Пример #30
0
def create_table(cursor, table_info, show_fixes=False):
	"""Creates a table from the item"""
	query = "drop table %s;" % table_info['Name']
	try:					cursor.execute(query)
	except Exception as e:	pass
	
	query = ["create table %s" % table_info['Name']]
	query.append("(")
	
	# for prop, prop_type in my_item.property_dict.items():
	pks = []
	for f in table_info['Fields']:
		if f.primary_key: pks.append(f.name)
		
		query.append(f.create_field())
	
	query.append("primary key (%s)" % (",".join(pks)))
	query.append(")")
	
	# Bring it all together
	query = "\n".join(query)
	
	# print(query)
	# exit()
	
	if show_fixes:
		output = [query]
	else:
		try:	 cursor.execute(query)
		except Exception as e:
			raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
	
		output = [database.shell_text('%s created [g]successfully[/g]' % table_info['Name'])]
	
	
	# Any indexes? - Query comes from PhpPgAdmin
	index_queries = []
	found_indexes = []
	missing_indexes = []
	surplus_indexes = []
	
	query = """SELECT c2.relname AS indname, i.indisprimary, i.indisunique, pg_get_indexdef(i.indexrelid) AS inddef,
				obj_description(c.oid, 'pg_index') AS idxcomment
			FROM pg_class c, pg_class c2, pg_index i
			WHERE c.relname = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid""" % table_info['Name']
	try: cursor.execute(query)
	except Exception as e:
		raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
	
	for row in cursor:
		if row["indisprimary"] == True: continue
		
		real_index_name = row["indname"].replace('%s_index_' % table_info['Name'], '')
		
		if real_index_name in table_info['Indexes']:
			found_indexes.append(real_index_name)
		else:
			surplus_indexes.append(real_index_name)
			index_queries.append("drop index %s;" % (row["indname"]))
	
	# Any leftover indexes?
	for i_name, i_content in table_info['Indexes'].items():
		if not (i_name in found_indexes):			
			missing_indexes.append(i_name)
			index_queries.append("create index %s_index_%s on %s (%s);" % (table_info['Name'], i_name, table_info['Name'], i_content))
	
	for query in index_queries:
		if show_fixes:
			output.append(query)
		else:
			try: cursor.execute(query)
			except Exception as e:
				raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
			output.append(database.shell_text('index_%s added to %s [g]successfully[/g]' % (i_name, table_info['Name'])))
	
	return "\n".join(output)