Beispiel #1
0
def main(cursor):
	battle_id	= int(common.get_val("battle", -1))
	unit_id		= int(common.get_val("unit", -1))
	army_id		= int(common.get_val("army", -1))
	amount		= int(common.get_val("amount", 0))
	
	if battle_id < 1:
		raise Exception("No battle supplied")
	
	if army_id < 1:
		raise Exception("No army supplied")
	
	if unit_id < 1:
		raise Exception("No unit supplied")
	
	if amount == 0:
		return ""
		raise Exception("No amount supplied")
	
	if amount < 0:
		return refund_losses(cursor, battle_id, unit_id, army_id, amount)
	
	# Get all handles and instances
	the_battle		= battle_q.get_one_battle(cursor, battle_id)
	battle_squads	= the_battle.get_squads(cursor)
	squad_dict		= squad_q.get_squads_from_army_and_unit(cursor, army_id, unit_id)
	
	squad_list = [s for s in battle_squads if s in squad_dict]
	
	# return str(squad_list)
	squad_f.apply_losses_to_squads(cursor, amount, squad_list, battle_id)
	return ""
Beispiel #2
0
def main(cursor):
	battle_id = int(common.get_val('battle', -1))
	the_battle = battle_q.get_one_battle(cursor, battle_id)
	database.query(cursor, battle_f.make_delete_query(battle_id))
	
	# Redirect
	page_data['Redirect'] = 'list_battles&campaign={0:d}'.format(the_battle.campaign)
Beispiel #3
0
def refund_losses(cursor, battle_id, unit_id, army_id, amount):
	# Get all handles and instances
	the_battle		= battle_q.get_one_battle(cursor, battle_id)
	losses			= battle_q.get_all_battle_losses_by_squad(cursor, battle_id)
	squad_dict		= squad_q.get_squads_from_army_and_unit(cursor, army_id, unit_id)
	
	queries = []
	temp_amount = -amount
	for s, actual_amount in losses.items():
		if s in squad_dict and squad_dict[s].unit == unit_id:
			a = min(temp_amount, actual_amount)
			temp_amount -= a
			
			queries.append("UPDATE squads SET amount = amount + %d WHERE id = %d;" % (a, s))
			queries.append("UPDATE squad_battle_history SET losses = losses - %d WHERE squad = %d AND battle = %d;" % (a, s, battle_id))
	
	database.query(cursor, *queries)
	return ""
