예제 #1
0
def wonder_order(the_line, groups, debug=False):
	results = order_block.default_line_results(the_line)
	
	wonder_dict				= the_line.the_world.wonders()
	city_dict				= the_line.the_world.cities()
	
	cities_lookup			= the_line.the_world.cities_lookup(lower=True)
	
	the_team				= the_line.the_world.teams()[the_line.block.team]
	
	# DB checks
	#------------------------
	# Can we find the city?
	if groups['assist_city'].lower() not in cities_lookup:
		return order_block.fail(results, "there is no city by the name of '%s'" % groups['wonder_city'])
	else:
		assist_city = city_dict[cities_lookup[groups['assist_city'].lower()]]
	
	# Can we find the city?
	if groups['wonder_city'].lower() not in cities_lookup:
		return order_block.fail(results, "there is no city by the name of '%s'" % groups['wonder_city'])
	else:
		wonder_city = city_dict[cities_lookup[groups['wonder_city'].lower()]]
	
	# Rule checks
	#------------------------
	results = order_block.default_line_results(the_line, "%s could not be assist %s because" % (assist_city.name, wonder_city.name))
	
	# Our cities?
	if assist_city.team != the_team.id:
		return order_block.fail(results, "%s is not your city" % assist_city.name)
	
	if wonder_city.team != the_team.id:
		return order_block.fail(results, "%s is not your city" % wonder_city.name)
	
	# Nomads can't help build
	if assist_city.nomadic:
		return order_block.fail(results, "%s is nomadic" % assist_city.name)
	
	# Dead cities can't build
	if assist_city.dead > 0:
		return order_block.fail(results, "%s is dead" % assist_city.name)
	
	if wonder_city.dead > 0:
		return order_block.fail(results, "%s is dead" % wonder_city.name)
	
	# Check for the wonder
	the_wonder = None
	for w, tw in wonder_dict.items():
		if tw.city == wonder_city.id:
			the_wonder = tw
	
	# Was a wonder found?
	if the_wonder == None:
		return order_block.fail(results, "there is no wonder in %s" % wonder_city.name)
	
	# Is the wonder already completed
	if the_wonder.completed:
		return order_block.fail(results, "%s has already completed it's wonder" % wonder_city.name)
	
	# Does the city have any points left?
	if assist_city.wall_points_used >= 1:
		return order_block.fail(results, "%s has used up it's wall construction point already" % assist_city.name)
	
	if assist_city.building_points_used >= 1:
		return order_block.fail(results, "%s has used up it's normal construction point already" % assist_city.name)
	
	# Get build rate
	build_rate = city_rules.wonder_build_rate(assist_city, wonder_city)
	if the_team.resources.get("Stone") < 1:
		build_rate *= 0.5
	
	if build_rate < 1:
		return order_block.fail(results, "%s is too small or too far away to assist" % assist_city.name)
	
	# Cost
	if the_wonder.completion + build_rate > the_wonder.point_cost:
		build_rate = the_wonder.point_cost - the_wonder.completion
	
	material_per_point = the_wonder.material_cost/the_wonder.point_cost
	results['cost'] = res_dict.Res_dict("Materials:%s" % (material_per_point*build_rate))
	
	# Check affordability
	affordability = the_team.resources.affordable(results['cost'])[0]
	if not affordability:
		return order_block.fail_cost(results)
	
	
	# INSERTION/UPDATE
	#------------------------
	# First queries are cost
	the_wonder.completion += build_rate
	if the_wonder.completion >= the_wonder.point_cost:
		the_wonder.completed = True
	
	results['queries'].append("-- Wonder %s assisting %s for team:%d" % (assist_city.name, wonder_city.name, the_team.id))
	results['queries'].extend(wonder_f.completion_query(the_wonder.id, the_wonder.completion))
	
	# Work out results
	if the_wonder.completion >= the_wonder.point_cost:
		results['results'].append("%s assisted %s with %s construction points, the wonder is now complete" % (assist_city.name, wonder_city.name, build_rate))
	else:
		results['results'].append("%s assisted %s with %s construction points" % (assist_city.name, wonder_city.name, build_rate))
	
	# Apply cost
	the_team.resources -= results['cost'].discrete()
		
	# Update city points
	assist_city.wall_points_used		+= 1
	assist_city.building_points_used	+= 1	
	
	return order_block.success(results)
