def test_cost_for_next_level(self): vals = ( (spell.Spell({"tier":spell.tiers.index("Mid"),"category":spell.categories.index("Light")}), 5, 0, "Materials:11,Spell Points:100"), (spell.Spell({"tier":spell.tiers.index("Mid"),"category":spell.categories.index("Light")}), 5, 50, "Materials:11,Spell Points:50"), ) for the_Spell, level, completed, answer in vals: result = spell_rules.cost_for_next_level(Dead_cursor(), the_Spell, level, completed, in_spell_points=True) r_answer = res_dict.Res_dict(answer) self.assertEqual(result, r_answer)
def spells(the_world, the_team): output = {} spell_dict = the_world.spells() the_team.get_spells(the_world.cursor) for spell_id, spell_level in the_team.spell_levels.items(): if spell_level == 0 and the_team.spell_points[spell_id] < 1: continue output[spell_id] = { "spell_id": spell_id, "level": spell_level, "points": the_team.spell_points[spell_id], "points_to_next_level": spell_rules.cost_for_next_level(the_world.cursor, spell_dict[spell_level], spell_level, True).get("Spell points"), } return 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 spells(cursor, the_world, the_team): output = [] spell_dict = the_world.spells() the_team.get_spells(cursor) output.append('<div class="ti_section" id="spells_div">') output.append(""" <table style="width: 100%;" border="0" cellspacing="0" cellpadding="5"> <tr class="row2"> <th>#</th> <th> </th> </tr>""") max_spell_level = 1 skip_spell_list = {} table_dict = {} spell_points_spent = 0 for spell_id, spell_level in the_team.spell_levels.items(): if spell_level == 0 and the_team.spell_points[spell_id] < 1: continue if spell_level not in table_dict: table_dict[spell_level] = [] max_spell_level = max(max_spell_level, spell_level) # Total that they've spent spell_points_spent += spell_rules.cost_to_get_to_level(cursor, spell_dict[spell_id], spell_level).get("spell points") spell_points_spent += the_team.spell_points[spell_id] current_spell = spell_dict[spell_id] if the_team.spell_points[spell_id] > 0: table_dict[spell_level].append("""%(name)s (%(points)s/%(points_to_next)s)""" % { "name": current_spell.name, "points": the_team.spell_points[spell_id], "points_to_next": spell_rules.cost_for_next_level(cursor, spell_dict[spell_level], spell_level, True).get("Spell points"), }) else: table_dict[spell_level].append(current_spell.name,) for i in range(0, max_spell_level+1): if i not in table_dict: table_dict[i] = [] if table_dict[i] == [] and i > 10: continue output.append(""" <tr class="row%(row_count)s"> <td width="15">%(i)s</td> <td style="padding: 5px 2px 0 2px;">%(spells)s</td> </tr> """ % { "i": i, "row_count": (i%2), "spells": ", ".join(table_dict[i]), }) output.append('</table></div>') return "".join(output)
def research_spell(the_line, spell_id, debug=False): results = order_block.default_line_results(the_line) spell_dict = the_line.the_world.spells() the_team = the_line.the_world.teams()[the_line.block.team] the_spell = spell_dict[spell_id] lore_name = spell.categories[the_spell.category] current_level = the_team.spell_levels.get(spell_id, 0) current_points = the_team.spell_points.get(spell_id, 0) new_level = current_level + 1 new_points = 0 results = order_block.default_line_results(the_line, "%s could not be researched to level %s because" % (the_spell.name, current_level+1)) # Level limit if the_spell.max_level > 0: if current_level >= the_spell.max_level: return order_block.fail(results, "%s is it's maximum level" % the_spell.max_level) # Cost, it's a bit more complicated here... spell_cost = spell_rules.cost_for_next_level(the_line.the_world.cursor, the_spell, current_level, completed=current_points) # If we've started it then we don't need to worry about materials if current_points > 0: spell_cost.set("Materials", 0) # Lets get some stuff here for working out partiality of research points_cost = spell_cost.get("%s points" % lore_name) + spell_cost.get("Spell points") real_points_cost = points_cost# Used for output later points_availiable = the_team.resources.get("%s points" % lore_name) + the_team.resources.get("Spell points") afford_result = the_team.resources.affordable(spell_cost, overbudget_list=the_team.overbudget) # We cannot afford it if afford_result[0]: # We need to apply swappables spell_cost = res_dict.Res_dict(afford_result[1]) elif not afford_result[0]: # Check materials - Fail outright materials_cost = spell_cost.get("Materials") if materials_cost > the_team.resources.get("Materials"): if "Materials" not in the_team.overbudget: return order_block.fail(results, "you do not have enough materials for it") # Lets try points - Fail outright if 0 points (either spell or lore) if points_availiable == 0: return order_block.fail(results, "you do not have any more spell points availiable this turn") # At this point they cannot afford the whole spell, set cost to what's available - Fail partial if 0 < points_availiable < points_cost: spell_cost.set("Spell points", the_team.resources.get("Spell points")) spell_cost.set("%s points" % lore_name, the_team.resources.get("%s points" % lore_name)) new_level = current_level new_points = current_points + points_availiable # EXECUTION #------------------------ # Check we've got a DB row ready to update if current_level == 0 and current_points == 0: the_line.try_query(spell_f.check_row_exists(team_id=the_team.id, spell_id=the_spell.id)) # Tell them it's only being partially completed if new_level == current_level: result_line = "%s was researched to %s out of %s points towards level %d" % (the_spell.name, new_points, real_points_cost, new_level+1) # It's complete else: result_line = "%s is now level %s" % (the_spell.name, new_level) # Save cost into results dictionary results['cost'] = spell_cost # Queries results['queries'].append("-- Spell research %s to %d.%d for team:%d" % (the_spell.name, new_level, new_points, the_team.id)) results['queries'].extend(spell_f.research_query(the_team.id, spell_id, new_level, new_points)) # Apply cost the_team.resources -= results['cost'].discrete() # Update team spell level/points the_team.spell_levels[the_spell.id] = new_level the_team.spell_points[the_spell.id] = new_points # Result results['results'].append(result_line) return order_block.success(results)
def main(cursor): # Get team Id team_id = int(common.get_val('team', 0)) if team_id < 1: return "<div style='padding: 5px;'>%s</div>" % common.select_team_form(cursor, 'list_spells') # Build team item the_team = team_q.get_one_team(cursor, team_id) spell_dict = spell_q.get_all_spells(cursor) output = [] the_team.get_spells(cursor) output.append(""" <table style="width: 100%;" border="0" cellspacing="0" cellpadding="5"> <tr class="row2"> <th>#</th> <th> </th> </tr> """) max_spell_level = 1 skip_spell_list = {} table_dict = {} spell_points_spent = 0 for spell_id, spell_level in the_team.spell_levels.items(): if spell_level not in table_dict: table_dict[spell_level] = [] skip_spell_list[spell_level] = [] max_spell_level = max(max_spell_level, spell_level) # Total that they've spent the_spell = spell_dict[spell_id] spell_points_spent += spell_rules.cost_to_get_to_level(cursor, the_spell, spell_level, in_spell_points=True).get("Spell points", 0) spell_points_spent += the_team.spell_points[spell_id] skip_spell_list[spell_level].append(spell_id) table_dict[spell_level].append("""%(name)s <a class="red_link" href="exec.py?mode=set_spell&spell=%(spell_id)s&team=%(team_id)s&level=%(level_down)s&points=0"> < </a> %(points)s/%(points_to_next)s <a class="green_link" href="exec.py?mode=set_spell&spell=%(spell_id)s&team=%(team_id)s&level=%(level_up)s&points=0"> > </a>""" % { "name": the_spell.name, "spell_id": spell_id, "team_id": team_id, "points": the_team.spell_points[spell_id], "points_to_next": spell_rules.cost_for_next_level(cursor, the_spell, spell_level, in_spell_points=True).get("Spell points"), "level_down": spell_level-1, "level_up": spell_level+1, }) for i in range(0, max_spell_level+1): if i not in table_dict: table_dict[i] = [] skip_spell_list[i] = [] output.append(""" <tr class="row%(row_count)s"> <td width="15">%(i)s</td> <td style="padding: 5px 2px 0 2px;">%(spells)s <!-- <form id="spell_form_%(i)s" style="float: right;" action="exec.py" method="post" accept-charset="utf-8"> <input type="hidden" name="mode" value="set_spell" /> <input type="hidden" name="team" value="%(team_id)s" /> <input type="hidden" name="points" value="0" /> <input type="hidden" name="level" value="%(i)s" /> <a href="#" onclick="$('#spell_form_%(i)s').submit();" class="block_link" style="float: right;">Add</a> <select style="float: right;" name="spell">%(new_spell)s</select></form> --> </td> </tr> """ % { "i": i, "team_id": team_id, "row_count": (i%2), "spells": ", ".join(table_dict[i]), "new_spell": spell_f.spell_option_list(cursor, skip_spell_list[i]), }) # Add spells output.append(""" <tr class="row%(row_count)s"> <td colspan="2"> <form action="exec.py" method="post" accept-charset="utf-8"> <input type="hidden" name="mode" value="set_spell" /> <input type="hidden" name="team" value="%(team_id)s" /> <select name="spell" id="new_spell"> %(spell_list)s </select> L: <input type="text" name="level" id="new_level" value="" onblur="$('#points_to_next').load('web.py', {'ajax':'True','mode':'spell_points_for_next','spell':$('#new_spell').val(), 'level':$('#new_level').val()});" size="5"/> P: <input type="text" name="points" value="" size="5" />/<span id="points_to_next">0</span> <input type="submit" value="Add" /> </form> </td> </tr> %(onload)s """ % { "team_id": team_id, "row_count": ((i+1)%2), "spell_list": spell_f.spell_option_list(cursor), "onload": common.onload("$('#new_spell').focus();"), }) output.append("</table>") output.append("<br /> Total spell points spent: %d" % spell_points_spent) return "".join(output)
def main(cursor): spell_id = int(common.get_val("spell", 0)) level = int(common.get_val("level", 0)) return str(spell_rules.cost_for_next_level(cursor, spell_id, level, in_spell_points=True).get("Spell points"))