Beispiel #4
0
def main(cursor, campaign_id = -1):
	output = ["""<div style="padding: 5px;">"""]
	
	campaign_id	= int(common.get_val('campaign', campaign_id))
	battle_id	= int(common.get_val('battle', 0))
	ajax = common.get_val('ajax', 0)
	
	if campaign_id < 1:
		if battle_id < 1:
			return "No campaign selected"
		else:
			the_battle = battle_q.get_one_battle(cursor, battle_id)
			campaign_id = the_battle.campaign
	
	the_campaign	= campaign_q.get_one_campaign(cursor, campaign_id)
	battle_dict		= battle_q.get_battles_from_campaign(cursor, campaign_id)
	team_dict		= team_q.get_all_teams(cursor)
	city_dict		= city_q.get_all_cities(cursor)
	
	if not ajax:
		output.append("""
		<div style="float:right;border:0px solid #000; width: 50%;">
			<form id="delete_form" action="exec.py" method="post" accept-charset="utf-8">
				<input type="hidden" name="campaign" id="campaign" value="{id}" />
				<input type="hidden" name="mode" id="mode" value="remove_campaign" />
				<input style="float:right; margin-right:100px;" type="button" value="Delete campaign" onclick="var answer = confirm('Delete {esc_name}?')
				if (answer) $('#delete_form').submit();" />
			</form>
			<div id="campaign_info">
				&nbsp;
			</div>
			&nbsp;
		</div>
		
		<a href="web.py?mode=list_campaigns&amp;turn={turn}" class="block_link" style="display:inline; text-align:left;">Campaigns of this turn</a>
		<a href="web.py?mode=setup_campaign&amp;campaign={camp}" class="block_link" style="display:inline;">Setup campaign</a>
		<br /><br />
		
		<span class="stitle">{name}</span>
		<br /><br />
		<table border="0" cellspacing="0" cellpadding="5">
		""".format(
			turn			= common.current_turn(),
			camp			= the_campaign.id,
			name			= the_campaign.name,
			id				= campaign_id,
			esc_name		= common.js_name(the_campaign.name),
		))
		
	else:
		output.append("""<table border="0" cellspacing="0" cellpadding="5" width="100%">""")
	
	output.append("""
		<tr class="row2">
			<th>Battle</th>
			<th>Time</th>
			<th>Location</th>
			<th>Type</th>
			<th>Result</th>
			<th>&nbsp;</th>
			<th colspan="2">Perform</th>
		</tr>""")
	
	# Display battles
	i = -1
	for battle_id, the_battle in battle_dict.items():
		i += 1
		
		location = "%d, %d" % (the_battle.x, the_battle.y)
		city_link = ""
		
		if the_battle.city > 0:
			city_link = '<!-- PY --> <a href="web.py?mode=edit_city&amp;city=%d">Edit city</a><!-- PYEND -->' % the_battle.city
		
		
		output.append("""
		<tr class="row{i}">
			<td>{name} ({battle_id}){city_link}</td>
			<td>{start} : {duration}</td>
			<td>{location} &nbsp; {city}</td>
			<td>{type}</td>
			<td>{result}</td>
			<td style="padding:0px;"><a href="web.py?mode=setup_battle&amp;battle={battle_id}" class="block_link">Setup</a></td>
			<td style="padding:0px;"><a href="web.py?mode=perform_battle&amp;battle={battle_id}" class="block_link">By unit</a></td>
			<td style="padding:0px;"><a href="web.py?mode=perform_by_army&amp;battle={battle_id}" class="block_link">By army</a></td>
		</tr>
		""".format(
			i=i%2,
			city_link = city_link,
			name=common.doubleclick_text("battles", "name", battle_id, the_battle.name, label_style="font-weight:bold;"),
			start=common.doubleclick_text("battles", "start", battle_id, the_battle.start, size=2),
			duration=common.doubleclick_text("battles", "duration", battle_id, the_battle.duration, size=2),
			location=location,
			battle_id=battle_id,
			city = city_dict[the_battle.city].name,
			
			type = battle.battle_types[the_battle.type],
			result = battle.result_types[the_battle.result],
		))
		
	
	if ajax:
		onload = ""
	else:
		onload = common.onload("$('#form_city').focus();")
	
	# Add new battle
	i += 1
	output.append("""
	<tr class="row{i}">
		<form action="exec.py" id="add_battle_form" method="post" accept-charset="utf-8">
		<input type="hidden" name="mode" value="add_battle" />
		<input type="hidden" name="campaign" value="{campaign_id}" />
		<td style="padding: 1px;"><input type="text" name="name" id="new_name" value="" /></td>
		<td style="padding: 1px;">
			<input type="text" name="start" value="-1" size="2"/>
			<input type="text" name="duration" value="" size="2"/>
		</td>
		<td style="padding: 1px;">
			<input type="text" name="location" value="" size="8"/>
		</td>
		
		<td style="padding: 1px;">
			{type}
		</td>
		<td>&nbsp;</td>
		
		<td colspan="3" style="padding: 0px;">
			<!--<a class="block_link" href="#" onclick="$('#add_battle_form').submit();">Add</a>-->
			<input type="submit" value="Add" />
		</td>
		</form>
	</tr>""".format(
		i=i%2,
		campaign_id=campaign_id,
		type = common.option_box("type", elements = battle.battle_types, element_order = [], custom_id = ""),
	))
	
	# Add by city
	city_dict = city_q.get_cities_for_dropdown(cursor)
	keys, names = [], {}
	for c, the_city in city_dict.items():
		if the_city.dead > 0: continue
		if not team_dict[the_city.team].active: continue
		
		keys.append(c)
		names[c] = the_city.name
	
	i += 1
	output.append("""
	<tr class="row{i}">
		<form action="exec.py" id="add_battle_form" method="post" accept-charset="utf-8">
		<input type="hidden" name="mode" value="add_battle" />
		<input type="hidden" name="campaign" value="{campaign_id}" />
		<td style="padding: 1px;">{city_list}</td>
		<td style="padding: 1px;">
			<input type="text" name="start" value="-1" size="2"/>
			<input type="text" name="duration" value="" size="2"/>
		</td>
		<td>
			&nbsp;
		</td>
		
		<td style="padding: 1px;">
			{type}
		</td>
		<td>&nbsp;</td>
		
		<td colspan="3" style="padding: 0px;">
			<!--<a class="block_link" href="#" onclick="$('#add_battle_form').submit();">Add</a>-->
			<input type="submit" value="Add" />
		</td>
		</form>
		{onload}
	</tr>
	</table>
	<br /><br />
	""".format(
		i=i%2,
		campaign_id=campaign_id,
		onload=onload,
		city_list = common.option_box(
			name='city',
			elements=names,
			element_order=keys,
			custom_id="form_city",
		),
		type = common.option_box("type", elements = battle.battle_types, element_order = [], custom_id = ""),
	))
	
	
	# Chosen kills
	output.append("""
	<div style="float:right;border:0px solid #000; width: 60%;">
		<div id="player_kills">
			&nbsp;
		</div>
	</div>
	""")
	
	
	# Now for moving all the armies
	output.append("""
	<!-- PY -->
	<table border="0" cellspacing="0" cellpadding="5">
	<!--
		<tr class="row2">
			<th>Team</th>
			<th colspan="3">Move</th>
			<th>&nbsp;</th>
		</tr>
	-->""")
	
	the_campaign.get_sides_full(cursor)
	for s in range(1, the_campaign.sides+1):
		teams_on_side = the_campaign.sides_full[s]
		
		if len(teams_on_side) > 0:
			i = 0
			output.append("<tr class='row2'><td colspan='5' style='text-align:center;'>Side %d</td></tr>" % s)
			
			for t in teams_on_side:
				i += 1
				
				output.append("""
				<tr class="row{i}">
					<td>{team}</td>
					<td style='padding:0px;'>
						<!--
						<a class="block_link" href="#" onclick="$.get('exec.py', {{mode: 'move_armies', team:{team_id}, campaign:{campaign}, location: ''}}, function () {{$('#ajax_result_{team_id}').html('Moved to location of final battle').val());}}); return false;">Final battle</a>
						-->
						<a class="block_link" href="exec.py?mode=move_armies&team={team_id}&campaign={campaign}">Final battle</a>
					</td>
					<td style='padding:0px;'>
						<a class="block_link" href="#" onclick="$.get('exec.py', {{mode: 'move_armies', team:{team_id}, campaign:{campaign}, location: $('#location_{team_id}').value}}, function () {{$('#ajax_result_{team_id}').html('Moved to ' + $('#location_{team_id}').val());}}); return false;">Location:</a>
					</td>
					<td style="padding:2px;">
						<form action="exec.py" method="post" accept-charset="utf-8" id="team_form_{team_id}">
							<input type="hidden" name="mode" value="move_armies" />
							<input type="hidden" name="team" value="{team_id}" />
							<input type="hidden" name="campaign" value="{campaign}" />
							<input type="text" name="location" id="location_{team_id}" value="" size="6"/>
						</form>
					</td>
					<td id="ajax_result_{team_id}">
						&nbsp;
					</td>
				</tr>
				""".format(
					i = i % 2,
					team_id = t['team'],
					team = team_dict[t['team']].name,
					campaign = campaign_id,
				))
	output.append("</table><!-- PYEND -->")
	
	output.append("</div><!-- PY -->%s<!-- PYEND -->" % common.onload("""
		$('#campaign_info').load('web.py',{'mode':'campaign_info','ajax':'True','campaign':'%d'});
		$('#player_kills').load('web.py',{'mode':'player_kills','ajax':'True','campaign':'%d'});
	""" % (campaign_id, campaign_id)))
	
	if ajax:
		output.append("<br />")
	
	return "".join(output)