예제 #2
0
파일: list_cities.py 프로젝트: Teifion/Rob3
def main(cursor):
	# Get team Id
	team_id = int(common.get_val('team', 0))
	
	text_location = common.get_val('location', "")
	
	if team_id < 1:
		return "<div style='padding: 5px;'>{0}</div>".format(common.select_team_form(cursor, 'list_cities'))
	
	cities_dict = city_q.get_cities_from_team(cursor, team = team_id, include_dead = True)
	
	# Work out city points
	total_points = 0
	for city_id, the_city in cities_dict.items():
		if the_city.dead == True: continue
		total_points += the_city.point_value()
	
	output = []
	
	output.append("""
	<table border="0" cellspacing="0" cellpadding="5" style="width: 100%;">
		<tr class="row2">
			<th>City name</th>
			<th colspan="3">Location</th>
			<th>Port</th>
			<th>Secret</th>
			<th>Dead</th>
			<th>Nomadic</th>
			
			<th>Overlap</th>
			
			<th>Wonder speed</th>
			<th>Population</th>
			<th>Slaves</th>
			<th>Happiness</th>
			
			<th colspan="2">&nbsp;</th>
		</tr>""")
	
	# Work out how fast it can build wonders
	city_wonder_speed = {}
	for c1, city1 in cities_dict.items():
		city_wonder_speed[c1] = math.floor((city1.population + city1.slaves)/1000)
		c1_loc = (cities_dict[c1].x, cities_dict[c1].y)
		
		for c2, city2 in cities_dict.items():
			if city2.dead: continue
			
			if c2 != c1:
				amount = city_rules.wonder_build_rate(city2, city1)
				city_wonder_speed[c1] += amount
		
		city_wonder_speed[c1] = common.number_format(int(city_wonder_speed[c1]))
	
	count = -1
	if len(cities_dict) > 0:
		for city_id, the_city in cities_dict.items():
			count += 1
			
			city_share = 0
			if not the_city.dead:
				if total_points > 0:
					city_share = round(the_city.point_value()/total_points*100,1)
			
			# Happiness
			happiness = city.happiness_str(the_city.happiness)
			
			if happiness == "Rebellious":
				happiness = '<span style="font-weight:bold;color:#A00;">Rebellious</span>'
			
			if happiness == "Utopian":
				happiness = '<span style="font-weight:bold;color:#0A0;">Utopian</span>'
			
			output.append("""
			<tr class="row%(row)d" id="%(city_id)d">
				<td>%(name)s</td>
		
				<td>%(x)s</td>
				<td>%(y)s</td>
				<td style="padding: 0px;"><a class="block_link" href="web.py?mode=view_map&amp;%(map_link)s">Map link</a></td>
				
				<td style="text-align: center;">%(port)s</td>
				<td style="text-align: center;">%(secret)s</td>
				<td style="text-align: center;">%(dead)s</td>
				<td style="text-align: center;">%(nomadic)s</td>
				
				<td>%(overlap)s</td>
				
				<td>%(wonder_speed)s</td>
				<td>%(population)s</td>
				<td>%(slaves)s</td>
				
				<td>%(happiness)s</td>
				
				<!--<td style="padding: 0px;"><a class="block_link" href="web.py?mode=view_city_trade&amp;city=%(city_id)d">City trade</a></td>-->
				<td style="padding: 0px;"><a class="block_link" href="web.py?mode=view_city_matrix&amp;city=%(city_id)d">City matrix</a></td>
				<td style="padding: 0px;"><a class="block_link" href="web.py?mode=edit_city&amp;city=%(city_id)d">Edit city</a></td>
			</tr>
			""" % {	'row': (count % 2),
			
					'city_id': the_city.id,
					'name': common.doubleclick_text("cities", "name", the_city.id, the_city.name, "font-weight:bold"),
					'x': common.doubleclick_text("cities", "x", the_city.id, the_city.x, ""),
					'y': common.doubleclick_text("cities", "y", the_city.id, the_city.y, ""),
					"map_link":	the_city.map_link_args(),
					
					'port': common.bstr(the_city.port),
					'secret': common.bstr(the_city.secret),
					'dead': "" if the_city.dead < 1 else common.doubleclick_text("cities", "dead", the_city.id, the_city.dead, "", size=3),
					'nomadic': common.bstr(the_city.nomadic),
				
					'overlap': the_city.overlap,
				
					'population': common.doubleclick_text("cities", "population", the_city.id, the_city.population, ""),
					'slaves': common.doubleclick_text("cities", "slaves", the_city.id, the_city.slaves, ""),
				
					"wonder_speed": city_wonder_speed[the_city.id],
					"happiness":	happiness,
				})


	# Add new city
	count += 1
	output.append("""
	<tr class="row%(row)d">
		<form action="exec.py" id="add_city_form" method="post" accept-charset="utf-8">
		<input type="hidden" name="mode" value="add_city" />
		<input type="hidden" name="team" value="%(team_id)s" />
		<td style="padding: 1px;"><input type="text" name="name" id="new_name" value="" /></td>
		<td style="padding: 0px;" colspan="2">
			<a href="web.py?mode=view_map&amp;new_mode=list_cities&amp;team=%(team_id)s" class="block_link">Location:</a>
		</td>
		<td colspan="1" style="padding:1px;">
			%(location)s
		</td>
		<td style="padding: 2px;"><input type="checkbox" name="port" value="True" /></td>
		<td style="padding: 2px;"><input type="checkbox" name="secret" value="True" /></td>
		<td style="padding: 2px;"><input type="text" name="dead" id="dead" value="0" size="3"/></td>
		<td style="padding: 2px;"><input type="checkbox" name="nomadic" value="True" /></td>
	
		<td>&nbsp;</td>
		
		<td>&nbsp;</td>
	
		<td style="padding: 1px;"><input type="text" name="population" value="" size="10"/></td>
		<td style="padding: 1px;"><input type="text" name="slaves" value="" size="10"/></td>
	
		<td style="padding: 2px;" colspan="3"><input type="submit" value="Add" /></td>
		<!--
		<td style="padding: 0px;" colspan="3"><a class="block_link" href="#" onclick="$('#add_city_form').submit();">Add</a></td>
		-->
		</form>
		%(onload)s
	</tr>
	""" % {	'row': (count % 2),

			"team_id":	team_id,
			"location": common.text_box("text_location", text_location, custom_id="", size="7"),
			"onload":	common.onload("$('#new_name').focus();"),
			})


	output.append("</table>")

	return "".join(output)
