Exemple #1
0
def main(cursor):
	# Get team Id
	unit_id = int(common.get_val('unit', 0))
	
	if unit_id < 1:
		return "No unit selected"
	
	the_unit	= unit_q.get_one_unit(cursor, unit_id)
	army_dict	= army_q.get_all_armies(cursor)#get_armies_from_team(cursor, the_unit.team, include_garrisons=True)
	squad_dict	= squad_q.get_squads_from_team_of_type(cursor, the_unit.team, unit_id)
	equipment_dict = equipment_q.get_all_equipment(cursor)
	
	page_data['Title'] = "Edit unit: %s" % the_unit.name
	
	output = ["<div style='padding: 5px;'>"]
	
	# Unit cost breakdown
	unit_cost_breakdown = unit_rules.print_unit_cost(the_unit, cursor=cursor, breakdown_mode=True)
	
	w = world.World(cursor)
	real_cost = "Post override: %s/%s" % (
		unit_rules.unit_cost_override(w, the_unit, the_unit.costs['material_cost'], w.teams()[the_unit.team]),
		unit_rules.unit_cost_override(w, the_unit, the_unit.costs['iron_cost'], w.teams()[the_unit.team]),
	)
	real_upkeep = "Post override: %s/%s" % (
		unit_rules.unit_upkeep_override(w, the_unit, the_unit.costs['material_cost'], w.teams()[the_unit.team]),
		unit_rules.unit_upkeep_override(w, the_unit, the_unit.costs['iron_cost'], w.teams()[the_unit.team]),
	)
	# real_cost = 0
	# real_upkeep = 0
	
	output.append("""
	<div style="float: right; width: 50%;">
		<strong>Unit cost breakdown</strong><br />
		{cost_breakdown}<br />
		{real_cost}
		<br /><br />
	
		<strong>Unit upkeep breakdown</strong><br />
		{upkeep_breakdown}<br />
		{real_upkeep}
		<br /><br />
		
		<strong>Unit categories</strong><br />
		Type category: {type_cat}<br />
		Weapon category: {weapon_cat}<br />
		
		
		<br />
		<form id="delete_form" action="exec.py" method="post" accept-charset="utf-8">
			<input type="hidden" name="unit" id="unit" value="{unit_id}" />
			<input type="hidden" name="mode" id="mode" value="remove_unit" />
			<input style="float:right; margin-right:100px;" type="button" value="Delete unit" onclick="var answer = confirm('Delete {name}?')
			if (answer) $('#delete_form').submit();" />
		</form>
	</div>
	""".format(
		cost_breakdown		= "<br />".join(unit_cost_breakdown['cost']),
		upkeep_breakdown	= "<br />".join(unit_cost_breakdown['upkeep']),
		real_cost			= real_cost,
		real_upkeep			= real_upkeep,
		
		unit_id				= unit_id,
		name				= the_unit.name,
		
		type_cat			= unit.categories[the_unit.type_cat],
		weapon_cat			= unit.weapon_categories[the_unit.weapon_cat],
	))
	
	# Main unit stuff
	output.append("""
	<form action="exec.py" id="the_unit_form" method="post" accept-charset="utf-8">
		<input type="hidden" name="mode" value="edit_unit_commit" />
		<input type="hidden" name="id" value="%(unit_id)s" />
	
		<table border="0" cellspacing="5" cellpadding="5">
			<tr>
				<td><label for="name">Unit:</label></td>
				<td>%(name_text)s</td>
			
				<td>&nbsp;</td>
			
				<td><label for="team">Team:</label></td>
				<td>
					<select name="team" id="team">
						<option value="0">No team</option>
						%(team_option_box)s
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="5" style="padding: 0px;"><a class="block_link" href="#" onclick="$('#the_unit_form').submit();">Apply changes</a></td>
			</tr>
		</table>
	</form>
	<br />

	""" % {
		"unit_id":			unit_id,
		"name_text":	common.text_box("name", the_unit.name),
		"team_option_box":	team_f.structured_list(cursor, default=the_unit.team),
	})
	
	# Unit equipment
	output.append("""
	<span class="stitle" id="equipment">Equipment</span>
	<table border="0" cellspacing="0" cellpadding="5">
		<tr class="row2">
			<th>Item</th>
			<th>&nbsp;</th>
		</tr>""")

	the_unit.get_equipment(cursor)

	counter = -1
	for e in the_unit.equipment:
		counter += 1
		output.append("""
		<tr class="row%(row)s">
			<td>%(item_name)s</td>
			<td style="padding: 0px;">
				<form action="exec.py" id="form_%(item_id)s" method="post" accept-charset="utf-8">
					<input type="hidden" name="mode" value="remove_equipment" />
					<input type="hidden" name="unit" value="%(unit_id)s" />
					<input type="hidden" name="item" value="%(item_id)s" />
					<a href="#" class="block_link" onclick="$('#form_%(item_id)s').submit();">Remove</a>
				</form>
			</td>
		</tr>""" % {
			"row":			counter%2,
			"item_name":	equipment_dict[e].name,
			"unit_id":		unit_id,
			"item_id":		e,
		})

	counter += 1
	output.append("""
	<tr class="row%(row)s">
		<form action="exec.py" id="new_equipment_form" method="post" accept-charset="utf-8">
			<input type="hidden" name="mode" value="add_equipment" />
			<input type="hidden" name="unit" value="%(unit_id)s" />
			<td>
				<select name="item">
					%(equipment_list)s
				</select>
			</td>
			<td>
				<input type="submit" value="Apply" />
			</td>
		</form>
	</tr>
	""" % {
		"row":				counter%2,
		"unit_id":			unit_id,
		"equipment_list":	equipment_f.equipment_option_list(cursor, remove_list=the_unit.equipment),
	})

	output.append("</table>")
	
	# What squads does the unit appear in?
	output.append("""
	<br /><br />
	<span class="stitle" id="squads">Squads</span>
	<table border="0" cellspacing="0" cellpadding="5">
		<tr class="row2">
			<th>Army</th>
			<th>Squad</th>
			<th>Size</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
		</tr>""")

	the_unit.get_equipment(cursor)
	
	counter = -1
	for s, the_squad in squad_dict.items():
		counter += 1
		output.append("""
		<tr class="row%(row)s">
			<td>%(army_name)s</td>
			<td>%(name)s</td>
			<td>%(squad_size)s</td>
			<td style="padding: 0px;">
				<a href="web.py?mode=edit_army&amp;army=%(army_id)s" class="block_link">Edit army</a>
			</td>
			<td style="padding: 0px;">
				<a href="web.py?mode=edit_squad&amp;squad=%(squad_id)s" class="block_link">Edit squad</a>
			</td>
		</tr>""" % {
			"row":			counter%2,
			"army_name":	army_dict[the_squad.army].name,
			"army_id":		the_squad.army,
			"squad_id":		s,
			"name":			the_squad.name,
			"squad_size":	the_squad.amount,
		})
	
	output.append("</table>")
	output.append("</div>")
	
	return "".join(output)