Beispiel #5
0
def main(cursor, battle_id=-1):
	battle_id = int(common.get_val('battle', battle_id))
	ships = int(common.get_val('ships', True))
	
	if battle_id < 1:
		return "No battle selected"
	
	the_battle = battle_q.get_one_battle(cursor, battle_id)
	
	# Get some other stuff
	team_dict		= team_q.get_all_teams(cursor)
	the_campaign	= campaign_q.get_one_campaign(cursor, the_battle.campaign)
	
	output = ["""	
	<div style='padding: 5px;'>
	<a href="web.py?mode=list_campaigns&amp;turn={turn}" class="block_link" style="display:inline; text-align:left;">Campaigns of this turn</a>
	<a href="web.py?mode=setup_campaign&amp;campaign={c.id}" class="block_link" style="display:inline;">Setup campaign</a>
	<a href="web.py?mode=list_battles&amp;campaign={c.id}" class="block_link" style="display:inline;">List battles</a>
	<a href="web.py?mode=setup_battle&amp;battle={b.id}" class="block_link" style="display:inline;">Setup battle</a>
	<a href="web.py?mode=perform_battle&amp;battle={b.id}" class="block_link" style="display:inline;">By unit</a>
	<br /><br />
	
	<span class="stitle">{c.name} - {b.name}</span> - <a href="web.py?mode=perform_battle&amp;battle={b.id}&amp;ships=0">No ships</a>
	
	<br />
	<a href="#" onclick="$('.show_evo_wedge').show(); $('.evo_wedge').hide(); return false;">Hide evo wedges</a>
	 - 
	<a href="#" onclick="$('.show_evo_wedge').hide(); $('.evo_wedge').show(); return false;">Show evo wedges</a>
	""".format(
		c = the_campaign,
		b = the_battle,
		turn = common.current_turn(),
	)]
	
	#	Now for some stuff about the participants
	#------------------------
	the_campaign.get_sides_basic(cursor)
	the_campaign.get_armies_basic(cursor)
	the_battle.get_losses(cursor)
	the_battle.get_squads(cursor)
	
	team_list = []
	for s in range(1, the_campaign.sides+1):
		for t in the_campaign.sides_basic[s]:
			team_list.append(t)
	
	unit_dict = unit_q.get_units_from_team_list(cursor, team_list, special_units=True)
	
	evolution_dict = evolution_q.get_all_evolutions(cursor)
	
	# Sort squads by team
	# Doing this makes it O(2n) rather than O(t*n)
	army_dict = army_q.get_armies_from_list(cursor, the_battle.armies)
	squad_dict = squad_q.get_all_squads(cursor)
	squad_q.mass_get_squads(cursor, army_dict)
	armies_by_team = {}
	
	for army_id, the_army in army_dict.items():
		if the_army.team not in armies_by_team: armies_by_team[the_army.team] = []
		armies_by_team[the_army.team].append(army_id)
	
	for s in range(1, the_campaign.sides+1):
		teams_on_side = the_campaign.sides_basic[s]
		
		if len(teams_on_side) > 0:
			output.append('''<table border="1" cellspacing="0" cellpadding="5" width="100%">
			<tr>
				<td class="stitle" colspan="2">{s}</td>
			</tr>'''.format(s=s))
			
			for i, team_id in enumerate(teams_on_side):
				if team_id not in armies_by_team:
					armies_by_team[team_id] = []
				
				# Table tags for team cell
				if i % 2 == 0:	output.append('<tr><td>')
				else:			output.append('<td>')
				
				the_team = team_dict[team_id]
				
				# Team header
				output.append('''
				<span class="stitle">{name}</span>
				 - 
				<a href="web.py?mode=list_units&amp;team={id}">Military</a>
				 - 
				<a href="web.py?mode=list_armies&amp;team={id}">Armies</a>
				 - 
				<a href="#" onclick="$(this).parent().hide(); return false;">Hide</a>
				'''.format(
					name=the_team.name,
					id=team_id,
				))
				
				# Armies
				for army_id in armies_by_team[team_id]:
					the_army = army_dict[army_id]
					
					army_output = draw_army(the_battle, squad_dict, unit_dict, the_army)
					
					if army_output != None:
						output.append(army_output)
						output.append("<tr><td colspan='1'>&nbsp;</td></tr>")
				
				# Evos row
				evo_output = []
				
				the_team.get_evolutions(cursor)
				for evolution_id in the_team.evolutions:
					the_evo = evolution_dict[evolution_id]
				
					if the_team.evolutions[evolution_id] == 0: continue
					if not the_evo.combat_relevant: continue
				
				
					if the_evo.max_level == 1 and the_team.evolutions[evolution_id] == 1:
						evo_output.append("""<strong>%(evo_name)s</strong><br />""" % {
							"evo_name":	the_evo.name,
						})
					else:
						evo_output.append("""<strong>%(level)sx %(evo_name)s</strong><br />""" % {
							"level":	the_team.evolutions[evolution_id],
							"evo_name":	the_evo.name,
						})
				
				count = 0
				output.append("""
				<tr class="row%(count)s">
					<td colspan="11" style="padding:0px;">
						<div id="show_evos_%(team_id)s" style="display:nnone;" class="show_evo_wedge">
							<a href="#" class="block_link" onclick="$('#show_evos_%(team_id)s').hide(); $('#evos_%(team_id)s').fadeIn(250);return false;">Evos</a>
						</div>
						<div id="evos_%(team_id)s" style="display:none;" class="evo_wedge">
							<a href="#" class="block_link" onclick="$('#show_evos_%(team_id)s').fadeIn(250); $('#evos_%(team_id)s').hide(); return false;">Hide</a><br />
				
							%(evo_output)s
						</div>
					</td>
				</tr>
				""" % {
					"count":		count%2,
					"team_id":		team_id,
					"evo_output":	"".join(evo_output)
				})
				
				# End of team units
				output.append("</table>")
				
				
				# Table tags for team cell
				if i % 2 == 0:	output.append('</td>')
				else:			output.append('</td></tr>')
			
			# Not sure that we need to adhere to W3C specs for tables...
			# if i % 2 == 0:	output.append('<td>&nbsp;</td></tr>')
			
			output.append("</table><br />")
		else:
			output.append('No teams on side {s}'.format(s=s))
	
	# Field for the ids of the armies listed that we need to sort out
	# output.append('</table><br />')
	
	output.append("</div>")
	
	page_data['Title'] = "Perform by army - %s" % the_battle.name
	return "".join(output)