예제 #3
0
파일: edit_city.py 프로젝트: Teifion/Rob3
def main(cursor):
    # Get city Id
    city_id = int(common.get_val("city", 1))
    if city_id < 1:
        return "No city selected"

    # Build city item
    the_city = city_q.get_one_city(cursor, city_id)

    # Get buildings list
    building_dict = building_q.get_all_buildings(cursor)

    # 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_city.x, the_city.y)  # default value

    output = []

    output.append(
        """<div style="float: right; width: 50%;">
		<strong>Happiness</strong>
		<div id="happiness">
			
		</div>
	</div>"""
    )

    output.append(
        """<div style='padding: 5px;'>
	<form action="exec.py" method="post" accept-charset="utf-8">
		<input type="hidden" name="mode" id="mode" value="edit_city_commit" />
		<input type="hidden" name="id" id="id" value="%(city_id)s" />
		
		Editing: %(name_text)s
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="web.py?mode=edit_army&amp;garrison=%(city_id)s">Edit garrison</a>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="web.py?mode=list_operatives&amp;city=%(city_id)s">Operatives</a>
		<br /><br />
		
		<table border="0" cellspacing="5" cellpadding="5">
			<tr>
				<td><label for="team">Team:</label></td>
				<td colspan="4" style="padding: 1px;">%(owner_select)s</td>
				
				<td width="5">&nbsp;</td>
				
				<td>&nbsp;</td>
				<td colspan="6">&nbsp;</td>
			</tr>
			<tr>
				<td><label for="size">Size:</label></td>
				<td style="padding: 1px;">%(city_population_text)s</td>
				
				<td width="5">&nbsp;</td>
				
				<td><label for="slaves">Slaves:</label></td>
				<td style="padding: 1px;">%(city_slaves_text)s</td>
				
				<td width="5">&nbsp;</td>
				<td style="padding: 0px;">
					<a class="block_link" href="web.py?mode=view_map&amp;new_mode=edit_city&amp;city=%(city_id)s"">Location:</a>
				</td>
				<td style="padding: 1px;">%(city_location_text)s</td>
				
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td><label for="port">Port:</label></td>
				<td>%(city_port_checkbox)s</td>
				
				<td><label for="nomadic">Nomadic:</label></td>
				<td>%(city_nomadic_checkbox)s</td>
				
				<td><label for="dead">Dead:</label></td>
				<td>%(city_dead)s</td>
				
				<td><label for="secret">Secret:</label></td>
				<td>%(city_secret_checkbox)s</td>
				
				<td><label for="founded">Founded:</label></td>
				<td>%(founded_text)s</td>
			</tr>
			<tr>
				<td colspan="10">&nbsp;&nbsp;&nbsp;Description:<br />
					%(city_description_textarea)s
				</td>
			</tr>
		</table>
		<br />
		<input type="submit" value="Perform edit" />
	</form>
	<form id="delete_form" action="exec.py" method="post" accept-charset="utf-8">
		<input type="hidden" name="city" id="city" value="%(city_id)s" />
		<input type="hidden" name="mode" id="mode" value="remove_city" />
		<input style="float:right; margin-right:100px;" type="button" value="Delete city" onclick="var answer = confirm('Delete %(name_safe)s?')
		if (answer) $('#delete_form').submit();" />
	</form>
	<br /><br />"""
        % {
            "city_id": city_id,
            "name": the_city.name,
            "name_text": common.text_box("name", the_city.name, size=20),
            "owner_select": team_f.structured_list(cursor, default=the_city.team, field_name="team"),
            # "city_location_text":			common.text_box("location", "%s,%s" % (the_city.x, the_city.y), 10),
            "city_location_text": common.text_box("location", new_location, 10),
            "city_population_text": common.text_box("population", the_city.population, 10),
            "city_slaves_text": common.text_box(
                "slaves", the_city.slaves, 10, warn_on=lambda x: (True if int(x) < 0 else False)
            ),
            "city_port_checkbox": common.check_box("port", the_city.port),
            "city_dead": common.text_box("dead", the_city.dead, 5),
            "city_nomadic_checkbox": common.check_box("nomadic", the_city.nomadic),
            "city_secret_checkbox": common.check_box("secret", the_city.secret),
            "city_description_textarea": '<textarea name="description" id="description" rows="4" cols="40">%s</textarea>'
            % the_city.description,
            "founded_text": the_city.founded,
            "name_safe": common.js_name(the_city.name),
        }
    )

    # 	Buildings
    # ----------------------
    the_city.get_buildings(cursor)

    output.append(
        """
	<table style="float:left; margin-right: 25px;" border="0" cellspacing="0" cellpadding="5">
	<tr class="row2">
		<th>Building</th>
		<th>Progress</th>
		<th>Amount</th>
		<th>&nbsp;</th>
		<th>&nbsp;</th>
	</tr>
	"""
    )

    counter = -1
    building_remove_list = []
    for building_id, completion in the_city.buildings.items():
        counter += 1

        building_remove_list.append(building_id)

        output.append(
            """
		<tr class="row%(row)d">
			<form id="b_%(building_id)s" action="exec.py" method="get" accept-charset="utf-8">
				<input type="hidden" name="mode" value="set_building" />
				<input type="hidden" name="city" value="%(city_id)s" />
				<input type="hidden" name="building" value="%(building_id)s" />
				<td><label for="%(building_name)s">%(building_name)s</label></td>
				<td style="padding: 1px;">
					%(building_completion_text)s/%(building_build_time)s
				</td>
				<td style="padding: 1px;">
					%(building_amount_text)s
				</td>
				<td style="padding: 0px;">
					<!--<a class="block_link" href="#" onclick="$('#b_%(building_id)s').submit();">Edit</a>-->
					<input type="submit" value="Edit" />
				</td>
				<td style="padding: 0px;">
					<a class="block_link" href="exec.py?mode=set_building&amp;building=%(building_id)d&amp;city=%(city_id)d&amp;completion=0&amp;amount=0">Remove</a>
				</td>
			</form>
		</tr>"""
            % {
                "row": (counter % 2),
                "building_name": building_dict[building_id].name,
                "building_id": building_id,
                "building_build_time": building_dict[building_id].build_time,
                "city_id": city_id,
                "building_completion_text": common.text_box("completion", the_city.buildings[building_id], size=4),
                "building_amount_text": common.text_box("amount", the_city.buildings_amount[building_id], size=3),
            }
        )

    output.append(
        """
		<tr class="row%(row)d">
		<form id="city_add_building_form" action="exec.py" method="post" accept-charset="utf-8">
			<input type="hidden" name="mode" value="set_building" />
			<input type="hidden" name="city" value="%(city_id)s" />
			<td style="padding:1px;">
				<select id="new_building" name="building" onchange="$('#building_completion_span').load('web.py', {mode: 'get_building_build_time', building: document.getElementById('new_building').value, ajax:'True'});">
					%(building_option_box)s
				</select>
			</td>
			<td style="padding:1px;">
				%(building_completion_text)s/<span id="building_completion_span">000</span>
			</td>
			<td style="padding:1px;">
				%(building_amount_text)s
			</td>
			<td style="padding: 0px;" colspan="2">
				<input type="submit" value="Add" />
				<!--<a href="#" onclick="$('#city_add_building_form').submit();" class="block_link">Add</a>-->
			</td>
		</tr>
		</form>
	</table>
	%(onload)s
	"""
        % {
            "row": ((counter + 1) % 2),
            "city_id": the_city.id,
            "building_option_box": building_f.building_option_list(cursor, building_remove_list),
            "building_completion_text": common.text_box("completion", 0, size=4),
            "building_amount_text": common.text_box("amount", 0, size=3),
            "onload": common.onload("$('#new_building').focus();"),
        }
    )

    output.append(
        common.onload(
            "$('#building_completion_span').load('web.py', {mode: 'get_building_build_time', building: document.getElementById('new_building').value, 'ajax':1});"
        )
    )

    output.append("<strong>Wonder construction speed</strong>")
    output.append("<ul>")

    # Work out city points
    cities_dict = city_q.get_cities_from_team(cursor, the_city.team)
    total_points = 0
    for city_id2, city2 in cities_dict.items():
        if city2.team != the_city.team:
            continue
        if city2.dead == True:
            continue

        points = city_rules.wonder_build_rate(city2, the_city)
        if points > 0:
            output.append("<li>%s: %s</li>" % (city2.name, points))
            total_points += points

    output.append("<li><em>Total: %s</em></li>" % total_points)
    output.append("</ul>")

    output.append(
        common.onload(
            "$('#happiness').load('web.py', {'mode':'happiness_breakdown','city':'%d', 'ajax':'True'});"
            % int(the_city.id)
        )
    )

    # output.append('''<img src="%simages/grid_25.png" width="0" height="0" onload="">''' % common.data['media_path'])
    output.append("</div>")

    page_data["Title"] = "Edit city (%s)" % the_city.name
    return "".join(output)