def buildings(cursor): output = {} id_list = [] has_up_list = set() building_dict = building_q.get_all_buildings(cursor) for b, the_building in building_dict.items(): if not the_building.public: continue if the_building.upgrades > 0: has_up_list.add(the_building.upgrades) id_list.append(b) output[b] = { "building_id": b, "name": the_building.name, "build_time": the_building.build_time, "upgrades": the_building.upgrades, "has_upgrade": the_building.has_upgrade, "wall": the_building.wall, "economy": the_building.economy, "needs_port": the_building.needs_port, # "cost_per_turn": res_dict.Res_dict(the_building.cost_per_turn).value, # "cost_up_front": res_dict.Res_dict(the_building.cost_up_front).value, # "upkeep": res_dict.Res_dict(the_building.upkeep).value, "limit_per_city": the_building.limit_per_city, } return "buildings = %s; building_list = %s; building_has_up = %s;" % (json.dumps(output), id_list, list(has_up_list))
def check_for_amount(cursor, verbose): """Takes all 100% buildings and makes them 0% but adds one to the amount""" building_dict = building_q.get_all_buildings(cursor) queries = [] query = "SELECT city, building, completion FROM city_buildings" 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['completion'] >= building_dict[row['building']].build_time: queries.append("UPDATE city_buildings SET amount = amount+1, completion = 0 WHERE city = %d AND building = %d" % (row['city'], row['building'])) if verbose: if len(queries) > 1: print("cities_check.check_for_amount() found %d buildings to complete" % len(queries)) elif len(queries) == 1: print("cities_check.check_for_amount() found 1 building to complete") else: print("cities_check.check_for_amount() found 0 buildings to complete") for q in queries: try: cursor.execute(q) except Exception as e: print("Query: %s\n" % query) raise e
def building_option_list(cursor, remove_list = []): output = [] building_dict = building_q.get_all_buildings(cursor) for c, b in building_dict.items(): if c in remove_list: continue output.append("<option value='%s'>%s</option>" % (c, b.name)) return "".join(output)
def building_list(cursor, cat=None, page=None, **kwargs): building_dict = building_q.get_all_buildings(cursor) output = [] for building_id, the_building in building_dict.items(): if not the_building.public: continue # if the_trait.category not in output_dict: # output_dict[the_trait.category] = [] output.append(""" <strong><a class="clear_link" id="%(js_name)s" href="#%(js_name)s">%(building_name)s</a></strong> <br /> %(description)s <br /><br />""" % { "js_name": common.js_name(the_building.name), "building_name": the_building.name, "description": the_building.description, }) return "".join(output)
def buildings(cursor): output = {} building_dict = building_q.get_all_buildings(cursor) for b, the_building in building_dict.items(): if not the_building.public: continue output[b] = { "building_id": b, "name": the_building.name, "build_time": the_building.build_time, "upgrades": the_building.upgrades, "wall": the_building.wall, "economy": the_building.economy, "needs_port": the_building.needs_port, "cost_per_turn": res_dict.Res_dict(the_building.cost_per_turn).value, "cost_up_front": res_dict.Res_dict(the_building.cost_up_front).value, "upkeep": res_dict.Res_dict(the_building.upkeep).value, "limit_per_city": the_building.limit_per_city, "description": the_building.description, } return json.dumps(output)
def _setup_grid(self, cursor): self.check_size() # Teams self.team_dict = team_q.get_real_active_teams(cursor, skip_irs=False) # Cities self.city_dict = city_q.get_cities_for_map(cursor, left=self.left, top=self.top, right=self.right, bottom=self.bottom) # Buildings self.building_dict = building_q.get_all_buildings(cursor) # Very small saving city_q.mass_get_city_buildings(cursor, self.city_dict) # Huge time saving on this one city_f.mass_city_wall_check(cursor, self.city_dict) # Wonders self.wonder_dict = wonder_q.get_all_wonders(cursor) # Artefacts self.artefact_dict = artefact_q.get_all_artefacts(cursor) # Cache things so we don't need to iterate through all cities self.cities_with_wonder = [] self.cities_with_artefact = [] for k, v in self.wonder_dict.items(): self.cities_with_wonder.append(v.city) for k, v in self.artefact_dict.items(): self.cities_with_artefact.append(v.city) self.cities_with_wonder = set(self.cities_with_wonder) self.cities_with_artefact = set(self.cities_with_artefact)
def main(cursor): building_dict = building_q.get_all_buildings(cursor) count = -1 output = [""" <table border="0" cellspacing="0" cellpadding="5"> <tr class="row2"> <th>Name</th> <th>Turns</th> <th>Effect</th> </tr> """] for b, the_building in building_dict.items(): if the_building.public == False: continue count+=1 the_cost = res_dict.Res_dict(the_building.cost_per_turn) # Equipment output output.append(""" <tr class="row%(count)s"> <td>%(name)s</td> <td>%(turns)s</td> <td>%(effect)s</td> </tr>""" % { "count": count%2, "name": the_building.name, "turns": int(the_building.build_time/100.0), "effect": the_building.description, }) output.append("</table>") return "".join(output)
def main(cursor): building_dict = building_q.get_all_buildings(cursor, orderby="id") output = [] output.append(""" <table border="0" cellspacing="0" cellpadding="5" sstyle="width: 100%;"> <tr class="row2"> <th>ID</th> <th>Building</th> <th>Upgrades</th> <th>Build time</th> <th>Wall</th> <th>Needs port</th> <th>Public</th> </tr>""") # database.Varchar_field("cost_per_turn", max_length=50), # database.Varchar_field("cost_up_front", max_length=50), # database.Varchar_field("upkeep", max_length=50), # # database.Integer_field("limit_per_city"), # # database.Varchar_field("description", max_length=255), count = -1 for building_id, the_building in building_dict.items(): count += 1 upgrade_string = "" if the_building.upgrades > -1: upgrade_string = "%s (%d)" % (building_dict[the_building.upgrades].name, the_building.upgrades) output.append(""" <tr class="row{row}" id="{id}"> <td>{id}</td> <td>{name}</td> <td>{upgrades}</td> <td>{build_time}</td> <td>{wall}</td> <td>{needs_port}</td> <td>{public}</td> </tr> """.format( id=building_id, row=count%2, name = the_building.name, build_time = the_building.build_time, upgrades = upgrade_string, wall = the_building.wall, needs_port = the_building.needs_port, public = the_building.public, )) output.append("</table>") return "".join(output)
import database from classes import world, res_dict from classes import team, city, squad, army, unit from queries import equipment_q, building_q, deity_q, evolution_q, tech_q, spell_q, artefact_q, wonder_q # Need some live data for things like buildings etc temp_cursor = database.get_cursor() equipment_dict = equipment_q.get_all_equipment(temp_cursor) building_dict = building_q.get_all_buildings(temp_cursor) deity_dict = deity_q.get_all_deities(temp_cursor) evolution_dict = evolution_q.get_all_evolutions(temp_cursor) spell_dict = spell_q.get_all_spells(temp_cursor) tech_dict = tech_q.get_all_techs(temp_cursor) artefact_dict = artefact_q.get_all_artefacts(temp_cursor) wonder_dict = wonder_q.get_all_wonders(temp_cursor) def dummy_world(): w = world.World(Dead_cursor()) # LISTS #------------------------ w._equipment = equipment_dict w._buildings = building_dict w._deities = deity_dict w._evolutions = evolution_dict w._spells = spell_dict w._techs = tech_dict w._artefacts = artefact_dict # TEAMS #------------------------
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 <a href="web.py?mode=edit_army&garrison=%(city_id)s">Edit garrison</a> <a href="web.py?mode=list_operatives&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"> </td> <td> </td> <td colspan="6"> </td> </tr> <tr> <td><label for="size">Size:</label></td> <td style="padding: 1px;">%(city_population_text)s</td> <td width="5"> </td> <td><label for="slaves">Slaves:</label></td> <td style="padding: 1px;">%(city_slaves_text)s</td> <td width="5"> </td> <td style="padding: 0px;"> <a class="block_link" href="web.py?mode=view_map&new_mode=edit_city&city=%(city_id)s"">Location:</a> </td> <td style="padding: 1px;">%(city_location_text)s</td> <td> </td> <td> </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"> 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> </th> <th> </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&building=%(building_id)d&city=%(city_id)d&completion=0&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)