Beispiel #6
0
def main(cursor, battle_id = -1):
	battle_id = int(common.get_val('battle', battle_id))
	
	if battle_id < 1:
		return "No battle selected"
	
	the_battle = battle_q.get_one_battle(cursor, battle_id)
	
	# If we're being sent the info from the view_map page then this is the new location we need
	new_location = common.get_val('location', "")
	
	if new_location == "":
		new_location = "%s,%s" % (the_battle.x, the_battle.y)# default value
	
	# Get some other stuff
	cities_dict		= city_q.get_live_cities(cursor)
	team_dict		= team_q.get_all_teams(cursor)
	the_campaign	= campaign_q.get_one_campaign(cursor, the_battle.campaign)
	
	names = {0:"No city"}
	for c, the_city in cities_dict.items():
		names[c] = the_city.name
	
	city_keys = [0]
	city_keys.extend(list(cities_dict.keys()))
	
	output = ["<div style='padding: 5px;'>"]
	
	# output.append('<a href="web.py?mode=perform_battle&amp;battle={0}" class="block_link">Perform</a>'.format(battle_id))
	output.append("""
	<a href="web.py?mode=list_campaigns&amp;turn={turn}" class="block_link" style="display:inline; text-align:left;">Campaigns of this turn</a>
	<a href="web.py?mode=setup_campaign&amp;campaign={campaign_id}" class="block_link" style="display:inline;">Setup campaign</a>
	<a href="web.py?mode=list_battles&amp;campaign={campaign_id}" class="block_link" style="display:inline;">List battles</a>
	<a href="web.py?mode=perform_battle&amp;battle={battle_id}" class="block_link" style="display:inline;">Perform</a>
	<br /><br />
	<!-- PY -->
	<form action="exec.py" method="post" accept-charset="utf-8">
	<!-- PYEND -->
		<input type="hidden" name="mode" value="edit_battle_commit" />
		<input type="hidden" name="id" value="{battle_id}" />
		<input type="hidden" name="campaign" value="{campaign_id}" />
		
		Editing: {name_text}
		<br /><br />
		
		<table border="0" cellspacing="5" cellpadding="5">
			<tr>
				<td><label for="start">Start:</label></td>
				<td style="padding: 1px;">{battle_start_text}</td>
				
				<td width="5">&nbsp;</td>
				
				<td><label for="duration">Duration:</label></td>
				<td style="padding: 1px;">{battle_duration_text}</td>
			</tr>
			<tr>
				<td style="padding: 0px;">
					<a class="block_link" href="web.py?mode=view_map&amp;new_mode=setup_battle&amp;battle={battle_id}"">Location:</a>
				</td>
				<td style="padding: 1px;">{battle_location_text}</td>
				
				<td>&nbsp;</td>
				<td><label for="city">City:</label></td>
				<td style="padding: 1px;">{city_menu}</td>
			</tr>
			<tr>
				<td><label for="type">Type:</label></td>
				<td style="padding: 1px;">{type}</td>
				
				<td>&nbsp;</td>
				
				<td><label for="result">Result:</label></td>
				<td style="padding: 1px;">{result}</td>
			</tr>
		</table>
		<!-- PY -->
		<br />
		<input type="submit" value="Perform edit" />
	</form>
	<!-- PYEND -->
	<form id="delete_form" action="exec.py" method="post" accept-charset="utf-8">
		<input type="hidden" name="battle" id="battle" value="{battle_id}" />
		<input type="hidden" name="mode" id="mode" value="remove_battle" />
		<input style="float:right; margin-right:100px;" type="button" value="Delete battle" onclick="var answer = confirm('Delete {esc_name}?')
		if (answer) $('#delete_form').submit();" />
	</form>
	<br /><br />""".format(
		turn					= common.current_turn(),
		battle_id =				battle_id,
		campaign_id = 			the_battle.campaign,
		name =					the_battle.name,
		esc_name =				common.js_name(the_battle.name),
		name_text =				common.text_box("name", the_battle.name, size=20),
		battle_location_text	= common.text_box("location", new_location, 10),
		battle_start_text		= common.text_box("start", the_battle.start, 3),
		battle_duration_text	= common.text_box("duration", the_battle.duration, 3),
		city_menu				= common.option_box(
			name='city',
			elements=names,
			element_order=city_keys,
			custom_id="",
			selected=the_battle.city,
		),
		
		type = common.option_box("type", elements = battle.battle_types, element_order = [], selected=battle.battle_types[the_battle.type]),
		result = common.option_box("result", elements = battle.result_types, element_order = [], selected=battle.result_types[the_battle.result]),
	))
	
	output.append("</div>")
	
	return "".join(output)