Exemple #2
0
def _recruit(the_line, the_squad, the_army, amount, debug):
	# if not debug:
	# 	raise Exception("No debug")
	
	results = order_block.default_line_results(the_line)
	results['base_failure'] = "The squad %s into %s could not be recruited to because " % (the_squad.name, the_army.name)
	
	# Check amount
	if amount < 1:
		return order_block.fail(results, "a number smaller than 1 was supplied" % groups['amount'])
	
	# Handles
	city_dict		= the_line.the_world.cities_from_team(the_line.block.team)
	unit_dict		= the_line.the_world.units()
	the_team		= the_line.the_world.teams()[the_line.block.team]
	
	the_unit = unit_dict[the_squad.unit]
	
	#	AVAILABLE
	#------------------------
	if not the_unit.available:
		return order_block.fail(results, "the unit (%s) or it's equipment is not available" % (the_unit.name))
	
	# _distance_from_base
	if the_army.distance > military_rules.non_recruitment_distance:
		pass
		# if debug:
		# 	results['debug'].append("Failed at _distance_from_base")
		# return order_block.fail(results, "the distance from the base of operations of this army is too large")
	
	#	COST
	#------------------------
	if the_team.resources.get("Iron") > 0:
		# results['cost'] = the_unit.get_cost(the_world=the_line.the_world)['iron_cost']
		results['cost'] = the_unit.get_cost(the_world=the_line.the_world)['material_cost']
	else:
		results['cost'] = the_unit.get_cost(the_world=the_line.the_world)['iron_cost']
		# results['cost'] = the_unit.get_cost(the_world=the_line.the_world)['material_cost']
	
	if unit.categories[the_unit.type_cat] != "Ship" and \
		unit.categories[the_unit.type_cat] != "Airship":
		results['cost'] *= (amount/1000)
	else:
		results['cost'] *= amount
	
	man_amount = amount * the_unit.crew
	
	# Some units can get special reductions
	results['cost'] = unit_rules.unit_cost_override(the_line.the_world, the_unit, results['cost'], the_team)
	
	# Check affordability
	affordability = the_team.resources.affordable(results['cost'])[0]
	
	if not affordability and "Materials" not in the_team.overbudget:
		return order_block.fail_cost(results)
	
	#	INSERTION/UPDATE
	#------------------------
	# If there's no city defined, make sure the cities are all big enough
	supply_dict = {}
	for c_id, c in city_dict.items():
		if c.population > 5000:
			supply_dict[c_id] = True
			
			# if debug:
			# 	results['debug'].append("Added %s to supply dict with a population of %s" % (c.name, c.population))
	
	# Now to make sure no city will drop below a certain size
	# _supply_dict
	cities_changed = True
	if debug:
		results['debug'].append("Initial supply dict %s" % supply_dict)
	
	while cities_changed:
		if len(supply_dict) < 1:
			if debug:
				results['debug'].append("Failed at _supply_dict")
			return order_block.fail(results, "no cities could supply the required population")
		
		cities_to_delete	= []
		cities_changed		= False
		
		city_count			= len(supply_dict)
		amount_per_city		= math.ceil(amount/city_count)
		
		for c in supply_dict.keys():
			# Check the city size
			if city_dict[c].population < amount_per_city:
				if debug:
					results['debug'].append("Remvoing %s from supply_dict because it's too small" % c)
				cities_to_delete.append(c)
				cities_changed = True
		
		for c in cities_to_delete:
			del(supply_dict[c])
	
	# First queries are cost
	results['queries'].append("-- Unit: %s %s" % (amount, the_unit.name))
	results['queries'].extend(squad_f.make_reinforcement_query(the_squad.id, amount))
	results['queries'].extend(city_f.make_recruitment_queries(man_amount, supply_dict.keys()))
	
	# Apply cost(s)
	the_team.resources -= results['cost'].discrete()
	for c in supply_dict.keys():
		city_dict[c].population -= amount/len(supply_dict)
	
	# Rules final call and returning of values
	if man_amount != amount:
		results['results'].append("Recruited %s %s (%s men) into %s in %s" % (amount, the_unit.name, man_amount, the_squad.name, the_army.name))
	else:
		results['results'].append("Recruited %s %s into %s in %s" % (amount, the_unit.name, the_squad.name, the_army.name))
	
	return order_block.success(results)