def main(cursor): team_dict = team_q.get_real_active_teams(cursor, skip_irs=False) relations = team_q.get_relations(cursor) output = [] # Now the actual set of borders output.append(''' <div style="overflow: scroll;"> <table border="0" cellspacing="0" cellpadding="5" style="width:100%; overflow: scroll;"> <tr class='row2'> <th> </th>''') for t1, team1 in team_dict.items(): output.append('<th>%s</th>' % team1.name[0:6]) output.append('</tr>') # Data i = 1 for t1, team1 in team_dict.items(): i += 1 output.append("<tr class='row%d'><td style='font-weight:bold;'>%s</td>" % (i % 2, team1.name[0:15])) for t2, team2 in team_dict.items(): if t1 == t2: output.append('<td> </td>') continue output.append(""" <td style="padding:0px;"> <a class="block_link" href="web.py?mode=edit_relations&t1={t1}&t2={t2}">{other}</a> </td> """.format( other = team2.name[0:5], t1 = t1, t2 = t2, )) # Close form output.append("</table></div>") return "".join(output)
def main(cursor): t1 = team_q.get_one_team(cursor, int(common.get_val('t1'))) t2 = team_q.get_one_team(cursor, int(common.get_val('t2'))) relations = team_q.get_relations(cursor) output = [] output.append(""" <div style="padding:5px;"> <span class="stitle"><strong>{t1} relation to {t2}</strong></span><br /> <br /> {t1} is {t12_border} towards {t2}<br /> {t2} is {t21_border} towards {t1}<br /> <br /> {t1} has a tax rate of {t12_tax}% towards {t2}<br /> {t2} has a tax rate of {t21_tax}% towards {t1}<br /> <br /> </div> """.format( t1 = t1.name, t2 = t2.name, t12_border = team.border_states[relations.get(t1.id, {}).get(t2.id, {}).get('border', t1.default_borders)], t21_border = team.border_states[relations.get(t2.id, {}).get(t1.id, {}).get('border', t2.default_borders)], t12_tax = relations.get(t1.id, {}).get(t2.id, {}).get('taxes', t1.default_taxes), t21_tax = relations.get(t2.id, {}).get(t1.id, {}).get('taxes', t2.default_taxes), )) output.append("</div>") return "".join(output)
def relations(self, force_requery=False): if not force_requery and self._relations != {}: return self._relations self._relations = team_q.get_relations(self.cursor) return self._relations
def check_army_bases(cursor, verbose): team_dict = team_q.get_all_teams(cursor) army_dict = army_q.get_armies_from_team_list(cursor, list(team_dict.keys())) city_dict = city_q.get_live_cities(cursor) relations = team_q.get_relations(cursor)# [Host][Visitor] # Build up a shortlist dict of cities by team city_by_team = {} for team_id, the_team in team_dict.items(): if team_id not in city_by_team: city_by_team[team_id] = [] for city_id, the_city in city_dict.items(): if the_city.team == team_id or relations.get(the_city.team, {}).get(team_id, {}).get('border', team_dict[the_city.team].default_borders) >= team.border_states.index("Allied"): city_by_team[team_id].append(city_id) # Now to run through the armies and find their paths new_bases = set() # for i in progressbar(range(15), "Computing: ", 40): # for army_id, the_army in army_dict.items(): if verbose: it = cli_f.progressbar(army_dict.items(), "military_check.check_army_bases: ", 40, with_eta = True) else: it = army_dict.items() for army_id, the_army in it: army_terrain = mapper_q.get_terrain(cursor, the_army.x, the_army.y) for city_id in city_by_team[the_army.team]: if army_terrain < 1: if not the_city.port: continue the_city = city_dict[city_id] if the_city.dead > 0: continue dist = path_f.pythagoras((the_army.x, the_army.y), (the_city.x, the_city.y)) if dist < 10: the_army.base = city_id the_army.distance = (dist/map_data.movement_speeds['Marching'])*10# Distance over speed in km, 1 distance unit is 10k new_bases.add(army_id) # Extend the search a little if army_id not in new_bases: for city_id in city_by_team[the_army.team]: if army_terrain < 1: if not the_city.port: continue the_city = city_dict[city_id] dist = path_f.pythagoras((the_army.x, the_army.y), (the_city.x, the_city.y)) if dist < 30: the_army.base = city_id the_army.distance = (dist/map_data.movement_speeds['Marching'])*10# Distance over speed in km, 1 distance unit is 10k new_bases.add(army_id) # Extend the search a little if army_id not in new_bases: for city_id in city_by_team[the_army.team]: if army_terrain < 1: if not the_city.port: continue the_city = city_dict[city_id] dist = path_f.pythagoras((the_army.x, the_army.y), (the_city.x, the_city.y)) if dist < 60: the_army.base = city_id the_army.distance = (dist/map_data.movement_speeds['Marching'])*10# Distance over speed in km, 1 distance unit is 10k new_bases.add(army_id) # If we can't find an easy solution we start pathing if army_id not in new_bases: fastest_path = (-1, 9999999) for city_id in city_by_team[the_army.team]: the_city = city_dict[city_id] # Small cities won't support an army a long way away right? if the_city.size < 5000: continue # Is it a fleet? if army_terrain < 1: if not the_city.port: continue # Now lets try water dist = path_f.path(cursor, ((the_army.x, the_army.y), (the_city.x, the_city.y)), move_speed="Sailing", move_type="Sail") if dist.time_cost < fastest_path[1]: fastest_path = (city_id, dist.time_cost) else: # I'm sure there's a way to improve this... dist = path_f.path(cursor, ((the_army.x, the_army.y), (the_city.x, the_city.y)), move_speed="Marching", move_type="Medium foot") if dist.time_cost == 0: continue # if army_id == 960 and city_id == 1098: # print("") # print(dir(dist)) # print(((the_army.x, the_army.y), (the_city.x, the_city.y))) # print(dist.time_cost) # print(dist.walk_distance) # print(len(dist.steps)) # print("") if dist.time_cost < fastest_path[1]: fastest_path = (city_id, dist.time_cost) the_army.base = fastest_path[0] the_army.distance = fastest_path[1] new_bases.add(army_id) # Reset all armies query = """UPDATE armies SET base = -1, distance = -1 WHERE garrison = 0;""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for a in new_bases: query = """UPDATE armies SET base = %d, distance = %d WHERE id = %d;""" % (army_dict[a].base, army_dict[a].distance, a) try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) # Garrisons automatically go to their city query = """UPDATE armies SET base = garrison, distance = 0 WHERE garrison > 0;""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
def main(cursor): team_id = int(common.get_val('team', 0)) if team_id < 1: return "No team selected" team_dict = team_q.get_real_active_teams(cursor, skip_irs=False) the_team = team_dict[team_id] borders = team_q.get_relations(cursor) team_borders = borders[team_id] # Find our default state_find = 'value="%s"' % the_team.default_borders # What to replace it with state_replace = '%s selected="selected"' % state_find output = [""" <form action="exec.py" method="post" accept-charset="utf-8" style="padding:5px;"> <span class="stitle">Borders for {team.name}</span><br /><br /> <input type="hidden" name="mode" id="mode" value="edit_borders" /> <input type="hidden" name="team" id="team" value="{team.id}" /> """.format( team=the_team, )] # Now the actual set of borders output.append(''' Default border state: <select name="default_border_state" id="default_border_state"> {states} </select> <br /><br /> <table border="0" cellspacing="0" cellpadding="5"> <tr> <th>Team</th> <th>{team.name} state</th> <th>Their state</th> </tr>'''.format( states = states.replace('<option value="-1">Default</option>', '').replace(state_find, state_replace), team = the_team, )) for t, other_team in team_dict.items(): if t == the_team.id: continue # if other_team.hidden: continue # if not other_team.active: continue ir = "" if other_team.ir: ir = " <em>(IR)</em>" other_team_borders = borders[t].get(the_team.id, {}).get('border', other_team.default_borders) other_default_borders = other_team.default_borders # print(other_team_borders) # The state to find # state_find = 'value="%s"' % team_borders.get(t, default_borders) if t in team_borders: default_state = team_borders[t] else: default_state = "-1" state_find = 'value="%s"' % default_state # What to replace it with state_replace = '%s selected="selected"' % state_find # Their state to you # if the_team.id in other_team_borders: # other_state = team.border_states[other_team_borders[the_team.id]] # else: # other_state = team.border_states[other_default_borders] other_state = other_team_borders output.append(""" <tr> <td>%(name)s%(ir)s</td> <td> <select name="border_state_%(t)d" id="border_state_%(t)d"> %(states)s </select> </td> <td> <span style="color:#%(other_state_c)s;">%(other_state)s</span> </td> </tr> """ % { "t": t, "ir": ir, "name": other_team.name, "states": states.replace(state_find, state_replace), "other_state": team.border_states[other_state], "other_state_c": team.border_colour(other_state), }) # Close form output.append("""</table> <input type="submit" value="Save" /> </form>""".format( team=team_id )) return "".join(output)