Beispiel #7
0
def main(cursor, battle_id=-1):
	battle_id = int(common.get_val('battle', battle_id))
	ships = int(common.get_val('ships', True))
	
	if battle_id < 1:
		return "No battle selected"
	
	the_battle = battle_q.get_one_battle(cursor, battle_id)
	
	# Get some other stuff
	team_dict		= team_q.get_all_teams(cursor)
	the_campaign	= campaign_q.get_one_campaign(cursor, the_battle.campaign)
	
	output = ["""	
	<div style='padding: 5px;'>
	<a href="web.py?mode=list_campaigns&amp;turn={turn}" class="block_link" style="display:inline; text-align:left;">Campaigns of this turn</a>
	<a href="web.py?mode=setup_campaign&amp;campaign={c.id}" class="block_link" style="display:inline;">Setup campaign</a>
	<a href="web.py?mode=list_battles&amp;campaign={c.id}" class="block_link" style="display:inline;">List battles</a>
	<a href="web.py?mode=setup_battle&amp;battle={b.id}" class="block_link" style="display:inline;">Setup battle</a>
	<a href="web.py?mode=perform_by_army&amp;battle={b.id}" class="block_link" style="display:inline;">By army</a>
	<br /><br />
	
	<span class="stitle">{c.name} - {b.name}</span> - <a href="web.py?mode=perform_battle&amp;battle={b.id}&amp;ships=0">No ships</a>
	
	<br />
	<a href="#" onclick="$('.show_evo_wedge').show(); $('.evo_wedge').hide(); return false;">Hide evo wedges</a>
	 - 
	<a href="#" onclick="$('.show_evo_wedge').hide(); $('.evo_wedge').show(); return false;">Show evo wedges</a>
	""".format(
		c = the_campaign,
		b = the_battle,
		turn = common.current_turn(),
	)]
	
	#	Now for some stuff about the participants
	#------------------------
	the_campaign.get_sides_basic(cursor)
	the_campaign.get_armies_basic(cursor)
	the_battle.get_losses(cursor)
	the_battle.get_squads(cursor)
	
	team_list = []
	for s in range(1, the_campaign.sides+1):
		for t in the_campaign.sides_basic[s]:
			team_list.append(t)
	
	unit_dict = unit_q.get_units_from_team_list(cursor, team_list, special_units=True)
	
	evolution_dict = evolution_q.get_all_evolutions(cursor)
	
	# Sort squads by team
	# Doing this makes it O(2n) rather than O(t*n)
	squad_dict = squad_q.get_squads_from_list(cursor, the_battle.squads)
	squads_by_team = {}
	
	for squad_id, the_squad in squad_dict.items():
		if the_squad.team not in squads_by_team: squads_by_team[the_squad.team] = []
		squads_by_team[the_squad.team].append(squad_id)
	
	for s in range(1, the_campaign.sides+1):
		teams_on_side = the_campaign.sides_basic[s]
		
		if len(teams_on_side) > 0:
			output.append('''<table border="1" cellspacing="0" cellpadding="5" width="100%">
			<tr>
				<td class="stitle" colspan="2">{s}</td>
			</tr>'''.format(s=s))
			
			for i, team_id in enumerate(teams_on_side):
				if team_id not in squads_by_team:
					squads_by_team[team_id] = []
				
				# Table tags for team cell
				if i % 2 == 0:	output.append('<tr><td>')
				else:			output.append('<td>')
				
				the_team = team_dict[team_id]
				
				# Team header
				output.append('''
				<span class="stitle">{name}</span>
				 - 
				<a href="web.py?mode=list_units&amp;team={id}">Military</a>
				 - 
				<a href="web.py?mode=list_armies&amp;team={id}">Armies</a>
				 - 
				<a href="#" onclick="$(this).parent().hide(); return false;">Hide</a>
				'''.format(
					name=the_team.name,
					id=team_id,
				))
				
				# Build dictionaries/lists
				team_units = the_team.get_units(cursor)
				total_count = {}
				unit_count = {}
				unit_losses = {}
				
				army_list = the_campaign.get_armies_from_team(cursor, team_id)
				army_dict = army_q.get_armies_from_list(cursor, army_list)
				squad_q.mass_get_squads(cursor, army_dict)
				
				# armies			= the_battle.get_armies()
				# squads			= the_battle.get_squads()
				transport_lost = 0
				transport_capacity = 0
				army_size = 0
				
				for unit_id in team_units.keys():
					unit_count[unit_id] = 0
					unit_losses[unit_id] = 0
				
				for squad_id in squads_by_team[team_id]:
					unit_count[squad_dict[squad_id].unit] += squad_dict[squad_id].amount
					army_size += squad_dict[squad_id].amount
					unit_losses[squad_dict[squad_id].unit] += the_battle.losses.get(squad_id, 0)
				
				
				# print("")
				# print(the_battle.squads, "<br />")
				# print(unit_count, "<br />")
				# print(unit_losses, "<br />")
				# exit()
				
				output.append("""
				<table border="0" cellspacing="0" cellpadding="5" style="width: 100%">
					<tr class="row2">
						<th>Unit</th>
						<th colspan="4">&nbsp;</th>
						<th>Equipment</th>
						<th>Total</th>
						<th>Amount</th>
						<th colspan="2">Losses</th>
						<th>&nbsp;</th>
					</tr>
				""")
				
				
				# Build look-ahead
				lookahead = {}
				last_unit = -1
				for unit_id in team_units.keys():
					if unit_dict[unit_id].type_cat == unit.categories.index("Ship") and ships == False:
						continue
					
					if unit_count[unit_id] == 0 and unit_losses[unit_id] == 0:
						continue
					
					lookahead[last_unit] = unit_id
					last_unit = unit_id
				lookahead[last_unit] = 0
				
				# Unit row
				team_losses = 0
				team_size = 0
				count = -1
				for unit_id, amount in team_units.items():
					if unit_dict[unit_id].type_cat == unit.categories.index("Ship") and ships == False:
						continue
					
					if unit_count[unit_id] == 0 and unit_losses[unit_id] == 0:
						continue
					
					count += 1
					team_losses += unit_losses[unit_id]
					team_size += unit_count[unit_id]
					transport_capacity += (unit_dict[unit_id].transport * unit_count[unit_id])
				
					# Form JS
					form_js = """$('#ajax_target').load('web.py', {mode: 'add_unit_loss', ajax: 'True', battle: %(battle_id)s, unit: %(unit_id)s, team: %(team_id)s, amount: $('#amount_for_%(unit_id)s_%(team_id)s').attr('value')}, function ()
					{
						var loss			= parseInt($('#amount_for_%(unit_id)s_%(team_id)s').attr('value').replace(',', ''));
						var current_loss	= parseInt($('#losses_%(unit_id)s_%(team_id)s').html().replace(',', ''));
						var current_amount	= parseInt($('#amount_%(unit_id)s_%(team_id)s').html().replace(',', ''));
				
						$('#amount_%(unit_id)s_%(team_id)s').html(current_amount - loss);
						$('#losses_%(unit_id)s_%(team_id)s').html(current_loss + loss);
				
						$('#amount_for_%(unit_id)s_%(team_id)s').attr('value', '');
						$('#amount_for_%(ahead)s_%(team_id)s').focus();
					});
					return false;""" % {
						"battle_id":	battle_id,
						"team_id":		team_id,
						"unit_id":		unit_id,
						"ahead":		lookahead[unit_id],
					};
				
					# function add_unit_amount_%(unit_id)s ()
					# {
					# 	$('#amountFor_team').removeAttr('value');
					# 	$('#spanAddUnitFor_team').load('ajax.php', {mode: 'getUnitsNotInWar', team: $team, war: $warId});
					# });
					
					output.append("""
					<tr class="row{count}" id="row_{team_id}_{unit_id}">
						<td style='font-weight:bold;'>{unit_name}</td>
						
						<td>{weapon_cat}</td>
						<td>{armour_cat}</td>
						<td>{move_cat}</td>
						<td>{training_cat}</td>
						
						<td>{equipment}</td>
						<td>{total}</td>
						<td id="amount_{unit_id}_{team_id}">{amount}</td>
						<td id="losses_{unit_id}_{team_id}">{losses}</td>
						<td id="losses_cent_{unit_id}_{team_id}">{losses_cent}%</td>
						<td style="padding:1px;">
							<form action="exec.pyy" method="post" id="" onsubmit="{form_js}" accept-charset="utf-8">
								<input type="hidden" name="mode" id="mode" value="add_unit_loss" />
								<input type="hidden" name="battle" id="battle" value="{battle_id}" />
								<input type="hidden" name="unit" id="unit" value="{unit_id}" />
								<input type="hidden" name="team" id="team" value="{team_id}" />
								<input type="text" onfocus="{on_focus}" onblur="{on_blur}" name="amount" id="amount_for_{unit_id}_{team_id}" value="" size="5"/>
							</form>
						</td>
					</tr>
					""".format(
						count =		count%2,
						form_js =	form_js,
						unit_name =	unit_dict[unit_id].name,
						equipment =	unit_dict[unit_id].equipment_string,
						total		= common.number_format(unit_count[unit_id] + unit_losses[unit_id]),
						amount =	common.number_format(unit_count[unit_id]),
						losses =	common.number_format(unit_losses[unit_id]),
						losses_cent = round(unit_losses[unit_id]/(unit_count[unit_id]+unit_losses[unit_id])*100),
						battle_id =	battle_id,
						team_id =	team_id,
						unit_id =	unit_id,
						on_focus =	"$('#row_{0}_{1}').addClass('row3');".format(team_id, unit_id),
						on_blur =	"$('#row_{0}_{1}').removeClass('row3');".format(team_id, unit_id),
						
						weapon_cat		= "",
						armour_cat		= "",
						move_cat		= "",
						training_cat	= "",
						
						# xweapon_cat		= short_weapon_categories[unit_dict[unit_id].weapon_cat],
						# xarmour_cat		= short_armour_categories[unit_dict[unit_id].armour_cat],
						# xmove_cat		= short_move_categories[unit_dict[unit_id].move_cat],
						# xtraining_cat	= short_training_categories[unit_dict[unit_id].training_cat],
					))
				
				# Size row
				if (team_size+team_losses) == 0:
					losses_cent = ""
				else:
					losses_cent = "%d%%" % round(team_losses/(team_size+team_losses)*100)
				
				count += 1
				output.append("""
				<tr class="row%(count)s">
					<td colspan="6">
						Military size:
					</td>
					<td>
						%(size)s
					</td>
					<td>
						%(losses)s
					</td>
					<td>
						%(losses_cent)s
					</td>
					<td colspan="3">&nbsp;</td>
				</tr>
				<tr class="row%(count_)s">
					<td colspan="6">Transport:</td>
					<td>%(transport_capacity)s</td>
					<td colspan="5">&nbsp;</td>
				</tr>
				""" % {
					"count":	count%2,
					"count_":	(count+1)%2,
					"size":		format(army_size, ','),
					"losses":	team_losses,
					"losses_cent": losses_cent,
					"transport_capacity":	format(transport_capacity, ','),
				})
				count += 1
				
				# Evos row
				evo_output = []
				
				the_team.get_evolutions(cursor)
				for evolution_id in the_team.evolutions:
					the_evo = evolution_dict[evolution_id]
				
					if the_team.evolutions[evolution_id] == 0: continue
					if not the_evo.combat_relevant: continue
				
				
					if the_evo.max_level == 1 and the_team.evolutions[evolution_id] == 1:
						evo_output.append("""<strong>%(evo_name)s</strong><br />""" % {
							"evo_name":	the_evo.name,
						})
					else:
						evo_output.append("""<strong>%(level)sx %(evo_name)s</strong><br />""" % {
							"level":	the_team.evolutions[evolution_id],
							"evo_name":	the_evo.name,
						})
				
				count += 1
				output.append("""
				<tr class="row%(count)s">
					<td colspan="11" style="padding:0px;">
						<div id="show_evos_%(team_id)s" style="display:nnone;" class="show_evo_wedge">
							<a href="#" class="block_link" onclick="$('#show_evos_%(team_id)s').hide(); $('#evos_%(team_id)s').fadeIn(250);return false;">Evos</a>
						</div>
						<div id="evos_%(team_id)s" style="display:none;" class="evo_wedge">
							<a href="#" class="block_link" onclick="$('#show_evos_%(team_id)s').fadeIn(250); $('#evos_%(team_id)s').hide(); return false;">Hide</a><br />
				
							%(evo_output)s
						</div>
					</td>
				</tr>
				""" % {
					"count":		count%2,
					"team_id":		team_id,
					"evo_output":	"".join(evo_output)
				})
				
				# End of team units
				output.append("</table>")
				
				
				# Table tags for team cell
				if i % 2 == 0:	output.append('</td>')
				else:			output.append('</td></tr>')
			
			# Not sure that we need to adhere to W3C specs for tables...
			# if i % 2 == 0:	output.append('<td>&nbsp;</td></tr>')
			
			output.append("</table><br />")
		else:
			output.append('No teams on side {s}'.format(s=s))
	
	# Field for the ids of the armies listed that we need to sort out
	# output.append('</table><br />')
	
	output.append("</div>")
	
	page_data['Title'] = "Perform by unit - %s" % the_battle.name
	return "".join(output)
Beispiel #8
0
def build_battle(w, battle):
	battle = battle_q.get_one_battle(w.cursor, battle)
	return _build_battle(w, battle.id)