def send_tech(the_line, tech_id, groups, debug=False): results = order_block.default_line_results(the_line) teams_lookup = the_line.the_world.teams_lookup(lower=True) techs_lookup = the_line.the_world.techs_lookup() tech_dict = the_line.the_world.techs() team_dict = the_line.the_world.teams() the_team = team_dict[the_line.block.team] the_tech = tech_dict[tech_id] # Can we find the team? if groups['team_name'].lower() not in teams_lookup: return order_block.fail(results, "there is no team by the name of '%s'" % groups['team_name']) else: target_team = team_dict[teams_lookup[groups['team_name'].lower()]] # Limits of how many you can send a turn if the_team.research_sent >= 2: return order_block.fail(results, "%s could not be sent to %s because you have already sent the maximum number of spells/techs this turn" % (the_tech.name, target_team.name)) # Is the item tradable? if not the_tech.tradable: return order_block.fail(results, "%s could not be sent to %s because it is not tradable" % (the_tech.name, target_team.name)) # Do you have a high enough level? if the_team.tech_levels.get(tech_id, 0) <= target_team.tech_levels.get(tech_id, 0): return order_block.fail(results, "%s could not be sent to %s" % (the_tech.name, target_team.name)) # Do they have a too high a level of it? if target_team.tech_levels.get(tech_id, 0) >= 5: return order_block.fail(results, "%s could not be sent to %s" % (the_tech.name, target_team.name)) # EXECUTION #------------------------ # Check that there's an entry in the database for it if target_team.tech_levels.get(tech_id, 0) == 0: if target_team.tech_points.get(tech_id, 0) == 0: the_line.try_query(tech_f.check_row_exists(team_id=target_team.id, tech_id=the_tech.id)) # Queries results['foreign_queries'][target_team.id] = tech_f.trade_query(target_team.id, tech_id) # Apply change to our copy of the world target_team.tech_levels[tech_id] = target_team.tech_levels.get(tech_id, 0) + 1 target_team.tech_points[tech_id] = 0 # Result results['results'].append("%s was sent to %s" % (the_tech.name, target_team.name)) results['foreign_results'][target_team.id] = ["%s was sent from %s" % (the_tech.name, the_team.name)] # Update sending limits the_team.research_sent += 1 return order_block.success(results)
def research_tech(the_line, tech_id, debug=False): tech_dict = the_line.the_world.techs() the_team = the_line.the_world.teams()[the_line.block.team] the_tech = tech_dict[tech_id] current_level = the_team.tech_levels.get(tech_id, 0) current_points = the_team.tech_points.get(tech_id, 0) results = order_block.default_line_results(the_line, "%s could not be researched to level %s because" % (the_tech.name, current_level+1)) new_level = current_level + 1 new_points = 0 # Level limit if the_tech.max_level > 0: if current_level >= the_tech.max_level: return order_block.fail(results, "%s is it's maximum level" % (the_tech.max_level)) # Cost tech_cost = tech_rules.cost_for_next_level(the_line.the_world.cursor, the_tech, current_level, completed=current_points) # If we've started it then we don't need to worry about materials if current_points > 0: tech_cost.set("Materials", 0) # Lets get some stuff here for working out partiality of research points_cost = tech_cost.get("Tech points") real_points_cost = points_cost# Used for output later points_availiable = the_team.resources.get("Tech points") afford_result = the_team.resources.affordable(tech_cost, overbudget_list=the_team.overbudget) # It's possible we cannot afford it if not afford_result[0]: # Check materials - Fail outright materials_cost = tech_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 if points_availiable == 0: if current_points > 0: return order_block.fail(results, "you do not have any more tech points availiable this turn") else: return order_block.fail(results, "you do not have any more tech points availiable this turn") # At this point they cannot afford the whole tech, set cost to what's available - Fail partial if 0 < points_availiable < points_cost: tech_cost.set("Tech points", the_team.resources.get("Tech points")) 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(tech_f.check_row_exists(team_id=the_team.id, tech_id=the_tech.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_tech.name, new_points, real_points_cost, new_level+1) # It's complete else: result_line = "%s is now level %s" % (the_tech.name, new_level) # Save cost into results dictionary results['cost'] = tech_cost # Queries results['queries'].append("-- Tech research %s to %d.%d for team:%d" % (the_tech.name, new_level, new_points, the_team.id)) results['queries'].extend(tech_f.research_query(the_team.id, tech_id, new_level, new_points)) # Apply cost the_team.resources -= results['cost'].discrete() # Update team tech level/points the_team.tech_levels[the_tech.id] = new_level the_team.tech_points[the_tech.id] = new_points # Result results['results'].append(result_line) return order_block.success(results)