def units(cursor, the_world, the_team): unit_dict = the_world.units_from_team(the_team.id) special_unit_dict = the_world.units_from_team(0) monster_dict = the_world.monsters() the_team.get_units(cursor) output = [] output.append('''<div class="ti_section" id="units_div"> <table border="0" cellspacing="0" cellpadding="5" style="width:100%%;"> <tr class="row2"> <th>Amount</th> <th>Unit name</th> <th>Cost</th> <th>Equipment</th> </tr>''') unit_row = ''' <tr class="row{count}"> <td>{amount}</td> <td>{name}</td> <td>{cost}</td> <td>{equipment}</td> </tr> ''' count = -1 # Team units for u, the_unit in unit_dict.items(): count += 1 output.append(unit_row.format( count = count%2, amount = common.number_format(the_team.units.get(u, 0)), name = the_unit.name, cost = unit_rules.print_unit_cost(the_unit, cursor=cursor, the_world=the_world), equipment = the_unit.equipment_string, )) # Special units for u, the_unit in special_unit_dict.items(): count += 1 output.append(unit_row.format( count = count%2, amount = common.number_format(the_team.units.get(u, 0)), name = the_unit.name, cost = unit_rules.print_unit_cost(the_unit, cursor=cursor, the_world=the_world), equipment = the_unit.equipment_string, )) output.append('</table></div>') return "".join(output)
def main(cursor): # Get team Id team_id = int(common.get_val('team', 0)) # Build team the_team = team_q.get_one_team(cursor, team_id) if team_id < 1: return "<div style='padding: 5px;'>%s</div>" % common.select_team_form(cursor, 'list_units') unit_dict = unit_q.get_units_from_team(cursor, team=team_id) equipment_dict = equipment_q.get_all_equipment(cursor) the_team.get_units(cursor) output = [] output.append(""" <table border="0" cellspacing="0" cellpadding="5" style="width: 100%;"> <tr class="row2"> <th>Icon</th> <th>Amount</th> <th>Name</th> <th>Cost</th> <th colspan='4'>Categories</th> <th>Equipment</th> <th style="width:70px;">Edit</th> <!-- <th colspan="2">Add</th> --> </tr>""") names = {} count = -1 if len(unit_dict) > 0: # for team_id, team in team_dict.items(): for unit_id, the_unit in unit_dict.items(): if unit_id not in the_team.units: the_team.units[unit_id] = 0 names[unit_id] = the_unit.name count += 1 output.append(""" <tr class="row%(row)d" id="%(unit_id)d"> <td>%(icon)s</td> <td>%(count)s</td> <td>%(name)s</td> <td>%(cost)s</td> <td>%(weapon_cat)s</td> <td>%(armour_cat)s</td> <td>%(move_cat)s</td> <td>%(training_cat)s</td> <td>%(equipment)s</td> <td style="padding: 0px;"><a class="block_link" href="web.py?mode=edit_unit&unit=%(unit_id)d">Edit unit</a></td> <!-- <td style="padding: 1px;"> <form action="exec.py" method="post" accept-charset="utf-8"> <input type="text" name="amount" value="" size="8"/> <input type="hidden" name="mode" value="alter_unit_count" /> <input type="hidden" name="team" value="%(team_id)s" /> <input type="hidden" name="unit" value="%(unit_id)s" /> </form> </td> <td style="padding: 0px;"> <a class="block_link" href="#" onclick="$('#form_add_%(unit_id)s').submit();">Add unit</a> </td> --> </tr> """ % { 'row': (count % 2), "team_id": team_id, 'unit_id': unit_id, 'name': common.doubleclick_text("units", "name", unit_id, the_unit.name, "font-weight:bold"), 'icon': "", 'count': common.number_format(the_team.units[unit_id]), 'weapon_cat': unit.weapon_categories[the_unit.weapon_cat], 'armour_cat': unit.armour_categories[the_unit.armour_cat], 'move_cat': unit.move_categories[the_unit.move_cat], 'training_cat': unit.training_categories[the_unit.training_cat], "cost": unit_rules.print_unit_cost(the_unit, cursor=cursor, equipment_dict=equipment_dict), "equipment": the_unit.equipment_string, }) # Add unit type # armies_dict = army_q.get_armies_from_team(cursor, team=team_id, include_garrisons=1) count += 1 # armies_names = {} # has_non_garrison = False # for k, v in armies_dict.items(): # armies_names[k] = v.name # if v.garrison < 0: has_non_garrison = True # # armies_order.reverse() # armies_order.append("disabled") # armies_order.append("XYZ_all_garrisons") # if has_non_garrison == True: # armies_order.append("XYZ_all_non_garrisons") # armies_order.append("XYZ_all_armies") # armies_order.reverse() # # if has_non_garrison == True: # armies_names["XYZ_all_armies"] = "All armies" # armies_names["XYZ_all_non_garrisons"] = "All non garrisons" # armies_names["XYZ_all_garrisons"] = "All garrisons" output.append(""" <tr class="row%(row)d"> <form action="exec.py" method="post" id="new_unit_form" accept-charset="utf-8"> <input type="hidden" name="mode" value="create_new_unit" /> <input type="hidden" name="team" value="%(team_id)s" /> <td> </td> <td style="padding: 1px;"> </td> <td style="padding: 1px;"><input type="text" name="name" id="name" value="" size="13"/></td> <td colspan='4'> </td> <td style="padding: 1px;"> </td> <td style="padding: 2px;"> <textarea name="equipment_string" id="equipment_string" rows="1" cols="30"></textarea> </td> <td style="padding: 0px;"><a class="block_link" onclick="$('#new_unit_form').submit();" href="#">Add</a></td> %(onload)s </form> </tr> """ % { 'row': (count % 2), "team_id": team_id, 'onload': common.onload("$('#name').focus();"), }) # Now for solo units unit_dict = unit_q.get_units_from_team(cursor, team=0) if len(unit_dict) > 0: # for team_id, team in team_dict.items(): for unit_id, the_unit in unit_dict.items(): if unit_id not in the_team.units: the_team.units[unit_id] = 0 names[unit_id] = the_unit.name count += 1 output.append(""" <tr class="row%(row)d" id="%(unit_id)d"> <td>%(icon)s</td> <td>%(count)s</td> <td>%(name)s</td> <td>%(cost)s</td> <td colspan='4'> </td> <td>%(equipment)s</td> <td style="padding: 0px;"><a class="block_link" href="web.py?mode=edit_unit&unit=%(unit_id)d">Edit unit</a></td> <!-- <td style="padding: 1px;"> <form action="exec.py" method="post" accept-charset="utf-8"> <input type="text" name="amount" value="" size="8"/> <input type="hidden" name="mode" value="alter_unit_count" /> <input type="hidden" name="team" value="%(team_id)s" /> <input type="hidden" name="unit" value="%(unit_id)s" /> </form> </td> <td style="padding: 0px;"> <a class="block_link" href="#" onclick="$('#form_add_%(unit_id)s').submit();">Add unit</a> </td> --> </tr> """ % { 'row': (count % 2), "team_id": team_id, 'unit_id': unit_id, 'name': common.doubleclick_text("units", "name", unit_id, the_unit.name, "font-weight:bold"), 'icon': "", 'count': common.number_format(the_team.units[unit_id]), "cost": unit_rules.print_unit_cost(the_unit, cursor=cursor, equipment_dict=equipment_dict), "equipment": the_unit.equipment_string, }) output.append("</table>") page_data['Title'] = "%s unit list" % the_team.name return "".join(output)
def setup(self, true_team_list=[]): # Teams team_dict = self.the_world.teams() if true_team_list == []: true_team_list = team_dict.keys() # We use the full list of teams so that we don't get key errors later for t in team_dict.keys(): self.caches[t] = {} # Wonders cities_with_wonders = self.the_world.cities_with_wonders() city_dict = self.the_world.cities() for c in cities_with_wonders: if 'wonder_menu' not in self.caches[city_dict[c].team]: self.caches[city_dict[c].team]['wonder_menu'] = [] self.caches[city_dict[c].team]['wonder_menu'].append( '<option value="%s">%s</option>' % ( common.js_name(city_dict[c].name), city_dict[c].name ) ) # Founding for c, the_city in city_dict.items(): if the_city.team not in true_team_list: continue if the_city.dead > 0 or the_city.population < 5000: continue if 'founding_dropdown' not in self.caches[the_city.team]: self.caches[the_city.team]['founding_dropdown'] = ['<option value="">All cities</option>'] self.caches[the_city.team]['founding_dropdown'].append( '<option value="%s">%s</option>' % (the_city.name, the_city.name) ) # Relocation, same as founding minus the first item for t in true_team_list: self.caches[t]['relocation_dropdown'] = list(self.caches[t].get('founding_dropdown', '<option value=""></option>')) del(self.caches[t]['relocation_dropdown'][0]) # Army dropdown army_dict = self.the_world.armies() for army_id, the_army in army_dict.items(): if the_army.team not in true_team_list: continue if 'army_list' not in self.caches[the_army.team]: self.caches[the_army.team]['army_list'] = [] self.caches[the_army.team]['army_list'].append( '<option value="%s">%s</option>' % (the_army.name, the_army.name) ) # Unit dropdown unit_dict = self.the_world.units() for unit_id, the_unit in unit_dict.items(): if the_unit.team != 0 and the_unit.team not in true_team_list: continue if 'unit_list' not in self.caches[the_unit.team]: self.caches[the_unit.team]['unit_list'] = [] self.caches[the_unit.team]['unit_list'].append( '<option value="%s">%s %s</option>' % (the_unit.name, the_unit.name, unit_rules.print_unit_cost(the_unit, the_world=self.the_world)) ) # List of _all_ units for t in true_team_list: self.caches[t]['all_unit_list'] = list(self.caches[t].get('unit_list', [])) self.caches[t]['all_unit_list'].extend(self.caches[0]['unit_list']) # Squad dropdown squad_dict = self.the_world.squads() for squad_id, the_squad in squad_dict.items(): if the_squad.team not in true_team_list: continue if 'squad_list' not in self.caches[the_squad.team]: self.caches[the_squad.team]['squad_list'] = [] self.caches[the_squad.team]['squad_list'].append( '<option value="%s,%s">%s (%s)</option>' % (common.js_name(the_squad.name), common.js_name(army_dict[the_squad.army].name), the_squad.name, army_dict[the_squad.army].name) ) # Operative city dropdown city_dict = self.the_world.cities() self.caches[0]['operative_city_dropdown'] = [] for city_id, the_city in city_dict.items(): if the_city.dead > 0: continue if not team_dict[the_city.team].active: continue self.caches[0]['operative_city_dropdown'].append( '<option value="{0}">{1} ({2})</option>'.format(common.js_name(the_city.name), the_city.name.replace('\\', ''), team_dict[the_city.team].name)) # Spell list spell_dict = self.the_world.spells() for s, the_spell in spell_dict.items(): if the_spell.name == "NONE": continue for t in true_team_list: if 'spell_list' not in self.caches[t]: self.caches[t]['spell_list'] = ['<option value=""> </option>'] the_team = team_dict[t] if s in the_team.spell_levels: if the_team.spell_levels[s] >= the_spell.max_level: if the_spell.max_level > 0: self.caches[t]['spell_list'].append( '<option value="" disabled="disabled">%s</option>' % (the_spell.name) ) continue if the_team.spell_levels[s] > 0: points_needed = spell_rules.cost_for_next_level(None, the_spell, level=the_team.spell_levels[s], in_spell_points=True).get("Spell points") self.caches[t]['spell_list'].append( '<option value="%s">%s (%s: %s/%s)</option>' % ( the_spell.name, the_spell.name, the_team.spell_levels[s], the_team.spell_points[s], points_needed ) ) else: self.caches[t]['spell_list'].append( '<option value="%s">%s (%s)</option>' % ( the_spell.name, the_spell.name, the_team.spell_levels[s] ) ) else: self.caches[t]['spell_list'].append( '<option value="%s">%s</option>' % ( the_spell.name, the_spell.name ) ) # Tech list tech_dict = self.the_world.techs() for s, the_tech in tech_dict.items(): if the_tech.name == "NONE": continue for t in true_team_list: if 'tech_list' not in self.caches[t]: self.caches[t]['tech_list'] = ['<option value=""> </option>'] the_team = team_dict[t] if s in the_team.tech_levels: if the_team.tech_levels[s] >= the_tech.max_level: if the_tech.max_level > 0: self.caches[t]['tech_list'].append( '<option value="" disabled="disabled">%s</option>' % (the_tech.name) ) continue if the_team.tech_levels[s] > 0: points_needed = tech_rules.cost_for_next_level(None, the_tech, level=the_team.tech_levels[s]).get("Tech points") self.caches[t]['tech_list'].append( '<option value="%s">%s (%s: %s/%s)</option>' % ( the_tech.name, the_tech.name, the_team.tech_levels[s], the_team.tech_points[s], points_needed ) ) else: self.caches[t]['tech_list'].append( '<option value="%s">%s (%s)</option>' % ( the_tech.name, the_tech.name, the_team.tech_levels[s] ) ) else: self.caches[t]['tech_list'].append( '<option value="%s">%s</option>' % ( the_tech.name, the_tech.name ) ) # Team dropdown list for t in true_team_list: if 'team_list' not in self.caches[t]: self.caches[t]['team_list'] = ['<option value=""> </option>'] self.caches[t]['trade_list'] = ['<option value=""> </option>'] for team_id, the_team in team_dict.items(): if t == team_id or the_team.dead or not the_team.active: continue if path_f.find_trade_route(self.the_world.cursor, the_team.id, t, the_world=self.the_world) != (-1, -1): self.caches[t]['trade_list'].append('<option value="%s">%s</option>' % ( the_team.name, the_team.name) ) self.caches[t]['team_list'].append('<option value="%s">%s</option>' % (the_team.name, the_team.name)) # Monster list monster_list = [] for k, v in self.the_world.monsters().items(): monster_list.append('<option value="%s">%s</option>' % (v.name, v.name)) for t in true_team_list: self.caches[t]['monster_list'] = monster_list # Research trade master_tier = spell.tiers.index("Master") for t in true_team_list: if 'spell_trade' not in self.caches[t]: self.caches[t]['spell_trade'] = ['<option value=""> </option>'] for s, l in team_dict[t].spell_levels.items(): if l < 1: continue if spell_dict[s].tier == master_tier: continue if not spell_dict[s].tradable: continue if spell_dict[s].name == "NONE": continue self.caches[t]['spell_trade'].append( '<option value="%s">%s</option>' % ( common.js_name(spell_dict[s].name), spell_dict[s].name ) ) for t in true_team_list: if 'tech_trade' not in self.caches[t]: self.caches[t]['tech_trade'] = ['<option value=""> </option>'] for s, l in team_dict[t].tech_levels.items(): if l < 1: continue if not tech_dict[s].tradable: continue if tech_dict[s].name == "NONE": continue self.caches[t]['tech_trade'].append( '<option value="%s">%s</option>' % ( common.js_name(tech_dict[s].name), tech_dict[s].name ) ) # Resources dropdowns for t in true_team_list: team_resource = team_dict[t].resources if 'boolean_resources' not in self.caches[t]: self.caches[t]['boolean_resources'] = ['<option value=""> </option>'] self.caches[t]['discrete_resources'] = ['<option value=""> </option>'] for i, r in enumerate(resource_list.data_list): if not r.tradable: continue if r.type == "boolean": if team_resource.value[i] < 1: continue self.caches[t]['boolean_resources'].append('<option value="%s">%s</option>' % (r.name, r.name)) elif r.type == "discrete": if team_resource.value[i] < 1: continue self.caches[t]['discrete_resources'].append('<option value="%s">%s</option>' % (r.name, r.name)) # Zip them down into strings true_team_list = list(true_team_list) true_team_list.append(0) for t in true_team_list: for k, v in self.caches[t].items(): if type(v) == list or type(v) == tuple: self.caches[t][k] = "".join(v)
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> </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> </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> </th> <th> </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&army=%(army_id)s" class="block_link">Edit army</a> </td> <td style="padding: 0px;"> <a href="web.py?mode=edit_squad&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)